private boolean overlapsInternal(final PolyLine polyline, final boolean runReverseCheck) { for (final Location location : polyline) { if (fullyGeometricallyEncloses(location)) { return true; } } if (runReverseCheck && polyline instanceof Polygon && ((Polygon) polyline).overlapsInternal(this, false)) { return true; } return this.intersects(polyline); }
public boolean covers(final Rectangle bound) { boolean covers = false; for (final Polygon outer : this.boundary.outers()) { if (outer.fullyGeometricallyEncloses(bound)) { covers = true; break; } if (outer.overlaps(bound)) { covers = true; break; } } return covers; }
@Override public Iterable<Node> nodesWithin(final Polygon polygon) { final Iterable<Node> nodes = this.getNodeSpatialIndex().get(polygon.bounds()); if (polygon instanceof Rectangle) { return nodes; } return Iterables.filter(nodes, node -> polygon.fullyGeometricallyEncloses(node.getLocation())); }
@Test public void testCovers() { final Rectangle rwac = Rectangle .forLocations(Iterables.iterable(Location.TEST_3, Location.TEST_1)); logger.info("Polygon: " + this.quadrant); final boolean containsTest6 = this.quadrant.fullyGeometricallyEncloses(Location.TEST_6); final boolean containsTest5 = this.quadrant.fullyGeometricallyEncloses(Location.TEST_5); final boolean containsTestRectangle = this.quadrant .fullyGeometricallyEncloses(Rectangle.TEST_RECTANGLE_2); final boolean containsRWAC = this.quadrant.fullyGeometricallyEncloses(rwac); logger.info("Test 6: {} -> {}", Location.TEST_6, containsTest6); logger.info("Test 5: {} -> {}", Location.TEST_5, containsTest5); logger.info("Test Rectangle: {} -> {}", Rectangle.TEST_RECTANGLE_2, containsTestRectangle); logger.info("RWAC 2: {} -> {}", rwac, containsRWAC); Assert.assertTrue(!containsTest5); Assert.assertTrue(containsTest6); Assert.assertTrue(containsTestRectangle); Assert.assertTrue(!containsRWAC); }
@Override public Iterable<Point> pointsWithin(final Polygon polygon) { final Iterable<Point> points = this.getPointSpatialIndex().get(polygon.bounds()); if (polygon instanceof Rectangle) { return points; } return Iterables.filter(points, point -> polygon.fullyGeometricallyEncloses(point.getLocation())); }
@Override public Iterable<Area> areasCovering(final Location location) { return Iterables.stream(this.getAreaSpatialIndex().get(location.bounds())).filter(area -> { final Polygon areaPolygon = area.asPolygon(); return areaPolygon.fullyGeometricallyEncloses(location); }); }
@Override public Iterable<Area> areasWithin(final Polygon polygon) { return Iterables.stream(this.getAreaSpatialIndex().get(polygon.bounds())).filter(area -> { final Polygon areaPolygon = area.asPolygon(); return polygon.fullyGeometricallyEncloses(areaPolygon); }); }
@Override public Iterable<Line> linesWithin(final Polygon polygon) { return Iterables.stream(this.getLineSpatialIndex().get(polygon.bounds())).filter(line -> { final PolyLine polyline = line.asPolyLine(); return polygon.fullyGeometricallyEncloses(polyline); }); }
@Override public Iterable<Edge> edgesWithin(final Polygon polygon) { return Iterables.stream(this.getEdgeSpatialIndex().get(polygon.bounds())).filter(edge -> { final PolyLine polyline = edge.asPolyLine(); return polygon.fullyGeometricallyEncloses(polyline); }); }
@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 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 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 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 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 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)); }
private void verifyFullContainment(final Atlas atlas) { Assert.assertTrue( atlas.area(1).asPolygon().fullyGeometricallyEncloses(atlas.area(2).asPolygon())); }
private void verifyNoFullContainment(final Atlas atlas) { Assert.assertFalse(atlas.area(1).asPolygon() .fullyGeometricallyEncloses(atlas.edge(159019301).asPolyLine())); Assert.assertFalse(atlas.area(1).asPolygon() .fullyGeometricallyEncloses(atlas.edge(-159019301).asPolyLine())); }
private void verifyNoFullContainment(final Atlas atlas) { Assert.assertFalse( atlas.area(1).asPolygon().fullyGeometricallyEncloses(atlas.area(2).asPolygon())); }
private void verifyFullContainment(final Atlas atlas) { Assert.assertTrue(atlas.area(1).asPolygon() .fullyGeometricallyEncloses(atlas.edge(159019301).asPolyLine())); Assert.assertTrue(atlas.area(1).asPolygon() .fullyGeometricallyEncloses(atlas.edge(-159019301).asPolyLine())); }
/** * Triangulate this {@link Polygon}, using the JTS library. * * @return All the triangles that form this {@link Polygon}. */ public List<Polygon> triangles() { final ConformingDelaunayTriangulationBuilder trianguler = new ConformingDelaunayTriangulationBuilder(); // Populate the delaunay triangulation builder trianguler.setSites(JTS_POLYGON_CONVERTER.convert(this)); final GeometryCollection triangleCollection = (GeometryCollection) trianguler .getTriangles(JtsPrecisionManager.getGeometryFactory()); // Get the output and convert back to Core Polygons, filter out the extraneous polygons from // the Delaunay triangulation. return Iterables.stream(GeometryStreamer.streamPolygons(triangleCollection)) .map(JTS_POLYGON_CONVERTER.revert()) .filter(polygon -> fullyGeometricallyEncloses(polygon.center())).collectToList(); }