2

I'm trying to figure out a best practice for determining what are the names of layers in a GeoPackage and counting the total number of them.

I've been able to list all the layers of a GeoPackage using gdal info mypkg.gpkg but it only lists the layers, not count them.

The solution I need is not necessarily for scripting but simply for verifying whether my processes have successfully run. If there is a method to check via bash, QGIS3, Python, that would be very helpful.

EDIT: I should specify that the contents would include not only vector layers but raster layers as well.

  • If your tool is returning a python list then in python len(some list) will return a count. – Hornbydd Jun 29 '18 at 16:38
2

Use your favorite tool and make a SQL query to the "gpkg_contents" metadata table. Every GeoPackage has this table because it is mandatory. The structure of the table is defined in the standard as:

CREATE TABLE gpkg_contents (
  table_name TEXT NOT NULL PRIMARY KEY,
  data_type TEXT NOT NULL,
  identifier TEXT UNIQUE,
  description TEXT DEFAULT '',
  last_change DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),
  min_x DOUBLE,
  min_y DOUBLE,
  max_x DOUBLE,
  max_y DOUBLE,
  srs_id INTEGER,
  CONSTRAINT fk_gc_r_srs_id FOREIGN KEY (srs_id) REFERENCES gpkg_spatial_ref_sys(srs_id)
);

Currently the value of data_type can be either "features", "tiles", or "attributes". Thus if you prefer to use ogrinfo and you are interested in knowing the number of vector layers you can get the answer with a command like this:

ogrinfo gpkg_with_two_vector_layers.gpkg -sql "select count(*) from gpkg_contents where data_type='features'"
INFO: Open of `gpkg_with_two_vector_layers.gpkg'
      using driver `GPKG' successful.

Layer name: SELECT
Geometry: None
Feature Count: 1
Layer SRS WKT:
(unknown)
count(*): Integer (0.0)
OGRFeature(SELECT):0
  count(*) (Integer) = 2
  • This is really good to know. Unfortunately this is only valid for vector layers. Do you know of a similar process when the instances include raster layers? – Trevor J. Smith Jun 29 '18 at 17:50
  • 2
    If you read my answer or GeoPackage standard you should discover that you must just use 'tiles' instead of 'features'. – user30184 Jun 29 '18 at 17:53
  • Right. I haven't read too much on the SQL standards for storing raster data. I wasn't aware that "tiles" is the format. Sorry about that and thanks! – Trevor J. Smith Jun 29 '18 at 17:57

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.