TurfMeasurement { public static Feature center(Feature feature, @Nullable JsonObject properties, @Nullable String id) { return center(FeatureCollection.fromFeature(feature), properties, id); } private  TurfMeasurement(); static double bearing(@NonNull Point point1, @NonNull Point point2); @NonNull static Point destination(@NonNull Point point, @FloatRange(from = 0) double distance,
                                  @FloatRange(from = -180, to = 180) double bearing,
                                  @NonNull @TurfConstants.TurfUnitCriteria String units); static double distance(@NonNull Point point1, @NonNull Point point2); static double distance(@NonNull Point point1, @NonNull Point point2,
                                @NonNull @TurfConstants.TurfUnitCriteria String units); static double length(@NonNull LineString lineString,
                              @NonNull @TurfConstants.TurfUnitCriteria String units); static double length(@NonNull MultiLineString multiLineString,
                              @NonNull @TurfConstants.TurfUnitCriteria String units); static double length(@NonNull Polygon polygon,
                              @NonNull @TurfConstants.TurfUnitCriteria String units); static double length(@NonNull MultiPolygon multiPolygon,
                              @NonNull @TurfConstants.TurfUnitCriteria String units); static double length(List<Point> coords, String units); static Point midpoint(@NonNull Point from, @NonNull Point to); static Point along(@NonNull LineString line, @FloatRange(from = 0) double distance,
                            @NonNull @TurfConstants.TurfUnitCriteria String units); static Point along(@NonNull List<Point> coords, @FloatRange(from = 0) double distance,
                            @NonNull @TurfConstants.TurfUnitCriteria String units); static double[] bbox(@NonNull Point point); static double[] bbox(@NonNull LineString lineString); static double[] bbox(@NonNull MultiPoint multiPoint); static double[] bbox(@NonNull Polygon polygon); static double[] bbox(@NonNull MultiLineString multiLineString); static double[] bbox(MultiPolygon multiPolygon); static double[] bbox(GeoJson geoJson); static double[] bbox(FeatureCollection featureCollection); static double[] bbox(Feature feature); static double[] bbox(Geometry geometry); static Feature bboxPolygon(@NonNull BoundingBox boundingBox); static Feature bboxPolygon(@NonNull BoundingBox boundingBox,
                                    @Nullable JsonObject properties,
                                    @Nullable String id); static Feature bboxPolygon(@NonNull double[] bbox); static Feature bboxPolygon(@NonNull double[] bbox,
                                    @Nullable JsonObject properties,
                                    @Nullable String id); static Polygon envelope(GeoJson geoJson); static BoundingBox square(@NonNull BoundingBox boundingBox); static double area(@NonNull Feature feature); static double area(@NonNull FeatureCollection featureCollection); static double area(@NonNull Geometry geometry); static Feature center(Feature feature,
                               @Nullable JsonObject properties,
                               @Nullable String id); static Feature center(Feature feature); static Feature center(FeatureCollection featureCollection,
                               @Nullable JsonObject properties,
                               @Nullable String id); static Feature center(FeatureCollection featureCollection); static double EARTH_RADIUS; }
@Test public void centerFeature() { Feature expectedFeature = Feature.fromGeometry(Point.fromLngLat(133.5, -27.0)); Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON)); assertEquals(expectedFeature, TurfMeasurement.center(inputFeature, null, null)); }
@Test public void centerFeatureWithProperties() { JsonObject properties = new JsonObject(); properties.addProperty("key", "value"); Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON)); Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, properties, null); Point returnedPoint = (Point) returnedCenterFeature.geometry(); if (returnedPoint != null) { assertEquals(133.5, returnedPoint.longitude(), 0); assertEquals(-27.0, returnedPoint.latitude(), 0); if (returnedCenterFeature.properties() != null) { assertTrue(returnedCenterFeature.properties().toString().contains("{\"key\":\"value\"}")); } } }
@Test public void centerFeatureWithId() { final String testIdString = "testId"; Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON)); Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, null, testIdString); Point returnedPoint = (Point) returnedCenterFeature.geometry(); if (returnedPoint != null) { assertEquals(133.5, returnedPoint.longitude(), 0); assertEquals(-27.0, returnedPoint.latitude(), 0); if (returnedCenterFeature.id() != null) { assertEquals(returnedCenterFeature.id(), testIdString); } } }
@Test public void centerFeatureCollection() { FeatureCollection inputFeatureCollection = FeatureCollection.fromJson(loadJsonFixture(TURF_AREA_FEATURECOLLECTION_POLYGON_GEOJSON)); Feature returnedCenterFeature = TurfMeasurement.center(inputFeatureCollection, null, null); Point returnedPoint = (Point) returnedCenterFeature.geometry(); if (returnedPoint != null) { assertEquals(4.1748046875, returnedPoint.longitude(), DELTA); assertEquals(47.214224817196836, returnedPoint.latitude(), DELTA); } }