Skip to content

Instantly share code, notes, and snippets.

@goldfndr
Last active December 18, 2015 03:29
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 goldfndr/5719016 to your computer and use it in GitHub Desktop.
Save goldfndr/5719016 to your computer and use it in GitHub Desktop.
VBScript example for JOSM ext_tools plugin (http://wiki.openstreetmap.org/wiki/JOSM/Plugins/ExtTools)
Option Explicit
'An example script for the JOSM ExtTools (ext_tools) plugin.
'http://wiki.openstreetmap.org/wiki/JOSM/Plugins/ExtTools
'After installing the ext_tools plugin, copy this script to your JOSM plugins/ext_tools.
'Find the External Tools preferences panel, and within, set a new tool with this CmdLine:
' cscript.exe //nologo exttools_example.vbs {lat} {lon} {PPD}
Const pixels=5 'This number is only accurate for horizontal scaling; that's how PPD is calculated.
if WScript.Arguments.Count <> 3 then
WScript.StdErr.WriteLine "Syntax: " & WScript.ScriptName & " {lat} {lon} {PPD}" & vbLF _
& WScript.Arguments.Count & " parameters provided, need 3!"
WScript.Quit 3
end if
Dim lat : lat = WScript.Arguments(0)
Dim lon : lon = WScript.Arguments(1)
Dim ppd : ppd = E_Notation(WScript.Arguments(2))
WScript.StdErr.WriteLine "Parameters: lat='" & lat & "' lon='" & lon & "' PPD='" & ppd & "'"
Dim id : id = 0 - CLng(Timer * 100) 'Hope that, if editing continues past midnight, no seconds collision
Function E_Notation(n)
If not Instr(n, "E") then E_Notation=CDbl(n) : Exit Function
Dim a : a = Split(n, "E")
E_Notation = a(0) * 10^a(1)
End Function
Sub WriteNode(id, lat, lon, tags) 'tags is a "|" delimited string with key=values
WScript.StdOut.Write "<node id='" & id & "' lat='" & Coord(lat) & "' lon='" & Coord(lon) & "'"
if 0 = len(tags) then WScript.StdOut.WriteLine " />" : Exit Sub
WScript.StdOut.WriteLine ">"
Dim t : for each t in Split(tags, "|")
WriteTag t
next
WScript.StdOut.WriteLine "</node>"
End Sub
Function Coord(n)
Coord = FormatNumber(n, 7) 'OSM Node lat/lon has 7 decimal places; wiki.osm.org/wiki/Node
End Function
Sub WriteTag(tag) 'Pass a "key=value" string; assumes more than one "=" is part of the value
'This assumes that any passed tag is already properly escaped (if needed) for XML use.
Dim a : a = Split(tag, "=")
WScript.StdOut.Write " <tag k='" & a(0) & "'"
a(0) = ""
WScript.StdOut.WriteLine " v='" & Mid(Join(a, "="), 2) & "' />"
End Sub
'Preamble
WScript.StdOut.WriteLine "<osm version='0.6'>"
'Put a node in the center, name it for debugging purposes.
WriteNode id, lat, lon, "name=PPD=" & ppd
'Put nodes at offset extents
Dim offset : offset = pixels / ppd / 100000 'This was arrived at by trial and error; YMMV.
WriteNode id-1, lat+offset, lon-offset, ""
WriteNode id-2, lat+offset, lon+offset, ""
WriteNode id-3, lat-offset, lon+offset, ""
WriteNode id-4, lat-offset, lon-offset, ""
'Make a closed way out of the fudged extents.
WScript.StdOut.WriteLine "<way id='" & id & "'>"
Dim i : for i = 1 to 4
WScript.StdOut.WriteLine " <nd ref='" & id - i & "' />"
next
WScript.StdOut.WriteLine " <nd ref='" & id - 1 & "' />"
WriteTag "building=yes"
WScript.StdOut.WriteLine "</way>"
'Close out the XML
WScript.Echo "</osm>"
WScript.Quit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment