0

Is there any way to remove some layers from geopackage instead of creating new geopackage with only layers I need.

Example:- I have a geopackage containing:

  1. Bars (Point)
  2. Bars_reprojected (Point)
  3. Metro_reprojected (Point)
  4. metro_stations_buffer (Polygon)
  5. Metro (Point)

I want to delete Bars (Point) and Metro (Point) from this geopackage.

1
  • Using what software, programming language, or tools?
    – Spacedman
    Sep 26 at 6:50

1 Answer 1

4

You can do it with ogrinfo by using the -sql option. The usage is documented in https://gdal.org/drivers/vector/gpkg.html

Starting with GDAL 2.2, the “DROP TABLE layer_name” and “ALTER TABLE layer_name RENAME TO new_layer” statements can be used. They will update GeoPackage system tables.

Updating the system files is important and therefore you should not drop the table with a standard SQLite client like DB Browser.

Notice also this:

When dropping a table, or removing records from tables, the space they occupied is not immediately released and kept in the pool of file pages that SQLite may reuse later. If you need to shrink the file to its minimum size, you need to issue an explicit “VACUUM” SQL request. Note that this will result in a full rewrite of the file.

So run these two commands:

ogrinfo -sql "DROP TABLE layer_name" my_geopackage.gpkg
ogrinfo -sql "VACUUM" my_geopackage.gpkg

Deleting the layer is very fast but if you want also to shrink the database file then it would be as fast or even faster to create a new geopackage with only the layers that you need.

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.