Skip to content

Instantly share code, notes, and snippets.

@chekoopa
Created April 3, 2018 08:37
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 chekoopa/904dc56ac4f344b6db1a9a1f85229c82 to your computer and use it in GitHub Desktop.
Save chekoopa/904dc56ac4f344b6db1a9a1f85229c82 to your computer and use it in GitHub Desktop.
Google Earth Elevation API Example
import json, time
import urllib.request, urllib.parse
def elevation_list(locations):
KEY = "GENERATE YOUR OWN KEY, B-BAKA~ ~,~"
base_url = 'https://maps.googleapis.com/maps/api/elevation/json'
url = base_url + '?' + urllib.parse.urlencode({
'locations': "|".join("%s,%s" % (lat, lng) for lat, lng in locations),
'key': KEY,
})
current_delay = 0.1 # 100ms.
max_delay = 3600 # 1h.
while True:
try:
response = urllib.request.urlopen(url).read()
except IOError:
pass
else:
result = json.loads(response.decode().replace("\n", ""))
if result['status'] == 'OK':
return [elem['elevation'] for elem in result['results']]
elif result['status'] != 'UNKNOWN_ERROR':
# INVALID_REQUEST or ZERO_RESULTS
raise Exception(result['error_message'])
if current_delay > max_delay:
raise Exception('Too many retry attempts.')
print ('Waiting', current_delay, 'seconds before retrying.')
time.sleep(current_delay)
current_delay *= 2
def elevation_point(lat, lng):
return elevation_list([(lat, lng)])
elev = elevation_point(39.7391536,-104.9847034)
print ('Elevation:', elev)
print ("If it's not", 1608.637939453125, "you're in a problem.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment