Search in sources :

Example 6 with MultiLineString

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

Example 7 with MultiLineString

use of com.mapbox.geojson.MultiLineString in project mapbox-java by mapbox.

the class TurfMeasurementTest method bboxFromMultiLineString.

@Test
public void bboxFromMultiLineString() throws IOException, TurfException {
    MultiLineString multiLineString = MultiLineString.fromJson(loadJsonFixture(TURF_BBOX_MULTILINESTRING));
    double[] bbox = TurfMeasurement.bbox(multiLineString);
    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 : MultiLineString(com.mapbox.geojson.MultiLineString) Test(org.junit.Test)

Example 8 with MultiLineString

use of com.mapbox.geojson.MultiLineString 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 9 with MultiLineString

use of com.mapbox.geojson.MultiLineString in project mapbox-java by mapbox.

the class TurfConversionTest method explodeMultiLineStringSingleFeature.

@Test
public void explodeMultiLineStringSingleFeature() throws NullPointerException {
    MultiLineString multiLineString = MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING));
    assertEquals(4, TurfConversion.explode(Feature.fromGeometry(multiLineString)).features().size());
}
Also used : MultiLineString(com.mapbox.geojson.MultiLineString) Test(org.junit.Test)

Aggregations

MultiLineString (com.mapbox.geojson.MultiLineString)9 Test (org.junit.Test)7 Feature (com.mapbox.geojson.Feature)4 FeatureCollection (com.mapbox.geojson.FeatureCollection)4 MultiPoint (com.mapbox.geojson.MultiPoint)4 Point (com.mapbox.geojson.Point)4 MultiPolygon (com.mapbox.geojson.MultiPolygon)3 Geometry (com.mapbox.geojson.Geometry)2 LineString (com.mapbox.geojson.LineString)2 Polygon (com.mapbox.geojson.Polygon)2 ArrayList (java.util.ArrayList)2 GeometryCollection (com.mapbox.geojson.GeometryCollection)1