Skip to content

Instantly share code, notes, and snippets.

@mogenson
Created March 8, 2019 19:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mogenson/9c0c2acab4696644b954f954b41b4b65 to your computer and use it in GitHub Desktop.
Save mogenson/9c0c2acab4696644b954f954b41b4b65 to your computer and use it in GitHub Desktop.
Query speed limit for latitude longitude. Example for I-93: ./get-speed-limit.py 42.589634 -71.163586
#!/usr/bin/env python3
import requests
import sys
import re
# parse command line arguements
if len(sys.argv) != 3:
print("Usage: {} latitude longitude".format(sys.argv[0]))
sys.exit()
lat = float(sys.argv[1])
lon = float(sys.argv[2])
offset = 0.0001 # about 20 meters
# build a box and pass it to the web server
url = "http://www.overpass-api.de/api/xapi?*[maxspeed=*][bbox={:f},{:f},{:f},{:f}]".format(
lon - offset, lat - offset, lon + offset, lat + offset)
res = requests.get(url) # query openstreepmaps
# uncomment to see raw data returned
# print("status code:", res.status_code)
# print("xml: ", res.text)
if res.status_code != 200:
print("bad web request")
sys.exit()
# search data for the "maxspeed" tag
maxspeed_regex = re.compile(r'<tag k="maxspeed" v="(\d. mph)"')
search_results = maxspeed_regex.search(res.text)
if search_results:
print(search_results.group(1))
else:
print("no speed limit data found")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment