-
-
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
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)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment