|
1 | 1 | package com.mapbox.turf; |
2 | 2 |
|
| 3 | +import com.mapbox.geojson.BoundingBox; |
3 | 4 | import com.mapbox.geojson.Feature; |
4 | 5 | import com.mapbox.geojson.FeatureCollection; |
5 | 6 | import com.mapbox.geojson.Geometry; |
@@ -37,6 +38,9 @@ public class TurfMeasurementTest extends TestUtils { |
37 | 38 | private static final String TURF_BBOX_POLYGON = "turf-bbox/polygon.geojson"; |
38 | 39 | private static final String TURF_BBOX_MULTILINESTRING = "turf-bbox/multilinestring.geojson"; |
39 | 40 | private static final String TURF_BBOX_MULTIPOLYGON = "turf-bbox/multipolygon.geojson"; |
| 41 | + private static final String TURF_BBOX_POLYGON_LINESTRING = "turf-bbox-polygon/linestring.geojson"; |
| 42 | + private static final String TURF_BBOX_POLYGON_MULTIPOLYGON = "turf-bbox-polygon/multipolygon.geojson"; |
| 43 | + private static final String TURF_BBOX_POLYGON_MULTI_POINT = "turf-bbox-polygon/multipoint.geojson"; |
40 | 44 | private static final String LINE_DISTANCE_MULTILINESTRING |
41 | 45 | = "turf-line-distance/multilinestring.geojson"; |
42 | 46 |
|
@@ -340,4 +344,70 @@ public void bboxFromGeometryCollection() throws IOException, TurfException { |
340 | 344 | assertEquals(130, bbox[2], DELTA); |
341 | 345 | assertEquals(4, bbox[3], DELTA); |
342 | 346 | } |
| 347 | + |
| 348 | + @Test |
| 349 | + public void bboxPolygonFromLineString() throws IOException, TurfException { |
| 350 | + // Create a LineString |
| 351 | + LineString lineString = LineString.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_LINESTRING)); |
| 352 | + |
| 353 | + // Use the LineString object to calculate its BoundingBox area |
| 354 | + double[] bbox = TurfMeasurement.bbox(lineString); |
| 355 | + |
| 356 | + // Use the BoundingBox coordinates to create an actual BoundingBox object |
| 357 | + BoundingBox boundingBox = BoundingBox.fromPoints( |
| 358 | + Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3])); |
| 359 | + |
| 360 | + // Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method. |
| 361 | + Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox); |
| 362 | + |
| 363 | + assertNotNull(polygonRepresentingBoundingBox); |
| 364 | + assertEquals(0, polygonRepresentingBoundingBox.inner().size()); |
| 365 | + assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size()); |
| 366 | + assertEquals(Point.fromLngLat(102.0,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(0)); |
| 367 | + assertEquals(Point.fromLngLat(130,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(1)); |
| 368 | + assertEquals(Point.fromLngLat(130.0,4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(2)); |
| 369 | + assertEquals(Point.fromLngLat(102.0,4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(3)); |
| 370 | + assertEquals(Point.fromLngLat(102.0,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4)); |
| 371 | + } |
| 372 | + |
| 373 | + @Test |
| 374 | + public void bboxPolygonFromMultiPolygon() throws IOException, TurfException { |
| 375 | + // Create a MultiPolygon |
| 376 | + MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_MULTIPOLYGON)); |
| 377 | + |
| 378 | + // Use the MultiPolygon object to calculate its BoundingBox area |
| 379 | + double[] bbox = TurfMeasurement.bbox(multiPolygon); |
| 380 | + |
| 381 | + // Use the BoundingBox coordinates to create an actual BoundingBox object |
| 382 | + BoundingBox boundingBox = BoundingBox.fromPoints( |
| 383 | + Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3])); |
| 384 | + |
| 385 | + // Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method. |
| 386 | + Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox); |
| 387 | + |
| 388 | + assertNotNull(polygonRepresentingBoundingBox); |
| 389 | + assertEquals(0, polygonRepresentingBoundingBox.inner().size()); |
| 390 | + assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size()); |
| 391 | + assertEquals(Point.fromLngLat(100,0.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4)); |
| 392 | + } |
| 393 | + |
| 394 | + @Test |
| 395 | + public void bboxPolygonFromMultiPoint() throws IOException, TurfException { |
| 396 | + // Create a MultiPoint |
| 397 | + MultiPoint multiPoint = MultiPoint.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_MULTI_POINT)); |
| 398 | + |
| 399 | + // Use the MultiPoint object to calculate its BoundingBox area |
| 400 | + double[] bbox = TurfMeasurement.bbox(multiPoint); |
| 401 | + |
| 402 | + // Use the BoundingBox coordinates to create an actual BoundingBox object |
| 403 | + BoundingBox boundingBox = BoundingBox.fromPoints( |
| 404 | + Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3])); |
| 405 | + |
| 406 | + // Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method. |
| 407 | + Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox); |
| 408 | + |
| 409 | + assertNotNull(polygonRepresentingBoundingBox); |
| 410 | + assertEquals(0, polygonRepresentingBoundingBox.inner().size()); |
| 411 | + assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size()); |
| 412 | + } |
343 | 413 | } |
0 commit comments