TransWikia.com

How to keep feature ID within GeoJSON ogr2ogr import to PostGIS

Geographic Information Systems Asked on June 2, 2021

I’m attempting to import a GeoJSON file into PostGIS using ogr2ogr

The basic command is

ogr2ogr -f "PostgreSQL" PG:"dbname=my_db user=postgres" my_file.geojson -preserve_fid -nln geo_table -append

Using this sample how do I rewrite it so the id becomes a id Colum in the database table?

{"type":"Polygon","id":123,"coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]}

Produces

ERROR 1: ERROR: setval: value 0 is out of bounds for sequence

The "real" file contains both Polygon & MultiPolygon

One Answer

From the GeoJSON specification https://tools.ietf.org/html/rfc7946:

If a Feature has a commonly used identifier, that identifier SHOULD be included as a member of the Feature object with the name "id", and the value of this member is either a JSON string or number.

Your GeoJSON has id at wrong level as a member of Polygon object. Move it higher.

{ "type": "Feature", "id": 123, "properties": {"geometry":{"type":"Polygon","coordinates":[[[100,0],[101,0],[101,1],[100,1],[100,0]]]}}}

Correct answer by user30184 on June 2, 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