elastic/elasticsearch · error · UnsupportedOperationException

line ring cannot be serialized using WKT

Error message

line ring cannot be serialized using WKT

What it means

Thrown by the getWKTName visitor when serializing a LinearRing to WKT. A LinearRing is a closed LineString used only as a building block (the boundary of a Polygon or a ring in a MultiPolygon); it has no standalone WKT keyword, so attempting to write one is unsupported by design.

Source

Thrown at libs/geo/src/main/java/org/elasticsearch/geometry/utils/WellKnownText.java:802

        return geometry.visit(new GeometryVisitor<String, RuntimeException>() {
            @Override
            public String visit(Circle circle) {
                return "CIRCLE";
            }

            @Override
            public String visit(GeometryCollection<?> collection) {
                return "GEOMETRYCOLLECTION";
            }

            @Override
            public String visit(Line line) {
                return "LINESTRING";
            }

            @Override
            public String visit(LinearRing ring) {
                throw new UnsupportedOperationException("line ring cannot be serialized using WKT");
            }

            @Override
            public String visit(MultiLine multiLine) {
                return "MULTILINESTRING";
            }

            @Override
            public String visit(MultiPoint multiPoint) {
                return "MULTIPOINT";
            }

            @Override
            public String visit(MultiPolygon multiPolygon) {
                return "MULTIPOLYGON";
            }

            @Override

View on GitHub (pinned to db6a809a66)

Solutions

  1. Wrap the LinearRing in a Polygon before serializing: a Polygon's shell is a LinearRing, and Polygon has a valid WKT representation.
  2. If iterating a collection, filter or convert LinearRing elements to Polygon (or LineString if an open ring is acceptable) before writing.
  3. Avoid calling toWKT on a bare LinearRing; use it only as a component of a Polygon.

Example fix

// before: serializing a bare LinearRing
LinearRing ring = new LinearRing(new double[]{...}, new double[]{...});
String wkt = WellKnownText.toWKT(ring); // throws

// after: wrap in a Polygon
Polygon polygon = new Polygon(ring);
String wkt = WellKnownText.toWKT(polygon); // "POLYGON ((...))"
Defensive patterns

Strategy: type-guard

Type guard

// Guard against serializing a bare LinearRing
static boolean isDirectlySerializable(Geometry g) {
    return !(g instanceof LinearRing);
}
// before writing:
if (g instanceof LinearRing ring) {
    g = new Polygon(ring); // wrap into a Polygon which has a WKT keyword
}
String wkt = WellKnownText.toWKT(g);

Try / catch

try {
    String wkt = WellKnownText.toWKT(geometry);
} catch (UnsupportedOperationException e) {
    if (e.getMessage().contains("line ring")) {
        // wrap the LinearRing in a Polygon and retry
        geometry = new Polygon((LinearRing) geometry);
        wkt = WellKnownText.toWKT(geometry);
    } else throw e;
}

Prevention

When it happens

Trigger: Calling WellKnownText.toWKT (or any write path) on a Geometry that is a bare LinearRing rather than a Polygon. The visitor's visit(LinearRing) branch throws UnsupportedOperationException immediately because there is no WKT name for it.

Common situations: Code that constructs a LinearRing directly (e.g., for use as a polygon boundary) and then accidentally serializes it as a top-level geometry; a generic serializer iterating a GeometryCollection containing a LinearRing; test fixtures using LinearRing where Polygon was intended.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/dba70b54354c1775. Report an issue: GitHub.