Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ThomasG77/4622d75a8c55d5db6a0615c7bc35e494 to your computer and use it in GitHub Desktop.
Save ThomasG77/4622d75a8c55d5db6a0615c7bc35e494 to your computer and use it in GitHub Desktop.
IGN API elevation line
import json
import tempfile
import urllib.request
baseUrl='https://wxs.ign.fr/calcul/alti/rest/elevationLine.json'
layer = iface.activeLayer()
# Caution: our line is a linestring and use by default epsg 4326, so we already have lon, lat
# You also need to select a line for the layer
for feature in layer.selectedFeatures():
lons = '|'.join([str(coord.x()) for coord in feature.geometry().asPolyline()])
lats = '|'.join([str(coord.y()) for coord in feature.geometry().asPolyline()])
url = f"{baseUrl}?sampling=500&lon={lons}&lat={lats}"
response = json.load(urllib.request.urlopen(url))
geojsonFeatures = []
for pt in response.get('elevations'):
geojsonFeatures.append({
"type": "Feature",
"properties": {"acc": pt.get('acc'), "z": pt.get('z')},
"geometry": {
"type": "Point",
"coordinates": [
pt.get('lon'),
pt.get('lat'),
pt.get('z')
]
}
})
mygeojson = {
"type": "FeatureCollection",
"features": geojsonFeatures
}
with tempfile.NamedTemporaryFile(mode='w', encoding='utf-8', delete=False, suffix='.geojson') as infile:
outname = infile.name
json.dump(mygeojson, infile)
iface.addVectorLayer(outname, outname, 'ogr')
# Convert to local projection layer to get meters for distance
# Then open in QGIS plugin "Profile tool"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment