Let me state up front that I'm totally new to the field of GIS. I'm doing a project involving a database of buildings and their geometries in my home country (the Netherlands). At the moment, I'm trying to read geometry data from a geopackage using a python script.
If I understand the geopackage specification correctly, geometry data in a geopackage is a form of a well-known-binary. I tried to find a python package that can translate the binaries in the geopackage to usable geometries. I.e., I want to be able extract the points/linestrings/etc from the binaries.
I thought I could find a python package that would be able to do this, but I either don't understand how they work or they don't work with the data I have.
As an example, I have the following binary:
or, as represented in python:
b'\x40\x71\x00\x00\x71\x3d\x0a\xd7\x05\xc3\x0e\x41\x23\xc3\x0e\x41\x71\x3d\x0a\xd7\xe3\x1c\x22\x41\xec\x1c\x22\x41\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xeb\x03\x00\x00\x01\x00\x00\x00\xcd\xcc\xcc\xcc\x10\xc3\x0e\x41\x5c\x8f\xc2\x41\x00\x00\x00\x00\x00\x00\x00\x00\x71\x3d\x0a\x41\xcd\xcc\xcc\xcc\xe5\x1c\x22\x41\x00\x00\x00\x00\xcd\xcc\xcc\xcc\x18\xc3\x0e\x41\x71\x3d\x0a\x41\x00\x00\x00\x00\x00\x00\x00\x00\x29\x5c\x8f\x41\x52\xb8\x1e\x85\xea\x1c\x22\x41\x00\x00\x00\x00\xcd\xcc\xcc\xcc\x10\xc3\x0e\x41\x5c\x8f\xc2\x41\x00\x00\x00\x00\x00\x00\x00\x00'
When I try to read a binary using for example the shapely package, I get an error:
from shapely import wkb
geom = b'\x40\x71\x00\x00\x71\x3d\x0a\xd7\x05\xc3\x0e\x41\x23\xc3\x0e\x41\x71\x3d\x0a\xd7\xe3\x1c\x22\x41\xec\x1c\x22\x41\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01\xeb\x03\x00\x00\x01\x00\x00\x00\xcd\xcc\xcc\xcc\x10\xc3\x0e\x41\x5c\x8f\xc2\x41\x00\x00\x00\x00\x00\x00\x00\x00\x71\x3d\x0a\x41\xcd\xcc\xcc\xcc\xe5\x1c\x22\x41\x00\x00\x00\x00\xcd\xcc\xcc\xcc\x18\xc3\x0e\x41\x71\x3d\x0a\x41\x00\x00\x00\x00\x00\x00\x00\x00\x29\x5c\x8f\x41\x52\xb8\x1e\x85\xea\x1c\x22\x41\x00\x00\x00\x00\xcd\xcc\xcc\xcc\x10\xc3\x0e\x41\x5c\x8f\xc2\x41\x00\x00\x00\x00\x00\x00\x00\x00'
point = wkb.loads(geom)
print(point)
shapely.errors.WKBReadingError: Could not create geometry because of errors while reading input.
To read geometry data from geopackages in python, will I have to write my own parser?
Best Answer
You can use the gdal/ogr, fiona (built on gdal/ogr) or geopandas (built on fiona) python libraries.
Below is a fiona example:
Partial output for one record in my data:
From the fiona docs: