Skip to content

Commit 9c390ff

Browse files
author
Langston Smith
authored
Adding BoundingBoxPolygon Turf method (#1006)
1 parent 67a4678 commit 9c390ff

6 files changed

Lines changed: 233 additions & 2 deletions

File tree

docs/turf-port.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ Below's an on going list of the Turf functions which currently exist inside the
1111
- [x] turf-along
1212
- [ ] turf-area
1313
- [x] turf-bbox
14-
- [ ] turf-bbox-polygon
14+
- [x] turf-bbox-polygon
1515
- [x] turf-bearing
1616
- [ ] turf-center
1717
- [ ] turf-center-of-mass

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

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,18 @@
66
import android.support.annotation.FloatRange;
77
import android.support.annotation.NonNull;
88

9+
import com.mapbox.geojson.BoundingBox;
910
import com.mapbox.geojson.Geometry;
1011
import com.mapbox.geojson.GeometryCollection;
1112
import com.mapbox.geojson.LineString;
1213
import com.mapbox.geojson.MultiLineString;
1314
import com.mapbox.geojson.MultiPoint;
15+
import com.mapbox.geojson.MultiPolygon;
1416
import com.mapbox.geojson.Point;
1517
import com.mapbox.geojson.Polygon;
16-
import com.mapbox.geojson.MultiPolygon;
1718

1819
import java.util.ArrayList;
20+
import java.util.Arrays;
1921
import java.util.List;
2022

2123
/**
@@ -395,4 +397,46 @@ private static double[] bboxCalculator(List<Point> resultCoords) {
395397
}
396398
return bbox;
397399
}
400+
401+
/**
402+
* Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if
403+
* one of the polygons contains holes, the perimeter will also be included.
404+
*
405+
* @param multiPolygon geometry to measure
406+
* @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
407+
* @return total perimeter of the input polygons combined, in the units specified
408+
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
409+
* @since 1.2.0
410+
*/
411+
412+
/**
413+
* Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if
414+
* one of the polygons contains holes, the perimeter will also be included.
415+
*
416+
* @param multiPolygon geometry to measure
417+
* @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
418+
* @return total perimeter of the input polygons combined, in the units specified
419+
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
420+
* @since 1.2.0
421+
*/
422+
423+
/**
424+
* Takes a {@link BoundingBox} and uses its coordinates to create a {@link Polygon}
425+
* geometry.
426+
*
427+
* @param boundingBox a {@link BoundingBox} object to calculate with
428+
* @return a {@link Polygon} object
429+
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
430+
* @since 4.7.0
431+
*/
432+
public static Polygon bboxPolygon(@NonNull BoundingBox boundingBox) {
433+
return Polygon.fromLngLats(
434+
Arrays.asList(
435+
Arrays.asList(
436+
Point.fromLngLat(boundingBox.west(), boundingBox.south()),
437+
Point.fromLngLat(boundingBox.east(), boundingBox.south()),
438+
Point.fromLngLat(boundingBox.east(), boundingBox.north()),
439+
Point.fromLngLat(boundingBox.west(), boundingBox.north()),
440+
Point.fromLngLat(boundingBox.west(), boundingBox.south()))));
441+
}
398442
}

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

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

3+
import com.mapbox.geojson.BoundingBox;
34
import com.mapbox.geojson.Feature;
45
import com.mapbox.geojson.FeatureCollection;
56
import com.mapbox.geojson.Geometry;
@@ -37,6 +38,9 @@ public class TurfMeasurementTest extends TestUtils {
3738
private static final String TURF_BBOX_POLYGON = "turf-bbox/polygon.geojson";
3839
private static final String TURF_BBOX_MULTILINESTRING = "turf-bbox/multilinestring.geojson";
3940
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";
4044
private static final String LINE_DISTANCE_MULTILINESTRING
4145
= "turf-line-distance/multilinestring.geojson";
4246

@@ -340,4 +344,70 @@ public void bboxFromGeometryCollection() throws IOException, TurfException {
340344
assertEquals(130, bbox[2], DELTA);
341345
assertEquals(4, bbox[3], DELTA);
342346
}
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+
}
343413
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "LineString",
3+
"coordinates": [
4+
[
5+
102,
6+
-10
7+
],
8+
[
9+
103,
10+
1
11+
],
12+
[
13+
104,
14+
0
15+
],
16+
[
17+
130,
18+
4
19+
]
20+
]
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"type": "MultiPoint",
3+
"coordinates": [
4+
[
5+
102,
6+
-10
7+
],
8+
[
9+
103,
10+
1
11+
],
12+
[
13+
104,
14+
0
15+
],
16+
[
17+
130,
18+
4
19+
]
20+
]
21+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"type": "MultiPolygon",
3+
"coordinates": [
4+
[
5+
[
6+
[
7+
102,
8+
2
9+
],
10+
[
11+
103,
12+
2
13+
],
14+
[
15+
103,
16+
3
17+
],
18+
[
19+
102,
20+
3
21+
],
22+
[
23+
102,
24+
2
25+
]
26+
]
27+
],
28+
[
29+
[
30+
[
31+
100,
32+
0
33+
],
34+
[
35+
101,
36+
0
37+
],
38+
[
39+
101,
40+
1
41+
],
42+
[
43+
100,
44+
1
45+
],
46+
[
47+
100,
48+
0
49+
]
50+
],
51+
[
52+
[
53+
100.2,
54+
0.2
55+
],
56+
[
57+
100.8,
58+
0.2
59+
],
60+
[
61+
100.8,
62+
0.8
63+
],
64+
[
65+
100.2,
66+
0.8
67+
],
68+
[
69+
100.2,
70+
0.2
71+
]
72+
]
73+
]
74+
]
75+
}

0 commit comments

Comments
 (0)