http://qs321.pair.com?node_id=11141817


in reply to Geo Package files

You can use Geo::GDAL::FFI to read the contents of a geopackage (untested example code below). The geometry field is stored as Well Known Binary (WKB), or perhaps a variant of it. This can be converted to Well Known Text (WKT) to be human readable.

A definition of WKB is given in the libgeos docs at https://libgeos.org/specifications/wkb, and WKT at https://libgeos.org/specifications/wkt/.

The GDAL stack can be a beast to install if the aliens have to compile everything from source (GDAL, Proj, GEOS, libtiff, libsqlite3, optionally also spatialite, freexl and curl). If you are on a unix type machine then install the gdal-dev package using your system package manager, then the GDAL aliens will run system installs.

Example code:

# adapted from the Geo::GDAL::FFI docs, untested my $layer_name = 'some_layer'; my $db = Open('test.gpkg'); my $layer = $dataset->GetLayer($layer_name); $layer->ResetReading; while (my $feature = $layer->GetNextFeature) { my $value = $feature->GetField('name'); my $geom = $feature->GetGeomField; say $value, ' ', $geom->AsText; }

Update: More details of the geopackage geometry format are at http://www.geopackage.org/spec131/index.html#gpb_format. This should be of use if you decide to write your own parser to extract the first and last coordinates of each linestring.

Replies are listed 'Best First'.
Re^2: Geo Package files
by Bod (Parson) on Mar 04, 2022 at 00:57 UTC

    Alas I am on Windows and the way to install GDAL seems to be via Anaconda.

    If I can suss out the correct template for unpack then I shall do it that way. But if that proves impossible, or at least too tricky for me, then I'll come back to GDAL.

    The WKB link is certainly helpful, thanks.

      You could have a look at the GIS Internals stable releases. They provide precompiled binaries for windows with a broad range of dependencies (including almost all those I listed in my previous post). If the GIS Internals paths can be found by the various gdal-stack aliens then they will run system installs.

      Edit: Be sure to also get the download flagged as "compiled libraries and headers"

      Another edit: I just tested with the GIS Internals files and the alienfile probe for geos does not work (the probe currently uses geos-config, which is not included).

      Rather than try and get things to work on the inferior OS you might find it worth the effort (to say nothing of being less headaches) to install a docker host and see if you can't get a dedicated environment running a decent real OS instead. With that available then you could install the more off-the-shelf tool chain into it more easily (theoretically . . .). Instead of trying to jump through all these hoops getting this (as you say elsewhere in the thread) occasional procedure running "native" you can just spin up a container to process the results and dump the output out as you need it into a shared volume that you then work with from your "normal" platform.

      Edit: Looks like there's an osgeo/gdal available which you probably could build upon.

      The cake is a lie.
      The cake is a lie.
      The cake is a lie.

        As I've reached the point I've got to, my gut feeling is that it is worth continuing. Perhaps not for this project, but certainly for what I am learning in the process of doing this. Previously, the only use I've made of unpack is to split up an authentication string.

        I think knowing more about pack and unpack would be beneficial to me. Thanks to soonix and Marshall my understanding of these two functions in improving. That alone makes continuing worthwhile.