我想从GeoJSON文件中获取线段(线串)的高程。
我正在使用这个API:
- 文件:www.example.comhttps://api3.geo.admin.ch/services/sdiservices.html#profile
- 请求URL:https://api3.geo.admin.ch/rest/services/profile.csv
我的问题是,我无法摆脱一个错误,说传递的参数'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"
1条答案
按热度按时间pieyvz9o1#
看看文档中的例子,API想要一个单一的几何体,比如
{"type": "LineString", "coordinates": [[1,2], [3,4]]}
;您将为其提供整个FeatureCollection。