简体   繁体   中英

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.

It's possible with geocoder not with geopy:

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM