stratified_packager.toolbelt.gpkg
Plugin-agnostic GeoPackage helpers: introspection, table dropping, style/metadata SQL.
Identifier and value quoting lives in the pure-stdlib sql; this module consumes
it and adds the GDAL/OGR and sqlite3 work.
DO NOT import from the qgis package in this module (it must stay usable from background
threads and scripts/); osgeo (GDAL/OGR), sqlite3 and the standard library only.
Style and metadata payloads are written by raw SQL exactly the way QGIS writes them; a table
is dropped through OGR (DeleteLayer also cleans the gpkg system tables and the r-tree).
Functions
|
Fold any WAL content into the main database file (pre-copy safety). |
|
Create an attribute index on columns of table (idempotent). |
|
Drop a feature table from the GeoPackage, including its system-table registrations. |
|
Count the rows of table. |
|
Return the geometry column name of table, or |
|
List the feature tables registered in a GeoPackage. |
|
Report whether table exists in the GeoPackage. |
|
Hold the database in WAL journal mode, with a live |
|
Insert a QGIS layer-metadata (QMD) document for table into the GeoPackage. |
|
Insert one named style row into the GeoPackage's |
- stratified_packager.toolbelt.gpkg._connect_readonly(gpkg)[source]
Open a read-only connection (a plain
connectwould create a missing file).- Parameters:
gpkg (
Path) – GeoPackage path (must exist).- Return type:
- Returns:
The read-only connection.
- stratified_packager.toolbelt.gpkg.checkpoint_wal(gpkg, /)[source]
Fold any WAL content into the main database file (pre-copy safety).
A GeoPackage that some connection switched to WAL journal mode (e.g. QGIS/OGR reads) carries
-wal/-shmsidecars; copying or zipping only the main file would then ship a stale database.wal_checkpoint(TRUNCATE)moves every committed frame into the main file and zeroes the WAL, after which the main file is complete on its own. The follow-upjournal_mode=DELETEtidies the sidecars away entirely but needs exclusive access, so its failure (another connection still holds the file) is ignored — a fully checkpointed WAL-mode file without its sidecars is still a valid, complete database.
- stratified_packager.toolbelt.gpkg.create_attribute_index(gpkg, table, columns, /)[source]
Create an attribute index on columns of table (idempotent).
Speeds the per-stratum key filter (
key_fields IN (...)) the GeoPackage provider pushes down when a staged copy is read once per stratum. A single multi-column index serves both the single-fieldINand the composite-key equality forms. A no-op when columns is empty or the index already exists.
- stratified_packager.toolbelt.gpkg.drop_table(gpkg, table, /)[source]
Drop a feature table from the GeoPackage, including its system-table registrations.
Delegates to OGR’s
DeleteLayer, which cleansgpkg_contents,gpkg_geometry_columns, the r-tree index tables and the OGR feature-count side table along with the table itself.
- stratified_packager.toolbelt.gpkg.geometry_column_of(gpkg, table, /)[source]
Return the geometry column name of table, or
''for attribute-only tables.
- stratified_packager.toolbelt.gpkg.layer_names(gpkg, /)[source]
List the feature tables registered in a GeoPackage.
- stratified_packager.toolbelt.gpkg.table_exists(gpkg, table, /)[source]
Report whether table exists in the GeoPackage.
- stratified_packager.toolbelt.gpkg.wal_session(gpkg, /)[source]
Hold the database in WAL journal mode, with a live
-walsidecar, for the block.A reader that opens a SQLite file in nolock mode (as QGIS’s OGR connection pool does for read-only GeoPackage opens) fails mid-statement when another connection materializes the
-walsidecar afterwards — but an open against a file whose sidecar already exists is detected and cleanly retried without nolock. Merely flipping the journal mode is not enough: closing the flipping connection auto-checkpoints and removes the sidecar again, so later opens still go nolock. This context manager therefore switches to WAL, materializes the sidecar with a no-opgpkg_contentstouch (a plain table: no r-tree triggers fire), and keeps its connection open so the sidecar outlives every open made inside the block;checkpoint_wal()reverts the file before it is copied or zipped.- Parameters:
gpkg (
Path) – GeoPackage path.- Yield:
Whether the file is now held in WAL journal mode (
False= missing file or a SQLite error; the caller may proceed — the session is log-hygiene, not correctness).
- stratified_packager.toolbelt.gpkg.write_layer_metadata(gpkg, /, *, table, qmd_xml)[source]
Insert a QGIS layer-metadata (QMD) document for table into the GeoPackage.
Creates the
gpkg_metadata/gpkg_metadata_referencetables and theirgpkg_extensionsregistrations when missing, then inserts the metadata row (scopedataset, QGIS standard URI) and itstable-scoped reference, mirroring how QGIS itself stores metadata.
- stratified_packager.toolbelt.gpkg.write_layer_style(gpkg, /, *, table, geometry_column, style_name, qml, sld='', use_as_default=False, description='')[source]
Insert one named style row into the GeoPackage’s
layer_stylestable.Creates the table and its
gpkg_contentsregistration (data typeattributes) when missing, mirroring how QGIS itself stores styles.- Parameters:
gpkg (
Path) – GeoPackage path (the caller must be its only writer).table (
str) – Feature table the style applies to.geometry_column (
str) – The table’s geometry column (empty for attribute tables).style_name (
str) – Style name shown in QGIS’ style manager.qml (
str) – Full QML document.sld (
str) – Optional SLD document (QGIS fills both; SLD is best-effort here).use_as_default (
bool) – Whether QGIS should load this style by default.description (
str) – Optional style description.
- Return type:
- stratified_packager.toolbelt.gpkg._GPKG_METADATA_DDL: Final = "\nCREATE TABLE IF NOT EXISTS gpkg_metadata (\n id INTEGER CONSTRAINT m_pk PRIMARY KEY ASC NOT NULL,\n md_scope TEXT NOT NULL DEFAULT 'dataset',\n md_standard_uri TEXT NOT NULL,\n mime_type TEXT NOT NULL DEFAULT 'text/xml',\n metadata TEXT NOT NULL DEFAULT ''\n)\n"
gpkg_metadataDDL per the GeoPackage metadata extension.
- stratified_packager.toolbelt.gpkg._GPKG_METADATA_REFERENCE_DDL: Final = "\nCREATE TABLE IF NOT EXISTS gpkg_metadata_reference (\n reference_scope TEXT NOT NULL,\n table_name TEXT,\n column_name TEXT,\n row_id_value INTEGER,\n timestamp DATETIME NOT NULL DEFAULT (strftime('%Y-%m-%dT%H:%M:%fZ','now')),\n md_file_id INTEGER NOT NULL,\n md_parent_id INTEGER,\n CONSTRAINT crmr_mfi_fk FOREIGN KEY (md_file_id) REFERENCES gpkg_metadata(id),\n CONSTRAINT crmr_mpi_fk FOREIGN KEY (md_parent_id) REFERENCES gpkg_metadata(id)\n)\n"
gpkg_metadata_referenceDDL per the GeoPackage metadata extension.
- stratified_packager.toolbelt.gpkg._LAYER_STYLES_DDL: Final = '\nCREATE TABLE IF NOT EXISTS "layer_styles" (\n "id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,\n "f_table_catalog" TEXT(256),\n "f_table_schema" TEXT(256),\n "f_table_name" TEXT(256),\n "f_geometry_column" TEXT(256),\n "styleName" TEXT(30),\n "styleQML" TEXT,\n "styleSLD" TEXT,\n "useAsDefault" BOOLEAN,\n "description" TEXT,\n "owner" TEXT(30),\n "ui" TEXT(30),\n "update_time" DATETIME DEFAULT (strftime(\'%Y-%m-%dT%H:%M:%fZ\',\'now\'))\n)\n'
layer_stylesDDL exactly as QGIS creates it (captured from a 4.0.3-written gpkg).
- stratified_packager.toolbelt.gpkg._QGIS_METADATA_STANDARD_URI: Final = 'http://mrcc.com/qgis.dtd'
md_standard_uriQGIS stamps on its layer-metadata rows.