TransWikia.com

QGIS removes id field when saving as GeoJSON

Geographic Information Systems Asked by falcs on January 19, 2021

I have a GeoJSON file of points which I am editing using QGIS 2.16.1.

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "id": 1, "properties": { "geo_code": "E02002536", "avslope": 0.710642 }, "geometry": { "type": "Point", "coordinates": [ -1.295758646546615, 54.610678649992742 ] } },
{ "type": "Feature", "id": 2, "properties": { "geo_code": "E02002537", "avslope": 0.614510 }, "geometry": { "type": "Point", "coordinates": [ -1.277248239209249, 54.611301122046676 ] } },

After eding the file and using the Save As feature the id field has been removed

{
"type": "FeatureCollection",
"crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },

"features": [
{ "type": "Feature", "properties": { "geo_code": "E02002536", "avslope": 0.710642 }, "geometry": { "type": "Point", "coordinates": [ -1.295758646546615, 54.610678649992742 ] } },
{ "type": "Feature", "properties": { "geo_code": "E02002537", "avslope": 0.614510 }, "geometry": { "type": "Point", "coordinates": [ -1.277248239209249, 54.611301122046676 ] } },

I need this field, and it is part of the GeoJSON standard. Is it possible to save the file and include the id field. I have tried using the Attibute editor but the changes are made within properties not at the top level.

One Answer

Several years later, this is still an issue with QGIS 3.16 Hanover. But a workaround exists (thank you, @gioman). When you export to geoJSON, specify a "Custom Option" with id_field=my_field:

enter image description here

That saves the specified field as the geoJSON feature IDs.

If your geoJSON already exists, you can move the ID from properties to its proper place using the GDAL command-line tools:

ogr2ogr -lco id_field=my_field output.geojson input.geojson

Unfortunately I could not find a way to edit the geoJSON in-place using the command line tools, so you'll have to replace the original geoJSON file with the new one afterward. If you need to process multiple such files, you can write a for loop outside of ogr2ogr.

If you need to convert the IDs from properties inside multiple files, you can use the command-line tool jq inside a Bash for-loop:

for file in *.geojson; do jq '(.features[] | select(.properties.id != null)) |= (.id = .properties.id)' $file > "$file"_tmp; mv "$file"_tmp $file;done

If you would prefer a more user-friendly way to do this, please see QGIS issue #40876.

Answered by i-know-nothing on January 19, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP