2

I am currently looking to use ogr2ogr to convert the coordinate reference system of a GeoJSON file. I want the output file to have the same name as the input file. This is on a Linux PC running Ubuntu 20.04 LTS on Oracle Cloud.

This is the code I tried first with the output below

ogr2ogr -s_srs EPSG:32614 -t_srs EPSG:4326 -f GEOJSON plot_boundary.geojson plot_boundary.geojson

ERROR 1: Source and destination datasets must be different in non-update mode.

This is the code I tried second with the output below

ogr2ogr -update -s_srs EPSG:32614 -t_srs EPSG:4326 -f GEOJSON plot_boundary.geojson plot_boundary.geojson

ERROR 1: Layer plot_boundary already exists, and -append not specified.
        Consider using -append, or -overwrite.
ERROR 1: Terminating translation prematurely after failed
translation of layer plot_boundary (use -skipfailures to skip errors)

This is the code I tried third with the output below

ogr2ogr -overwrite -s_srs EPSG:32614 -t_srs EPSG:4326 -f GEOJSON plot_boundary.geojson plot_boundary.geojson

ERROR 5: -nln name must be specified combined with a single source layer name,
or a -sql statement, and name must be different from an existing layer.
2
  • 2
    Not possible with GeoJSON. Create a new GeoJSON file, delete the original and finally rename.
    – user30184
    Mar 17, 2021 at 17:13
  • 1
    @user30184 post it as an answer and I will accept it. Mar 17, 2021 at 17:22

1 Answer 1

3

Ogr2ogr cannot be used for updating an existing layer. It is better not to try any tricks but simply convert GeoJSON data into a new file and after that delete the original file and rename the result file. Because GeoJSON is a text based format it must be re-written in any case so trying to do something more clever with updates in-place does not make sense, even it might do so with some database formats.

The error message seems to suggest that ogr2ogr in update mode might work but is does not, not even for adding new features.

ogr2ogr -of geojson -update -append point.json point.json
ERROR 5: -nln name must be specified combined with a single
source layer name, or a -sql statement, and name must be 
different from an existing layer.

Updates are supported to some extent with ogrinfo and SQL. This command almost works with GeoJSON.

ogrinfo -dialect SQLite -sql "update point set geometry=ST_Transform(geometry,3857)" point.json

The geometries get transformed but the coordinate reference system in the GeoJSON file is not rewritten and the result is probably unusable. Re-projecting geometries in-place will also fail with more advanced database formats because the tables have SRID (projection) constraints. At some moment the table should contain some features in the original SRID and other features in the new SRID and that is not possible. Thus even with databases the workflow to re-project data is to create a new table, drop the original, and rename. However, for other kind of updates ogrinfo with SQL is a very handy tool.

Your Answer

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

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