Java Code Examples for com.mapbox.geojson.LineString#fromPolyline()

The following examples show how to use com.mapbox.geojson.LineString#fromPolyline() . These examples are extracted from open source projects. 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
/**
 * Calculates the distance remaining in the step from the current users snapped position, to the
 * next maneuver position.
 */
static double stepDistanceRemaining(Point snappedPosition, int legIndex, int stepIndex,
                                    DirectionsRoute directionsRoute, List<Point> coordinates) {
  List<LegStep> steps = directionsRoute.legs().get(legIndex).steps();
  Point nextManeuverPosition = nextManeuverPosition(stepIndex, steps, coordinates);

  LineString lineString = LineString.fromPolyline(steps.get(stepIndex).geometry(),
    Constants.PRECISION_6);
  // If the users snapped position equals the next maneuver
  // position or the linestring coordinate size is less than 2,the distance remaining is zero.
  if (snappedPosition.equals(nextManeuverPosition) || lineString.coordinates().size() < 2) {
    return 0;
  }
  LineString slicedLine = TurfMisc.lineSlice(snappedPosition, nextManeuverPosition, lineString);
  return TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
}
 
Example 2
@Test
public void distanceRemaining_equalsStepDistanceAtBeginning() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LineString lineString = LineString.fromPolyline(firstLeg.steps().get(5).geometry(), Constants.PRECISION_6);
  double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);

  double stepDistanceRemaining = firstLeg.steps().get(5).distance();
  double legDistanceRemaining = firstLeg.distance();
  double distanceRemaining = route.distance();
  int stepIndex = 4;
  RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
    legDistanceRemaining, distanceRemaining, stepIndex, 0);
  RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

  assertEquals(stepDistance, routeStepProgress.distanceRemaining(), BaseTest.LARGE_DELTA);
}
 
