Skip to content

WKTWriter emits invalid dimensional EMPTY tokens for nested empty geometries #1223

Description

@jiayuasu

Description

WKTWriter can emit an invalid dimensional token when a non-empty geometry collection contains an empty child geometry. The dimension marker inherited from the collection is concatenated directly with EMPTY, producing ZEMPTY, MEMPTY, or ZMEMPTY.

The generated text cannot be read back by JTS's own WKTReader.

Reproducer

Using JTS 1.20.0:

import org.locationtech.jts.geom.Geometry;
import org.locationtech.jts.io.ParseException;
import org.locationtech.jts.io.WKTReader;
import org.locationtech.jts.io.WKTWriter;

WKTReader reader = new WKTReader();
Geometry geometry =
    reader.read(
        "GEOMETRYCOLLECTION Z ("
            + "LINESTRING Z (0 0 1, 1 1 2), "
            + "MULTILINESTRING Z EMPTY)");

String written = new WKTWriter(4).write(geometry);
System.out.println(written);
reader.read(written);

Actual output:

GEOMETRYCOLLECTION Z(LINESTRING Z(0 0 1, 1 1 2), MULTILINESTRING ZEMPTY)

The final line then throws:

org.locationtech.jts.io.ParseException: Expected EMPTY or ( but found 'ZEMPTY' (line 1)

The corresponding measured and ZM inputs fail in the same way:

MULTILINESTRING MEMPTY
MULTILINESTRING ZMEMPTY

Expected behavior

WKTWriter should keep the dimension marker and EMPTY as separate tokens, for example:

MULTILINESTRING Z EMPTY
MULTILINESTRING M EMPTY
MULTILINESTRING ZM EMPTY

Every WKT produced by WKTWriter should be accepted by WKTReader, subject to the configured output dimensions.

Notes

The issue appears to be that appendOrdinateText writes the dimension marker without a trailing separator, after which the empty geometry text writes EMPTY immediately. The same code remains present on current master at 03517d9b4249f7712569c4016c0d0c7d055f4b07.

Regression coverage should include nested empty Z, M, and ZM geometries and writer-to-reader round trips.

Environment

  • JTS 1.20.0
  • OpenJDK 17.0.13

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions