Python请求从GeoJSON文件获取海拔高度

vwkv1x7d  于 2023-02-10  发布在  Python
关注(0)|答案(1)|浏览(260)

我想从GeoJSON文件中获取线段(线串)的高程。
我正在使用这个API:

我的问题是,我无法摆脱一个错误,说传递的参数'geom'不是GeoJSON类型。
我的geojson文件:

{
"type": "FeatureCollection",
"name": "test",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },
"features": [
{ "type": "Feature", "properties": { "Name": null, "description": "", "altitudeMode": "clampToGround", "tessellate": 1, "type": "linepolygon" }, "geometry": { "type": "LineString", "coordinates": [ [ 7.349510580151255, 45.998132989830559 ], [ 7.346422156898689, 46.039529058312063 ], [ 7.287112064012824, 46.093617348068292 ], [ 7.236173542687846, 46.127135334945002 ] ] } }
]
}

我的代码:

import requests
import json

api_url = "https://api3.geo.admin.ch/rest/services/profile.csv"
file_path = "geojson.GEOJSON"

with open(file_path) as f:
    geojson = json.load(f)
    r = requests.get(api_url, params=dict(geom=geojson))

print(r.json())

输出:

{'error': {'code': 400, 'message': 'Invalid geom parameter, must be a GEOJSON'}, 'success': False}
    • 编辑:解决方案**
import requests
import json

api_url = "https://api3.geo.admin.ch/rest/services/profile.csv"
file_path = "geojson.GEOJSON"

with open(file_path) as f:
    geojson, = json.load(f)['features']
    geom = json.dumps(geojson['geometry'])
    r = requests.get(api_url, params=dict(geom=geom))

print(r.json())

注意:仍然有一个问题,但与参数'sr':Requesting raising "JSONDecodeError: Extra data"

pieyvz9o

pieyvz9o1#

看看文档中的例子,API想要一个单一的几何体,比如{"type": "LineString", "coordinates": [[1,2], [3,4]]};您将为其提供整个FeatureCollection。

相关问题