Skip to content

Instantly share code, notes, and snippets.

@smmaurer
Last active August 29, 2015 14:15
Show Gist options
  • Save smmaurer/c0e20b1bce754c92e149 to your computer and use it in GitHub Desktop.
Save smmaurer/c0e20b1bce754c92e149 to your computer and use it in GitHub Desktop.
Code snippet for queries to a Google REST API
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