Example 3
/**
 * 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 4
private List<Point> generateRouteCoordinates(DirectionsRoute route) {
  if (route == null) {
    return Collections.emptyList();
  }
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  return lineString.coordinates();
}
 
Example 5
private List<Point> generateRouteCoordinates(DirectionsRoute route) {
  if (route == null) {
    return Collections.emptyList();
  }
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  return lineString.coordinates();
}
 
Example 6
private List<Point> calculateStepPoints() {
  List<Point> stepPoints = new ArrayList<>();

  LineString line = LineString.fromPolyline(
    route.legs().get(currentLeg).steps().get(currentStep).geometry(), Constants.PRECISION_6);
  stepPoints.addAll(sliceRoute(line));
  increaseIndex();

  return stepPoints;
}
 
Example 7
@Nullable
private static LineString createUpcomingLineString(RouteLegProgress legProgress, boolean distanceRemainingZero) {
  LineString upcomingLineString = null;
  if (distanceRemainingZero && legProgress.upComingStep() != null) {
    String upcomingGeometry = legProgress.upComingStep().geometry();
    upcomingLineString = LineString.fromPolyline(upcomingGeometry, PRECISION_6);
  }
  return upcomingLineString;
}
 
Example 8
@Test
public void isUserOffRoute_AssertFalseTwoUpdatesAwayFromManeuverThenOneTowards() throws Exception {
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  LegStep currentStep = routeProgress.currentLegProgress().currentStep();

  LineString lineString = LineString.fromPolyline(currentStep.geometry(), Constants.PRECISION_6);
  List<Point> coordinates = lineString.coordinates();

  Location firstLocationUpdate = buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637);
  offRouteDetector.isUserOffRoute(firstLocationUpdate, routeProgress, options);

  Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location secondLocationUpdate = buildDefaultLocationUpdate(
    lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteFirstTry = offRouteDetector.isUserOffRoute(secondLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteFirstTry);

  Point secondLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location thirdLocationUpdate = buildDefaultLocationUpdate(
    secondLastPointInCurrentStep.longitude(), secondLastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteSecondTry = offRouteDetector.isUserOffRoute(thirdLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteSecondTry);

  Location fourthLocationUpdate = buildDefaultLocationUpdate(
    lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteThirdTry = offRouteDetector.isUserOffRoute(fourthLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteThirdTry);
}
 
Example 9
@Test
public void dynamicRerouteDistanceTolerance_userCloseToIntersection() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  double distanceToIntersection = route.distance() - 39;
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  Point closePoint = TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS);

  double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress);

  assertEquals(50.0, tolerance, DELTA);
}
 
Example 10
@Test
public void dynamicRerouteDistanceTolerance_userJustPastTheIntersection() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  double distanceToIntersection = route.distance();
  LineString lineString = LineString.fromPolyline(route.geometry(), Constants.PRECISION_6);
  Point closePoint = TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS);

  double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress);

  assertEquals(50.0, tolerance, DELTA);
}
 
Example 11
@Test
public void distanceRemaining_equalsCorrectValueAtIntervals() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LegStep firstStep = route.legs().get(0).steps().get(0);
  LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
  double stepDistance = TurfMeasurement.length(lineString, TurfConstants.UNIT_METERS);
  double stepSegments = 5;

  for (double i = 0; i < stepDistance; i += stepSegments) {
    Point point = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS);

    if (point.equals(route.legs().get(0).steps().get(1).maneuver().location())) {
      return;
    }

    LineString slicedLine = TurfMisc.lineSlice(point,
      route.legs().get(0).steps().get(1).maneuver().location(), lineString);

    double stepDistanceRemaining = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
    double legDistanceRemaining = firstLeg.distance();
    double distanceRemaining = route.distance();
    RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
      legDistanceRemaining, distanceRemaining, 0, 0);
    RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();

    assertEquals(stepDistanceRemaining, routeStepProgress.distanceRemaining(), BaseTest.DELTA);
  }
}
 
Example 12
@Test
public void fractionTraveled_equalsCorrectValueAtIntervals() throws Exception {
  DirectionsRoute route = buildTestDirectionsRoute();
  RouteLeg firstLeg = route.legs().get(0);
  LegStep firstStep = route.legs().get(0).steps().get(0);
  LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
  List<Float> fractionsRemaining = new ArrayList<>();
  List<Float> routeProgressFractionsTraveled = new ArrayList<>();
  double stepSegments = 5;

  for (double i = 0; i < firstStep.distance(); i += stepSegments) {
    Point point = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS);
    LineString slicedLine = TurfMisc.lineSlice(point,
      route.legs().get(0).steps().get(1).maneuver().location(), lineString);
    double stepDistanceRemaining = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
    int stepIndex = 0;
    int legIndex = 0;
    double legDistanceRemaining = firstLeg.distance();
    double distanceRemaining = route.distance();
    RouteProgress routeProgress = buildTestRouteProgress(route, stepDistanceRemaining,
      legDistanceRemaining, distanceRemaining, stepIndex, legIndex);
    RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
    float fractionRemaining = (float) ((firstStep.distance() - stepDistanceRemaining) / firstStep.distance());
    if (fractionRemaining < 0) {
      fractionRemaining = 0;
    }
    fractionsRemaining.add(fractionRemaining);
    routeProgressFractionsTraveled.add(routeStepProgress.fractionTraveled());
  }

  assertTrue(fractionsRemaining.equals(routeProgressFractionsTraveled));
}
 
Example 13
@NonNull
private static LineString createCurrentLineString(RouteLegProgress legProgress) {
  String currentGeometry = legProgress.currentStep().geometry();
  return LineString.fromPolyline(currentGeometry, PRECISION_6);
}
 
Example 14
@Test
public void isUserOffRoute_AssertTrueWhenOnRouteButMovingAwayFromManeuver() throws Exception {
  RouteProgress routeProgress = buildDefaultTestRouteProgress();
  LegStep currentStep = routeProgress.currentLegProgress().currentStep();

  LineString lineString = LineString.fromPolyline(currentStep.geometry(), Constants.PRECISION_6);
  List<Point> coordinates = lineString.coordinates();

  Location firstLocationUpdate = buildDefaultLocationUpdate(-77.0339782574523, 38.89993519985637);
  offRouteDetector.isUserOffRoute(firstLocationUpdate, routeProgress, options);

  Point lastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location secondLocationUpdate = buildDefaultLocationUpdate(
    lastPointInCurrentStep.longitude(), lastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteFirstTry = offRouteDetector.isUserOffRoute(secondLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteFirstTry);

  Point secondLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location thirdLocationUpdate = buildDefaultLocationUpdate(
    secondLastPointInCurrentStep.longitude(), secondLastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteSecondTry = offRouteDetector.isUserOffRoute(thirdLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteSecondTry);

  Point thirdLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location fourthLocationUpdate = buildDefaultLocationUpdate(
    thirdLastPointInCurrentStep.longitude(), thirdLastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteThirdTry = offRouteDetector.isUserOffRoute(fourthLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteThirdTry);

  Point fourthLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location fifthLocationUpdate = buildDefaultLocationUpdate(
    fourthLastPointInCurrentStep.longitude(), fourthLastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteFourthTry = offRouteDetector.isUserOffRoute(fifthLocationUpdate, routeProgress, options);
  assertFalse(isUserOffRouteFourthTry);

  Point fifthLastPointInCurrentStep = coordinates.remove(coordinates.size() - 1);
  Location sixthLocationUpdate = buildDefaultLocationUpdate(
    fifthLastPointInCurrentStep.longitude(), fifthLastPointInCurrentStep.latitude()
  );
  boolean isUserOffRouteFifthTry = offRouteDetector.isUserOffRoute(sixthLocationUpdate, routeProgress, options);
  assertTrue(isUserOffRouteFifthTry);
}
 
Example 15
@NonNull
List<Point> createCoordinatesFromCurrentStep(RouteProgress progress) {
  LegStep currentStep = progress.currentLegProgress().currentStep();
  LineString lineString = LineString.fromPolyline(currentStep.geometry(), PRECISION_6);
  return lineString.coordinates();
}