Skip to content

Commit

Permalink
Adjustments to TurfMeasurement#bboxPolygon() to return a Feature inst…
Browse files Browse the repository at this point in the history
…ead of Polygon (#1055)
  • Loading branch information
Langston Smith authored Jun 24, 2019
1 parent ec8c41f commit a4b4f05
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 14 deletions.
56 changes: 45 additions & 11 deletions services-turf/src/main/java/com/mapbox/turf/TurfMeasurement.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import android.support.annotation.FloatRange;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import com.google.gson.JsonObject;
import com.mapbox.geojson.BoundingBox;
import com.mapbox.geojson.Feature;
import com.mapbox.geojson.FeatureCollection;
Expand Down Expand Up @@ -457,38 +459,71 @@ private static double[] bboxCalculator(List<Point> resultCoords) {
* geometry.
*
* @param boundingBox a {@link BoundingBox} object to calculate with
* @return a {@link Polygon} object
* @return a {@link Feature} object
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
* @since 4.7.0
* @since 4.9.0
*/
public static Feature bboxPolygon(@NonNull BoundingBox boundingBox) {
return bboxPolygon(boundingBox, null, null);
}

/**
* Takes a {@link BoundingBox} and uses its coordinates to create a {@link Polygon}
* geometry.
*
* @param boundingBox a {@link BoundingBox} object to calculate with
* @param properties a {@link JsonObject} containing the feature properties
* @param id common identifier of this feature
* @return a {@link Feature} object
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
* @since 4.9.0
*/
public static Polygon bboxPolygon(@NonNull BoundingBox boundingBox) {
return Polygon.fromLngLats(
public static Feature bboxPolygon(@NonNull BoundingBox boundingBox,
@Nullable JsonObject properties,
@Nullable String id) {
return Feature.fromGeometry(Polygon.fromLngLats(
Collections.singletonList(
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()))));
Point.fromLngLat(boundingBox.west(), boundingBox.south())))), properties, id);
}

/**
* Takes a bbox and uses its coordinates to create a {@link Polygon} geometry.
*
* @param bbox a double[] object to calculate with
* @return a {@link Polygon} object
* @return a {@link Feature} object
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
* @since 4.9.0
*/
public static Polygon bboxPolygon(@NonNull double[] bbox) {
return Polygon.fromLngLats(
public static Feature bboxPolygon(@NonNull double[] bbox) {
return bboxPolygon(bbox, null, null);
}

/**
* Takes a bbox and uses its coordinates to create a {@link Polygon} geometry.
*
* @param bbox a double[] object to calculate with
* @param properties a {@link JsonObject} containing the feature properties
* @param id common identifier of this feature
* @return a {@link Feature} object
* @see <a href="http://turfjs.org/docs/#bboxPolygon">Turf BoundingBox Polygon documentation</a>
* @since 4.9.0
*/
public static Feature bboxPolygon(@NonNull double[] bbox,
@Nullable JsonObject properties,
@Nullable String id) {
return Feature.fromGeometry(Polygon.fromLngLats(
Collections.singletonList(
Arrays.asList(
Point.fromLngLat(bbox[0], bbox[1]),
Point.fromLngLat(bbox[2], bbox[1]),
Point.fromLngLat(bbox[2], bbox[3]),
Point.fromLngLat(bbox[0], bbox[3]),
Point.fromLngLat(bbox[0], bbox[1]))));
Point.fromLngLat(bbox[0], bbox[1])))), properties, id);
}

/**
Expand All @@ -499,10 +534,9 @@ public static Polygon bboxPolygon(@NonNull double[] bbox) {
* @since 4.9.0
*/
public static Polygon envelope(GeoJson geoJson) {
return bboxPolygon(bbox(geoJson));
return (Polygon) bboxPolygon(bbox(geoJson)).geometry();
}


/**
* Takes a bounding box and calculates the minimum square bounding box
* that would contain the input.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,11 +359,40 @@ public void bboxPolygonFromLineString() throws IOException, TurfException {
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);
Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

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 bboxPolygonFromLineStringWithId() 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.
Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox, null, "TEST_ID");
Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
assertEquals("TEST_ID", featureRepresentingBoundingBox.id());
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));
Expand All @@ -384,7 +413,9 @@ public void bboxPolygonFromMultiPolygon() throws IOException, TurfException {
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);
Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
Expand All @@ -405,7 +436,9 @@ public void bboxPolygonFromMultiPoint() throws IOException, TurfException {
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);
Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

assertNotNull(polygonRepresentingBoundingBox);
assertEquals(0, polygonRepresentingBoundingBox.inner().size());
Expand Down

0 comments on commit a4b4f05

Please sign in to comment.