OtterMind/Chat2DB · info · IllegalArgumentException

EWKB linear ring must contain at least four coordinates

Error message

EWKB linear ring must contain at least four coordinates

What it means

Thrown by PostgreSQLGeometryProcessor.inspectLinearRing when parsing the ring of a POLYGON in EWKB data. Per the OGC SFA specification, every linear ring (exterior or interior boundary of a polygon) must contain at least four coordinates, with the first equal to the last to close the ring. A declared count below four is structurally invalid. This runs inside convertJDBCValueByType and is caught at line 80, so the raw hex value is returned gracefully.

Source

Thrown at chat2db-community-server/chat2db-community-plugins/chat2db-community-postgresql/src/main/java/ai/chat2db/plugin/postgresql/value/sub/PostgreSQLGeometryProcessor.java:241

            allFinite &= Double.isFinite(ordinate);
        }
        if (!allNaN && !allFinite) {
            throw new IllegalArgumentException("EWKB point contains an invalid empty coordinate");
        }
    }

    private void inspectLineString(WkbCursor cursor, ByteOrder byteOrder, int coordinateDimension) {
        int coordinateCount = readCount(cursor, byteOrder);
        if (coordinateCount == 1) {
            throw new IllegalArgumentException("EWKB line string must be empty or contain at least two coordinates");
        }
        inspectFiniteCoordinates(cursor, byteOrder, coordinateCount, coordinateDimension);
    }

    private void inspectLinearRing(WkbCursor cursor, ByteOrder byteOrder, int coordinateDimension) {
        int coordinateCount = readCount(cursor, byteOrder);
        if (coordinateCount < 4) {
            throw new IllegalArgumentException("EWKB linear ring must contain at least four coordinates");
        }

        double[] firstCoordinate = readFiniteCoordinate(cursor, byteOrder, coordinateDimension);
        inspectFiniteCoordinates(cursor, byteOrder, coordinateCount - 2, coordinateDimension);
        double[] lastCoordinate = readFiniteCoordinate(cursor, byteOrder, coordinateDimension);
        for (int i = 0; i < 2; i++) {
            if (firstCoordinate[i] != lastCoordinate[i]) {
                throw new IllegalArgumentException("EWKB linear ring is not closed");
            }
        }
    }

    private void inspectFiniteCoordinates(WkbCursor cursor, ByteOrder byteOrder,
                                          int coordinateCount, int coordinateDimension) {
        for (int coordinate = 0; coordinate < coordinateCount; coordinate++) {
            for (int ordinate = 0; ordinate < coordinateDimension; ordinate++) {
                if (!Double.isFinite(cursor.readDouble(byteOrder))) {
                    throw new IllegalArgumentException("EWKB coordinate is not finite");

View on GitHub (pinned to 5ee1e990e7)

Solutions

  1. No action required in production — convertJDBCValueByType catches this and returns the raw hex value
  2. Repair the source data so every polygon ring has >=4 coordinates (including the closing point)
  3. Use ST_MakeValid or ST_IsValidDetail in PostgreSQL to detect and reconstruct valid polygon rings

Example fix

-- before: polygon ring with only 3 points (invalid)
SELECT ST_GeomFromEWKT('POLYGON((0 0, 1 0, 1 1))');
-- after: polygon ring with 4 closed points
SELECT ST_GeomFromEWKT('POLYGON((0 0, 1 0, 1 1, 0 0))');
Defensive patterns

Strategy: try-catch

Validate before calling

// Already caught in convertJDBCValueByType. For direct testing:
try {
    processor.convertJDBCValueByType(dataValue);
} catch (Exception e) {
    // raw value returned; check debug logs for details
}

Try / catch

try {
    WkbHeader header = inspectEwkb(ewkb);
} catch (IllegalArgumentException e) {
    log.debug("EWKB validation failed", e);
    return value;
}

Prevention

When it happens

Trigger: Converting a PostgreSQL POLYGON geometry (baseType 3, dispatched at lines 202-207) whose EWKB declares a ring with fewer than 4 coordinates. Each ring is validated via inspectLinearRing at line 205.

Common situations: Corrupt polygon data; rings produced by a buggy serialization library; data migrated from a system with looser ring validation; manually crafted EWKB binary.

Related errors


AI-assisted analysis of OtterMind/Chat2DB@5ee1e990e7 (2026-08-14). Data as JSON: /api/errors/245c22d9528243d9. Report an issue: GitHub.