jts icon indicating copy to clipboard operation
jts copied to clipboard

PackedCoordinateSequence fails with two-dimensional coordinates

Open aseldawy opened this issue 5 years ago • 0 comments

I use PackedCoordinateSequence to conserve memory and use two-dimensions whenever possible. However, simple geometric operations, such as intersection, fail as it internally tries to access a third dimension that does not exist. The following code snippet throws an exception while it should not.

GeometryFactory factory = new GeometryFactory(PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
CoordinateSequence seq1 = factory.getCoordinateSequenceFactory().create(5, 2);
seq1.setOrdinate(0, 0, 0);
seq1.setOrdinate(0, 1, 0);
seq1.setOrdinate(1, 0, 1);
seq1.setOrdinate(1, 1, 0);
seq1.setOrdinate(2, 0, 1);
seq1.setOrdinate(2, 1, 1);
seq1.setOrdinate(3, 0, 0);
seq1.setOrdinate(3, 1, 1);
seq1.setOrdinate(4, 0, 0);
seq1.setOrdinate(4, 1, 0);
Polygon poly1 = factory.createPolygon(seq1);
CoordinateSequence seq2 = factory.getCoordinateSequenceFactory().create(5, 2);
seq2.setOrdinate(0, 0, 0+0.5);
seq2.setOrdinate(0, 1, 0+0.5);
seq2.setOrdinate(1, 0, 1+0.5);
seq2.setOrdinate(1, 1, 0+0.5);
seq2.setOrdinate(2, 0, 1+0.5);
seq2.setOrdinate(2, 1, 1+0.5);
seq2.setOrdinate(3, 0, 0+0.5);
seq2.setOrdinate(3, 1, 1+0.5);
seq2.setOrdinate(4, 0, 0+0.5);
seq2.setOrdinate(4, 1, 0+0.5);
Polygon poly2 = factory.createPolygon(seq2);
poly1.intersection(poly2);

If I just change the number of dimensions to three, even though I do not need the third dimension, it will work fine but it will use 50% more memory which is not desirable. This issue can be related to #434 but this example does not explicitly use CoordinateXY; it just uses the one in the factory to make the code portable.

aseldawy avatar Sep 20 '20 02:09 aseldawy