Java Code Examples for com.mapbox.geojson.Feature#geometry()

The following examples show how to use com.mapbox.geojson.Feature#geometry() . 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: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void testTurfPointOnLinePointsInFrontOfLastPoint() throws TurfException {
  List<Point> line = new ArrayList<>();
  line.add(Point.fromLngLat(-122.45616137981413, 37.72125936929241));
  line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638));
  line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215));

  Point last = line.get(2);

  List<Point> pts = new ArrayList<>();
  pts.add(Point.fromLngLat(-122.45696067810057, 37.7181405249708));
  pts.add(Point.fromLngLat(-122.4573630094528, 37.71813203814049));
  pts.add(Point.fromLngLat(-122.45730936527252, 37.71797927502795));
  pts.add(Point.fromLngLat(-122.45718061923981, 37.71704571582896));

  for (Point pt : pts) {
    Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line);
    Point snapped = (Point) snappedFeature.geometry();
    // pt behind start moves to last vertex
    assertEquals(last, snapped);
  }
}
 
Example 2
Source File: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void testLineSliceAlongRoute1() throws IOException, TurfException {
  Feature route1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_ROUTE_ONE));
  LineString lineStringRoute1 = (LineString)route1.geometry();

  double start = 500;
  double stop = 750;

  Point start_point = TurfMeasurement.along(lineStringRoute1, start, TurfConstants.UNIT_MILES);
  Point end_point = TurfMeasurement.along(lineStringRoute1, stop, TurfConstants.UNIT_MILES);

  LineString sliced = TurfMisc.lineSliceAlong(route1, start, stop, TurfConstants.UNIT_MILES);

  assertEquals(sliced.coordinates().get(0).coordinates(),
    start_point.coordinates());
  assertEquals(sliced.coordinates().get(sliced.coordinates().size() - 1).coordinates(),
    end_point.coordinates());
}
 
Example 3
Source File: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void testLineSliceAlongLine1() throws IOException, TurfException {
  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_LINE_ONE));
  LineString lineStringLine1 = (LineString) line1.geometry();

  double start = 500;
  double stop = 750;

  Point start_point = TurfMeasurement.along(lineStringLine1, start, TurfConstants.UNIT_MILES);
  Point end_point = TurfMeasurement.along(lineStringLine1, stop, TurfConstants.UNIT_MILES);
  LineString sliced = TurfMisc.lineSliceAlong(line1, start, stop, TurfConstants.UNIT_MILES);

  assertEquals(sliced.coordinates().get(0).coordinates(),
    start_point.coordinates());
  assertEquals(sliced.coordinates().get(sliced.coordinates().size() - 1).coordinates(),
    end_point.coordinates());
}
 
