5

According to the GeoJSON Format Specification

"If a feature has a commonly used identifier, that identifier should be included as a member of the feature object with the name "id"."

My question is how do I add this to my GeoJSON?

If I create it as an attribute and then save it as GeoJSON in QGIS it ends up in Properties instead of in Features.

This is what I want to do:

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

"features": [
{ "type": "Feature", "id":"1", "properties": { "Namn":.................

This is what QGIS produces:

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

"features": [
{ "type": "Feature", "properties": { "id": 1, "Name".................. 

I have also tried PyGeoj https://github.com/karimbahgat/PyGeoj. This has a function to add a unique id but it also adds it under properties.

If I open the GeoJSON and write it in by hand then it works but I don't want to do this for all of my layers, some of which contain many features.

fantomen
  • 81
  • 2
  • 8

4 Answers4

2

Thought I would write here how I have solved it in case anyone else comes across the same problem.

A simple python script:

#create empty file to be writen to
file = open("kommun_id.geojson", "w")
count = 0

#read original file
with open('kommun.geojson', 'r')as myfile:
    for line in myfile:

       #lines that don't need to be edited with an 'id'
       if not line.startswith('{ "type": '):
            file.write(line)
       else:
            #lines that need to be edited
            count = count +1
            idNr = str(count)
            file.write(line[0:20] + '"id":'+ '"'+ idNr + '",' +line[21:])

file.close()

It might not work for all geoGJSONs but it works for the ones I have created with QGIS

fantomen
  • 81
  • 2
  • 8
2

You can fix this by using the preserve_fid flag in ogr2ogr. You're going to "convert" the bad GeoJSON that QGIS dumps to the format you want, with the id field exposed.

ogr2ogr -f GeoJSON fixed.geojson qgis.geojson -preserve_fid

Here qgis.geojson is the one the QGIS created and fixed.geojson is the new version with the exposed id field.

andrew
  • 3,929
  • 1
  • 25
  • 38
  • 1
    this creates an incremental id but not take it from the " id " property on the geojson itself, what if i want that outside id field to be taken from a property ? – Ibrahim Mohammed Sep 15 '18 at 01:24
1

This is due to ogr2ogr not knowing which QGIS field to use as the geoJSON ID.

Per this QGIS issue (thank you @gioman), a workaround is to manually specify the ID field in Custom Options --> Layer Settings when you do the export:

enter image description here

If you need to do this to geoJSONs that have already been created, you can use the standalone ogr2ogr command-line program:

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

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
i-know-nothing
  • 789
  • 1
  • 7
  • 14
  • This works, however, be aware that it requires `GDAL` version of at least `2.3` as indicated in [GDAL GeoJSON driver docs](https://gdal.org/drivers/vector/geojson.html#layer-creation-options). I wasn't able to find in which exact version `QGIS` reached `GDAL 2.3`, but it definitely wasn't in `3.2 Bonn` and it definiely is in current `3.22 Białowieża` – mika.koshonson Feb 04 '22 at 21:16
0

I had the same problem and i managed with this little python script.

 import json

 with open('georef-spain-municipio.geojson') as json_file:
      data = json.load(json_file)
      i = 0
      for p in data['features']:
           i+=1
           p['id'] = i

 with open('georef-spain-municipio_id.geojson', 'w') as outfile:
 json.dump(data, outfile)

I hope it helps