4

I have a Geopandas GeoDataFrame with a couple of categorical columns that I want to write to a GeoPackage file. GeoPandas/Fiona/GeoPackage does not support categorical columns directly, so I'm changing those columns to their int codes before writing the GeoPackage and making a dict mapping code to category. So far so good.

Now, on reading in the GeoPackage file, I need to know which columns were categorical, and, ideally, get back the code--category mappings. And I'm stumbling trying to find a way to include that metadata with a GeoPackage file, at least as written from GeoPandas or Fiona. I see the GeoPackage standard provides for metadata, but I can't find any way to include it using Fiona or GeoPandas that survives writing the file and reading it back in.

4

The metadata tables in the GeoPackage are made for the metadata according to ISO: 19115-1:2014 Geographic information — Metadata — Part 1: Fundamentals (2014). It is also possible to register other metadata domains, including your application specific ones, into gpkg_metadata_reference but that feel all too complicated for your needs. I would rather have a look at the GeoPackage Schema extension http://www.geopackage.org/guidance/extensions/schema.html.

Probably you could utilize a Data Column Constraint of type "enum".

Data Column Constraints

The lowercase gpkg_data_column_constraints constraint_type column value specifies the type of constraint: "range", "enum", or "glob" (GLOB is a text pattern match - see [33]). The case sensitive value column contains an enumerated legal value for constraint_type "enum", a pattern match string for constraint_type "glob", or NULL for constraint_type "range". The set of value column values in rows of constraint_type "enum" with the same constraint_name contains all possible enumerated values for the constraint name.

With the enum constraint you could define and query which columns are categorized and which values are valid in each column. The code -- category mapping is supported directly because the table has place for code values and descriptions.

CREATE TABLE gpkg_data_column_constraints (
constraint_name TEXT NOT NULL,
constraint_type TEXT NOT NULL, // 'range' | 'enum' | 'glob'
value TEXT,
min NUMERIC,
min_is_inclusive BOOLEAN, // 0 = false, 1 = true
max NUMERIC,
max_is_inclusive BOOLEAN, // 0 = false, 1 = true
description TEXT,
CONSTRAINT gdcc_ntv UNIQUE (constraint_name, constraint_type, value)
)

This mail https://lists.osgeo.org/pipermail/gdal-dev/2021-April/053827.html suggests that it was not implemented but the corresponding pull request https://github.com/OSGeo/gdal/pull/3638 does have code for the GeoPackage driver and even on ogrinfo example about how the enumerated values appear in the ogrinfo report. In a comments you can also read discussion about how to access the field domains from fiona.

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.