Search in sources :

Example 6 with Geometry

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);
}
Also used : MultiPoint(com.mapbox.geojson.MultiPoint) MultiLineString(com.mapbox.geojson.MultiLineString) ArrayList(java.util.ArrayList) MultiPoint(com.mapbox.geojson.MultiPoint) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature) Geometry(com.mapbox.geojson.Geometry) LineString(com.mapbox.geojson.LineString) MultiLineString(com.mapbox.geojson.MultiLineString) MultiPolygon(com.mapbox.geojson.MultiPolygon) Polygon(com.mapbox.geojson.Polygon) MultiPolygon(com.mapbox.geojson.MultiPolygon)

Example 7 with Geometry

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);
}
Also used : Geometry(com.mapbox.geojson.Geometry) Test(org.junit.Test)

Example 8 with Geometry

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);
    }
}
Also used : TileRegionGeometryCallback(com.mapbox.common.TileRegionGeometryCallback) WritableMap(com.facebook.react.bridge.WritableMap) WritableArray(com.facebook.react.bridge.WritableArray) ArrayList(java.util.ArrayList) TileRegionError(com.mapbox.common.TileRegionError) CountDownLatch(java.util.concurrent.CountDownLatch) TileRegion(com.mapbox.common.TileRegion) Geometry(com.mapbox.geojson.Geometry) TileRegionError(com.mapbox.common.TileRegionError)

Aggregations

Geometry (com.mapbox.geojson.Geometry)8 ArrayList (java.util.ArrayList)7 LineString (com.mapbox.geojson.LineString)3 MultiPolygon (com.mapbox.geojson.MultiPolygon)3 Point (com.mapbox.geojson.Point)3 Test (org.junit.Test)3 Feature (com.mapbox.geojson.Feature)2 GeometryCollection (com.mapbox.geojson.GeometryCollection)2 MultiLineString (com.mapbox.geojson.MultiLineString)2 MultiPoint (com.mapbox.geojson.MultiPoint)2 Polygon (com.mapbox.geojson.Polygon)2 PointF (android.graphics.PointF)1 WritableArray (com.facebook.react.bridge.WritableArray)1 WritableMap (com.facebook.react.bridge.WritableMap)1 MoveDistancesObject (com.mapbox.android.gestures.MoveDistancesObject)1 TileRegion (com.mapbox.common.TileRegion)1 TileRegionError (com.mapbox.common.TileRegionError)1 TileRegionGeometryCallback (com.mapbox.common.TileRegionGeometryCallback)1 BoundingBox (com.mapbox.geojson.BoundingBox)1 FeatureCollection (com.mapbox.geojson.FeatureCollection)1