TurfJoins { 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); } 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 testWithin() 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)); ArrayList<List<Point>> coordinates = new ArrayList<>(); coordinates.add(pointList); Polygon poly = Polygon.fromLngLats(coordinates); Point pt = Point.fromLngLat(50, 50); ArrayList<Feature> features1 = new ArrayList<>(); features1.add(Feature.fromGeometry(poly)); FeatureCollection polyFeatureCollection = FeatureCollection.fromFeatures(features1); ArrayList<Feature> features2 = new ArrayList<>(); features2.add(Feature.fromGeometry(pt)); FeatureCollection ptFeatureCollection = FeatureCollection.fromFeatures(features2); FeatureCollection counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection); assertNotNull(counted); assertEquals(counted.features().size(), 1); Polygon poly1 = Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(0, 0), Point.fromLngLat(10, 0), Point.fromLngLat(10, 10), Point.fromLngLat(0, 10), Point.fromLngLat(0, 0)))); Polygon poly2 = Polygon.fromLngLats(Arrays.asList(Arrays.asList( Point.fromLngLat(10, 0), Point.fromLngLat(20, 10), Point.fromLngLat(20, 20), Point.fromLngLat(20, 0), Point.fromLngLat(10, 0)))); polyFeatureCollection = FeatureCollection.fromFeatures(new Feature[] { Feature.fromGeometry(poly1), Feature.fromGeometry(poly2)}); Point pt1 = Point.fromLngLat(1, 1); Point pt2 = Point.fromLngLat(1, 3); Point pt3 = Point.fromLngLat(14, 2); Point pt4 = Point.fromLngLat(13, 1); Point pt5 = Point.fromLngLat(19, 7); Point pt6 = Point.fromLngLat(100, 7); ptFeatureCollection = FeatureCollection.fromFeatures(new Feature[] { Feature.fromGeometry(pt1), Feature.fromGeometry(pt2), Feature.fromGeometry(pt3), Feature.fromGeometry(pt4), Feature.fromGeometry(pt5), Feature.fromGeometry(pt6)}); counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection); assertNotNull(counted); assertEquals(counted.features().size(), 5); }