Last active
August 29, 2015 14:15
-
-
Save smmaurer/c0e20b1bce754c92e149 to your computer and use it in GitHub Desktop.
Code snippet for queries to a Google REST API
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
for feature in data['features']: | |
# Break after a couple of features for testing | |
# if feature['id'] == 2: | |
# break | |
coords = feature['geometry']['coordinates'] | |
# Assemble locations for elevation query: 'lat,lng|lat,lng|etc' | |
locStr = '' | |
for latlng in coords: | |
if locStr != '': | |
locStr += '|' | |
locStr += str(latlng[1]) + ',' + str(latlng[0]) | |
# Assemble elevation query | |
url = baseUrl + '?locations=' + locStr + '&sensor=false' | |
# The Elevation API prevents you from running queries too frequently, | |
# so we have to catch failed queries and re-try them after a delay | |
queryStatus = '' | |
while queryStatus != 'OK': | |
# Run elevation query | |
response = json.load(urllib.urlopen(url)) | |
print 'query ' + str(queryCount + 1) + ': ' + response['status'] | |
if response['status'] == 'OK': | |
queryStatus = 'OK' | |
queryCount += 1 | |
time.sleep(0.1) | |
# Add elevations to GeoJSON feature coordinates | |
for i in range(0, len(coords)): | |
elev = response['results'][i]['elevation'] | |
coords[i].append(elev) | |
else: | |
time.sleep(10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment