@Test public void testSelfIntersectsPolygonNoIntersections() { final Polygon polygon = Polygon.wkt("POLYGON ((1 1, 2 2, 3 3, 1 3, 1 1))"); Assert.assertFalse(polygon.selfIntersects()); Assert.assertEquals(0, polygon.selfIntersections().size()); }
@Test public void testMaxWdithPolygonCausesAwtOverflow() { final Polygon maxWidthPolygon = Polygon.wkt( "POLYGON ((-180 -89.9999, -180 -62.8442818, 180 -62.8442818, 180 -89.9999, -180 -89.9999))"); final Rectangle innerRectangle = Rectangle.forString("-80,-160:-70, 170"); final Boolean encloses = maxWidthPolygon.fullyGeometricallyEncloses(innerRectangle); Assert.assertEquals(true, encloses); }
@Test public void testRelationsWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); final Polygon polygon = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); Assert.assertEquals( "There is a single relation that has all members fully within the polygon", 1, Iterables.size(atlas.relationsWithEntitiesWithin(polygon))); }
@Test public void testAreasWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); final Polygon polygon = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); Assert.assertEquals("There are 2 areas within this Polygon", 2, Iterables.size(atlas.areasWithin(polygon))); }
@Test public void testEdgesWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); final Polygon polygon = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); Assert.assertEquals("There are exactly 2 edges within this Polygon", 2, Iterables.size(atlas.edgesWithin(polygon))); }
@Test public void testLineItemsWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); final Polygon polygon = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); Assert.assertEquals("There are 2 lines and 2 edges within this Polygon", 4, Iterables.size(atlas.lineItemsWithin(polygon))); }
@Test public void testLinesWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); final Polygon polygon = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); Assert.assertEquals("There are exactly 2 lines within this Polygon", 2, Iterables.size(atlas.linesWithin(polygon))); }
@Test public void testEmptyClip() { final Polygon outer = Polygon.wkt( "POLYGON ((-122.05576 37.332439, -122.009566 37.36531, -122.031007 37.390535, -122.05576 37.332439))"); final MultiPolygon clipping = MultiPolygon.forPolygon(outer); final MultiPolygon clipped = SUBJECT_MULTIPOLYGON.clip(clipping, ClipType.AND) .getClipMultiPolygon(); Assert.assertTrue(clipped.isEmpty()); }
@Test public void testFullyGeometricallyEnclosingMultiPolygon() { final MultiPolygon multiPolygon1 = MultiPolygon .wkt("MULTIPOLYGON (" + "((-10 10, 10 10, 10 -10, -10 -10, -10 10)," + "(-6 6, 6 6, 6 -6, -6 -6, -6 6)))"); final MultiPolygon multiPolygon2 = MultiPolygon.wkt("MULTIPOLYGON (" + "((-4 4, 4 4, 4 -4, -4 -4, -4 4)," + "(-3 3, 3 3, 3 -3, -3 -3, -3 3)))"); final Polygon polygon1 = Polygon.wkt("POLYGON " + "((-8 8, 8 8, 8 -8, -8 -8, -8 8))"); final Polygon polygon2 = Polygon.wkt("POLYGON " + "((-5 5, 5 5, 5 -5, -5 -5, -5 5))"); Assert.assertFalse(polygon2.fullyGeometricallyEncloses(multiPolygon1)); Assert.assertTrue(polygon1.fullyGeometricallyEncloses(multiPolygon2)); Assert.assertFalse(polygon1.fullyGeometricallyEncloses(multiPolygon1)); Assert.assertTrue(polygon2.fullyGeometricallyEncloses(multiPolygon2)); }
@Test public void testPolygonWithFlatZeroAreaPartContains() { // Shape is a triangle, with a zero area line protruding from one of the corners as if // extending from the base final Polygon polygon = Polygon.wkt("POLYGON ((-0.0000449 0.0091179, -0.0063331 0.0007635, " + "-0.0174722 0.0007635, 0.0135196 0.0007635, -0.0000449 0.0091179))"); final Location onZeroAreaPart = Location.forString("0.0007635, 0.0132954"); final Location endpoint = Location.forString("0.0007635,-0.0174722"); Assert.assertTrue(polygon.fullyGeometricallyEncloses(onZeroAreaPart)); // see awt definition of contains Assert.assertFalse(polygon.fullyGeometricallyEncloses(endpoint)); }
@Test public void testSubAtlasWithPolygonAndEdgeAtBoundary() { final Atlas source = this.rule.getAtlasWithEdgeAlongBoundary(); final Polygon boundary = Polygon .wkt("POLYGON ((-121.7540269 37.0463639, -121.75403 37.04635, " + "-121.75408 37.0462, -121.75408 37.04611, -121.75406 37.04606, " + "-121.75399 37.04599, -121.75344 37.04557, -121.75338 37.0455, " + "-121.7533422 37.0454102, -121.7544982 37.0454102, " + "-121.7544982 37.0463639, -121.7540269 37.0463639))"); final Atlas result = source.subAtlas(boundary, AtlasCutType.SOFT_CUT).get(); Assert.assertEquals(4, result.numberOfEdges()); // Does not clip with JTS Assert.assertNotNull(result.edge(67)); // Does clip with JTS Assert.assertNotNull(result.edge(-67)); // Does clip with JTS Assert.assertNotNull(result.edge(76)); // Does not clip with JTS Assert.assertNotNull(result.edge(-76)); } }
@Test public void testPolygonWith45DegreeZeroAreaPartContains() { // Shape is a triangle, with a zero area line protruding from one of the corners on an // incline final Polygon polygon = Polygon.wkt("POLYGON ((-0.0065127 0.0214697, -0.0092975 0.0054797," + " -0.0233112 -0.0085339, 0.0027398 0.0175171, -0.0065127 0.0214697))"); final Location middleZeroAreaPart = polygon.segmentForIndex(1).middle(); final Location endpointZeroAreaPart = polygon.segmentForIndex(1).end(); final Location middleThirdSegment = polygon.segmentForIndex(2).middle(); // Locations on the zero area part are still on the boundary, and therefore contained Assert.assertTrue(polygon.fullyGeometricallyEncloses(middleZeroAreaPart)); // see awt definition of contains Assert.assertFalse(polygon.fullyGeometricallyEncloses(endpointZeroAreaPart)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(middleThirdSegment)); }
@Test public void testAtlasItemsWithinPolygon() { final Atlas atlas = this.rule.getWithinTestAtlas(); // These are equivalent representations of the same shape. final Polygon polygonBoundary = Polygon.wkt(WITHIN_TEST_POLYGON_WKT); final Rectangle rectangleBoundary = Rectangle.forCorners( Location.forWkt("POINT(-122.289001 47.6182798)"), Location.forWkt("POINT(-122.2886447 47.618416)")); // However, when the Polygon is not represented as a Rectangle, we rely on the underlying // awt definition of insideness - which considers Node 4 from edge 0 to be "outside" the // polygon, even though it is on the boundary. Assert.assertEquals("There are 2 lines, 2 edges, 2 Areas and 2 Nodes within this Polygon", 8, Iterables.size(atlas.itemsWithin(polygonBoundary))); // If we represent the Polygon as a Rectangle, then we forego the awt call and the same Node // 4 is now considered inside. This is an unfortunate side-affect of the awt dependency. Assert.assertEquals("There are 2 lines, 2 edges, 2 Areas and 3 Nodes within this Polygon", 9, Iterables.size(atlas.itemsWithin(rectangleBoundary))); }
@Test public void testMassivePolygonWithJTS() { final Polygon polygon = Polygon .wkt("POLYGON ((-160 -20, 179 -20, 179 -85, -160 -85, -160 -20))"); final Location interior1 = Location.forString("-25,-150"); final Location onBoundary1 = Location.forString("-20, -160"); final Location onBoundary2 = Location.forString("-20, 179"); final Location exterior1 = Location.forString("20, 0"); Assert.assertTrue(polygon.fullyGeometricallyEncloses(interior1)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(onBoundary1)); // this would be excluded by awt definition of contains Assert.assertTrue(polygon.fullyGeometricallyEncloses(onBoundary2)); Assert.assertFalse(polygon.fullyGeometricallyEncloses(exterior1)); }
@Test public void testSelfIntersectingPolygon() { // shape is a figure 8, self intersecting in the middle final Polygon polygon = Polygon .wkt("POLYGON ((-0.0256918 0.0054797, -0.0220985 0.0120374, -0.0119475 0.0121272, -0.0054797 0.006917," + " -0.007995 -0.0128459, -0.0007186 -0.0194934, 0.0103306 -0.0186849, 0.0125764 -0.0098814, " + "0.0026051 -0.0048509, -0.013834 -0.0050305, -0.0242545 -0.0008983, -0.0256918 0.0054797))"); final Location area1 = Location.forString("0.0034136, -0.016529"); final Location area2 = Location.forString("-0.0126662, 0.0026051"); final Location outside1 = Location.forString("0.0007187, 0.0029644"); final Location boundary1 = Location.forString("0.0120374, -0.0220985"); // both areas formed by the self intersection are contained Assert.assertTrue(polygon.fullyGeometricallyEncloses(area1)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(area2)); Assert.assertFalse(polygon.fullyGeometricallyEncloses(outside1)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(boundary1)); }
@Test public void testFourSelfIntersectingPolygonContains() { // Shape represents a square with an additional smaller square on each corner, formed by 4 // self intersections final Polygon polygon = Polygon .wkt("POLYGON ((-0.0380885 0.0238053, -0.0381783 0.0334173, -0.0255121 0.0337766, -0.0247036 -0.0263206, " + "-0.0368309 -0.0268596, -0.03728 -0.0141933, 0.0270392 -0.0132052, 0.0273986 -0.0246138," + " 0.0171578 -0.0248833, 0.0153611 0.0342258, 0.0253324 0.0344953, 0.0257816 0.0256918, " + "-0.0380885 0.0238053))"); final Location upperLeft = Location.forString("0.0279376, -0.0300936"); final Location upperRight = Location.forString("0.0297342, 0.0212002"); final Location lowerLeft = Location.forString("-0.0203918, -0.0302732"); final Location lowerRight = Location.forString("-0.0196731, 0.022782"); final Location center = Location.forString("0.06378, -0.0053899"); Assert.assertTrue(polygon.fullyGeometricallyEncloses(upperLeft)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(upperRight)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(lowerLeft)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(lowerRight)); // Interior square is considered exterior to the polygon Assert.assertFalse(polygon.fullyGeometricallyEncloses(center)); }
@Test public void testComplexPolygon() { final Polygon polygon = Polygon .wkt("POLYGON ((0.0269943 0.0252426, 0.0441521 0.0453649, 0.0584354 0.0381783, " + "0.0706524 0.0269494, 0.0770305 0.011678, 0.0716406 -0.0064678, 0.05062 -0.007995, " + "0.0498115 -0.0120374, 0.0594235 -0.0167086, 0.0675982 -0.013295, 0.074066 -0.0169781, " + "0.0688558 -0.0273986, 0.0557404 -0.0238053, 0.0534946 -0.0191341, 0.0469369 -0.0153611," + " 0.0464878 -0.0259613, 0.0557404 -0.029824, 0.0641846 -0.0317105, 0.060232 -0.0455445," + " 0.0462183 -0.0389868, 0.0313062 -0.0173374, 0.0269943 0.0252426))"); final Location inside1 = Location.forString("-0.0209583, 0.0651123"); final Location onBoundary1 = Location.forString("0.0301426, 0.0669962"); final Location onBoundary2 = Location.forString("-0.0170728, 0.0317908"); final Location onBoundary3 = Location.forString("0.0269494, 0.0706524"); final Location outside1 = Location.forString(".0427412, .0722947"); final Location outside2 = Location.forString("-0.022018, 0.0499233"); Assert.assertTrue(polygon.fullyGeometricallyEncloses(inside1)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(onBoundary1)); Assert.assertTrue(polygon.fullyGeometricallyEncloses(onBoundary2)); // see awt definition of contains Assert.assertFalse(polygon.fullyGeometricallyEncloses(onBoundary3)); Assert.assertFalse(polygon.fullyGeometricallyEncloses(outside1)); Assert.assertFalse(polygon.fullyGeometricallyEncloses(outside2)); }
final Polygon polygon1 = Polygon.wkt("POLYGON ((6 6, -6 6, -6 -6, 6 -6, 6 6))");