Java Code Examples for com.mapbox.geojson.FeatureCollection#fromFeatures()

The following examples show how to use com.mapbox.geojson.FeatureCollection#fromFeatures() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MapUtils.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * Takes a {@link FeatureCollection} and creates a map GeoJson source using the sourceId also
 * provided.
 *
 * @param mapboxMap  that the current mapView is using
 * @param collection the feature collection to be added to the map style
 * @param sourceId   the source's id for identifying it when adding layers
 * @since 0.8.0
 */
public static void updateMapSourceFromFeatureCollection(@NonNull MapboxMap mapboxMap,
                                                        @Nullable FeatureCollection collection,
                                                        @NonNull String sourceId) {
  if (collection == null) {
    collection = FeatureCollection.fromFeatures(new Feature[] {});
  }

  GeoJsonSource source = mapboxMap.getSourceAs(sourceId);
  if (source == null) {
    GeoJsonOptions routeGeoJsonOptions = new GeoJsonOptions().withMaxZoom(16);
    GeoJsonSource routeSource = new GeoJsonSource(sourceId, collection, routeGeoJsonOptions);
    mapboxMap.addSource(routeSource);
  } else {
    source.setGeoJson(collection);
  }
}
 
Example 2
Source File: TurfConversionTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void combine_featureCollectionSizeCheck() throws Exception {
  FeatureCollection pointMultiPolygonAndLineStringFeatureCollection =
    FeatureCollection.fromFeatures(
      Arrays.asList(
        Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)),
        Feature.fromGeometry(MultiPolygon.fromPolygons(Arrays.asList(
          Polygon.fromLngLats(Arrays.asList(Arrays.asList(
            Point.fromLngLat(11.42578125, 16.636191878397664),
            Point.fromLngLat(7.91015625, -9.102096738726443),
            Point.fromLngLat(31.113281249999996, 17.644022027872726),
            Point.fromLngLat(11.42578125, 16.636191878397664)
          )))))),
        Feature.fromGeometry(LineString.fromLngLats(
          Arrays.asList(Point.fromLngLat(-11.25, 55.7765),
            Point.fromLngLat(41.1328, 22.91792)))
        )));

  FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointMultiPolygonAndLineStringFeatureCollection);
  assertNotNull(combinedFeatureCollection);
  assertEquals(3, combinedFeatureCollection.features().size());
}
 
Example 3
Source File: TurfJoins.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Takes a {@link FeatureCollection} of {@link Point} and a {@link FeatureCollection} of
 * {@link Polygon} and returns the points that fall within the polygons.
 *
 * @param points   input points.
 * @param polygons input polygons.
 * @return points that land within at least one polygon.
 * @since 1.3.0
 */
public static FeatureCollection pointsWithinPolygon(FeatureCollection points,
                                                    FeatureCollection polygons) {
  ArrayList<Feature> features = new ArrayList<>();
  for (int i = 0; i < polygons.features().size(); i++) {
    for (int j = 0; j < points.features().size(); j++) {
      Point point = (Point) points.features().get(j).geometry();
      boolean isInside
        = TurfJoins.inside(point, (Polygon) polygons.features().get(i).geometry());
      if (isInside) {
        features.add(Feature.fromGeometry(point));
      }
    }
  }
  return FeatureCollection.fromFeatures(features);
}
 
Example 4
Source File: TurfConversionTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void combinePointAndMultiPointToMultiPoint() throws Exception {
  FeatureCollection pointAndMultiPointFeatureCollection =
      FeatureCollection.fromFeatures(
          Arrays.asList(
              Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)),
              Feature.fromGeometry(MultiPoint.fromLngLats(
                  Arrays.asList(Point.fromLngLat(41.83, 7.3624),
                      Point.fromLngLat(100, 101)))
              )));

  FeatureCollection combinedFeatureCollection =
      TurfConversion.combine(pointAndMultiPointFeatureCollection);

  assertNotNull(combinedFeatureCollection);

  MultiPoint multiPoint = (MultiPoint) combinedFeatureCollection.features().get(0).geometry();
  assertNotNull(multiPoint);

  assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA);
  assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA);
  assertEquals(41.83, multiPoint.coordinates().get(1).longitude(), DELTA);
  assertEquals(7.3624, multiPoint.coordinates().get(1).latitude(), DELTA);
  assertEquals(100, multiPoint.coordinates().get(2).longitude(), DELTA);
  assertEquals(101, multiPoint.coordinates().get(2).latitude(), DELTA);
}
 
Example 5
Source File: TurfConversionTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void combinePointsToMultiPoint() throws Exception {
  FeatureCollection pointFeatureCollection =
    FeatureCollection.fromFeatures(
      Arrays.asList(
        Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)),
        Feature.fromGeometry(Point.fromLngLat(41.83, 7.3624))
      ));

  FeatureCollection featureCollectionWithNewMultiPointObject = TurfConversion.combine(pointFeatureCollection);
  assertNotNull(featureCollectionWithNewMultiPointObject);

  MultiPoint multiPoint = (MultiPoint) featureCollectionWithNewMultiPointObject.features().get(0).geometry();
  assertNotNull(multiPoint);

  assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA);
  assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA);
  assertEquals(41.83, multiPoint.coordinates().get(1).longitude(), DELTA);
  assertEquals(7.3624, multiPoint.coordinates().get(1).latitude(), DELTA);
}
 
Example 6
Source File: TurfMetaTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void coordAllFeatureCollection() throws TurfException {
  String multipolygonJson = "{type: 'MultiPolygon', coordinates: [[[[0, 0], [1, 1], [0, 1], [0, 0]]]]}";
  String lineStringJson = "{type: 'LineString', coordinates: [[0, 0], [1, 1]]}";
  FeatureCollection featureCollection = FeatureCollection.fromFeatures(
    new Feature[] {
      Feature.fromGeometry(MultiPolygon.fromJson(multipolygonJson)),
      Feature.fromGeometry(LineString.fromJson(lineStringJson))}
  );
  assertNotNull(featureCollection);
  assertEquals(5, TurfMeta.coordAll(featureCollection,true).size());
  assertEquals(0, TurfMeta.coordAll(featureCollection,true).get(0).latitude(), DELTA);
  assertEquals(0, TurfMeta.coordAll(featureCollection,true).get(0).longitude(), DELTA);
  assertEquals(1, TurfMeta.coordAll(featureCollection,true).get(4).latitude(), DELTA);
  assertEquals(1, TurfMeta.coordAll(featureCollection,true).get(4).longitude(), DELTA);
}
 
Example 7
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * The routes also display an icon for each waypoint in the route, we use symbol layers for this.
 */
private FeatureCollection waypointFeatureCollection(DirectionsRoute route) {
  final List<Feature> waypointFeatures = new ArrayList<>();
  for (RouteLeg leg : route.legs()) {
    waypointFeatures.add(getPointFromLineString(leg, 0));
    waypointFeatures.add(getPointFromLineString(leg, leg.steps().size() - 1));
  }
  return FeatureCollection.fromFeatures(waypointFeatures);
}
 
Example 8
Source File: TurfConversionTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void combineTwoLineStringsToMultiLineString() throws Exception {
  FeatureCollection lineStringFeatureCollection =
      FeatureCollection.fromFeatures(
          Arrays.asList(
              Feature.fromGeometry(LineString.fromLngLats(
                  Arrays.asList(Point.fromLngLat(-11.25, 55.7765),
                      Point.fromLngLat(41.1328, 22.91792)))),
              Feature.fromGeometry(LineString.fromLngLats(
                  Arrays.asList(Point.fromLngLat(3.8671, 19.3111),
                      Point.fromLngLat(20.742, -20.3034))))
          ));

  FeatureCollection featureCollectionWithNewMultiLineStringObject = TurfConversion.combine(lineStringFeatureCollection);
  assertNotNull(featureCollectionWithNewMultiLineStringObject);

  MultiLineString multiLineString = (MultiLineString) featureCollectionWithNewMultiLineStringObject.features().get(0).geometry();
  assertNotNull(multiLineString);

  // Checking the first LineString in the MultiLineString
  assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA);
  assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA);

  // Checking the second LineString in the MultiLineString
  assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA);
  assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA);
}
 
