Skip to content

Instantly share code, notes, and snippets.

@RussNelson
Created July 2, 2018 21:27
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 RussNelson/a2da4104eb1b83b3a66fc6bc32a82ec4 to your computer and use it in GitHub Desktop.
Save RussNelson/a2da4104eb1b83b3a66fc6bc32a82ec4 to your computer and use it in GitHub Desktop.
convert a sample of the Microsoft buildings data into a .osm file
#!/usr/bin/python
inl = '''{"type":"Feature","geometry":{"type":"Polygon","coordinates":[[[-79.017141657351175,43.19780192629009],[-79.017342809996279,43.197800746032968],[-79.0173418817583,43.197716672803352],[-79.017253374594461,43.19771719211721],[-79.017252424769524,43.19763116357705],[-79.017139779288271,43.19763182452288],[-79.017141657351175,43.19780192629009]]]}},
'''
osm_xml = """<?xml version='1.0' encoding='UTF-8'?>
<osm version='0.6' generator='JOSM'>
<node id='-100425' action='modify' lat='41.57544047908' lon='-75.42956033631' />
<node id='-100426' action='modify' lat='41.57540557904' lon='-75.42937613631' />
<node id='-100427' action='modify' lat='41.57544907909' lon='-75.42936143631' />
<node id='-100428' action='modify' lat='41.57547727912' lon='-75.42935183631' />
<node id='-100429' action='modify' lat='41.57551217915' lon='-75.42953593631' />
<way id='-100424' action='modify'>
<nd ref='-100425' />
<nd ref='-100426' />
<nd ref='-100427' />
<nd ref='-100428' />
<nd ref='-100429' />
<nd ref='-100425' />
<tag k='building' v='prison' />
<tag k='source' v='Bing' />
</way>
</osm>
"""
osmid = -1
import json
print """<?xml version='1.0' encoding='UTF-8'?>
<osm version='0.6' generator='JOSM'>"""
for inl in open("NewYork.json"):
if inl.find("Feature") < 0: continue
try:
jjj = json.loads(inl[:-3])
except ValueError, e:
print e
continue
if (-75.2 > jjj['geometry']['coordinates'][0][0][0] > -75.8 and
44.1 > jjj['geometry']['coordinates'][0][0][1] > 43.8):
startnode = osmid
for ccc in jjj['geometry']['coordinates'][0]:
print ("<node id='%d' action='modify' lat='%f' lon='%f' />" % (
osmid, ccc[1], ccc[0]))
osmid -= 1
print "<way id='%d' action='modify'>" % (osmid)
node = startnode
while node > osmid:
print "<nd ref='%d' />" % (node)
node -= 1
print "<nd ref='%d' />" % (startnode)
print "<tag k='building' v='yes' />"
print "<tag k='source' v='Bing' />"
print "</way>"
osmid -= 1
print "</osm>"
@jwalseth-tableau
Copy link

Thanks for making this available Russ. I notice that lat/long values for the area of interest are in several places. Am I correct in assuming I need to change all of those?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment