Skip to content

Instantly share code, notes, and snippets.

@eww125
Forked from jacohend/overpass_speed.py
Created June 13, 2019 16:01
Show Gist options
  • Save eww125/19132f946a0369705103aecf6afb5182 to your computer and use it in GitHub Desktop.
Save eww125/19132f946a0369705103aecf6afb5182 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))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment