Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Writing and reading WKB loses M dimension #733

Open
kaiwinter opened this issue May 26, 2021 · 2 comments · May be fixed by #1092
Open

Writing and reading WKB loses M dimension #733

kaiwinter opened this issue May 26, 2021 · 2 comments · May be fixed by #1092

Comments

@kaiwinter
Copy link
Contributor

When using WKBWriter to write a Geometry which has XYM coordinates and afterwards using WKBReader for reading the result. The Geometry has XYZ coordinates instead of XYM coordinates.

Here is small example:

    String wkt = "MULTILINESTRING M((1 1 1, 2 2 2))";
    WKTReader wktReader = new WKTReader();
    Geometry geometryBefore = wktReader.read(wkt);

    WKBWriter wkbWriter = new WKBWriter(3);
    byte[] write = wkbWriter.write(geometryBefore);

    WKBReader wkbReader = new WKBReader();
    Geometry geometryAfter = wkbReader.read(write);

    System.out.println(Arrays.asList(geometryBefore.getCoordinates()));
    System.out.println(Arrays.asList(geometryAfter.getCoordinates()));

When running this you get the following output:

[(1.0, 1.0 m=1.0), (2.0, 2.0 m=2.0)]
[(1.0, 1.0, 1.0), (2.0, 2.0, 2.0)]

The geometryBefore has XYM coordinates. The geometryAfter has XYZ coordinates.
In WKBReader.readGeometry are two variables evaluated hasZ and hasM. In the above example hasZ is true and hasM is false. I think this is because WKBWriter.writeGeometryType always writes 0x80000000 regardless of the coordinate type. Debugging WKBReader.readGeometry makes me think 0x40000000 should be used for M coordinates in WKBWriter.writeGeometryType.

What do you think? Can this be improved or did I just misunderstood how this should work.

@kaiwinter kaiwinter changed the title Writing and reading WKB looses M dimension Writing and reading WKB loses M dimension May 26, 2021
@jnh5y
Copy link
Contributor

jnh5y commented May 26, 2021

I think you've identified a bug here. Any interest in working on it?

The Contributing guide has some info about that here: https://github.com/locationtech/jts/blob/master/CONTRIBUTING.md

@kaiwinter
Copy link
Contributor Author

Yes sure. Currently I'm using a workaround with a GeometryFactory but I would love to get rid of it as it most probably impacts performance.

When thinking of a fix my first thought was to do an instanceof check of the result of g.getCoordinate() in writeGeometry. But that would be pretty ugly. Is there a better way to get the subtype of the Coordinate (whether it is CoordinateXYM)?

When looking at the WKTWriter there is the method setOutputOrdinates() . I was using this method to correctly write XYM coordinates as text, so I think this pattern may be applied to the WTBWriter as well. Do you have other ideas?

kaiwinter added a commit to kaiwinter/jts that referenced this issue May 26, 2021
M coordinate is read as Z coordinate after writing/reading.
kaiwinter added a commit to kaiwinter/jts that referenced this issue May 28, 2021
Referring to WKTReader/WKTWriter I added the EnumSet<Ordinate>
ordinateFlags. The WKBWriter uses these ordinateFlags to set the correct
flags on the byte stream.
kaiwinter added a commit to kaiwinter/jts that referenced this issue May 28, 2021
Referring to WKTReader/WKTWriter I added the EnumSet<Ordinate>
ordinateFlags. The WKBWriter uses these ordinateFlags to set the correct
flags on the byte stream.

Signed-off-by: Kai Winter <[email protected]>
kaiwinter added a commit to kaiwinter/jts that referenced this issue Aug 23, 2024
M coordinate is read as Z coordinate after writing/reading.
kaiwinter added a commit to kaiwinter/jts that referenced this issue Aug 23, 2024
Referring to WKTReader/WKTWriter I added the EnumSet<Ordinate>
ordinateFlags. The WKBWriter uses these ordinateFlags to set the correct
flags on the byte stream.

Signed-off-by: Kai Winter <[email protected]>
jodygarnett pushed a commit that referenced this issue Aug 26, 2024
* Unit test for #733

M coordinate is read as Z coordinate after writing/reading.

* Fixed lost M coordinates (#733)

Referring to WKTReader/WKTWriter I added the EnumSet<Ordinate>
ordinateFlags. The WKBWriter uses these ordinateFlags to set the correct
flags on the byte stream.

Signed-off-by: Kai Winter <[email protected]>

* Fixed Javadoc and exception message

* Fixed handling of XYZM coordinates

Signed-off-by: Kai Winter <[email protected]>
# Conflicts:
#	modules/core/src/main/java/org/locationtech/jts/io/WKBWriter.java

---------

Signed-off-by: Kai Winter <[email protected]>
@Kontinuation Kontinuation linked a pull request Oct 17, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants