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

Calculating difference and intersection between two polygons is resulting in a polygon having incorrect shape #1030

Open
s6ch13 opened this issue Jan 18, 2024 · 1 comment

Comments

@s6ch13
Copy link

s6ch13 commented Jan 18, 2024

Calculating difference and intersection between two polygons is resulting in a polygon having incorrect shape. Could have something to do with PrecisionModel. This is seen with v1.19 and also the latest 1.20 snapshot.

public static void intersectionDifference() throws ParseException {

	GeometryFactory gf = new GeometryFactory();

	Coordinate[] c1 = new Coordinate[] { 
			new Coordinate(3.5,2), 
			new Coordinate(3, 3.5), 
			new Coordinate(5, 2.5),
			new Coordinate(3.5, 2) };
	Geometry polygon1 = gf.createPolygon(gf.createLinearRing(c1), null);

	Coordinate[] c2 = new Coordinate[] { 
			new Coordinate(3, 2.5), 
			new Coordinate(5, 2.5),
			new Coordinate(5,3.5), 
			new Coordinate(3, 3.5), 
			new Coordinate(3, 2.5) };
	Geometry polygon2 = gf.createPolygon(gf.createLinearRing(c2), null);

	System.out.println("input");
	System.out.println(polygon1.toString());
	System.out.println(polygon2.toString());
			
	Geometry intersection = polygon1.intersection(polygon2);
	System.out.println("Geometry intersection");
	System.out.println(intersection.toString());
	
	Geometry d1 = polygon1.difference(intersection);
	System.out.println("polygon difference 1");
	System.out.println(d1.toString());
}

Result :
input
POLYGON ((3.5 2, 3 3.5, 5 2.5, 3.5 2))
POLYGON ((3 2.5, 5 2.5, 5 3.5, 3 3.5, 3 2.5))
Geometry intersection
POLYGON ((3.3333333333333335 2.5, 3 3.5, 5 2.5, 3.3333333333333335 2.5))
polygon difference 1
POLYGON ((3.5 2, 3 3.5, 3.3333333333333335 2.5, 5 2.5, 3.5 2))

Expected result :
polygon difference 1 should be
POLYGON((3.3333333333333335 2.5, 5 2.5, 3.5 2, 3.3333 2.5))

Scan2024-01-18_192927

This may have something to do with precision model. This works correctly If i set the geometry factory to

GeometryFactory gf = new GeometryFactory(new PrecisionModel(PrecisionModel.FLOATING_SINGLE));

But there are other similar cases which work with default precision model fail with PrecisionModel.FLOATING_SINGLE.

This issue may be related to https://github.com/locationtech/jts/issues/990

@dr-jts
Copy link
Contributor

dr-jts commented Mar 18, 2024

This is definitely a numerical robustness issue.

This code is using the old overlay implementation, which is more subject to robustness issues. It's better to change to using OverlayNG. You can do this via setting the system property jts.overlayng, or calling OverlayNGRobust directly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants