10

it is possible?

i tryed:

from geopy.point import Point
from geopy import geocoders
[...]
p = Point(Latitude, Longitude)
lat, lon, altitude = p
height_metres = altitude

but height_metres is always 0.

Kara
  • 6,115
  • 16
  • 50
  • 57
Sven Wiesel
  • 101
  • 1
  • 1
  • 3

3 Answers3

18

Note that with geocoder you will need a Google elevation API key which now requires a project with billing enabled (as of July 2018).

As an alternative, you can use the open elevation public API. Here's an example function to return the elevation (Note python 3.6 used for string formatting):

import requests
import pandas as pd

# script for returning elevation from lat, long, based on open elevation data
# which in turn is based on SRTM
def get_elevation(lat, long):
    query = ('https://api.open-elevation.com/api/v1/lookup'
             f'?locations={lat},{long}')
    r = requests.get(query).json()  # json object, various ways you can extract value
    # one approach is to use pandas json functionality:
    elevation = pd.io.json.json_normalize(r, 'results')['elevation'].values[0]
    return elevation

Note, the API may change in the future and I can't comment on veracity of the data but currently it's a nice alternative to Google and spot checks suggest it works fine.

Iain D
  • 471
  • 5
  • 6
  • 1
    the code works fine if I query latitudes and longitudes intermittently. However as soon I try to use it continuously to fill a Series I get `JSONDecodeError: ('Expecting value: line 1 column 1 (char 0)', 'occurred at index 0')`. This is probably not your fault but just wanted to point it out. – Bn.F76 May 07 '19 at 15:06
  • This works but with the occasional error that @Bn.F76 Pointed out and it's also a bit slow at times. I guess this may be a server issue – Vasco Cansado Carvalho Dec 10 '20 at 23:37
  • I have no affiliation with the developers of open-elevation but looking at their docs it suggests you request multiple locations at once which may resolve the issue above. There is a limit for the GET api but they suggest using the POST apie instead. https://github.com/Jorl17/open-elevation/blob/master/docs/api.md – Iain D Dec 11 '20 at 13:29
9

It's possible with geocoder not with geopy:

# pip install geocoder
>>> import geocoder
>>> g = geocoder.elevation('<address or [lat,lng]>')
>>> print (g.meters)
3

I would eat my socks if geopy knew the altitude of every single point on the globe. This isn't possible (afaik) without doing some fancy GoogleEarth/other database searching to figure out the altitude.

The reason why lat, lon, altitude = p works is because the Point has an altitude attribute. According to the source, the only time in the constructor altitude is altered is in the line altitude = float(altitude or 0), which doesn't get the altitude.

Snakes and Coffee
  • 8,747
  • 4
  • 40
  • 60
  • Thanks for your comment! =) but there are not a other method for python? There are a lot of websites where you can paste lat/long and return altitude. – Sven Wiesel Oct 22 '13 at 10:32