Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 605
Sven Wiesel Avatar asked Oct 22 '13 08:10

Sven Wiesel


People also ask

What is geopy in Python?

geopy is a Python client for several popular geocoding web services. geopy makes it easy for Python developers to locate the coordinates of addresses, cities, countries, and landmarks across the globe using third-party geocoders and other data sources.

What is geopy distance?

Geopy is a Python library that simplifies the calculation of geographic distances between two points. It makes it easier for developers to retrieve coordinates of various locations using third-party geocoders, as well as other data sources.


3 Answers

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.

like image 121
Iain D Avatar answered Oct 06 '22 12:10

Iain D


It's possible with geocoder not with geopy:

# pip install geocoder
>>> import geocoder
>>> g = geocoder.elevation('<address or [lat,lng]>')
>>> print (g.meters)
like image 25
Konstantin Varik Avatar answered Oct 06 '22 11:10

Konstantin Varik


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.

like image 3
Snakes and Coffee Avatar answered Oct 06 '22 12:10

Snakes and Coffee