Search in sources :

Example 1 with GeometryCollection

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

the class TurfMetaTest method coordAllGeometryCollection.

@Test
public void coordAllGeometryCollection() throws TurfException {
    List<Point> points = new ArrayList<>();
    points.add(Point.fromLngLat(1.0, 2.0));
    points.add(Point.fromLngLat(2.0, 3.0));
    LineString lineString = LineString.fromLngLats(points);
    List<Geometry> geometries = new ArrayList<>();
    geometries.add(points.get(0));
    geometries.add(lineString);
    BoundingBox bbox = BoundingBox.fromLngLats(1.0, 2.0, 3.0, 4.0);
    GeometryCollection geometryCollection = GeometryCollection.fromGeometries(geometries, bbox);
    FeatureCollection featureCollection = FeatureCollection.fromFeature(Feature.fromGeometry(geometryCollection));
    assertNotNull(featureCollection);
    assertNotNull(TurfMeta.coordAll(featureCollection, true));
    assertEquals(3, TurfMeta.coordAll(featureCollection, true).size());
    assertEquals(1.0, TurfMeta.coordAll(featureCollection, true).get(0).longitude(), DELTA);
    assertEquals(2.0, TurfMeta.coordAll(featureCollection, true).get(0).latitude(), DELTA);
}
Also used : Geometry(com.mapbox.geojson.Geometry) GeometryCollection(com.mapbox.geojson.GeometryCollection) LineString(com.mapbox.geojson.LineString) FeatureCollection(com.mapbox.geojson.FeatureCollection) BoundingBox(com.mapbox.geojson.BoundingBox) ArrayList(java.util.ArrayList) Point(com.mapbox.geojson.Point) Test(org.junit.Test)

Example 2 with GeometryCollection

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

the class TurfConversionTest method explodeGeometryCollectionSingleFeature.

@Test
public void explodeGeometryCollectionSingleFeature() throws NullPointerException {
    GeometryCollection geometryCollection = GeometryCollection.fromJson(loadJsonFixture(TURF_EXPLODE_GEOMETRY_COLLECTION));
    assertEquals(3, TurfConversion.explode(Feature.fromGeometry(geometryCollection)).features().size());
}
Also used : GeometryCollection(com.mapbox.geojson.GeometryCollection) Test(org.junit.Test)

Example 3 with GeometryCollection

use of com.mapbox.geojson.GeometryCollection 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)

Aggregations

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