Skip to content

Instantly share code, notes, and snippets.

@Alliages
Created April 28, 2016 12:44
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save Alliages/a858a59b2487d81eb66c0c3b62d042be to your computer and use it in GitHub Desktop.
Save Alliages/a858a59b2487d81eb66c0c3b62d042be to your computer and use it in GitHub Desktop.
A very simple python script that get elevation from latitude and longitude with google maps API
#
# elevation: A very simple python script that get elevation from latitude and longitude with google maps API by Guillaume Meunier
#
# -----------------------------------
# NO DEPENDANCIES except JSON and URLLIB
# -----------------------------------
#
# Copyright (c) 2016, Guillaume Meunier <alliages@gmail.com>
# GEOJSON_export is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published
# by the Free Software Foundation; either version 3 of the License,
# or (at your option) any later version.
#
# GEOJSON_export is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with GEOJSON_export; If not, see <http://www.gnu.org/licenses/>.
#
# @license GPL-3.0+ <http://spdx.org/licenses/GPL-3.0+>
#
# EXAMPLE :
# print elevation(48.8445548,2.4222176)
import json
import urllib
def elevation(lat, lng):
apikey = "USE YOUR OWN KEY !!!"
url = "https://maps.googleapis.com/maps/api/elevation/json"
request = urllib.urlopen(url+"?locations="+str(lat)+","+str(lng)+"&key="+apikey)
try:
results = json.load(request).get('results')
if 0 < len(results):
elevation = results[0].get('elevation')
# ELEVATION
return elevation
else:
print 'HTTP GET Request failed.'
except ValueError, e:
print 'JSON decode failed: '+str(request)
@iadamo1
Copy link

iadamo1 commented Feb 7, 2018

Hi! I was trying to use your script but when I run it get this message:
File "elevation.py", line 15
print 'HTTP GET Request failed.'
^
SyntaxError: Missing parentheses in call to 'print'
can it be fixed?

@Khanimar
Copy link

You should take your own API key and replace the apikey variable with your own.

@RameshKamath
Copy link

RameshKamath commented Apr 12, 2018

@iadamo1 I think you are using python 3 but the code is written for python 2. Try to add parentheses to print functions.
Example print('HTTP GET Request failed.')

Also, I had issues with URLLIB in python 3 so I edited same code for requests module.
https://gist.github.com/RameshKamath/bb7fdb9efdedb15deab60665db82c3aa

@hs-harsh
Copy link

I implemented the code and called the function by "print elevation(48.8445548,2.4222176)" but it gave error:
URLError: <urlopen error [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond>

@ranpelta
Copy link

ranpelta commented Sep 2, 2018

hi, I've tried you code but I keep getting:
HTTP GET Request failed.
I'm using python 2.7, and used my own API key

@PetarPepe
Copy link

PetarPepe commented Sep 2, 2018

Hi everyone, I would really like some feedback on this. I tried using this code and it only works for coordinates given as example here, whenever I try to change those coordinates I get print('HTTP GET Request failed.')

Also tried using this code:

from json import loads
from time import sleep
from urllib.request import Request, urlopen
 
locations=[(40.714728,-73.998672), (48.8445548,2.4222176)] #(lat,lon) pairs
for loc in locations: 
    try:
        request = Request('https://maps.googleapis.com/maps/api/elevation/json?locations={0},{1}&key=ejebigasadamigosjebasansenisanrazmisljanavrime'.format(loc[0],loc[1]))
        response = urlopen(request).read() 
        places = loads(response)
        print('At {0} elevation is: {1}'.format(loc, places['results'][0]['elevation']))
        sleep(1)
    except:
        print('Error for location: {0}'.format(loc))

And I get the same thing, works only for these coordinates print elevation(48.8445548,2.4222176).

@tomstrain92
Copy link

Hi! I was trying to use your script but when I run it get this message:
File "elevation.py", line 15
print 'HTTP GET Request failed.'
^
SyntaxError: Missing parentheses in call to 'print'
can it be fixed?

Hi! I was trying to use your script but when I run it get this message:
File "elevation.py", line 15
print 'HTTP GET Request failed.'
^
SyntaxError: Missing parentheses in call to 'print'
can it be fixed?

Are you using python3? if so it's
print('HTTP GET Request failed.')

@UCF10
Copy link

UCF10 commented Apr 15, 2021

import json
import urllib.request

def elevation(lat, lng):
apikey = "Put in your Key"
url = "https://maps.googleapis.com/maps/api/elevation/json"
request = urllib.request.urlopen(url+"?locations="+str(lat)+","+str(lng)+"&key="+apikey)

try:
    results = json.load(request).get('results')
    if 0 < len(results):
        elevation = results[0].get('elevation')
        return elevation
    else:
        print('HTTP GET Request failed.')
except ValueError as e:
    print('JSON decode failed: '+str(request))

if name == "main":
evl = elevation(37.48976,-113.32631)
print(evl)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment