/** * This takes the currently defined values found inside this instance and converts it to a GeoJson * string. * * @return a JSON string which represents this Feature * @since 1.0.0 */ @Override public String toJson() { GsonBuilder gson = new GsonBuilder(); gson.registerTypeAdapter(Point.class, new PointSerializer()); gson.registerTypeAdapter(BoundingBox.class, new BoundingBoxSerializer()); // Empty properties -> should not appear in json string Feature feature = this; if (properties().size() == 0) { feature = new AutoValue_Feature(TYPE, bbox(), id(), geometry(), null); } return gson.create().toJson(feature); }
Feature closestPt = Feature.fromGeometry( Point.fromLngLat(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY)); closestPt.addNumberProperty("dist", Double.POSITIVE_INFINITY); Feature start = Feature.fromGeometry(coords.get(i)); Feature stop = Feature.fromGeometry(coords.get(i + 1)); start.addNumberProperty("dist", TurfMeasurement.distance( pt, (Point) start.geometry(), TurfConstants.UNIT_MILES)); stop.addNumberProperty("dist", TurfMeasurement.distance( pt, (Point) stop.geometry(), TurfConstants.UNIT_MILES)); start.properties().get("dist").getAsDouble(), stop.properties().get("dist").getAsDouble() ); double direction = TurfMeasurement.bearing((Point) start.geometry(), (Point) stop.geometry()); Feature perpendicularPt1 = Feature.fromGeometry( TurfMeasurement.destination(pt, heightDistance, direction + 90, TurfConstants.UNIT_MILES)); Feature perpendicularPt2 = Feature.fromGeometry( TurfMeasurement.destination(pt, heightDistance, direction - 90, TurfConstants.UNIT_MILES)); LineIntersectsResult intersect = lineIntersects( ((Point) perpendicularPt1.geometry()).longitude(), ((Point) perpendicularPt1.geometry()).latitude(), ((Point) perpendicularPt2.geometry()).longitude(), ((Point) perpendicularPt2.geometry()).latitude(),
/** * Enforce expectations about types of {@link Feature} inputs for Turf. Internally this uses * {@link Feature#type()} to judge geometry types. * * @param feature with an expected geometry type * @param type type expected GeoJson type * @param name name of calling function * @see <a href="http://turfjs.org/docs/#featureof">Turf featureOf documentation</a> * @since 1.2.0 */ public static void featureOf(Feature feature, String type, String name) { if (TextUtils.isEmpty(name)) { throw new TurfException(".featureOf() requires a name"); } if (feature == null || !feature.type().equals("Feature") || feature.geometry() == null) { throw new TurfException(String.format( "Invalid input to %s, Feature with geometry required", name)); } if (feature.geometry() == null || !feature.geometry().type().equals(type)) { throw new TurfException(String.format( "Invalid input to %s: must be a %s, given %s", name, type, feature.geometry().type())); } }
/** * Convenience method to check for a member by name as well as non-null value. * * @param key name of the member * @return true if member is present with non-null value, false otherwise. * @since 1.3.0 */ public boolean hasNonNullValueForProperty(String key) { return hasProperty(key) && !getProperty(key).isJsonNull(); } }
public static void main(String[] args) { Feature feature = Feature.fromGeometry(Point.fromLngLat(1.0, 2.0)); System.out.println(feature.toJson()); } }
@Test public void testLineDistanceWithGeometries() throws IOException, TurfException { Feature route1 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_ONE)); Feature route2 = Feature.fromJson(loadJsonFixture(LINE_DISTANCE_ROUTE_TWO)); assertEquals(202, Math.round(TurfMeasurement.length((LineString) route1.geometry(), TurfConstants.UNIT_MILES))); Assert.assertEquals(741.7787396994203, TurfMeasurement.length((LineString) route2.geometry(), TurfConstants.UNIT_KILOMETERS), DELTA); }
/** * Convenience method to get a JsonElement member. * * @param key name of the member * @return the value of the member, null if it doesn't exist * @since 1.0.0 */ public JsonElement getProperty(String key) { return properties().get(key); }
@Test public void test_point_feature_fromJson() throws IOException { final String json = "{ \"type\": \"Feature\"," + "\"geometry\": { \"type\": \"Point\", \"coordinates\": [ 125.6, 10.1] }," + "\"properties\": {\"name\": \"Dinagat Islands\" }}"; Feature geo = Feature.fromJson(json); assertEquals(geo.type(), "Feature"); assertEquals(geo.geometry().type(), "Point"); assertEquals(((Point)geo.geometry()).longitude(), 125.6, DELTA); assertEquals(((Point)geo.geometry()).latitude(), 10.1, DELTA); assertEquals(geo.properties().get("name").getAsString(), "Dinagat Islands"); }
@Test public void testTurfAlong() throws IOException, TurfException { Feature feature = Feature.fromJson(loadJsonFixture(TURF_ALONG_DC_LINE)); LineString line = (LineString) feature.geometry(); Point pt8 = TurfMeasurement.along(line, 0, "miles"); FeatureCollection fc = FeatureCollection.fromFeatures(new Feature[] { Feature.fromGeometry(pt1), Feature.fromGeometry(pt2), Feature.fromGeometry(pt3), Feature.fromGeometry(pt4), Feature.fromGeometry(pt5), Feature.fromGeometry(pt6), Feature.fromGeometry(pt7), Feature.fromGeometry(pt8) }); assertEquals("Feature", f.type()); assertEquals("Point", f.geometry().type()); assertEquals(((Point) fc.features().get(7).geometry()).longitude(), pt8.longitude(), DELTA); assertEquals(((Point) fc.features().get(7).geometry()).latitude(), pt8.latitude(), DELTA);
/** * Unwrap a coordinate {@link Point} from a Feature with a Point geometry. * * @param obj any value * @return a coordinate * @see <a href="http://turfjs.org/docs/#getcoord">Turf getCoord documentation</a> * @since 3.2.0 */ public static Point getCoord(Feature obj) { if (obj.geometry() instanceof Point) { return (Point) obj.geometry(); } throw new TurfException("A Feature with a Point geometry is required."); } }
/** * 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 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 testNonNullProperties() { List<Point> coordinates = new ArrayList<>(); coordinates.add(Point.fromLngLat(0.1, 2.3)); coordinates.add(Point.fromLngLat(4.5, 6.7)); LineString line = LineString.fromLngLats(coordinates); JsonObject properties = new JsonObject(); properties.addProperty("key", "value"); Feature feature = Feature.fromGeometry(line, properties); String jsonString = feature.toJson(); assertTrue(jsonString.contains("\"properties\":{\"key\":\"value\"}")); // Feature (non-empty Properties) -> Json (non-empty Properties) -> Equavalent Feature assertEquals(Feature.fromJson(jsonString), feature); }
@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 bbox_nullWhenNotSet() 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); assertNull(feature.bbox()); }
Feature stopVertex = nearestPointOnLine(stopPt, coords); List<Feature> ends = new ArrayList<>(); if ((int) startVertex.getNumberProperty(INDEX_KEY) <= (int) stopVertex.getNumberProperty(INDEX_KEY)) { ends.add(startVertex); ends.add(stopVertex); points.add((Point) ends.get(0).geometry()); for (int i = (int) ends.get(0).getNumberProperty(INDEX_KEY) + 1; i < (int) ends.get(1).getNumberProperty(INDEX_KEY) + 1; i++) { points.add(coords.get(i)); points.add((Point) ends.get(1).geometry()); return LineString.fromLngLats(points);
@Test public void testInvariantFeatureOf4() { String json = "{ type: 'Feature', geometry: { type: 'Point', coordinates: [0, 0]}, " + "properties: {}}"; TurfAssertions.featureOf(Feature.fromJson(json), "Point", "myfn"); }
@Test public void testNullPropertiesJson() { String jsonString = "{\"type\":\"Feature\",\"bbox\":[1.0,2.0,3.0,4.0],\"geometry\":" + "{\"type\":\"LineString\",\"coordinates\":[[1.0,2.0],[2.0,3.0]]}}"; Feature feature = Feature.fromJson(jsonString); // Json( null Properties) -> Feature (empty Properties) -> Json(null Properties) String fromFeatureJsonString = feature.toJson(); assertEquals(fromFeatureJsonString, jsonString); }
/** * Convenience method to check if a member with the specified name is present in this object. * * @param key name of the member * @return true if there is the member has the specified name, false otherwise. * @since 1.0.0 */ public boolean hasProperty(String key) { return properties().has(key); }