locationtech / jts

The JTS Topology Suite is a Java library for creating and manipulating vector geometry.
Other
2k stars 442 forks source link

Densifier convert 2d to 3d #635

Closed halset closed 3 years ago

halset commented 4 years ago

Looks like Densifier on a 2d line convert it to a 3d line. It would be better if it would keep the dimension.

      GeometryFactory gf = new GeometryFactory();
      LineString line = gf
              .createLineString(new Coordinate[] { new CoordinateXY(1, 2), new CoordinateXY(3, 4) });
      assertEquals(2, line.getCoordinateSequence().getDimension());

      line = (LineString) Densifier.densify(line, 0.1);
      // the following assert fails, but should not.
      assertEquals(2, line.getCoordinateSequence().getDimension());

This got me in to a problem with a GeoTools transformation failing.

java.lang.IllegalArgumentException: Invalid ordinate index: 2
    at org.locationtech.jts.geom.CoordinateXY.getOrdinate(CoordinateXY.java:106)
    at org.locationtech.jts.geom.impl.CoordinateArraySequence.getOrdinate(CoordinateArraySequence.java:256)
    at org.geotools.geometry.jts.DefaultCoordinateSequenceTransformer.transform(DefaultCoordinateSequenceTransformer.java:123)
    at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.projectCoordinateSequence(GeometryCoordinateSequenceTransformer.java:262)
    at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformStraightLineString(GeometryCoordinateSequenceTransformer.java:214)
    at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transformLineString(GeometryCoordinateSequenceTransformer.java:193)
    at org.geotools.geometry.jts.GeometryCoordinateSequenceTransformer.transform(GeometryCoordinateSequenceTransformer.java:139)
    at org.geotools.geometry.jts.JTS.transform(JTS.java:437)
halset commented 4 years ago

LineSegment.pointAlong (and other methods on LineSegment) create a new Coordinate even though both coordinates of the segment are of type CoordinateXY. Changing this fixes the problem for me, but perhaps there is a better way?

dr-jts commented 4 years ago

Sigh... yes, the handling of Coordinate subclasses is imperfect, and needs work. Thanks for pointing this out.

I think maybe one part of the solution is to provide create methods on Coordinate and subclasses, which instantiate a Coordinate of appropriate type.

halset commented 4 years ago

@dr-jts I have sketched one possible solution over at https://github.com/locationtech/jts/pull/636. It introduces a non-static Coordinate.createEmptyCopy that return a Coordinate of the same type.

halset commented 4 years ago

Created a new pull-request to fix signature/agreement-stuff. More green marks on this one. #637.

dr-jts commented 3 years ago

Fixed by #637