10

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.

4
  • what version of gdal are you using and which OS? (Help > QGIS, or use gdal-config --version from command line). I can reproduce this on 2.16.1 with gdal 1.11.2 on Ubuntu.
    – Steven Kay
    Sep 26, 2016 at 19:19
  • I can reproduce it (ie. if I open a GeoJSON with an id field, then do nothing apart from exporting it in GeoJSON, there is no more id field) using QGIS 2.16.1 (ubuntu 16.04.1 / >>> osgeo.__version__ '1.11.3' in QGIS).
    – mgc
    Sep 26, 2016 at 20:07
  • It is QGIS 2.16.1 on Windows 10 64 bit
    – falcs
    Sep 27, 2016 at 8:37
  • 1
    Same happens with QGIS 3.4.4.
    – user30184
    Jun 28, 2019 at 12:37

1 Answer 1

5

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.