use of com.mapbox.geojson.Geometry 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);
}
use of com.mapbox.geojson.Geometry in project mapbox-java by mapbox.
the class TurfMeasurementTest method bboxFromGeometry.
@Test
public void bboxFromGeometry() throws IOException, TurfException {
Geometry geometry = MultiPolygon.fromJson(loadJsonFixture(TURF_BBOX_MULTIPOLYGON));
double[] bbox = TurfMeasurement.bbox(geometry);
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.Geometry in project maps by rnmapbox.
the class RCTMGLOfflineModule method convertRegionsToJSON.
private void convertRegionsToJSON(List<TileRegion> tileRegions, Promise promise) {
CountDownLatch countDownLatch = new CountDownLatch(tileRegions.size());
ArrayList<TileRegionError> errors = new ArrayList<>();
ArrayList<Geometry> geometries = new ArrayList<>();
try {
for (TileRegion region : tileRegions) {
getTileStore().getTileRegionGeometry(region.getId(), new TileRegionGeometryCallback() {
@Override
public void run(@NonNull Expected<TileRegionError, Geometry> result) {
if (result.isValue()) {
geometries.add(result.getValue());
} else {
errors.add(result.getError());
}
countDownLatch.countDown();
}
});
}
} catch (Error error) {
Logger.e("OS", "a");
}
try {
countDownLatch.await();
WritableArray result = Arguments.createArray();
for (Geometry geometry : geometries) {
result.pushMap(fromOfflineRegion(geometry));
}
for (TileRegionError error : errors) {
WritableMap errorMap = Arguments.createMap();
errorMap.putString("type", "error");
errorMap.putString("message", error.getMessage());
errorMap.putString("errorType", error.getType().toString());
result.pushMap(errorMap);
}
promise.resolve(result);
} catch (InterruptedException interruptedException) {
promise.reject(interruptedException);
}
}
Aggregations