Example 9
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfAlong() throws IOException, TurfException {
  Feature feature = Feature.fromJson(loadJsonFixture(TURF_ALONG_DC_LINE));
  LineString line = (LineString) feature.geometry();

  Point pt1 = TurfMeasurement.along(line, 1, "miles");
  Point pt2 = TurfMeasurement.along(line, 1.2, "miles");
  Point pt3 = TurfMeasurement.along(line, 1.4, "miles");
  Point pt4 = TurfMeasurement.along(line, 1.6, "miles");
  Point pt5 = TurfMeasurement.along(line, 1.8, "miles");
  Point pt6 = TurfMeasurement.along(line, 2, "miles");
  Point pt7 = TurfMeasurement.along(line, 100, "miles");
  Point pt8 = TurfMeasurement.along(line, 0, "miles");
  FeatureCollection fc = FeatureCollection.fromFeatures(new Feature[] {
    Feature.fromGeometry(pt1),
    Feature.fromGeometry(pt2),
    Feature.fromGeometry(pt3),
    Feature.fromGeometry(pt4),
    Feature.fromGeometry(pt5),
    Feature.fromGeometry(pt6),
    Feature.fromGeometry(pt7),
    Feature.fromGeometry(pt8)
  });

  for (Feature f : fc.features()) {
    assertNotNull(f);
    assertEquals("Feature", f.type());
    assertEquals("Point", f.geometry().type());
  }

  assertEquals(8, fc.features().size());
  assertEquals(((Point) fc.features().get(7).geometry()).longitude(), pt8.longitude(), DELTA);
  assertEquals(((Point) fc.features().get(7).geometry()).latitude(), pt8.latitude(), DELTA);
}
 
Example 10
Source File: TurfConversionTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void explodeFeatureCollection() throws NullPointerException {
  FeatureCollection featureCollection = FeatureCollection.fromFeatures(new Feature[] {
    Feature.fromGeometry(MultiLineString.fromJson(loadJsonFixture(TURF_EXPLODE_MULTILINESTRING))),
    Feature.fromGeometry(MultiPolygon.fromJson(loadJsonFixture(TURF_EXPLODE_MULTIPOLYGON)))
  });
  assertEquals(16, TurfConversion.explode(featureCollection).features().size());
}
 
Example 11
Source File: NavigationMapRoute.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * If the {@link DirectionsRoute} request contains congestion information via annotations, breakup
 * the source into pieces so data-driven styling can be used to change the route colors
 * accordingly.
 */
private FeatureCollection addTrafficToSource(DirectionsRoute route, int index) {
  final List<Feature> features = new ArrayList<>();
  LineString originalGeometry = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  buildRouteFeatureFromGeometry(index, features, originalGeometry);
  routeLineStrings.put(originalGeometry, route);

  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  buildTrafficFeaturesFromRoute(route, index, features, lineString);
  return FeatureCollection.fromFeatures(features);
}
 
Example 12
Source File: TurfJoinsTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void testWithin() throws TurfException {
  // Test with a single point
  ArrayList<Point> pointList = new ArrayList<>();
  pointList.add(Point.fromLngLat(0, 0));
  pointList.add(Point.fromLngLat(0, 100));
  pointList.add(Point.fromLngLat(100, 100));
  pointList.add(Point.fromLngLat(100, 0));
  pointList.add(Point.fromLngLat(0, 0));
  ArrayList<List<Point>> coordinates = new ArrayList<>();
  coordinates.add(pointList);
  Polygon poly = Polygon.fromLngLats(coordinates);

  Point pt = Point.fromLngLat(50, 50);

  ArrayList<Feature> features1 = new ArrayList<>();
  features1.add(Feature.fromGeometry(poly));
  FeatureCollection polyFeatureCollection = FeatureCollection.fromFeatures(features1);

  ArrayList<Feature> features2 = new ArrayList<>();
  features2.add(Feature.fromGeometry(pt));
  FeatureCollection ptFeatureCollection = FeatureCollection.fromFeatures(features2);

  FeatureCollection counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection);
  assertNotNull(counted);
  assertEquals(counted.features().size(), 1); // 1 point in 1 polygon

  // test with multiple points and multiple polygons
  Polygon poly1 = Polygon.fromLngLats(Arrays.asList(Arrays.asList(
    Point.fromLngLat(0, 0),
    Point.fromLngLat(10, 0),
    Point.fromLngLat(10, 10),
    Point.fromLngLat(0, 10),
    Point.fromLngLat(0, 0))));

  Polygon poly2 = Polygon.fromLngLats(Arrays.asList(Arrays.asList(
    Point.fromLngLat(10, 0),
    Point.fromLngLat(20, 10),
    Point.fromLngLat(20, 20),
    Point.fromLngLat(20, 0),
    Point.fromLngLat(10, 0))));
  
  polyFeatureCollection = FeatureCollection.fromFeatures(new Feature[] {
    Feature.fromGeometry(poly1),
    Feature.fromGeometry(poly2)});

  Point pt1 = Point.fromLngLat(1, 1);
  Point pt2 = Point.fromLngLat(1, 3);
  Point pt3 = Point.fromLngLat(14, 2);
  Point pt4 = Point.fromLngLat(13, 1);
  Point pt5 = Point.fromLngLat(19, 7);
  Point pt6 = Point.fromLngLat(100, 7);
  ptFeatureCollection = FeatureCollection.fromFeatures(new Feature[] {
    Feature.fromGeometry(pt1), Feature.fromGeometry(pt2), Feature.fromGeometry(pt3),
    Feature.fromGeometry(pt4), Feature.fromGeometry(pt5), Feature.fromGeometry(pt6)});

  counted = TurfJoins.pointsWithinPolygon(ptFeatureCollection, polyFeatureCollection);
  assertNotNull(counted);
  assertEquals(counted.features().size(), 5); // multiple points in multiple polygons
}
 
