Created
January 18, 2016 06:45
-
-
Save jacohend/1e943771d89ab3de067d to your computer and use it in GitHub Desktop.
Get speed limits from OSM's Overpass API within a certain radius of gps coordinates
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
import overpy | |
import sys | |
import simplejson as json | |
#pip install overpy | |
#python overpass_speed.py 37.7833 -122.4167 500 | |
def maxspeed(coordinates, radius): | |
lat, lon = coordinates | |
api = overpy.Overpass() | |
# fetch all ways and nodes | |
result = api.query(""" | |
way(around:""" + radius + """,""" + lat + """,""" + lon + """) ["maxspeed"]; | |
(._;>;); | |
out body; | |
""") | |
results_list = [] | |
for way in result.ways: | |
road = {} | |
road["name"] = way.tags.get("name", "n/a") | |
road["speed_limit"] = way.tags.get("maxspeed", "n/a") | |
nodes = [] | |
for node in way.nodes: | |
nodes.append((node.lat, node.lon)) | |
road["nodes"] = nodes | |
results_list.append(road) | |
return results_list | |
results = maxspeed((sys.argv[1], sys.argv[2]), sys.argv[3]) | |
print(json.dumps(results)) |
Hi @ranakhan. The following snippet will help you extract the maxspeed:
maxspeed=[] data = response.json() for dct in data['elements']: if dct['type']=='way': maxspeed.append(dct['tags']['maxspeed']) break #break if you want the maxspeed closest to your coordinate. Comment it if you want a list of all maxspeeds in the radius.
Thanks! it works great!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi Jacohend, I am trying to follow your script (I am new to python and GIS). I need to extract maxspeed or all attributes for whole Canada. Can you please guide me, how can I extract all attributes for any way/node etc.
Thanks in advance!