3

I have a gpkg of several layers. How can I rename them in the gpkg as well as in the QGIS layers list?

Basically, I have layer foo in my QGIS project, whose source is filename.gpkg|layername=foo. I now want to call it bar.

It's easy (rightclick, rename) to change foo to bar in the QGIS project layer list. But it's still called foo inside the gpkg. I can't rename it inside the gpkg from the GeoPackage connection inside the QGIS browser: it lets me rightclick and choose rename, but OK is greyed out (presumably since it's in use).

As a workaround, I can rename it bar in the QGIS project layer list, export it from the QGIS project layer to a temporary gpkg (where it becomes named bar and foo is forgotten), import it from there as a duplicate to the right gpkg, use the changeDataSource plugin to point the project layer to the new name, and then clean up by deleting the old foo in the gpkg. And eventually vacuum the db.

But that's a hassle given I've set up a naming convention to use across projects, and now need to rename 20+ layers across several gpkg to reflect it (and so I and others can re-use those gpkgs in other projects).

3

You can rename the tables in GeoPackage with the DB Manager

enter image description here

Another option is to use ogrinfo with the OGR SQL dialect as documented in https://www.gdal.org/drv_geopackage.html. Usage:

ogrinfo my_geopackage.gpkg -sql "ALTER TABLE layer_name RENAME TO new_layer"
  • Helpful, thanks. The DB Manager approach, however, unloads the layer from the current project so you have to manually re-add it (and hopefully you saved the style...). Good find on the orginfo (would still need to use changeDataSource, of course); I didn't think of using a -sql but am reassured from those docs that '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.' – Houska Mar 11 at 23:57
  • Renaming the table is not a simple task because the corresponding table that holds the spatial index must be renamed as well, a bunch of database triggers must be deleted and created again, and records in the geopackage metadata tables must be updated. – user30184 Mar 12 at 8:51

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.