NiVO / BY TECHNOGISGermany · Indonesia
GIS geometry interpretation

GeoPackage Geometry Headers: A Read-Only Drone Survey Audit Tutorial

Inspect a GeoPackage geometry BLOB header without confusing it with raw WKB, while preserving byte order, envelope flags and the spatial-reference identifier.

GeoPackage Geometry Headers: A Read-Only Drone Survey Audit Tutorial

Identify the feature before interpreting its stored bytes

A GeoPackage can open in a GIS while a custom export script fails to interpret one geometry value. One possible mistake is treating the complete geometry BLOB as raw Well-Known Binary, commonly called WKB. The GeoPackage representation has an additional header that needs interpretation first. For a NiVO survey delivery, begin with the accepted package, layer, geometry column and feature identity. This tutorial examines that encoding boundary. It does not diagnose an actual customer database, establish a NiVO export capability or claim that any survey geometry was decoded during article preparation.

Work from an identified, consistently released copy and preserve the received package. The earlier NiVO WAL tutorial addresses database-state consistency; this article addresses a geometry value within such a copy. Keep those questions separate. Record the source application, package revision, expected geometry type and intended use of the inspection. If the feature is missing or the delivered revision is uncertain, resolve that evidence question before interpreting arbitrary bytes. A successful header read from another row cannot establish that the required feature or the latest accepted geometry reached the recipient.

Separate the GeoPackage header from its WKB payload

The GeoPackage 1.4.0 encoding specification defines a geometry header containing the GP signature, a version byte, flags, a signed spatial-reference identifier and an optional envelope before the WKB geometry. The flags describe header byte order and other encoding properties. The WKB payload has its own byte-order declaration. Therefore skipping a fixed number of bytes or using the header order indiscriminately for the payload can misinterpret an otherwise readable record. Retain the original BLOB and use the declared structure to locate the geometry rather than searching for a byte sequence that happens to decode.

GDAL provides a primary implementation of this boundary in its GeoPackage utility code. Its header reader checks the signature and supported version, interprets flags and envelope dimensions, reads the spatial-reference identifier and calculates the header length before a separate geometry reader handles WKB. This is useful evidence for understanding the workflow, not a reason to copy an incomplete parser into production. A bounded teaching helper can expose selected declarations while leaving geometry decoding, extension support and full conformance checks to maintained software appropriate to the actual package and review task.

NiVO VTOL V7 with a white fuselage, lift rotors, and yellow wing tips
NiVO VTOL V7 with a white fuselage, lift rotors, and yellow wing tips.Source: NiVO / TechnoGIS catalogue materials supplied by the site owner · Catalogue photo
Product context only; this photograph does not depict the constructed examples in this article.

Inspect a bounded header without changing the geometry

The function below accepts an already selected byte string and reports a limited header interpretation. It performs no database query, write or coordinate conversion. It restricts the profile to the supported version and standard encoding, rejects reserved flags and invalid envelope codes, and checks that bytes remain beyond the calculated header. Header byte order controls the signed integer read through Python struct. The original bytes remain unchanged. No actual BLOB or Python execution is offered as evidence here, and the function should not be presented as a complete GeoPackage validator.

The helper also rejects an empty flag combined with a declared envelope, consistent with the reviewed empty-geometry rule. It does not inspect envelope values or prove that the empty flag agrees with the WKB content. Its returned offset merely locates where a supported reader should begin interpreting that content. Keep the geometry and its metadata relationship available for the next step. A successful return does not establish valid topology, correct coordinates, required dimensionality or a sound survey interpretation. If the bounded profile rejects a value, preserve the reason and investigate the actual encoding rather than editing bytes until it passes.

# Partial read-only inspection; not a full geometry or package validator.
import struct

