Skip to content

Commit c53f4a0

Browse files
author
Langston Smith
authored
[turf] Ported TurfMeasurement.center() method (#1150)
1 parent 3e8cd54 commit c53f4a0

3 files changed

Lines changed: 111 additions & 2 deletions

File tree

docs/turf-port.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Below's an on going list of the Turf functions which currently exist inside the
1212
- [x] turf-bbox
1313
- [x] turf-bbox-polygon
1414
- [x] turf-bearing
15-
- [ ] turf-center
15+
- [x] turf-center
1616
- [ ] turf-center-of-mass
1717
- [ ] turf-centroid
1818
- [x] turf-destination

services-turf/src/main/java/com/mapbox/turf/TurfMeasurement.java

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -631,7 +631,7 @@ public static double area(@NonNull FeatureCollection featureCollection) {
631631
}
632632

633633
/**
634-
* Takes one {@link Geometry} and returns it's area in square meters.
634+
* Takes one {@link Geometry} and returns its area in square meters.
635635
*
636636
* @param geometry input {@link Geometry}
637637
* @return area in square meters
@@ -719,4 +719,64 @@ private static double ringArea(@NonNull List<Point> coordinates) {
719719
private static double rad(double num) {
720720
return num * Math.PI / 180;
721721
}
722+
723+
/**
724+
* Takes a {@link Feature} and returns the absolute center of the {@link Feature}.
725+
*
726+
* @param feature the single {@link Feature} to find the center of.
727+
* @param properties a optional {@link JsonObject} containing the properties that should be
728+
* placed in the returned {@link Feature}.
729+
* @param id an optional common identifier that should be placed in the returned {@link Feature}.
730+
* @return a {@link Feature} with a {@link Point} geometry type.
731+
* @since 5.3.0
732+
*/
733+
public static Feature center(Feature feature,
734+
@Nullable JsonObject properties,
735+
@Nullable String id) {
736+
return center(FeatureCollection.fromFeature(feature), properties, id);
737+
}
738+
739+
/**
740+
* Takes a {@link Feature} and returns the absolute center of the {@link Feature}.
741+
*
742+
* @param feature the single {@link Feature} to find the center of.
743+
* @return a {@link Feature} with a {@link Point} geometry type.
744+
* @since 5.3.0
745+
*/
746+
public static Feature center(Feature feature) {
747+
return center(FeatureCollection.fromFeature(feature), null, null);
748+
}
749+
750+
/**
751+
* Takes {@link FeatureCollection} and returns the absolute center
752+
* of the {@link Feature}s in the {@link FeatureCollection}.
753+
*
754+
* @param featureCollection the single {@link FeatureCollection} to find the center of.
755+
* @param properties a optional {@link JsonObject} containing the properties that should be
756+
* placed in the returned {@link Feature}.
757+
* @param id an optional common identifier that should be placed in the returned {@link Feature}.
758+
* @return a {@link Feature} with a {@link Point} geometry type.
759+
* @since 5.3.0
760+
*/
761+
public static Feature center(FeatureCollection featureCollection,
762+
@Nullable JsonObject properties,
763+
@Nullable String id) {
764+
double[] ext = bbox(featureCollection);
765+
double finalCenterLongitude = (ext[0] + ext[2]) / 2;
766+
double finalCenterLatitude = (ext[1] + ext[3]) / 2;
767+
return Feature.fromGeometry(Point.fromLngLat(finalCenterLongitude, finalCenterLatitude),
768+
properties, id);
769+
}
770+
771+
/**
772+
* Takes {@link FeatureCollection} and returns the absolute center
773+
* of the {@link Feature}s in the {@link FeatureCollection}.
774+
*
775+
* @param featureCollection the single {@link FeatureCollection} to find the center of.
776+
* @return a {@link Feature} with a {@link Point} geometry type.
777+
* @since 5.3.0
778+
*/
779+
public static Feature center(FeatureCollection featureCollection) {
780+
return center(featureCollection, null, null);
781+
}
722782
}

services-turf/src/test/java/com/mapbox/turf/TurfMeasurementTest.java

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mapbox.turf;
22

3+
import com.google.gson.JsonObject;
34
import com.mapbox.geojson.BoundingBox;
45
import com.mapbox.geojson.Feature;
56
import com.mapbox.geojson.FeatureCollection;
@@ -25,6 +26,7 @@
2526
import static org.junit.Assert.assertEquals;
2627
import static org.junit.Assert.assertNotEquals;
2728
import static org.junit.Assert.assertNotNull;
29+
import static org.junit.Assert.assertTrue;
2830

2931
public class TurfMeasurementTest extends TestUtils {
3032

@@ -507,5 +509,52 @@ public void areaFeatureCollection() {
507509
assertEquals(expected, TurfMeasurement.area(FeatureCollection.fromJson(loadJsonFixture(TURF_AREA_FEATURECOLLECTION_POLYGON_GEOJSON))), 1);
508510
}
509511

512+
@Test
513+
public void centerFeature() {
514+
Feature expectedFeature = Feature.fromGeometry(Point.fromLngLat(133.5, -27.0));
515+
Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
516+
assertEquals(expectedFeature, TurfMeasurement.center(inputFeature, null, null));
517+
}
510518

519+
@Test
520+
public void centerFeatureWithProperties() {
521+
JsonObject properties = new JsonObject();
522+
properties.addProperty("key", "value");
523+
Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
524+
Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, properties, null);
525+
Point returnedPoint = (Point) returnedCenterFeature.geometry();
526+
if (returnedPoint != null) {
527+
assertEquals(133.5, returnedPoint.longitude(), 0);
528+
assertEquals(-27.0, returnedPoint.latitude(), 0);
529+
if (returnedCenterFeature.properties() != null) {
530+
assertTrue(returnedCenterFeature.properties().toString().contains("{\"key\":\"value\"}"));
531+
}
532+
}
533+
}
534+
535+
@Test
536+
public void centerFeatureWithId() {
537+
final String testIdString = "testId";
538+
Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
539+
Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, null, testIdString);
540+
Point returnedPoint = (Point) returnedCenterFeature.geometry();
541+
if (returnedPoint != null) {
542+
assertEquals(133.5, returnedPoint.longitude(), 0);
543+
assertEquals(-27.0, returnedPoint.latitude(), 0);
544+
if (returnedCenterFeature.id() != null) {
545+
assertEquals(returnedCenterFeature.id(), testIdString);
546+
}
547+
}
548+
}
549+
550+
@Test
551+
public void centerFeatureCollection() {
552+
FeatureCollection inputFeatureCollection = FeatureCollection.fromJson(loadJsonFixture(TURF_AREA_FEATURECOLLECTION_POLYGON_GEOJSON));
553+
Feature returnedCenterFeature = TurfMeasurement.center(inputFeatureCollection, null, null);
554+
Point returnedPoint = (Point) returnedCenterFeature.geometry();
555+
if (returnedPoint != null) {
556+
assertEquals(4.1748046875, returnedPoint.longitude(), DELTA);
557+
assertEquals(47.214224817196836, returnedPoint.latitude(), DELTA);
558+
}
559+
}
511560
}

0 commit comments

Comments
 (0)