Python request to get altitude from a GeoJSON file
Question:
I want to get the elevation of a segment (linestring) from a GeoJSON file.
I’m using this API:
- Documentation: https://api3.geo.admin.ch/services/sdiservices.html#profile
- request URL: https://api3.geo.admin.ch/rest/services/profile.csv
My problem is that I cannot get rid of an error saying the passed parameter ‘geom’ is not of GeoJSON type.
My geojson file:
{
"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 ] ] } }
]
}
My code:
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())
Output:
{'error': {'code': 400, 'message': 'Invalid geom parameter, must be a GEOJSON'}, 'success': False}
Answers:
Look at the example in the docs. The API wants a single geometry, something like {"type": "LineString", "coordinates": [[1,2], [3,4]]}
; you are giving it an entire FeatureCollection.
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.content)
This answer was posted as an edit to the question Python request to get altitude from a GeoJSON file by the OP axgibs under CC BY-SA 4.0.
I want to get the elevation of a segment (linestring) from a GeoJSON file.
I’m using this API:
- Documentation: https://api3.geo.admin.ch/services/sdiservices.html#profile
- request URL: https://api3.geo.admin.ch/rest/services/profile.csv
My problem is that I cannot get rid of an error saying the passed parameter ‘geom’ is not of GeoJSON type.
My geojson file:
{
"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 ] ] } }
]
}
My code:
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())
Output:
{'error': {'code': 400, 'message': 'Invalid geom parameter, must be a GEOJSON'}, 'success': False}
Look at the example in the docs. The API wants a single geometry, something like {"type": "LineString", "coordinates": [[1,2], [3,4]]}
; you are giving it an entire FeatureCollection.
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.content)
This answer was posted as an edit to the question Python request to get altitude from a GeoJSON file by the OP axgibs under CC BY-SA 4.0.