Skip to content

Instantly share code, notes, and snippets.

@mbafford
Last active November 11, 2018 13:20
Show Gist options
  • Save mbafford/b5244cbbde0b8620c331a24aae9a83c7 to your computer and use it in GitHub Desktop.
Save mbafford/b5244cbbde0b8620c331a24aae9a83c7 to your computer and use it in GitHub Desktop.
OSM API - search and bulk change
#!.env/bin/python
import overpass # https://github.com/mvexel/overpass-api-python-wrapper
import osmapi # http://osmapi.metaodi.ch/#osmapi.OsmApi.OsmApi.ChangesetClose
opass = overpass.API()
osm = osmapi.OsmApi( username = "USERNAME", password = "PASSWORD")
# nodes of interest were manually determined using a query on
# https://overpass-turbo.eu/
desired_updates = {
"356894449": ["063", "SULLY RD, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Sully Road - West" ],
"63398761": ["064", "SULLY RD, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Sully Road - East" ],
"63399230": ["065", "CENTREVIL, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Centreville Road - West" ],
"63399105": ["067", "FAIRFAX PK, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Fairfax County Parkway - West"],
"356727456": ["068", "FAIRFAX PK, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Fairfax County Parkway - East"],
"63399050": ["069", "RESTON PK, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Reston Parkway - West" ],
"63367966": ["070", "RESTON PK, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Reston Parkway - East" ],
"63398812": ["071", "WIEHLE AV, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Wiehle Avenue - West" ],
"63487518": ["072", "WIEHLE AV, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Wiehle Avenue - East" ],
"63375322": ["073", "HUNTER ML, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Hunter Mill Road - West" ],
"63368089": ["074", "HUNTER ML, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Hunter Mill Road - West" ],
"63403811": ["076", "ROUTE 7, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Route 7 - East" ],
"5654853763": ["076", "ROUTE 7, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Route 7 - East" ],
"63371801": ["077", "MAIN LINE, WEST", "DULLES TOLL ROAD", "Dulles Toll Road - Main Line - West" ],
"63409796": ["078", "MAIN LINE, EAST", "DULLES TOLL ROAD", "Dulles Toll Road - Main Line - East" ],
}
# query all toll road nodes for an area
# this isn't actually necessary for this example, since we can just use the
# manually identified node IDs from "desired_updates"
def find_tollbooths():
nodes = opass.get("""
node
[barrier=toll_booth]
(38,-78,39,-76);
out;
""")
return set( [ node["id"] for node in nodes["features"] if node["properties"]["barrier"] == "toll_booth" ] )
# For editing, you must create a changeset to wrap one or more edits
#
# use this to start a new changeset
# changeset = osm.ChangesetCreate({u"comment": u"Update Dulles Toll Road tollbooths with descriptive names and EZPass associated IDs"})
#
# use this to update an existing opened Changeset (is there an API method for opening an existing changeset?)
# osm._CurrentChangesetId = 64365437
for id,values in desired_updates.items():
node = None
try:
node = osm.NodeGet( id )
except:
print("Node #%s not found on using OSMAPI - skipping" % id)
continue
tag_updates = node["tag"].copy()
tag_updates["bicycle" ] = "no"
tag_updates["motor_vehicle" ] = "yes"
tag_updates["horse" ] = "no"
tag_updates["foot" ] = "no"
tag_updates["ezpass:plaza" ] = values[0]
tag_updates["ezpass:name" ] = values[1]
tag_updates["ezpass:facility"] = values[2]
tag_updates["name" ] = values[3]
tag_updates["website" ] = "http://www.dullestollroad.com/"
changes = {}
for key in tag_updates.keys():
if node["tag"].get(key, None) != tag_updates.get(key, None):
changes[key] = [ node["tag"].get(key, None), tag_updates.get(key, None) ]
if not changes:
print("Node #%s is already updated. Skipping." % id)
continue
update = {
"id": node["id"],
"lat": node["lat"],
"lon": node["lon"],
"version": node["version"],
"tag": tag_updates
}
print("Updating node #%s with changes: %s" % ( id, changes ) )
print(update)
# to update the node:
# updated = osm.NodeUpdate(update)
# print(updated)
print("")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment