Skip to content

Instantly share code, notes, and snippets.

@jacohend
Created January 18, 2016 06:45
Show Gist options
  • Save jacohend/1e943771d89ab3de067d to your computer and use it in GitHub Desktop.
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
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))
@ranakhan
Copy link

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!

@vaibhavoberoi
Copy link

vaibhavoberoi commented May 25, 2020

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.

@mohcinemadkour
Copy link

mohcinemadkour commented Jan 8, 2021

Thanks! it works great!

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