Can I get the altitude with geopy in Python? (with Longitude/Latitude)

Sven Wiesel picture Sven Wiesel · Oct 22, 2013 · Viewed 9.5k times · Source

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.

Answer

Iain D picture Iain D · Jun 27, 2018

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.