Created
March 8, 2019 19:50
-
-
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
This file contains hidden or 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
#!/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