简体   繁体   English

使用 Python 高效地使用 OpenElevation API

[英]Efficiently using the OpenElevation API using Python

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.我想使用 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 .根据他们的 API Docs,我可以通过 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).正如您从示例 URL 中看到的那样,可以在单个请求中获得许多海拔高度(前提是您使用的是 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."}'结果如下: 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.提交的 URL 是: https ://api.open-elevation.com/api/v1/lookup?locations=10%2C10,格式肯定不正确。

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 POST 端点要求参数以 JSON 格式在请求正文中发送,并带有适当的标头

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}]}'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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