Example 4
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void bboxPolygonFromLineStringWithId() throws IOException, TurfException {
  // Create a LineString
  LineString lineString = LineString.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_LINESTRING));

  // Use the LineString object to calculate its BoundingBox area
  double[] bbox = TurfMeasurement.bbox(lineString);

  // Use the BoundingBox coordinates to create an actual BoundingBox object
  BoundingBox boundingBox = BoundingBox.fromPoints(
    Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3]));

  // Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method.
  Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox, null, "TEST_ID");
  Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

  assertNotNull(polygonRepresentingBoundingBox);
  assertEquals(0, polygonRepresentingBoundingBox.inner().size());
  assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
  assertEquals("TEST_ID", featureRepresentingBoundingBox.id());
  assertEquals(Point.fromLngLat(102.0, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(0));
  assertEquals(Point.fromLngLat(130, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(1));
  assertEquals(Point.fromLngLat(130.0, 4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(2));
  assertEquals(Point.fromLngLat(102.0, 4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(3));
  assertEquals(Point.fromLngLat(102.0, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4));
}
 
Example 5
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
public void bboxPolygonFromLineString() throws IOException, TurfException {
  // Create a LineString
  LineString lineString = LineString.fromJson(loadJsonFixture(TURF_BBOX_POLYGON_LINESTRING));

  // Use the LineString object to calculate its BoundingBox area
  double[] bbox = TurfMeasurement.bbox(lineString);

  // Use the BoundingBox coordinates to create an actual BoundingBox object
  BoundingBox boundingBox = BoundingBox.fromPoints(
    Point.fromLngLat(bbox[0], bbox[1]), Point.fromLngLat(bbox[2], bbox[3]));

  // Use the BoundingBox object in the TurfMeasurement.bboxPolygon() method.
  Feature featureRepresentingBoundingBox = TurfMeasurement.bboxPolygon(boundingBox);

  Polygon polygonRepresentingBoundingBox = (Polygon) featureRepresentingBoundingBox.geometry();

  assertNotNull(polygonRepresentingBoundingBox);
  assertEquals(0, polygonRepresentingBoundingBox.inner().size());
  assertEquals(5, polygonRepresentingBoundingBox.coordinates().get(0).size());
  assertEquals(Point.fromLngLat(102.0, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(0));
  assertEquals(Point.fromLngLat(130, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(1));
  assertEquals(Point.fromLngLat(130.0, 4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(2));
  assertEquals(Point.fromLngLat(102.0, 4.0), polygonRepresentingBoundingBox.coordinates().get(0).get(3));
  assertEquals(Point.fromLngLat(102.0, -10.0), polygonRepresentingBoundingBox.coordinates().get(0).get(4));
}
 
Example 6
Source File: TurfMiscTest.java    From mapbox-java with MIT License 6 votes vote down vote up
@Test
 public void testLineSliceAlongOvershootLine1() throws IOException, TurfException {
  Feature line1 = Feature.fromJson(loadJsonFixture(LINE_SLICE_ALONG_LINE_ONE));
  LineString lineStringLine1 = (LineString) line1.geometry();

  double start = 500;
  double stop = 1500;

  Point start_point = TurfMeasurement.along(lineStringLine1, start, TurfConstants.UNIT_MILES);
  Point end_point = TurfMeasurement.along(lineStringLine1, stop, TurfConstants.UNIT_MILES);
  LineString sliced = TurfMisc.lineSliceAlong(line1, start, stop, TurfConstants.UNIT_MILES);

  assertEquals(sliced.coordinates().get(0).coordinates(),
    start_point.coordinates());
  assertEquals(sliced.coordinates().get(sliced.coordinates().size() - 1).coordinates(),
    end_point.coordinates());
}
 
Example 7
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfPointOnLinePointAlongLine() throws TurfException {
  List<Point> line = new ArrayList<>();
  line.add(Point.fromLngLat(-122.45717525482178, 37.7200330638563));
  line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215));

  Point pt = TurfMeasurement.along(
    LineString.fromLngLats(line), 0.019, TurfConstants.UNIT_MILES);
  Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line);
  Point snapped = (Point) snappedFeature.geometry();
  double shift = TurfMeasurement.distance(pt, snapped, TurfConstants.UNIT_MILES);

  // pt did not shift far
  assertTrue(shift < 0.00001);
}
 
Example 8
Source File: TurfMeasurementTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void centerFeatureWithId() {
  final String testIdString = "testId";
  Feature inputFeature = Feature.fromJson(loadJsonFixture(TURF_AREA_POLYGON_GEOJSON));
  Feature returnedCenterFeature = TurfMeasurement.center(inputFeature, null, testIdString);
  Point returnedPoint = (Point) returnedCenterFeature.geometry();
  if (returnedPoint != null) {
    assertEquals(133.5, returnedPoint.longitude(), 0);
    assertEquals(-27.0, returnedPoint.latitude(), 0);
    if (returnedCenterFeature.id() != null) {
      assertEquals(returnedCenterFeature.id(), testIdString);
    }
  }
}
 
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: TurfJoinsTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testInputPositions() throws IOException, TurfException {
  Point ptInPoly = Point.fromLngLat(-86.72229766845702, 36.20258997094334);
  Point ptOutsidePoly = Point.fromLngLat(-86.75079345703125, 36.18527313913089);
  Feature polyHole = Feature.fromJson(loadJsonFixture(POLY_WITH_HOLE_FIXTURE));

  Polygon polygon = (Polygon) polyHole.geometry();

  assertTrue(TurfJoins.inside(ptInPoly, polygon));
  assertFalse(TurfJoins.inside(ptOutsidePoly, polygon));
}
 
Example 11
Source File: TurfAssertions.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Enforce expectations about types of {@link Feature} inputs for Turf. Internally this uses
 * {@link Feature#type()} to judge geometry types.
 *
 * @param feature with an expected geometry type
 * @param type    type expected GeoJson type
 * @param name    name of calling function
 * @see <a href="http://turfjs.org/docs/#featureof">Turf featureOf documentation</a>
 * @since 1.2.0
 */
public static void featureOf(Feature feature, String type, String name) {
  if (name == null || name.length() == 0) {
    throw new TurfException(".featureOf() requires a name");
  }
  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 12
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfPointOnLinePointsOnTopOfLine() throws TurfException {
  List<Point> line = new ArrayList<>();
  line.add(Point.fromLngLat(-0.10919809341430663, 51.52204224896724));
  line.add(Point.fromLngLat(-0.10923027992248535, 51.521942114455435));
  line.add(Point.fromLngLat(-0.10916590690612793, 51.52186200668747));
  line.add(Point.fromLngLat(-0.10904788970947266, 51.52177522311313));
  line.add(Point.fromLngLat(-0.10886549949645996, 51.521601655468345));
  line.add(Point.fromLngLat(-0.10874748229980469, 51.52138135712038));
  line.add(Point.fromLngLat(-0.10855436325073242, 51.5206870765674));
  line.add(Point.fromLngLat(-0.10843634605407713, 51.52027984939518));
  line.add(Point.fromLngLat(-0.10839343070983887, 51.519952729849024));
  line.add(Point.fromLngLat(-0.10817885398864746, 51.51957887606202));
  line.add(Point.fromLngLat(-0.10814666748046874, 51.51928513164789));
  line.add(Point.fromLngLat(-0.10789990425109863, 51.518624199789016));
  line.add(Point.fromLngLat(-0.10759949684143065, 51.51778299991493));

  double dist = TurfMeasurement.length(LineString.fromLngLats(line), TurfConstants.UNIT_MILES);
  double increment = dist / 10;

  for (int i = 0; i < 10; i++) {
    Point pt = TurfMeasurement.along(
      LineString.fromLngLats(line), increment * i, TurfConstants.UNIT_MILES);
    Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line);
    Point snapped = (Point) snappedFeature.geometry();

    double shift = TurfMeasurement.distance(pt, snapped, TurfConstants.UNIT_MILES);

    // pt did not shift far
    assertTrue(shift < 0.000001);
  }
}
 
Example 13
Source File: TurfMiscTest.java    From mapbox-java with MIT License 5 votes vote down vote up
@Test
public void testTurfPointOnLineFirstPoint() throws TurfException {
  List<Point> line = new ArrayList<>();
  line.add(Point.fromLngLat(-122.45717525482178, 37.72003306385638));
  line.add(Point.fromLngLat(-122.45717525482178, 37.718242366859215));

  Point pt = Point.fromLngLat(-122.45717525482178, 37.72003306385638);

  Feature snappedFeature = TurfMisc.nearestPointOnLine(pt, line);
  Point snapped = (Point) snappedFeature.geometry();
  // pt on start does not move
  assertEquals(pt, snapped);
}
 
Example 14
Source File: SnapToRoute.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * Logic used to snap the users location coordinates to the closest position along the current
 * step.
 *
 * @param location        the raw location
 * @param stepCoordinates the list of step geometry coordinates
 * @return the altered user location
 * @since 0.4.0
 */
private static Location snapLocationLatLng(Location location, List<Point> stepCoordinates) {
  Location snappedLocation = new Location(location);
  Point locationToPoint = Point.fromLngLat(location.getLongitude(), location.getLatitude());

  // Uses Turf's pointOnLine, which takes a Point and a LineString to calculate the closest
  // Point on the LineString.
  if (stepCoordinates.size() > 1) {
    Feature feature = TurfMisc.nearestPointOnLine(locationToPoint, stepCoordinates);
    Point point = ((Point) feature.geometry());
    snappedLocation.setLongitude(point.longitude());
    snappedLocation.setLatitude(point.latitude());
  }
  return snappedLocation;
}
 
Example 15
Source File: NavigationHelper.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
/**
 * Takes in a raw location, converts it to a point, and snaps it to the closest point along the
 * route. This is isolated as separate logic from the snap logic provided because we will always
 * need to snap to the route in order to get the most accurate information.
 */
static Point userSnappedToRoutePosition(Location location, List<Point> coordinates) {
  if (coordinates.size() < 2) {
    return Point.fromLngLat(location.getLongitude(), location.getLatitude());
  }

  Point locationToPoint = Point.fromLngLat(location.getLongitude(), location.getLatitude());

  // Uses Turf's pointOnLine, which takes a Point and a LineString to calculate the closest
  // Point on the LineString.
  Feature feature = TurfMisc.nearestPointOnLine(locationToPoint, coordinates);
  return ((Point) feature.geometry());
}
 
Example 16
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 17
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 18
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 19
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 20
Source File: TurfMisc.java    From mapbox-java with MIT License 3 votes vote down vote up
/**
 * Takes a {@link LineString}, a specified distance along the line to a start {@link Point},
 * and a specified distance along the line to a stop point
 * and returns a subsection of the line in-between those points.
 *
 * This can be useful for extracting only the part of a route between two distances.
 *
 * @param line input line
 * @param startDist distance along the line to starting point
 * @param stopDist distance along the line to ending point
 * @param units one of the units found inside {@link TurfConstants.TurfUnitCriteria}
 *              can be degrees, radians, miles, or kilometers
 * @return sliced line
 * @throws TurfException signals that a Turf exception of some sort has occurred.
 * @see <a href="http://turfjs.org/docs/#lineslicealong">Turf Line slice documentation</a>
 * @since 3.1.0
 */
@NonNull
public static LineString lineSliceAlong(@NonNull Feature line,
                                        @FloatRange(from = 0) double startDist,
                                        @FloatRange(from = 0) double stopDist,
                                        @NonNull @TurfConstants.TurfUnitCriteria String units) {
  if (line.geometry() == null) {
    throw new NullPointerException("Feature.geometry() == null");
  }
  if (!line.geometry().type().equals("LineString")) {
    throw new TurfException("input must be a LineString Feature or Geometry");
  }

  return lineSliceAlong((LineString)line.geometry(), startDist, stopDist, units);
}