Skip to content

Instantly share code, notes, and snippets.

Created October 24, 2010 18:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/94dc92bb6931ca7f71b9 to your computer and use it in GitHub Desktop.
Save anonymous/94dc92bb6931ca7f71b9 to your computer and use it in GitHub Desktop.
' Shp-layer2osm.vb
' This script export selected shp layer to osm file
' @author Rafał Rudzik, (RafalR) OpenStreetMap
' @license GPL
' @version v2 2010-oct-24
'How to
' Download MapWindow GIS
' Open SHP file (projction properties must be set), select layer
' Plug-ins -> Script...
Imports MapWindow.Interfaces
Imports MapWinGIS
Imports System.Windows.Forms
Imports Microsoft.VisualBasic
Imports System
Public Module OSM
Public Sub ScriptMain(ByRef m_MapWin As IMapWin)
Dim Lr As Layer
Dim Osm, osmFile, SrcProj, DstProj As String
Dim SrcFile, DstFile As New MapWinGIS.Shapefile()
Dim Sh As new MapWinGIS.shape
Dim Poly() As Integer
Dim success As Boolean
Dim a, b, ElCount, NodCount As Integer
If -1=m_MapWin.Layers.CurrentLayer Then
MsgBox("Select layer first!")
return
End If
Lr=m_MapWin.Layers.Item(m_MapWin.Layers.CurrentLayer)
'If IsNothing(Lr.GetGridObject)
' MsgBox("Select raster layer first!")
' Return
'End if
SrcFile.Open(Lr.FileName)
SrcProj=Lr.Projection
DstProj = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
Try
success = MapWinGeoProc.SpatialReference.ProjectShapefile(SrcProj, DstProj, SrcFile, DstFile)
If success Then
BakeOsmCake()
Else
MsgBox("Projection failed")
End If
Catch
MsgBox("Error caught!")' Projection must be defined
End Try
' ^ Ok this WORKS :)
End Sub
Private Function BakeOsmCake()
Osm = "<?xml version='1.0' encoding='UTF-8'?>" & vbNewLine & "<osm version='0.6' generator='MapWindow GIS Script'>" & vbNewLine
ElCount = 0
NodCount= 0
ReDim Poly(DstFile.NumShapes - 1)
for a = 0 to DstFile.NumShapes - 1
Sh=DstFile.Shape(a)
for b = 0 to Sh.numPoints - 1
ElCount += 1
Osm = Osm & vbTab & "<node id='-" & ElCount & "' visible='true' lat='" & new String(Sh.Point(b).y & "").Replace(",",".") & "' lon='" & new String("" & Sh.Point(b).x).Replace(",",".") & "' />" & vbNewLine
next b
Poly(a) = Sh.numPoints - 1
next a
for a = 0 to Poly.Length -1
ElCount += 1
Osm = Osm & vbTab & "<way id='-" & ElCount & "' visible='true'>" & vbNewLine
for b = 0 to Poly(a)
NodCount += 1
Osm = Osm & vbTab & vbTab & "<nd ref='-" & NodCount & "' />" & vbNewLine
next b
Osm = Osm & vbTab & vbTab & "<tag k='debug:file_name' v='" & IO.Path.GetFileName(Lr.FileName) & "' />" & vbNewLine
Osm = Osm & vbTab & "</way>" & vbNewLine
next a
Osm = Osm & "</osm>"
osmFile=IO.Path.GetFullPath(Lr.FileName) & ".osm"
FileOpen (1,osmFile,OpenMode.Output,OpenAccess.Write)
PrintLine(1,Osm)
FileClose (1)
MsgBox("Done!")
End Function
End Module
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment