|
1 | 1 | package com.mapbox.turf; |
2 | 2 |
|
| 3 | +import com.google.gson.JsonObject; |
3 | 4 | import com.mapbox.geojson.BoundingBox; |
4 | 5 | import com.mapbox.geojson.Feature; |
5 | 6 | import com.mapbox.geojson.FeatureCollection; |
|
25 | 26 | import static org.junit.Assert.assertEquals; |
26 | 27 | import static org.junit.Assert.assertNotEquals; |
27 | 28 | import static org.junit.Assert.assertNotNull; |
| 29 | +import static org.junit.Assert.assertTrue; |
28 | 30 |
|
29 | 31 | public class TurfMeasurementTest extends TestUtils { |
30 | 32 |
|
@@ -507,5 +509,52 @@ public void areaFeatureCollection() { |
507 | 509 | assertEquals(expected, TurfMeasurement.area(FeatureCollection.fromJson(loadJsonFixture(TURF_AREA_FEATURECOLLECTION_POLYGON_GEOJSON))), 1); |
508 | 510 | } |
509 | 511 |
|
| 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 | + } |
510 | 518 |
|
| 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 | + } |
511 | 560 | } |
0 commit comments