use of com.mapbox.geojson.MultiPolygon in project mapbox-java by mapbox.
the class TurfMeasurement method bbox.
/**
* Takes an arbitrary {@link Geometry} and calculates a bounding box.
*
* @param geometry a {@link Geometry} object
* @return a double array defining the bounding box in this order {@code [minX, minY, maxX, maxY]}
* @since 2.0.0
*/
public static double[] bbox(Geometry geometry) {
if (geometry instanceof Point) {
return bbox((Point) geometry);
} else if (geometry instanceof MultiPoint) {
return bbox((MultiPoint) geometry);
} else if (geometry instanceof LineString) {
return bbox((LineString) geometry);
} else if (geometry instanceof MultiLineString) {
return bbox((MultiLineString) geometry);
} else if (geometry instanceof Polygon) {
return bbox((Polygon) geometry);
} else if (geometry instanceof MultiPolygon) {
return bbox((MultiPolygon) geometry);
} else if (geometry instanceof GeometryCollection) {
List<Point> points = new ArrayList<>();
for (Geometry geo : ((GeometryCollection) geometry).geometries()) {
// recursive
double[] bbox = bbox(geo);
points.add(Point.fromLngLat(bbox[0], bbox[1]));
points.add(Point.fromLngLat(bbox[2], bbox[1]));
points.add(Point.fromLngLat(bbox[2], bbox[3]));
points.add(Point.fromLngLat(bbox[0], bbox[3]));
}
return TurfMeasurement.bbox(MultiPoint.fromLngLats(points));
} else {
throw new RuntimeException(("Unknown geometry class: " + geometry.getClass()));
}
}
use of com.mapbox.geojson.MultiPolygon in project mapbox-java by mapbox.
the class TurfMeasurementTest method bboxFromMultiPolygon.
@Test
public void bboxFromMultiPolygon() throws IOException, TurfException {
MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON));
double[] bbox = TurfMeasurement.bbox(multiPolygon);
assertEquals(4, bbox.length);
assertEquals(100, bbox[0], DELTA);
assertEquals(0, bbox[1], DELTA);
assertEquals(103, bbox[2], DELTA);
assertEquals(3, bbox[3], DELTA);
}
use of com.mapbox.geojson.MultiPolygon in project mapbox-java by mapbox.
the class TurfMeasurementTest method bboxFromGeometryCollection.
@Test
public void bboxFromGeometryCollection() throws IOException, TurfException {
// Check that geometry collection and direct bbox are equal
MultiPolygon multiPolygon = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON));
assertArrayEquals(TurfMeasurement.bbox(multiPolygon), TurfMeasurement.bbox(GeometryCollection.fromGeometry(multiPolygon)), DELTA);
// Check all geometry types
List<Geometry> geometries = new ArrayList<>();
geometries.add(Feature.fromJson(loadJsonFixture(TURF_BBOX_POINT)).geometry());
geometries.add(MultiPoint.fromJson(loadJsonFixture(TURF_BBOX_MULTI_POINT)));
geometries.add(LineString.fromJson(loadJsonFixture(TURF_BBOX_LINESTRING)));
geometries.add(MultiLineString.fromJson(loadJsonFixture(TURF_BBOX_MULTILINESTRING)));
geometries.add(Feature.fromJson(loadJsonFixture(TURF_BBOX_POLYGON)).geometry());
geometries.add(MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON)));
geometries.add(GeometryCollection.fromGeometry(Point.fromLngLat(-1., -1.)));
double[] bbox = TurfMeasurement.bbox(GeometryCollection.fromGeometries(geometries));
assertEquals(4, bbox.length);
assertEquals(-1, bbox[0], DELTA);
assertEquals(-10, bbox[1], DELTA);
assertEquals(130, bbox[2], DELTA);
assertEquals(4, bbox[3], DELTA);
}
use of com.mapbox.geojson.MultiPolygon in project mapbox-java by mapbox.
the class TurfMetaTest method coordAllMultiPolygon.
@Test
public void coordAllMultiPolygon() throws TurfException {
String multipolygonString = "{type: 'MultiPolygon', coordinates: [[[[0, 0], [1, 1], [0, 1], [0, 0]]]]}";
MultiPolygon multiPolygonGeometry = MultiPolygon.fromJson(multipolygonString);
List<Point> resultList = TurfMeta.coordAll(multiPolygonGeometry, false);
assertEquals(resultList.size(), 4, DELTA);
assertEquals(resultList.get(0), Point.fromLngLat(0, 0));
assertEquals(resultList.get(1), Point.fromLngLat(1, 1));
assertEquals(resultList.get(2), Point.fromLngLat(0, 1));
assertEquals(resultList.get(3), Point.fromLngLat(0, 0));
}
use of com.mapbox.geojson.MultiPolygon in project mapbox-java by mapbox.
the class TurfConversion method combine.
/**
* Combines a FeatureCollection of geometries and returns
* a {@link FeatureCollection} with "Multi-" geometries in it.
* If the original FeatureCollection parameter has {@link Point}(s)
* and/or {@link MultiPoint}s), the returned
* FeatureCollection will include a {@link MultiPoint} object.
*
* If the original FeatureCollection parameter has
* {@link LineString}(s) and/or {@link MultiLineString}s), the returned
* FeatureCollection will include a {@link MultiLineString} object.
*
* If the original FeatureCollection parameter has
* {@link Polygon}(s) and/or {@link MultiPolygon}s), the returned
* FeatureCollection will include a {@link MultiPolygon} object.
*
* @param originalFeatureCollection a {@link FeatureCollection}
*
* @return a {@link FeatureCollection} with a "Multi-" geometry
* or "Multi-" geometries.
*
* @since 4.10.0
*/
public static FeatureCollection combine(@NonNull FeatureCollection originalFeatureCollection) {
if (originalFeatureCollection.features() == null) {
throw new TurfException("Your FeatureCollection is null.");
} else if (originalFeatureCollection.features().size() == 0) {
throw new TurfException("Your FeatureCollection doesn't have any Feature objects in it.");
}
List<Point> pointList = new ArrayList<>(0);
List<LineString> lineStringList = new ArrayList<>(0);
List<Polygon> polygonList = new ArrayList<>(0);
for (Feature singleFeature : originalFeatureCollection.features()) {
Geometry singleFeatureGeometry = singleFeature.geometry();
if (singleFeatureGeometry instanceof Point || singleFeatureGeometry instanceof MultiPoint) {
if (singleFeatureGeometry instanceof Point) {
pointList.add((Point) singleFeatureGeometry);
} else {
pointList.addAll(((MultiPoint) singleFeatureGeometry).coordinates());
}
} else if (singleFeatureGeometry instanceof LineString || singleFeatureGeometry instanceof MultiLineString) {
if (singleFeatureGeometry instanceof LineString) {
lineStringList.add((LineString) singleFeatureGeometry);
} else {
lineStringList.addAll(((MultiLineString) singleFeatureGeometry).lineStrings());
}
} else if (singleFeatureGeometry instanceof Polygon || singleFeatureGeometry instanceof MultiPolygon) {
if (singleFeatureGeometry instanceof Polygon) {
polygonList.add((Polygon) singleFeatureGeometry);
} else {
polygonList.addAll(((MultiPolygon) singleFeatureGeometry).polygons());
}
}
}
List<Feature> finalFeatureList = new ArrayList<>(0);
if (!pointList.isEmpty()) {
finalFeatureList.add(Feature.fromGeometry(MultiPoint.fromLngLats(pointList)));
}
if (!lineStringList.isEmpty()) {
finalFeatureList.add(Feature.fromGeometry(MultiLineString.fromLineStrings(lineStringList)));
}
if (!polygonList.isEmpty()) {
finalFeatureList.add(Feature.fromGeometry(MultiPolygon.fromPolygons(polygonList)));
}
return finalFeatureList.isEmpty() ? originalFeatureCollection : FeatureCollection.fromFeatures(finalFeatureList);
}
Aggregations