Created
April 3, 2018 08:37
-
-
Save chekoopa/904dc56ac4f344b6db1a9a1f85229c82 to your computer and use it in GitHub Desktop.
Google Earth Elevation API Example
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
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