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

The following examples show how to use com.mapbox.geojson.FeatureCollection#features() . 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: TurfAssertions.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Enforce expectations about types of {@link FeatureCollection} inputs for Turf. Internally
 * this uses {@link Feature#type()}} to judge geometry types.
 *
 * @param featureCollection for which features will be judged
 * @param type              expected GeoJson type
 * @param name              name of calling function
 * @see <a href="http://turfjs.org/docs/#collectionof">Turf collectionOf documentation</a>
 * @since 1.2.0
 */
public static void collectionOf(FeatureCollection featureCollection, String type, String name) {
  if (name == null || name.length() == 0) {
    throw new TurfException("collectionOf() requires a name");
  }
  if (featureCollection == null || !featureCollection.type().equals("FeatureCollection")
    || featureCollection.features() == null) {
    throw new TurfException(String.format(
      "Invalid input to %s, FeatureCollection required", name));
  }
  for (Feature feature : featureCollection.features()) {
    if (feature == null || !feature.type().equals("Feature") || feature.geometry() == null) {
      throw new TurfException(String.format(
        "Invalid input to %s, Feature with geometry required", name));
    }
    if (feature.geometry() == null || !feature.geometry().type().equals(type)) {
      throw new TurfException(String.format(
        "Invalid input to %s: must be a %s, given %s", name, type, feature.geometry().type()));
    }
  }
}
 
Example 2
Source File: TurfClassificationTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void testLineDistanceWithGeometries() throws IOException, TurfException {
  Point pt = (Point) Feature.fromJson(loadJsonFixture(PT)).geometry();
  FeatureCollection pts = FeatureCollection.fromJson(loadJsonFixture(PTS));

  List<Point> pointList = new ArrayList<>();
  for (Feature feature : pts.features()) {
    pointList.add((Point) (feature.geometry()));
  }
  Point closestPt = TurfClassification.nearestPoint(pt, pointList);

  Assert.assertNotNull(closestPt);
  Assert.assertEquals(closestPt.type(), "Point");
  Assert.assertEquals(closestPt.longitude(), -75.33, DELTA);
  Assert.assertEquals(closestPt.latitude(), 39.44, DELTA);
}
 
Example 3
Source File: TurfMeasurement.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Takes one {@link FeatureCollection} and returns it's area in square meters.
 *
 * @param featureCollection input {@link FeatureCollection}
 * @return area in square meters
 * @since 4.10.0
 */
public static double area(@NonNull FeatureCollection featureCollection) {
  List<Feature> features = featureCollection.features();
  double total = 0.0f;
  if (features != null) {
    for (Feature feature : features) {
      total += area(feature);
    }
  }
  return total;
}
 
Example 4
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 5
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 6
Source File: CircleManager.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a list of circles on the map.
 * <p>
 * Circles are going to be created only for features with a matching geometry.
 * <p>
 * All supported properties are:<br>
 * CircleOptions.PROPERTY_CIRCLE_RADIUS - Float<br>
 * CircleOptions.PROPERTY_CIRCLE_COLOR - String<br>
 * CircleOptions.PROPERTY_CIRCLE_BLUR - Float<br>
 * CircleOptions.PROPERTY_CIRCLE_OPACITY - Float<br>
 * CircleOptions.PROPERTY_CIRCLE_STROKE_WIDTH - Float<br>
 * CircleOptions.PROPERTY_CIRCLE_STROKE_COLOR - String<br>
 * CircleOptions.PROPERTY_CIRCLE_STROKE_OPACITY - Float<br>
 * Learn more about above properties in the <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>.
 * <p>
 * Out of spec properties:<br>
 * "is-draggable" - Boolean, true if the circle should be draggable, false otherwise
 *
 * @param featureCollection the featureCollection defining the list of circles to build
 * @return the list of built circles
 */
@UiThread
public List<Circle> create(@NonNull FeatureCollection featureCollection) {
  List<Feature> features = featureCollection.features();
  List<CircleOptions> options = new ArrayList<>();
  if (features != null) {
    for (Feature feature : features) {
      CircleOptions option = CircleOptions.fromFeature(feature);
      if (option != null) {
        options.add(option);
      }
    }
  }
  return create(options);
}
 
Example 7
Source File: FillManager.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a list of fills on the map.
 * <p>
 * Fills are going to be created only for features with a matching geometry.
 * <p>
 * All supported properties are:<br>
 * FillOptions.PROPERTY_FILL_OPACITY - Float<br>
 * FillOptions.PROPERTY_FILL_COLOR - String<br>
 * FillOptions.PROPERTY_FILL_OUTLINE_COLOR - String<br>
 * FillOptions.PROPERTY_FILL_PATTERN - String<br>
 * Learn more about above properties in the <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>.
 * <p>
 * Out of spec properties:<br>
 * "is-draggable" - Boolean, true if the fill should be draggable, false otherwise
 *
 * @param featureCollection the featureCollection defining the list of fills to build
 * @return the list of built fills
 */
@UiThread
public List<Fill> create(@NonNull FeatureCollection featureCollection) {
  List<Feature> features = featureCollection.features();
  List<FillOptions> options = new ArrayList<>();
  if (features != null) {
    for (Feature feature : features) {
      FillOptions option = FillOptions.fromFeature(feature);
      if (option != null) {
        options.add(option);
      }
    }
  }
  return create(options);
}
 
Example 8
Source File: SymbolManager.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a list of symbols on the map.
 * <p>
 * Symbols are going to be created only for features with a matching geometry.
 * <p>
 * All supported properties are:<br>
 * SymbolOptions.PROPERTY_SYMBOL_SORT_KEY - Float<br>
 * SymbolOptions.PROPERTY_ICON_SIZE - Float<br>
 * SymbolOptions.PROPERTY_ICON_IMAGE - String<br>
 * SymbolOptions.PROPERTY_ICON_ROTATE - Float<br>
 * SymbolOptions.PROPERTY_ICON_OFFSET - Float[]<br>
 * SymbolOptions.PROPERTY_ICON_ANCHOR - String<br>
 * SymbolOptions.PROPERTY_TEXT_FIELD - String<br>
 * SymbolOptions.PROPERTY_TEXT_FONT - String[]<br>
 * SymbolOptions.PROPERTY_TEXT_SIZE - Float<br>
 * SymbolOptions.PROPERTY_TEXT_MAX_WIDTH - Float<br>
 * SymbolOptions.PROPERTY_TEXT_LETTER_SPACING - Float<br>
 * SymbolOptions.PROPERTY_TEXT_JUSTIFY - String<br>
 * SymbolOptions.PROPERTY_TEXT_RADIAL_OFFSET - Float<br>
 * SymbolOptions.PROPERTY_TEXT_ANCHOR - String<br>
 * SymbolOptions.PROPERTY_TEXT_ROTATE - Float<br>
 * SymbolOptions.PROPERTY_TEXT_TRANSFORM - String<br>
 * SymbolOptions.PROPERTY_TEXT_OFFSET - Float[]<br>
 * SymbolOptions.PROPERTY_ICON_OPACITY - Float<br>
 * SymbolOptions.PROPERTY_ICON_COLOR - String<br>
 * SymbolOptions.PROPERTY_ICON_HALO_COLOR - String<br>
 * SymbolOptions.PROPERTY_ICON_HALO_WIDTH - Float<br>
 * SymbolOptions.PROPERTY_ICON_HALO_BLUR - Float<br>
 * SymbolOptions.PROPERTY_TEXT_OPACITY - Float<br>
 * SymbolOptions.PROPERTY_TEXT_COLOR - String<br>
 * SymbolOptions.PROPERTY_TEXT_HALO_COLOR - String<br>
 * SymbolOptions.PROPERTY_TEXT_HALO_WIDTH - Float<br>
 * SymbolOptions.PROPERTY_TEXT_HALO_BLUR - Float<br>
 * Learn more about above properties in the <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>.
 * <p>
 * Out of spec properties:<br>
 * "is-draggable" - Boolean, true if the symbol should be draggable, false otherwise
 *
 * @param featureCollection the featureCollection defining the list of symbols to build
 * @return the list of built symbols
 */
@UiThread
public List<Symbol> create(@NonNull FeatureCollection featureCollection) {
  List<Feature> features = featureCollection.features();
  List<SymbolOptions> options = new ArrayList<>();
  if (features != null) {
    for (Feature feature : features) {
      SymbolOptions option = SymbolOptions.fromFeature(feature);
      if (option != null) {
        options.add(option);
      }
    }
  }
  return create(options);
}
 
Example 9
Source File: LineManager.java    From mapbox-plugins-android with BSD 2-Clause "Simplified" License 3 votes vote down vote up
/**
 * Create a list of lines on the map.
 * <p>
 * Lines are going to be created only for features with a matching geometry.
 * <p>
 * All supported properties are:<br>
 * LineOptions.PROPERTY_LINE_JOIN - String<br>
 * LineOptions.PROPERTY_LINE_OPACITY - Float<br>
 * LineOptions.PROPERTY_LINE_COLOR - String<br>
 * LineOptions.PROPERTY_LINE_WIDTH - Float<br>
 * LineOptions.PROPERTY_LINE_GAP_WIDTH - Float<br>
 * LineOptions.PROPERTY_LINE_OFFSET - Float<br>
 * LineOptions.PROPERTY_LINE_BLUR - Float<br>
 * LineOptions.PROPERTY_LINE_PATTERN - String<br>
 * Learn more about above properties in the <a href="https://www.mapbox.com/mapbox-gl-js/style-spec/">Style specification</a>.
 * <p>
 * Out of spec properties:<br>
 * "is-draggable" - Boolean, true if the line should be draggable, false otherwise
 *
 * @param featureCollection the featureCollection defining the list of lines to build
 * @return the list of built lines
 */
@UiThread
public List<Line> create(@NonNull FeatureCollection featureCollection) {
  List<Feature> features = featureCollection.features();
  List<LineOptions> options = new ArrayList<>();
  if (features != null) {
    for (Feature feature : features) {
      LineOptions option = LineOptions.fromFeature(feature);
      if (option != null) {
        options.add(option);
      }
    }
  }
  return create(options);
}
 
Example 10
Source File: TurfMeta.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Get all coordinates from a {@link FeatureCollection} object, returning a
 * {@code List} of {@link Point} objects.
 *
 * @param featureCollection the {@link FeatureCollection} that you'd like
 *                          to extract the Points from.
 * @param excludeWrapCoord  whether or not to include the final coordinate of LinearRings that
 *                          wraps the ring in its iteration. Used if a {@link Feature} in the
 *                          {@link FeatureCollection} that's passed through this method, is a
 *                          {@link Polygon} or {@link MultiPolygon} geometry.
 * @return a {@code List} made up of {@link Point}s
 * @since 4.8.0
 */
@NonNull
public static List<Point> coordAll(@NonNull FeatureCollection featureCollection,
                                   @NonNull boolean excludeWrapCoord) {
  List<Point> finalCoordsList = new ArrayList<>();
  for (Feature singleFeature : featureCollection.features()) {
    addCoordAll(finalCoordsList, singleFeature, excludeWrapCoord);
  }
  return finalCoordsList;
}