Skip to content

PackedCoordinateSequenceFactory does not work correctly when dealing with CoordinateXY. #434

Open

Description

@pascalriquier

PackedCoordinateSequence$Double calls getOrdinate(2) or getOrdinate(3) on the coordinates and that throws an exception in CoordinateXY.

Polygon polygonA = new GeometryFactory(
	new PackedCoordinateSequenceFactory()).createPolygon(
		new Coordinate[] {
			new CoordinateXY(0, 0),
			new CoordinateXY(10, 0),
			new CoordinateXY(10, 10),
			new CoordinateXY(0, 10),
			new CoordinateXY(0, 0)
		}
	);
Polygon polygonB = new GeometryFactory().createPolygon(
		new Coordinate[] {
			new CoordinateXY(5, 5),
			new CoordinateXY(15, 5),
			new CoordinateXY(15, 15),
			new CoordinateXY(5, 15),
			new CoordinateXY(5, 5)
		}
	);
polygonB.intersection(polygonA);
polygonA.intersection(polygonB);

In the above example, the first intersection does not throw an exception while the second does.
PackedCoordinateSequence$Float does this differently by calling getZ() on the coordinates. So possibly the solution is to call getZ() from getOrdinate() in CoordinateXY.

Activity

  1. dr-jts commented on Jun 7, 2019

    @dr-jts
    Contributor

    The root cause of this is that internally the intersection algorithm winds up working with an array of Coordinates of mixed dimension. Also, there isn't enough metadata around to determine that the output should be XY, not XYZ.

    Not sure how to fix this. I suspect it would take quite a bit to figure out how to get everything to work correctly with the right dimensions and coordinate types where needed.

    Part of the problem is the decision to throw error when accessing dimensions that do not exist in CoordinateXY (and other Coordinate subclasses). I know @jodygarnett has the opinion that it's better to fail fast, but if we don't understand how to prevent this problem, then it may be better to Do No Harm and simply return NaN.

  2. dr-jts commented on Jun 7, 2019

    @dr-jts
    Contributor

    . So possibly the solution is to call getZ() from getOrdinate() in CoordinateXY.

    I don't think this will work, because the getOrdinate(2) call may be accessing either Z or M depending on the source.

  3. dr-jts commented on Jun 7, 2019

    @dr-jts
    Contributor

    Further to the idea of making invalid ordinate access succeed, this is already done in CoordinateXY.getZ(). So making getOrdinate(2) return NaN would be consistent.

  4. airbreather commented on Aug 28, 2019

    @airbreather
    Contributor

    NetTopologySuite/NetTopologySuite#336 has another repro that I found independently on the .NET side of the world, though perhaps the root cause of that one is that JTS itself is creating the mixed-dimension coordinates, and not any fault of PackedCoordinateSequence?

    Pasting the repro here:

    package com.company;
    
    import org.locationtech.jts.geom.Coordinate;
    import org.locationtech.jts.geom.CoordinateXY;
    import org.locationtech.jts.geom.LineString;
    import org.locationtech.jts.geom.GeometryFactory;
    import org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory;
    
    public class Main {
    
        public static void main(String[] args) {
            var fac = new GeometryFactory(PackedCoordinateSequenceFactory.DOUBLE_FACTORY);
            var coordinates = new Coordinate[] {
                new CoordinateXY(1, 1),
                new CoordinateXY(2, 2),
                new CoordinateXY(1, 2),
                new CoordinateXY(2, 1),
            };
            LineString l = fac.createLineString(coordinates);
            l.union();
        }
    }

    The stack trace is:

    Exception in thread "main" java.lang.IllegalArgumentException: Invalid ordinate index: 2
    	at org.locationtech.jts.geom.CoordinateXY.getOrdinate(CoordinateXY.java:106)
    	at org.locationtech.jts.geom.impl.PackedCoordinateSequence$Double.<init>(PackedCoordinateSequence.java:301)
    	at org.locationtech.jts.geom.impl.PackedCoordinateSequenceFactory.create(PackedCoordinateSequenceFactory.java:81)
    	at org.locationtech.jts.geom.GeometryFactory.createLineString(GeometryFactory.java:573)
    	at org.locationtech.jts.operation.overlay.LineBuilder.buildLines(LineBuilder.java:158)
    	at org.locationtech.jts.operation.overlay.LineBuilder.build(LineBuilder.java:55)
    	at org.locationtech.jts.operation.overlay.OverlayOp.computeOverlay(OverlayOp.java:254)
    	at org.locationtech.jts.operation.overlay.OverlayOp.getResultGeometry(OverlayOp.java:183)
    	at org.locationtech.jts.operation.overlay.OverlayOp.overlayOp(OverlayOp.java:86)
    	at org.locationtech.jts.operation.overlay.snap.SnapIfNeededOverlayOp.getResultGeometry(SnapIfNeededOverlayOp.java:75)
    	at org.locationtech.jts.operation.overlay.snap.SnapIfNeededOverlayOp.overlayOp(SnapIfNeededOverlayOp.java:37)
    	at org.locationtech.jts.operation.union.UnaryUnionOp.unionNoOpt(UnaryUnionOp.java:275)
    	at org.locationtech.jts.operation.union.UnaryUnionOp.union(UnaryUnionOp.java:211)
    	at org.locationtech.jts.operation.union.UnaryUnionOp.union(UnaryUnionOp.java:113)
    	at org.locationtech.jts.geom.Geometry.union(Geometry.java:1477)
    	at com.company.Main.main(Main.java:20)
    

    I guess I should mention that I'm using org.locationtech.jts:jts-core:1.16.1 from Maven, using whatever magic IntelliJ IDEA used to make it possible for me to run this code (I have no clue how the Java world works, I just wanted to speak the same language as you guys).

  5. airbreather commented on Aug 28, 2019

    @airbreather
    Contributor

    Further to the idea of making invalid ordinate access succeed, this is already done in CoordinateXY.getZ(). So making getOrdinate(2) return NaN would be consistent.

    For the record, I'd like to voice my strong support for consistency around this area.

    Trying to fetch the value of an ordinate that we know is not present should either return NaN or throw an exception, consistently, regardless of whether we're using a name or a number, regardless of whether we're accessing it through Coordinate or CoordinateSequence, with the possible exception of these kinds of things where it was decided to sacrifice safety / correctness / consistency to gain performance.

    • Though, on that topic, I'm not sure that there's actually a meaningful performance benefit to those? Measurements would trump any speculation, of course, but I'd be surprised if mainstream JVM implementations actually penalize that case enough to warrant this sacrifice.
    • Adding a check to return NaN if ordinate >= dimension isn't raising any warning flags in my head (though my intuition is admittedly more geared towards .NET implementations).
  6. added a commit that references this issue on Mar 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions