[gdal-dev] ogr2ogr - id field in database table is not being written to geojson file

Even Rouault even.rouault at spatialys.com
Fri Sep 14 08:51:12 PDT 2018


On vendredi 14 septembre 2018 16:25:27 CEST David McKelvie wrote:
> Hi;
> 
> I have a question about ogr2ogr output from a postgres database to geojson.
> 
> One of the fields in my table, ie the primary key called 'id' is not being
> output to the json file. I wonder if this is a bug or a 'feature'.
> 
> Can anybody clarify this?
> 
> The database table is:
> -------------------------------
> \d areas
>               Table "public.areas"
>       Column       |            Type
> -------------------+-----------------------------
>  id                | integer
>  type              | character varying(31)
>  name              | character varying(255)
>  created           | timestamp without time zone
>  last_modified     | timestamp without time zone
>  location          | geometry(Geometry,27700)
>  is_deleted        | boolean
>  ons_code          | character varying(9)
>  extended_location | geometry(Geometry,27700)
>  uprn              | character varying(12)
>  info              | text
>  name_vector       | tsvector
> --------------------------
> 
> The command run was:
> ----------------------
> ogr2ogr -f "GeoJSON" \
> -lco "COORDINATE_PRECISION=2" \
> /tmp/divisions.json \
> "PG:dbname=capetown" areas
> ----------------------
> 
> The output file /tmp/divisions.json
> had entries with properties like:
> --------------------------------------
> properties": {
>    "type": "division",
>    "name": "Divn  01",
>    "created": "2018\/09\/14 12:19:41",
>    "last_modified": "2018\/09\/14 12:19:41",
>    "is_deleted": 0,
>    "ons_code": null,
>    "uprn": null,
>    "info": null,
>    "name_vector": "'01':2 'divn':1"
> }
> ---------------------------------------
> 
> So all the database table fields EXCEPT for
> the geometry fields and 'id' which is defined as
>     id | integer | not null default nextval('areas_id_seq'::regclass)
> and is the primary key for the table.
> 
> https://www.gdal.org/drv_geojson.html does not mention anything special
> about 'id' or primary key fields.
> 
> I am using ogr2ogr on Ubuntu Linux from the gdal-bin package . version
> 1.11.3+dfsg-3build2


There's a specific processing for the 'id' field indeed. It is written at the 
'id' JSon member, at the same level of 'properties'. And thus on reading, it 
is exposed in the OGR Feature ID, not as an attribute.
Note: There have been some variations in recent GDAL versions about this 
behaviour (particularly if the id field is string)

Even

-- 
Spatialys - Geospatial professional services
http://www.spatialys.com


More information about the gdal-dev mailing list