locationtech / jts

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

Intersection of mixed-dimension polygons creates inconsistent CoordinateArraySequence #375

Open jodygarnett opened 5 years ago

jodygarnett commented 5 years ago

Follow on to #374 we have found GeoTools test cases producing inconsistent coordinate sequence through a sequence of JTS calls ending in an intersection call.

This is an interesting case using intersection() between mixed dimensions:

We suspect one of the internal JTS methods in the overlay process path is calling new Coordinate() rather than relying on CoordinateSequence.createCoordinate().

The net effect is closing a LinearRing with a Coordinate when all the other coordinates are CoordinateXY. This produces a CoordinateArraySequence with inconsistent Coordinates, which will start failing when the CoordinateArraySequence constructor is tightened up to check this as in #374.

    private boolean polygonOverlap(Geometry g1, Geometry g2) {
        // TODO: try to use relate instead
        Geometry intersection = g1.intersection(g2);
        return intersection != null && intersection.getDimension() == 2;
    }
dr-jts commented 5 years ago

There is probably no test case currently which creates this combination of coordinate arrays. One should be added, at least to test intersection.

There may well be other places where JTS is creating Coordinates directly - these should be tracked down as well.

dr-jts commented 5 years ago

See also this comment: https://github.com/locationtech/jts/issues/370#issuecomment-468926825