phalcon/cphalcon · error · Phalcon\Db\Exceptions\InvalidWkb

Invalid WKB: unknown geometry type {baseType}

Error message

Invalid WKB: unknown geometry type {baseType}

What it means

After reading the geometry type code, WkbParser reduces it to a base type 1-7 (POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION). Any other value reaches the default branch and throws InvalidWkb('unknown geometry type N'), meaning the payload is corrupt or the bytes at the type position are not a WKB type code.

Source

Thrown at phalcon/Db/Geometry/WkbParser.zep:137

                        i       = i + 1;
                }

                if baseType === 4 {
                    return new MultiPoint(items, srid);
                }

                if baseType === 5 {
                    return new MultiLineString(items, srid);
                }

                if baseType === 6 {
                    return new MultiPolygon(items, srid);
                }

                return new GeometryCollection(items, srid);

            default:
                throw new InvalidWkb("unknown geometry type " . baseType);
        }
    }

    protected function readPoint(bool little, bool hasZ, bool hasM, int srid) -> <Point>
    {
        var x, y;

        let x = this->readDouble(little),
            y = this->readDouble(little);

        this->skipExtraOrdinates(little, hasZ, hasM);

        return new Point(x, y, srid);
    }

    protected function readPointList(bool little, bool hasZ, bool hasM) -> array
    {
        var count, i, x, y, points = [];

View on GitHub (pinned to b7419de9cd)

Solutions

  1. Verify the value at the source with the database's own functions (ST_AsBinary / ST_AsEWKB) and re-export the geometry
  2. Skip or quarantine rows that fail to parse instead of aborting the batch
  3. Confirm nothing transformed the value between fetch and parse (casts, serializers, JSON round-trips)

Example fix

// before
foreach ($rows as $row) {
    $geoms[] = $parser->parse($row['geom']);
}

// after
foreach ($rows as $row) {
    try {
        $geoms[] = $parser->parse($row['geom']);
    } catch (\Phalcon\Db\Exceptions\InvalidWkb $e) {
        $bad[] = $row['id'];
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

try {
    $geom = $parser->parse($raw);
} catch (\Phalcon\Db\Exceptions\InvalidWkb $e) {
    $badRows[] = $row['id']; // quarantine instead of aborting the batch
    $geom = null;
}

Prevention

When it happens

Trigger: Corrupt bytes stored in a geometry column; values that are neither hex nor MySQL-format WKB but survive the initial checks; offsets desynchronized from a wrong format detection so a coordinate byte is read as the type code.

Common situations: Application strings stored in a column later parsed as geometry; binary data damaged by UTF-8 conversion; vendor-specific encodings the parser does not understand.

Related errors


AI-assisted analysis of phalcon/cphalcon@b7419de9cd (2026-08-21). Data as JSON: /api/errors/a6ff3976de9c7221. Report an issue: GitHub.