def inspect_gpkg_header(blob):
    if len(blob) < 8 or blob[:2] != b"GP" or blob[2] != 0:
        raise ValueError("Outside the supported header profile")
    flags = blob[3]
    if flags & 0xC0 or flags & 0x20:
        raise ValueError("Reserved or extended encoding needs review")
    envelope_code = (flags >> 1) & 7
    if envelope_code > 4:
        raise ValueError("Invalid envelope code")
    empty = bool(flags & 0x10)
    if empty and envelope_code != 0:
        raise ValueError("Empty geometry must not declare an envelope")
    doubles = (0, 4, 6, 6, 8)[envelope_code]
    wkb_offset = 8 + doubles * 8
    if len(blob) <= wkb_offset:
        raise ValueError("Missing geometry payload")
    order = "<" if flags & 1 else ">"
    srs_id = struct.unpack_from(order + "i", blob, 4)[0]
    return {"header_byte_order": order, "srs_id": srs_id,
            "empty_flag": empty, "envelope_code": envelope_code,
            "wkb_offset": wkb_offset}

Relate the spatial-reference identifier to package metadata

After identifying the header, compare its reference declaration with the appropriate layer metadata and spatial-reference definition in the accepted package. The reviewed specification links geometry-column metadata to gpkg_spatial_ref_sys and the corresponding contents row. Preserve that relationship and any discrepancy found by the actual supported inspection. A numerical identifier is not a coordinate transformation, and changing a label cannot move vertices into a different project reference. Keep the defined reference, source coordinates and intended delivery reference distinct when explaining why a feature appears misplaced or a recipient tool interprets it unexpectedly.

Avoid assuming that every useful identifier must resemble a familiar authority code. Read the package definition and producing application evidence rather than guessing from the integer alone. If a transformation is required, use a documented operation with the relevant reference information and preserve the accepted source geometry. This tutorial supplies no transformation parameters or accuracy claim. It supports a more precise diagnostic question: which stored geometry, metadata declaration and reference definition are being compared? That question is more reproducible than repeatedly assigning coordinate systems until a layer happens to align with a background map.

Distinguish envelope, emptiness and actual geometry validity

An envelope is a compact extent declaration; it is not the full geometry. Likewise, an empty-geometry flag is not the same situation as a missing database value or a malformed byte sequence. Keep those cases separate in the review record. Use a maintained geometry reader to interpret the payload and suitable validation tools for the intended geometry type. Preserve actual diagnostics and representative feature identities. A header that appears plausible cannot establish that a polygon is valid, a line contains the expected survey interpretation or the stored bounds agree with all of the coordinates.

When a discrepancy appears, locate whether it belongs to packaging, metadata, binary interpretation, geometry structure or the project evidence. A repair operation can alter shapes or records, so it should produce a traceable candidate revision with its own acceptance checks. Do not overwrite the received BLOB merely to silence a custom parser. Compare the candidate against the preserved original using the defined task and actual results. If the available evidence supports only an encoding observation, state that limit instead of describing the survey deliverable as either fully validated or conclusively defective.

Deliver the encoding finding with a clear scope

A useful handoff names the package revision, feature table, geometry column, stable feature identity, tool version and the actual interpretation performed. Retain the source bytes through the preserved package and record any reported flags, reference identifier and payload boundary. Explain which subsequent geometry and reference checks were completed and which remain unresolved. Another analyst should be able to locate the same feature without relying on a row order that may change during export. Selected header checks should remain selected checks, with no implication that every layer, extension or feature in the container passed conformance testing.

For NiVO survey discussions, this turns an obscure binary-read error into a specific question about the header, payload and metadata relationship. It separates representation from the quality of the source survey and keeps a small diagnostic recipe within its actual capabilities. Preserve the limitations and identify the next evidence needed before making a delivery decision. The resulting review is easier to reproduce because it retains the accepted original, names the encoding boundary being examined and avoids claiming that a parsed integer, a visible envelope or a successful file opening establishes spatial correctness on its own.

Technical references

  1. OGC GeoPackage 1.4.0 — geometry encoding and reference metadata ↗
  2. GDAL GeoPackage utility implementation — header and WKB boundaries ↗
  3. Python struct — explicit byte order and unpack_from ↗
← All articles

Related articles

All articles ↗
LET’S TAKE FLIGHT

Your next mission
starts with a conversation.

Talk to our team