Example 13
Source File: TurfConversion.java    From mapbox-java with MIT License 4 votes vote down vote up
/**
 * 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);
}
 
Example 14
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void combineLineStringAndMultiLineStringToMultiLineString() throws Exception {
  FeatureCollection lineStringFeatureCollection =
      FeatureCollection.fromFeatures(
          Arrays.asList(
              Feature.fromGeometry(LineString.fromLngLats(Arrays.asList(
                  Point.fromLngLat(-11.25, 55.7765),
                  Point.fromLngLat(41.1328, 22.91792)))),
              Feature.fromGeometry(
                  MultiLineString.fromLineStrings(Arrays.asList(
                      LineString.fromLngLats(Arrays.asList(
                          Point.fromLngLat(102, -10),
                          Point.fromLngLat(130.0, 4.0)
                      )),
                      LineString.fromLngLats(Arrays.asList(
                          Point.fromLngLat(40.0, -20.0),
                          Point.fromLngLat(150.0, 18.0)
                      ))
                  )))
          ));

  FeatureCollection featureCollectionWithNewMultiLineStringObject =
      TurfConversion.combine(lineStringFeatureCollection);
  assertNotNull(featureCollectionWithNewMultiLineStringObject);

  MultiLineString multiLineString = (MultiLineString) featureCollectionWithNewMultiLineStringObject.
      features().get(0).geometry();
  assertNotNull(multiLineString);

  // Checking the first LineString in the MultiLineString
  assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA);
  assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA);

  assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA);
  assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA);

  // Checking the second LineString in the MultiLineString
  assertEquals(102, multiLineString.coordinates().get(1).get(0).longitude(), DELTA);
  assertEquals(-10, multiLineString.coordinates().get(1).get(0).latitude(), DELTA);

  assertEquals(130.0, multiLineString.coordinates().get(1).get(1).longitude(), DELTA);
  assertEquals(4.0, multiLineString.coordinates().get(1).get(1).latitude(), DELTA);

  // Checking the third LineString in the MultiLineString
  assertEquals(40.0, multiLineString.coordinates().get(2).get(0).longitude(), DELTA);
  assertEquals(-20.0, multiLineString.coordinates().get(2).get(0).latitude(), DELTA);

  assertEquals(150.0, multiLineString.coordinates().get(2).get(1).longitude(), DELTA);
  assertEquals(18.0, multiLineString.coordinates().get(2).get(1).latitude(), DELTA);
}
 
Example 15
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void combinePolygonToMultiPolygon() throws Exception {
  FeatureCollection polygonFeatureCollection =
      FeatureCollection.fromFeatures(
          Arrays.asList(
              Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList(
                  Arrays.asList(
                      Point.fromLngLat(61.938950426660604, 5.9765625),
                      Point.fromLngLat(52.696361078274485, 33.046875),
                      Point.fromLngLat(69.90011762668541, 28.828124999999996),
                      Point.fromLngLat(61.938950426660604, 5.9765625))))),
              Feature.fromGeometry(Polygon.fromLngLats(Arrays.asList(
                  Arrays.asList(
                      Point.fromLngLat(11.42578125, 16.636191878397664),
                      Point.fromLngLat(7.91015625, -9.102096738726443),
                      Point.fromLngLat(31.113281249999996, 17.644022027872726),
                      Point.fromLngLat(11.42578125, 16.636191878397664)
                  ))))
          ));

  FeatureCollection featureCollectionWithNewMultiPolygonObject = TurfConversion.combine(polygonFeatureCollection);
  assertNotNull(featureCollectionWithNewMultiPolygonObject);

  MultiPolygon multiPolygon = (MultiPolygon) featureCollectionWithNewMultiPolygonObject.features().get(0).geometry();
  assertNotNull(multiPolygon);

  // Checking the first Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA);
  assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA);
  assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA);

  // Checking the second Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA);
  assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA);
  assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA);
}
 
Example 16
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void combinePolygonAndMultiPolygonAndPointToMultiPolygon() throws Exception {
  FeatureCollection featureCollectionWithPointPolygonAndMultiPolygon =
      FeatureCollection.fromFeatures(
          Arrays.asList(
              Feature.fromGeometry(
                  Point.fromLngLat(-2.46, 27.6835)),
              Feature.fromGeometry(
                  Polygon.fromLngLats(Arrays.asList(Arrays.asList(
                      Point.fromLngLat(61.938950426660604, 5.9765625),
                      Point.fromLngLat(52.696361078274485, 33.046875),
                      Point.fromLngLat(69.90011762668541, 28.828124999999996),
                      Point.fromLngLat(61.938950426660604, 5.9765625))))),
              Feature.fromGeometry(
                  MultiPolygon.fromPolygons(Arrays.asList(
                      Polygon.fromLngLats(Arrays.asList(Arrays.asList(
                      Point.fromLngLat(11.42578125, 16.636191878397664),
                      Point.fromLngLat(7.91015625, -9.102096738726443),
                      Point.fromLngLat(31.113281249999996, 17.644022027872726),
                      Point.fromLngLat(11.42578125, 16.636191878397664)
                  ))),
                      Polygon.fromLngLats(Arrays.asList(Arrays.asList(
                      Point.fromLngLat(30.0, 0.0),
                      Point.fromLngLat(102.0, 0.0),
                      Point.fromLngLat(103.0, 1.0),
                      Point.fromLngLat(30.0, 0.0)
                  )))
              )))
          ));

  FeatureCollection combinedFeatureCollection = TurfConversion.combine(featureCollectionWithPointPolygonAndMultiPolygon);
  assertNotNull(combinedFeatureCollection);
  MultiPolygon multiPolygon = null;
  MultiPoint multiPoint = null;
  for (int x = 0; x < combinedFeatureCollection.features().size(); x++) {
    Feature singleFeature = combinedFeatureCollection.features().get(x);
    if (singleFeature.geometry() instanceof MultiPolygon) {
      multiPolygon = (MultiPolygon) combinedFeatureCollection.features().get(x).geometry();
    }
    if (singleFeature.geometry() instanceof MultiPoint) {
      multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry();
    }
  }
  assertNotNull(multiPolygon);
  assertNotNull(multiPoint);

  // Checking the first Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(61.938950426660604, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA);
  assertEquals(5.9765625, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(52.696361078274485, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA);
  assertEquals(33.046875, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA);

  // Checking the second Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(11.42578125, multiPolygon.coordinates().get(1).get(0).get(0).longitude(), DELTA);
  assertEquals(16.636191878397664, multiPolygon.coordinates().get(1).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(7.91015625, multiPolygon.coordinates().get(1).get(0).get(1).longitude(), DELTA);
  assertEquals(-9.102096738726443, multiPolygon.coordinates().get(1).get(0).get(1).latitude(), DELTA);

  // Checking the third Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(30.0, multiPolygon.coordinates().get(2).get(0).get(0).longitude(), DELTA);
  assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(102.0, multiPolygon.coordinates().get(2).get(0).get(1).longitude(), DELTA);
  assertEquals(0.0, multiPolygon.coordinates().get(2).get(0).get(1).latitude(), DELTA);
}
 
Example 17
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void combinePointAndLineStringGeometry() throws Exception {
  FeatureCollection pointAndLineStringFeatureCollection =
    FeatureCollection.fromFeatures(
      Arrays.asList(
        Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)),
        Feature.fromGeometry(
          LineString.fromLngLats(
            Arrays.asList(Point.fromLngLat(-11.25, 55.7765),
              Point.fromLngLat(41.1328, 22.91792)))
        )));

  FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointAndLineStringFeatureCollection);
  assertNotNull(combinedFeatureCollection);
  MultiPoint multiPoint = null;
  MultiLineString multiLineString = null;
  for (int x = 0; x < combinedFeatureCollection.features().size(); x++) {
    Feature singleFeature = combinedFeatureCollection.features().get(x);
    if (singleFeature.geometry() instanceof MultiPoint) {
      multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry();
    }
    if (singleFeature.geometry() instanceof MultiLineString) {
      multiLineString = (MultiLineString) combinedFeatureCollection.features().get(x).geometry();
    }
  }
  assertNotNull(multiPoint);
  assertNotNull(multiLineString);

  // Checking the LineString in the MultiLineString

  // Checking the first LineString location
  assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA);
  assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA);

  // Checking the second LineString location
  assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA);
  assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA);

  // Checking the Point in the MultiPoint

  // Checking the first and only Point
  assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA);
  assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA);
}
 
Example 18
Source File: TurfConversionTest.java    From mapbox-java with MIT License 4 votes vote down vote up
@Test
public void combinePointAndMultiPolygonAndLineStringGeometry() throws Exception {
  FeatureCollection pointMultiPolygonAndLineStringFeatureCollection =
    FeatureCollection.fromFeatures(
      Arrays.asList(
        Feature.fromGeometry(Point.fromLngLat(-2.46, 27.6835)),
        Feature.fromGeometry(MultiPolygon.fromPolygons(Arrays.asList(
          Polygon.fromLngLats(Arrays.asList(Arrays.asList(
            Point.fromLngLat(11.42578125, 16.636191878397664),
            Point.fromLngLat(7.91015625, -9.102096738726443),
            Point.fromLngLat(31.113281249999996, 17.644022027872726),
            Point.fromLngLat(11.42578125, 16.636191878397664)
          )))))),
        Feature.fromGeometry(LineString.fromLngLats(
          Arrays.asList(Point.fromLngLat(-11.25, 55.7765),
            Point.fromLngLat(41.1328, 22.91792)))
        )));

  FeatureCollection combinedFeatureCollection = TurfConversion.combine(pointMultiPolygonAndLineStringFeatureCollection);
  assertNotNull(combinedFeatureCollection);
  MultiPoint multiPoint = null;
  MultiLineString multiLineString = null;
  MultiPolygon multiPolygon = null;
  for (int x = 0; x < combinedFeatureCollection.features().size(); x++) {
    Feature singleFeature = combinedFeatureCollection.features().get(x);
    if (singleFeature.geometry() instanceof MultiPoint) {
      multiPoint = (MultiPoint) combinedFeatureCollection.features().get(x).geometry();
    }
    if (singleFeature.geometry() instanceof MultiLineString) {
      multiLineString = (MultiLineString) combinedFeatureCollection.features().get(x).geometry();
    }
    if (singleFeature.geometry() instanceof MultiPolygon) {
      multiPolygon = (MultiPolygon) combinedFeatureCollection.features().get(x).geometry();
    }
  }
  assertNotNull(multiPoint);
  assertNotNull(multiLineString);
  assertNotNull(multiPolygon);

  // Checking the Polygon in the MultiPolygon

  // Checking the first Point
  assertEquals(11.42578125, multiPolygon.coordinates().get(0).get(0).get(0).longitude(), DELTA);
  assertEquals(16.636191878397664, multiPolygon.coordinates().get(0).get(0).get(0).latitude(), DELTA);

  // Checking the second Point
  assertEquals(7.91015625, multiPolygon.coordinates().get(0).get(0).get(1).longitude(), DELTA);
  assertEquals(-9.102096738726443, multiPolygon.coordinates().get(0).get(0).get(1).latitude(), DELTA);

  // Checking the LineString in the MultiLineString

  // Checking the first LineString location
  assertEquals(-11.25, multiLineString.coordinates().get(0).get(0).longitude(), DELTA);
  assertEquals(55.7765, multiLineString.coordinates().get(0).get(0).latitude(), DELTA);

  // Checking the second LineString location
  assertEquals(41.1328, multiLineString.coordinates().get(0).get(1).longitude(), DELTA);
  assertEquals(22.91792, multiLineString.coordinates().get(0).get(1).latitude(), DELTA);

  // Checking the Point in the MultiPoint

  // Checking the first and only Point
  assertEquals(-2.46, multiPoint.coordinates().get(0).longitude(), DELTA);
  assertEquals(27.6835, multiPoint.coordinates().get(0).latitude(), DELTA);
}
 
Example 19
Source File: TurfConversion.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes a {@link Feature}  and
 * returns its position as a {@link Point} objects.
 *
 * @param feature a {@link Feature} object
 * @return a new {@link FeatureCollection} object with {@link Point} objects
 * @since 4.8.0
 */
public static FeatureCollection explode(@NonNull Feature feature) {
  List<Feature> finalFeatureList = new ArrayList<>();
  for (Point singlePoint : TurfMeta.coordAll(feature, true)) {
    finalFeatureList.add(Feature.fromGeometry(singlePoint));
  }
  return FeatureCollection.fromFeatures(finalFeatureList);
}
 
Example 20
Source File: TurfConversion.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes a {@link FeatureCollection} and
 * returns all positions as {@link Point} objects.
 *
 * @param featureCollection a {@link FeatureCollection} object
 * @return a new {@link FeatureCollection} object with {@link Point} objects
 * @since 4.8.0
 */
public static FeatureCollection explode(@NonNull FeatureCollection featureCollection) {
  List<Feature> finalFeatureList = new ArrayList<>();
  for (Point singlePoint : TurfMeta.coordAll(featureCollection, true)) {
    finalFeatureList.add(Feature.fromGeometry(singlePoint));
  }
  return FeatureCollection.fromFeatures(finalFeatureList);
}