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
Description
WKTWritercan 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 withEMPTY, producingZEMPTY,MEMPTY, orZMEMPTY.The generated text cannot be read back by JTS's own
WKTReader.Reproducer
Using JTS 1.20.0:
Actual output:
The final line then throws:
The corresponding measured and ZM inputs fail in the same way:
Expected behavior
WKTWritershould keep the dimension marker andEMPTYas separate tokens, for example:Every WKT produced by
WKTWritershould be accepted byWKTReader, subject to the configured output dimensions.Notes
The issue appears to be that
appendOrdinateTextwrites the dimension marker without a trailing separator, after which the empty geometry text writesEMPTYimmediately. The same code remains present on currentmasterat03517d9b4249f7712569c4016c0d0c7d055f4b07.Regression coverage should include nested empty Z, M, and ZM geometries and writer-to-reader round trips.
Environment