/** * 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 fromJson() throws IOException { final String json = loadJsonFixture(SAMPLE_FEATURECOLLECTION); FeatureCollection geo = FeatureCollection.fromJson(json); assertEquals(geo.type(), "FeatureCollection"); assertEquals(geo.features().size(), 3); assertEquals(geo.features().get(0).type(), "Feature"); assertEquals(geo.features().get(0).geometry().type(), "Point"); assertEquals(geo.features().get(1).type(), "Feature"); assertEquals(geo.features().get(1).geometry().type(), "LineString"); assertEquals(geo.features().get(2).type(), "Feature"); assertEquals(geo.features().get(2).geometry().type(), "Polygon"); }
@Test public void bbox_nullWhenNotSet() throws Exception { List<Feature> features = new ArrayList<>(); features.add(Feature.fromGeometry(null)); features.add(Feature.fromGeometry(null)); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features); assertNull(featureCollection.bbox()); }
throw new TurfException("collectionOf() requires a name"); if (featureCollection == null || !featureCollection.type().equals("FeatureCollection") || featureCollection.features() == null) { throw new TurfException(String.format( "Invalid input to %s, FeatureCollection required", name)); for (Feature feature : featureCollection.features()) { if (feature == null || !feature.type().equals("Feature") || feature.geometry() == null) { throw new TurfException(String.format(
@Test @Ignore public void name() throws Exception { Feature feature = Feature.fromJson(loadJsonFixture(CIRCLE_IN)); Polygon polygon = TurfTransformation.circle((Point) feature.geometry(), feature.getNumberProperty("radius").doubleValue()); FeatureCollection featureCollection = FeatureCollection.fromJson(loadJsonFixture(CIRCLE_OUT)); compareJson(featureCollection.features().get(1).geometry().toJson(), polygon.toJson()); } }
@Test public void passingInSingleFeature_doesHandleCorrectly() throws Exception { Point geometry = Point.fromLngLat(1.0, 2.0); Feature feature = Feature.fromGeometry(geometry); FeatureCollection geo = FeatureCollection.fromFeature(feature); assertNotNull(geo.features()); assertEquals(1, geo.features().size()); assertEquals(2.0, ((Point) geo.features().get(0).geometry()).coordinates().get(1), DELTA); }
@Override public void onResponse(Call<FeatureCollection> call, Response<FeatureCollection> response) { System.out.println("Total results: " + response.body().features().size()); for (Feature feature : response.body().features()) { if (feature.properties().has("name")) { System.out.println("" + feature.properties().get("name").getAsString()); } else { System.out.println("Unnamed feature."); } } }
@Test public void bbox_doesNotSerializeWhenNotPresent() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); LineString lineString = LineString.fromLngLats(points); Feature feature = Feature.fromGeometry(lineString); List<Feature> features = new ArrayList<>(); features.add(feature); features.add(feature); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features); compareJson(featureCollection.toJson(), "{\"type\":\"FeatureCollection\",\"features\":[{\"type\":\"Feature\"," + "\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]}," + "\"properties\":{}},{\"type\":\"Feature\"," + "\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]}," + "\"properties\":{}}]}"); }
@Test public void sanity() throws Exception { List<Feature> features = new ArrayList<>(); features.add(Feature.fromGeometry(null)); features.add(Feature.fromGeometry(null)); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features); assertNotNull(featureCollection); }
@Test public void testInvariantCollectionOf4() { String json = "{type: 'FeatureCollection', features: [{ type: 'Feature', geometry: { " + "type: 'Point', coordinates: [0, 0]}, properties: {}}]}"; TurfAssertions.collectionOf(FeatureCollection.fromJson(json), "Point", "myfn"); } }
@Test public void testLineDistanceWithGeometries() throws IOException, TurfException { Point pt = (Point) Feature.fromJson(loadJsonFixture(PT)).geometry(); FeatureCollection pts = FeatureCollection.fromJson(loadJsonFixture(PTS)); List<Point> pointList = new ArrayList<>(); for (Feature feature : pts.features()) { pointList.add((Point) (feature.geometry())); } Point closestPt = TurfClassification.nearestPoint(pt, pointList); Assert.assertNotNull(closestPt); Assert.assertEquals(closestPt.type(), "Point"); Assert.assertEquals(closestPt.longitude(), -75.33, DELTA); Assert.assertEquals(closestPt.latitude(), 39.44, DELTA); } }
@Test public void executeCall_optionalParamLimitHonored() throws Exception { MapboxTilequery clientAppParams = MapboxTilequery.builder() .accessToken(ACCESS_TOKEN) .query("-122.42901,37.80633") .mapIds("mapbox.mapbox-streets-v7") .baseUrl(mockUrl.toString()) .layers("poi_label") .geometry("point") .radius(500) .limit(2) .layers("poi_label") .build(); Response<FeatureCollection> response = clientAppParams.executeCall(); assertEquals(200, response.code()); assertNotNull(response.body()); FeatureCollection featureCollection = (FeatureCollection)response.body(); assertEquals(2, featureCollection.features().size()); } }
@Test public void bbox_doesSerializeWhenPresent() throws Exception { List<Point> points = new ArrayList<>(); points.add(Point.fromLngLat(1.0, 2.0)); points.add(Point.fromLngLat(2.0, 3.0)); LineString lineString = LineString.fromLngLats(points); Feature feature = Feature.fromGeometry(lineString); List<Feature> features = new ArrayList<>(); features.add(feature); features.add(feature); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features, bbox); compareJson(featureCollection.toJson(), "{\"type\":\"FeatureCollection\",\"bbox\":[1.0,2.0,3.0,4.0]," + "\"features\":[{\"type\":\"Feature\"," + "\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]},\"properties\":{}}," + "{\"type\":\"Feature\"," + "\"geometry\":{\"type\":\"LineString\",\"coordinates\":[[1,2],[2,3]]},\"properties\":{}}" + "]}"); }
@Test public void testInvariantCollectionOf3() throws TurfException { String json = "{type: 'FeatureCollection'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("Invalid input to foo, FeatureCollection required")); TurfAssertions.collectionOf(FeatureCollection.fromJson(json), "Polygon", "foo"); }
FeatureCollection polyFeatureCollection = FeatureCollection.fromFeatures(features1); FeatureCollection ptFeatureCollection = FeatureCollection.fromFeatures(features2); assertEquals(counted.features().size(), 1); // 1 point in 1 polygon Point.fromLngLat(10, 0)))); polyFeatureCollection = FeatureCollection.fromFeatures(new Feature[] { Feature.fromGeometry(poly1), Feature.fromGeometry(poly2)}); 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)}); assertEquals(counted.features().size(), 5); // multiple points in multiple polygons
/** * Test whether we are rounding correctly to conform to the RFC 7946 GeoJson spec. * * @throws IOException If fixture fails loading. * @see <a href="https://tools.ietf.org/html/rfc7946#section-3.1.10">section 3.1.10</a> */ @Test public void testSevenDigitRounding() throws IOException { Point roundDown = Point.fromLngLat(1.12345678, 1.12345678); Point noRound = Point.fromLngLat(1.1234, 1.12345); Point matchRound = Point.fromLngLat(1.1234567, 1.1234567); Point roundLat = Point.fromLngLat(1.1234567, 1.12345678); Point roundLon = Point.fromLngLat(1.12345678, 1.1234567); Point largeRound = Point.fromLngLat(105.12345678, 89.1234567); Point negRound = Point.fromLngLat(-105.12345678, -89.1234567); List<Feature> features = new ArrayList<>(); features.add(Feature.fromGeometry(roundDown)); features.add(Feature.fromGeometry(noRound)); features.add(Feature.fromGeometry(matchRound)); features.add(Feature.fromGeometry(roundLat)); features.add(Feature.fromGeometry(roundLon)); features.add(Feature.fromGeometry(largeRound)); features.add(Feature.fromGeometry(negRound)); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features); compareJson(loadJsonFixture(GEOJSON_FIXTURE), featureCollection.toJson()); } }
@Test public void bbox_returnsCorrectBbox() throws Exception { List<Feature> features = new ArrayList<>(); features.add(Feature.fromGeometry(null)); features.add(Feature.fromGeometry(null)); BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0); FeatureCollection featureCollection = FeatureCollection.fromFeatures(features, bbox); assertNotNull(featureCollection.bbox()); assertEquals(1.0, featureCollection.bbox().west(), DELTA); assertEquals(2.0, featureCollection.bbox().south(), DELTA); assertEquals(3.0, featureCollection.bbox().east(), DELTA); assertEquals(4.0, featureCollection.bbox().north(), DELTA); }
@Test public void testInvariantCollectionOf2() throws TurfException { String json = "{type: 'FeatureCollection'}"; thrown.expect(TurfException.class); thrown.expectMessage(startsWith("collectionOf() requires a name")); TurfAssertions.collectionOf(FeatureCollection.fromJson(json), "Polygon", null); }
Point pt7 = TurfMeasurement.along(line, 100, "miles"); Point pt8 = TurfMeasurement.along(line, 0, "miles"); FeatureCollection fc = FeatureCollection.fromFeatures(new Feature[] { Feature.fromGeometry(pt1), Feature.fromGeometry(pt2), }); for (Feature f : fc.features()) { assertNotNull(f); assertEquals("Feature", f.type()); assertEquals(8, fc.features().size()); assertEquals(((Point) fc.features().get(7).geometry()).longitude(), pt8.longitude(), DELTA); assertEquals(((Point) fc.features().get(7).geometry()).latitude(), pt8.latitude(), DELTA);