1

can we use open-elevation with python? I tried to get the API using request and didn't work

#overpass api url
elevation_request = "https://api.open-elevation.com/api/v1/lookup\?locations\=10,10\|20,20\|41.161758,-8.583933"
elevation = requests.get(elevation_request)
data_json = elevation.json()

how can we integrate this API with python?

noob
  • 672
  • 10
  • 28
  • The link you added does not seem to load in a browser? Takes quite some time and responds with a `504`. – PacketLoss Jan 16 '21 at 10:07
  • the link open-elevation is working fine if you mean the link inside the elevation request its the API request – noob Jan 16 '21 at 10:32
  • It seems odd. Even following the API docs, the endpoint returns a http 504. Could be a service side issue? – PacketLoss Jan 16 '21 at 10:40
  • probably but I did read one February 2020 that the API is still not available for python – noob Jan 16 '21 at 10:43
  • That is not the cause of your issue. If you use the API docs examples for both `GET` and `POST`, you do not get a response. The server responds with HTTP 504 - Gateway Timeout in both the browser and via `requests` – PacketLoss Jan 16 '21 at 10:45

2 Answers2

4

I am the author of https://open-elevation.com.

The service should be working much more reliably now.

Sadly, the service is widely used for free, but there is very little in the way of donations, so I have been unable to upgrade it properly to handle the load. A migration in the coming month (with more money coming out of my pocket each month) has eased this situation, but I can't tell you how long it will last.

Jorl17
  • 361
  • 3
  • 5
1

The endpoints are valid, but they are many times slow or sometimes unresponsive. The code below is an adaptation of this answer to deal with response status codes and timeout:

from requests import get
from pandas import json_normalize

def get_elevation(lat = None, long = None):
    '''
        script for returning elevation in m from lat, long
    '''
    if lat is None or long is None: return None
    
    query = ('https://api.open-elevation.com/api/v1/lookup'
             f'?locations={_lat},{_long}')
    
    # Request with a timeout for slow responses
    r = get(query, timeout = 20)

    # Only get the json response in case of 200 or 201
    if r.status_code == 200 or r.status_code == 201:
        elevation = json_normalize(r.json(), 'results')['elevation'].values[0]
    else: 
        elevation = None
    return elevation
oscgonfer
  • 21
  • 2