TurfJoins { public static boolean inside(Point point, Polygon polygon) { List<List<Point>> coordinates = polygon.coordinates(); List<List<List<Point>>> multiCoordinates = new ArrayList<>(); multiCoordinates.add(coordinates); return inside(point, MultiPolygon.fromLngLats(multiCoordinates)); } private  TurfJoins(); static boolean inside(Point point, Polygon polygon); static boolean inside(Point point, MultiPolygon multiPolygon); static FeatureCollection pointsWithinPolygon(FeatureCollection points,
                                                      FeatureCollection polygons);  }
@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)); }
@Test public void testFeatureCollection() throws TurfException { ArrayList<Point> pointList = new ArrayList<>(); pointList.add(Point.fromLngLat(0, 0)); pointList.add(Point.fromLngLat(0, 100)); pointList.add(Point.fromLngLat(100, 100)); pointList.add(Point.fromLngLat(100, 0)); pointList.add(Point.fromLngLat(0, 0)); List<List<Point>> coordinates = new ArrayList<>(); coordinates.add(pointList); Polygon poly = Polygon.fromLngLats(coordinates); Point ptIn = Point.fromLngLat(50, 50); Point ptOut = Point.fromLngLat(140, 150); assertTrue(TurfJoins.inside(ptIn, poly)); assertFalse(TurfJoins.inside(ptOut, poly)); pointList = new ArrayList<>(); pointList.add(Point.fromLngLat(0, 0)); pointList.add(Point.fromLngLat(50, 50)); pointList.add(Point.fromLngLat(0, 100)); pointList.add(Point.fromLngLat(100, 100)); pointList.add(Point.fromLngLat(100, 0)); pointList.add(Point.fromLngLat(0, 0)); coordinates = new ArrayList<>(); coordinates.add(pointList); Polygon concavePoly = Polygon.fromLngLats(coordinates); ptIn = Point.fromLngLat(75, 75); ptOut = Point.fromLngLat(25, 50); assertTrue(TurfJoins.inside(ptIn, concavePoly)); assertFalse(TurfJoins.inside(ptOut, concavePoly)); }
@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 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())); }