/** * Takes a {@link Point} and a {@link Polygon} and determines if the point resides inside the * polygon. The polygon can be convex or concave. The function accounts for holes. * * @param point which you'd like to check if inside the polygon * @param polygon which you'd like to check if the points inside * @return true if the Point is inside the Polygon; false if the Point is not inside the Polygon * @see <a href="http://turfjs.org/docs/#inside">Turf Inside documentation</a> * @since 1.3.0 */ public static boolean inside(Point point, Polygon polygon) { // This API needs to get better List<List<Point>> coordinates = polygon.coordinates(); List<List<List<Point>>> multiCoordinates = new ArrayList<>(); multiCoordinates.add(coordinates); return inside(point, MultiPolygon.fromLngLats(multiCoordinates)); }
FeatureCollection ptFeatureCollection = FeatureCollection.fromFeatures(features2); FeatureCollection counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection); assertNotNull(counted); assertEquals(counted.features().size(), 1); // 1 point in 1 polygon Feature.fromGeometry(pt4), Feature.fromGeometry(pt5), Feature.fromGeometry(pt6)}); counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection); assertNotNull(counted); assertEquals(counted.features().size(), 5); // multiple points in multiple polygons
/** * Takes a {@link FeatureCollection} of {@link Point} and a {@link FeatureCollection} of * {@link Polygon} and returns the points that fall within the polygons. * * @param points input points. * @param polygons input polygons. * @return points that land within at least one polygon. * @since 1.3.0 */ public static FeatureCollection pointsWithinPolygon(FeatureCollection points, FeatureCollection polygons) { ArrayList<Feature> features = new ArrayList<>(); for (int i = 0; i < polygons.features().size(); i++) { for (int j = 0; j < points.features().size(); j++) { Point point = (Point) points.features().get(j).geometry(); boolean isInside = TurfJoins.inside(point, (Polygon) polygons.features().get(i).geometry()); if (isInside) { features.add(Feature.fromGeometry(point)); } } } return FeatureCollection.fromFeatures(features); }
@Test public void testMultipolygonWithHole() throws TurfException, IOException { Point ptInHole = Point.fromLngLat(-86.69208526611328, 36.20373274711739); Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334); Point ptInPoly2 = Point.fromLngLat(-86.75079345703125, 36.18527313913089); Point ptOutsidePoly = Point.fromLngLat(-86.75302505493164, 36.23015046460186); Feature multiPolyHole = Feature.fromJson(loadJsonFixture(MULTIPOLY_WITH_HOLE_FIXTURE)); assertFalse(TurfJoins.inside(ptInHole, (MultiPolygon) multiPolyHole.geometry())); assertTrue(TurfJoins.inside(ptInPoly, (MultiPolygon) multiPolyHole.geometry())); assertTrue(TurfJoins.inside(ptInPoly2, (MultiPolygon) multiPolyHole.geometry())); assertFalse(TurfJoins.inside(ptOutsidePoly, (MultiPolygon) multiPolyHole.geometry())); }
@Test public void testPolyWithHole() throws TurfException, IOException { Point ptInHole = Point.fromLngLat(-86.69208526611328, 36.20373274711739); Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334); Point ptOutsidePoly = Point.fromLngLat(-86.75079345703125, 36.18527313913089); Feature polyHole = Feature.fromJson(loadJsonFixture(POLY_WITH_HOLE_FIXTURE)); assertFalse(TurfJoins.inside(ptInHole, (Polygon) polyHole.geometry())); assertTrue(TurfJoins.inside(ptInPoly, (Polygon) polyHole.geometry())); assertFalse(TurfJoins.inside(ptOutsidePoly, (Polygon) polyHole.geometry())); }
@Test public void testInputPositions() throws IOException, TurfException { Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334); Point ptOutsidePoly = Point.fromLngLat(-86.75079345703125, 36.18527313913089); Feature polyHole = Feature.fromJson(loadJsonFixture(POLY_WITH_HOLE_FIXTURE)); Polygon polygon = (Polygon) polyHole.geometry(); assertTrue(TurfJoins.inside(ptInPoly, polygon)); assertFalse(TurfJoins.inside(ptOutsidePoly, polygon)); }