1

I have a large set of latitude/longitude coordinates and would like to get the elevation for each. I want to use the OpenElevation API. According to their API Docs, I can get elevation data through the URL: https://api.open-elevation.com/api/v1/lookup?locations=10,10|20,20|41.161758,-8.583933. As you can see from the example URL, it is possible to get many elevations in a single request (provided you are using POST). However, when I try to implement the example they have used:


elevation = requests.post('https://api.open-elevation.com/api/v1/lookup', params={"locations": "10,10"}).content
print(elevation)

This is the result: b'{"error": "Invalid JSON."}'

The URL being submitted is: https://api.open-elevation.com/api/v1/lookup?locations=10%2C10 which is definitely in incorrect format.

okayatcp12
  • 308
  • 1
  • 9
  • Can you print the URL that is being submitted? E.g. `print(requests.post(...).url)` – David Parks Jul 08 '22 at 16:00
  • The GET request you posted gets multiple elevations at once. Why not use that? When requesting with curl `curl "https://api.open-elevation.com/api/v1/lookup?locations=10,10%7C20,20%7C41.161758,-8.583933" ` I get 3 elevation results. – Bruno Robert Jul 08 '22 at 16:01
  • @BrunoRobert I would use a GET request, but the number of "elevations" I need to get is more than 13,000, and the Docs say that the GET request has a limit of 1024 bytes, whereas the POST request has none. – okayatcp12 Jul 08 '22 at 16:04
  • Perhaps this works: `params={"locations": [10, 10]}`? I'm only guessing here. The format of `params` certainly seems to be key here. – David Parks Jul 08 '22 at 16:04
  • @DavidParks sorry this doesn't work either. The URL becomes: https://api.open-elevation.com/api/v1/lookup?locations=10&locations=10 – okayatcp12 Jul 08 '22 at 16:06
  • Perhaps this is useful: https://stackoverflow.com/questions/32313623/passing-comma-separated-values-in-request-get-python – David Parks Jul 08 '22 at 16:06

1 Answers1

3

After taking a quick look at the documentation. The POST endpoint requires the parameters be sent in the request body in JSON format with the appropriate headers

Here is a working example:

import requests
import json

response = requests.post(
            url="https://api.open-elevation.com/api/v1/lookup",
            headers={
                "Accept": "application/json",
                "Content-Type": "application/json; charset=utf-8",
            },
            data=json.dumps({
                "locations": [
                    {
                        "longitude": 10,
                        "latitude": 10
                    },
                    {
                        "longitude": 20,
                        "latitude": 20
                    }
                ]
            })
        )

print('Response HTTP Status Code: {status_code}'.format(status_code=response.status_code))
print('Response HTTP Response Body: {content}'.format(content=response.content))

Response:

Response HTTP Status Code: 200
Response HTTP Response Body: b'{"results": [{"latitude": 10, "longitude": 10, "elevation": 515}, {"latitude": 20, "longitude": 20, "elevation": 545}]}'
Bruno Robert
  • 302
  • 2
  • 8