locationtech / locationtech/jts

WKTWriter emits invalid dimensional EMPTY tokens for nested empty geometries

Open
#1,223 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
2.2k
Forks
475
Avg merge
14d 10h
Merged PRs (30d)
1

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in WKTWriter, especially appendOrdinateText, and reproduce the nested empty Z, M, and ZM cases from the issue. Add regression coverage for writer-to-reader round trips and verify that dimensional markers and EMPTY remain separate tokens.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Quiet
Clarity
Clearly specified
Newbie friendliness
78/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.