Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding BoundingBoxPolygon turf method #1006

Merged
merged 1 commit into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/turf-port.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Below's an on going list of the Turf functions which currently exist inside the
- [x] turf-along
- [ ] turf-area
- [x] turf-bbox
- [ ] turf-bbox-polygon
- [x] turf-bbox-polygon
- [x] turf-bearing
- [ ] turf-center
- [ ] turf-center-of-mass
Expand Down
46 changes: 45 additions & 1 deletion services-turf/src/main/java/com/mapbox/turf/TurfMeasurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;

import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Geometry;
import com.mapbox.geojson.GeometryCollection;
import com.mapbox.geojson.LineString;
import com.mapbox.geojson.MultiLineString;
import com.mapbox.geojson.MultiPoint;
import com.mapbox.geojson.MultiPolygon;
import com.mapbox.geojson.Point;
import com.mapbox.geojson.Polygon;
import com.mapbox.geojson.MultiPolygon;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand Down Expand Up @@ -395,4 +397,46 @@ private static double[] bboxCalculator(List<Point> resultCoords) {
}
return bbox;
}

/**
* Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if
* one of the polygons contains holes, the perimeter will also be included.
*
* @param multiPolygon geometry to measure
* @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
* @return total perimeter of the input polygons combined, in the units specified
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
* @since 1.2.0
*/

/**
* Takes a {@link MultiPolygon} and measures each polygons perimeter in the specified units. if
* one of the polygons contains holes, the perimeter will also be included.
*
* @param multiPolygon geometry to measure
* @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
* @return total perimeter of the input polygons combined, in the units specified
* @see <a href="http://turfjs.org/docs/#linedistance">Turf Line Distance documentation</a>
* @since 1.2.0
*/

/**
* Takes a {@link BoundingBox} and uses its coordinates to create a {@link Polygon}
* geometry.
*
* @param boundingBox a {@link BoundingBox} object to calculate with
* @return a {@link Polygon} object
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
* @since 4.7.0
*/
public static Polygon bboxPolygon(@NonNull BoundingBox boundingBox) {
return Polygon.fromLngLats(
Arrays.asList(
Arrays.asList(
Point.fromLngLat(boundingBox.west(), boundingBox.south()),
Point.fromLngLat(boundingBox.east(), boundingBox.south()),
Point.fromLngLat(boundingBox.east(), boundingBox.north()),
Point.fromLngLat(boundingBox.west(), boundingBox.north()),
Point.fromLngLat(boundingBox.west(), boundingBox.south()))));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mapbox.turf;

import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
import com.mapbox.geojson.Geometry;
Expand Down Expand Up @@ -37,6 +38,9 @@ public class TurfMeasurementTest extends TestUtils {
private static final String TURF_BBOX_POLYGON = "turf-bbox/polygon.geojson";
private static final String TURF_BBOX_MULTILINESTRING = "turf-bbox/multilinestring.geojson";
private static final String TURF_BBOX_MULTIPOLYGON = "turf-bbox/multipolygon.geojson";
private static final String TURF_BBOX_POLYGON_LINESTRING = "turf-bbox-polygon/linestring.geojson";
private static final String TURF_BBOX_POLYGON_MULTIPOLYGON = "turf-bbox-polygon/multipolygon.geojson";
private static final String TURF_BBOX_POLYGON_MULTI_POINT = "turf-bbox-polygon/multipoint.geojson";
private static final String LINE_DISTANCE_MULTILINESTRING
= "turf-line-distance/multilinestring.geojson";

Expand Down Expand Up @@ -340,4 +344,70 @@ public void bboxFromGeometryCollection() throws IOException, TurfException {
assertEquals(130, bbox[2], DELTA);
assertEquals(4, bbox[3], DELTA);
}

@Test
public void bboxPolygonFromLineString() throws IOException, TurfException {
// Create a LineString
LineString lineString = LineString.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_LINESTRING));

// Use the LineString object to calculate its BoundingBox area
double[] bbox = TurfMeasurement.bbox(lineString);

// Use the BoundingBox coordinates to create an actual BoundingBox object
BoundingBox boundingBox = BoundingBox.fromPoints(
Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3]));

// Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method.
Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
assertEquals(Point.fromLngLat(102.0,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(0));
assertEquals(Point.fromLngLat(130,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(1));
assertEquals(Point.fromLngLat(130.0,4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(2));
assertEquals(Point.fromLngLat(102.0,4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(3));
assertEquals(Point.fromLngLat(102.0,-10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4));
}

@Test
public void bboxPolygonFromMultiPolygon() throws IOException, TurfException {
// Create a MultiPolygon
MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_MULTIPOLYGON));

// Use the MultiPolygon object to calculate its BoundingBox area
double[] bbox = TurfMeasurement.bbox(multiPolygon);

// Use the BoundingBox coordinates to create an actual BoundingBox object
BoundingBox boundingBox = BoundingBox.fromPoints(
Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3]));

// Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method.
Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
assertEquals(Point.fromLngLat(100,0.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4));
}

@Test
public void bboxPolygonFromMultiPoint() throws IOException, TurfException {
// Create a MultiPoint
MultiPoint multiPoint = MultiPoint.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_MULTI_POINT));

// Use the MultiPoint object to calculate its BoundingBox area
double[] bbox = TurfMeasurement.bbox(multiPoint);

// Use the BoundingBox coordinates to create an actual BoundingBox object
BoundingBox boundingBox = BoundingBox.fromPoints(
Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3]));

// Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method.
Polygon polygonRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "LineString",
"coordinates": [
[
102,
-10
],
[
103,
1
],
[
104,
0
],
[
130,
4
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"type": "MultiPoint",
"coordinates": [
[
102,
-10
],
[
103,
1
],
[
104,
0
],
[
130,
4
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
{
"type": "MultiPolygon",
"coordinates": [
[
[
[
102,
2
],
[
103,
2
],
[
103,
3
],
[
102,
3
],
[
102,
2
]
]
],
[
[
[
100,
0
],
[
101,
0
],
[
101,
1
],
[
100,
1
],
[
100,
0
]
],
[
[
100.2,
0.2
],
[
100.8,
0.2
],
[
100.8,
0.8
],
[
100.2,
0.8
],
[
100.2,
0.2
]
]
]
]
}