Created
June 6, 2013 05:02
-
-
Save goldfndr/5719417 to your computer and use it in GitHub Desktop.
JScript example for JOSM ext_tools plugin (http://wiki.openstreetmap.org/wiki/JOSM/Plugins/ExtTools)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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.js {lat} {lon} {PPD} | |
*/ | |
var pixels=5; //This number is only accurate for horizontal scaling; that's how PPD is calculated. | |
if (WScript.Arguments.length != 3) { | |
WScript.StdErr.WriteLine("Syntax: " & WScript.ScriptName & " {lat} {lon} {PPD}" + vbLF | |
+ WScript.Arguments.Count + " parameters provided, need 3!"); | |
WScript.Quit(3); | |
} | |
var lat = Number(WScript.Arguments(0)); | |
var lon = Number(WScript.Arguments(1)); | |
var ppd = Number(WScript.Arguments(2)); | |
//WScript.StdErr.WriteLine("Parameters: lat='" + lat + "' lon='" + lon + "' PPD='" + ppd + "'"); | |
var d = new Date(); | |
var id = 0 - d.getHours() * 360000 - d.getMinutes() * 60000 - d.getSeconds() * 100 - d.getMilliseconds() * 10; //Hope that, if editing continues past midnight, no seconds collision | |
function 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 == tags.length) { | |
WScript.StdOut.WriteLine(" />"); | |
return; | |
} | |
WScript.StdOut.WriteLine(">"); | |
a = tags.split("|"); | |
for (t in a) { | |
WriteTag(a[t]); | |
} | |
WScript.StdOut.WriteLine("</node>"); | |
} | |
function Coord(n) { | |
return n.toFixed(7); //OSM Node lat/lon has 7 decimal places; wiki.osm.org/wiki/Node | |
} | |
function 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. | |
var a = tag.split("="); | |
WScript.StdOut.Write(" <tag k='" + a[0] + "'"); | |
WScript.StdOut.WriteLine(" v='" + a.slice(1).join("=") + "' />"); | |
} | |
//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.toString()); | |
//Put nodes at offset extents | |
var 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 + "'>"); | |
for (var i = 1; i <= 4; i++) { | |
WScript.StdOut.WriteLine(" <nd ref='" + (id - i).toString() + "' />"); | |
} | |
WScript.StdOut.WriteLine(" <nd ref='" + (id - 1).toString() + "' />"); | |
WriteTag("building=yes"); | |
WScript.StdOut.WriteLine("</way>"); | |
//Close out the XML | |
WScript.StdOut.WriteLine("</osm>"); | |
WScript.Quit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://gist.github.com/goldfndr/5719016 for a nearly identical example written in VBScript. (No idea why search isn't bringing it up).