2

I am trying to modify a dataset by replacing the geometry of each feature by its buffer. I try to do it directly, in update mode (not very secure I know). Here is what I tried:

ogr2ogr -update test.gpkg test.gpkg -dialect sqlite -sql "UPDATE test SET geom = ST_buffer(geom,100000)"

but it does not work. No error is returned but the dataset has then empty geometries.

Any idea about something I missed ?

The geometry column is geom and the CRS is in meters, so that the distance 100000 is realistic.

5
  • Are the original geometries in the "geom" column already polygons, or do they use the generic "geometry" type? Is the SRID of your geometries something meter based so that buffer or 100 kilometers makes sense? Buffer of hundred thousand degrees would not make sense.
    – user30184
    Nov 9, 2020 at 20:30
  • Do you get the SpatiaLite version with command ogrinfo -sql "select spatialite_version()" test.gpkg -dialect sqlite? I need to use -dialect indirect_sqlite.
    – user30184
    Nov 9, 2020 at 20:50
  • I am interested in the geometry type, not in the name of the geometry column. What do you see in ogrinfo report after Geometry:. See the report in my answer as an example.
    – user30184
    Nov 9, 2020 at 20:52
  • Oh, I was blind, sorry. Ogr2ogr is not the tool for updates. Use ogrinfo. You can also test that your SQL makes sense first with ogrinfo test.gpkg -sql "select ST_Buffer(geom,100000) from test.
    – user30184
    Nov 9, 2020 at 21:00
  • 1
    Your question is good, you are not the first person who gets surprised because ogrinfo can do updates.
    – user30184
    Nov 9, 2020 at 21:10

1 Answer 1

4

You are trying to use wrong tool for updating your GeoPackage. It is undefined what your ogr2ogr command does and it is not worth further investigations. It is just wrong tool.

Use ogrinfo instead. It works for me with GDAL 3.2 and with the following command.

I made this GeoPackage to start with:

ogrinfo polygon.gpkg -al
INFO: Open of `polygon.gpkg'
      using driver `GPKG' successful.

Layer name: polygon
Metadata:
  DBF_DATE_LAST_UPDATE=2020-11-06
Geometry: Polygon
Feature Count: 1
Extent: (665.000000, 155.000000) - (946.000000, 372.000000)
Layer SRS WKT:
(unknown)
FID Column = fid
Geometry Column = geom
OGRFeature(polygon):0
  POLYGON ((665 294,817 372,946 155,665 294))

Notice that the geometry type is Polygon. ST_Buffer function creates polygons as well so there will me no problem in updating the geometries. If I had geometry type Point then triggers would prevent the insertion of polygons into that table.

Then I run ogrinfo command with SQL dialect "indirect_SQLite" in order to get the SpatiaLite functions. Otherwise GDAL is running commands directly against GeoPackage with the standard functions without loading the mod_spatialite extension. In that case ST_Buffer is not found and GDAL errors out.

ogrinfo -dialect indirect_sqlite -sql "update polygon set geom=ST_Buffer(geom,10)" polygon.gpkg

The geometry of the only feature in the GeoPackage has been changed.

OGRFeature(polygon):0
  POLYGON ((660.566182558625 285.036671215639,660.101288123696 285.2820517349,659.649930712494 285.551523295949,659.213357599026 285.844341245292,658.792775202....
3
  • Thank you ! I did not expect to use ogrinfo for that... It is quite obscure how the ogr2ogr -update parameter should be used then...
    – julien
    Nov 10, 2020 at 11:21
  • It is documented but it is indeed obscure for new users -update Open existing output **datasource** in update mode rather than trying to create a new one. Datasource is one object for GDAL and layer is another one. Datasource has one or more layers and in your case datasource is the gpkg file. So instead of creating a new you edit an existing GeoPackage. But on layer level ogr2ogr does not have an option to update or delete. It can only insert new features, possibly into an existing layer with -append Append to existing **layer** instead of creating new.
    – user30184
    Nov 10, 2020 at 12:05
  • Thanks a lot, your answer worked. I only had to use a update lay set geom=ST_Multi(ST_Buffer(geom,0)) so that all geometries are seen as multi polygons. Thanks !
    – julien
    Nov 10, 2020 at 16:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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