Java Code Examples for com.mapbox.geojson.Point#equals()

The following examples show how to use com.mapbox.geojson.Point#equals() . 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: NavigationHelper.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * 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
Source File: NavigationHelper.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
/**
 * Creates a list of pairs {@link StepIntersection} and double distance in meters along a step.
 * <p>
 * Each pair represents an intersection on the given step and its distance along the step geometry.
 * <p>
 * The first intersection is the same point as the first point of the list of step points, so will
 * always be zero meters.
 *
 * @param stepPoints    representing the step geometry
 * @param intersections along the step to be measured
 * @return list of measured intersection pairs
 * @since 0.13.0
 */
@NonNull
public static List<Pair<StepIntersection, Double>> createDistancesToIntersections(List<Point> stepPoints,
                                                                           List<StepIntersection> intersections) {
  boolean lessThanTwoStepPoints = stepPoints.size() < TWO_POINTS;
  boolean noIntersections = intersections.isEmpty();
  if (lessThanTwoStepPoints || noIntersections) {
    return Collections.emptyList();
  }

  LineString stepLineString = LineString.fromLngLats(stepPoints);
  Point firstStepPoint = stepPoints.get(FIRST_POINT);
  List<Pair<StepIntersection, Double>> distancesToIntersections = new ArrayList<>();

  for (StepIntersection intersection : intersections) {
    Point intersectionPoint = intersection.location();
    if (firstStepPoint.equals(intersectionPoint)) {
      distancesToIntersections.add(new Pair<>(intersection, ZERO_METERS));
    } else {
      LineString beginningLineString = TurfMisc.lineSlice(firstStepPoint, intersectionPoint, stepLineString);
      double distanceToIntersectionInMeters = TurfMeasurement.length(beginningLineString, TurfConstants.UNIT_METERS);
      distancesToIntersections.add(new Pair<>(intersection, distanceToIntersectionInMeters));
    }
  }
  return distancesToIntersections;
}
 
Example 3
Source File: ToleranceUtils.java    From graphhopper-navigation-android with MIT License 6 votes vote down vote up
public static double dynamicRerouteDistanceTolerance(Point snappedPoint,
                                                     RouteProgress routeProgress) {
  List<StepIntersection> intersections
    = routeProgress.currentLegProgress().currentStepProgress().intersections();
  List<Point> intersectionsPoints = new ArrayList<>();
  for (StepIntersection intersection : intersections) {
    intersectionsPoints.add(intersection.location());
  }

  Point closestIntersection = TurfClassification.nearestPoint(snappedPoint, intersectionsPoints);

  if (closestIntersection.equals(snappedPoint)) {
    return NavigationConstants.MINIMUM_DISTANCE_BEFORE_REROUTING;
  }

  double distanceToNextIntersection = TurfMeasurement.distance(snappedPoint, closestIntersection,
    TurfConstants.UNIT_METERS);

  if (distanceToNextIntersection <= NavigationConstants.MANEUVER_ZONE_RADIUS) {
    return NavigationConstants.MINIMUM_DISTANCE_BEFORE_REROUTING / 2;
  }
  return NavigationConstants.MINIMUM_DISTANCE_BEFORE_REROUTING;
}
 
Example 4
Source File: PolylineUtils.java    From mapbox-java with MIT License 6 votes vote down vote up
/**
 * Basic distance-based simplification.
 *
 * @param points a list of points to be simplified
 * @param sqTolerance square of amount of simplification
 * @return a list of simplified points
 */
private static List<Point> simplifyRadialDist(List<Point> points, double sqTolerance) {
  Point prevPoint = points.get(0);
  ArrayList<Point> newPoints = new ArrayList<>();
  newPoints.add(prevPoint);
  Point point = null;

  for (int i = 1, len = points.size(); i < len; i++) {
    point = points.get(i);

    if (getSqDist(point, prevPoint) > sqTolerance) {
      newPoints.add(point);
      prevPoint = point;
    }
  }

  if (!prevPoint.equals(point)) {
    newPoints.add(point);
  }
  return newPoints;
}
 
Example 5
Source File: RouteStepProgressTest.java    From graphhopper-navigation-android with MIT License 5 votes vote down vote up
@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 6
Source File: TurfMisc.java    From mapbox-java with MIT License 5 votes vote down vote up
/**
 * Takes a line, a start {@link Point}, and a stop point and returns the line in between those
 * points.
 *
 * @param startPt used for calculating the lineSlice
 * @param stopPt  used for calculating the lineSlice
 * @param line    geometry that should be sliced
 * @return a sliced {@link LineString}
 * @see <a href="http://turfjs.org/docs/#lineslice">Turf Line slice documentation</a>
 * @since 1.2.0
 */
@NonNull
public static LineString lineSlice(@NonNull Point startPt, @NonNull Point stopPt,
                                   @NonNull LineString line) {

  List<Point> coords = line.coordinates();

  if (coords.size() < 2) {
    throw new TurfException("Turf lineSlice requires a LineString made up of at least 2 "
      + "coordinates.");
  } else if (startPt.equals(stopPt)) {
    throw new TurfException("Start and stop points in Turf lineSlice cannot equal each other.");
  }

  Feature startVertex = nearestPointOnLine(startPt, coords);
  Feature stopVertex = nearestPointOnLine(stopPt, coords);
  List<Feature> ends = new ArrayList<>();
  if ((int) startVertex.getNumberProperty(INDEX_KEY)
    <= (int) stopVertex.getNumberProperty(INDEX_KEY)) {
    ends.add(startVertex);
    ends.add(stopVertex);
  } else {
    ends.add(stopVertex);
    ends.add(startVertex);
  }
  List<Point> points = new ArrayList<>();
  points.add((Point) ends.get(0).geometry());
  for (int i = (int) ends.get(0).getNumberProperty(INDEX_KEY) + 1;
       i < (int) ends.get(1).getNumberProperty(INDEX_KEY) + 1; i++) {
    points.add(coords.get(i));
  }
  points.add((Point) ends.get(1).geometry());
  return LineString.fromLngLats(points);
}
 
Example 7
Source File: OffRouteDetector.java    From graphhopper-navigation-android with MIT License 4 votes vote down vote up
/**
 * Checks to see if the current point is moving away from the maneuver.
 * <p>
 * If the current point is farther away from the maneuver than the last point in the
 * stack, add it to the stack.
 * <p>
 * If the stack if >= 3 distances, return true to fire an off-route event as it
 * can be considered that the user is no longer going in the right direction.
 *
 * @param routeProgress             for the upcoming step maneuver
 * @param distancesAwayFromManeuver current stack of distances away
 * @param stepPoints                current step points being traveled along
 * @param currentPoint              to determine if moving away or not
 * @return true if moving away from maneuver, false if not
 */
private static boolean movingAwayFromManeuver(RouteProgress routeProgress,
                                              RingBuffer<Integer> distancesAwayFromManeuver,
                                              List<Point> stepPoints,
                                              Point currentPoint) {
  boolean invalidUpcomingStep = routeProgress.currentLegProgress().upComingStep() == null;
  boolean invalidStepPointSize = stepPoints.size() < TWO_POINTS;
  if (invalidUpcomingStep || invalidStepPointSize) {
    return false;
  }

  LineString stepLineString = LineString.fromLngLats(stepPoints);
  Point maneuverPoint = stepPoints.get(stepPoints.size() - 1);
  Point userPointOnStep = (Point) TurfMisc.nearestPointOnLine(currentPoint, stepPoints).geometry();

  if (userPointOnStep == null || maneuverPoint.equals(userPointOnStep)) {
    return false;
  }

  LineString remainingStepLineString = TurfMisc.lineSlice(userPointOnStep, maneuverPoint, stepLineString);
  double userDistanceToManeuver = TurfMeasurement.length(remainingStepLineString, TurfConstants.UNIT_METERS);

  boolean hasDistances = !distancesAwayFromManeuver.isEmpty();
  boolean validOffRouteDistanceTraveled = hasDistances && distancesAwayFromManeuver.peekLast()
    - distancesAwayFromManeuver.peekFirst() < MINIMUM_BACKUP_DISTANCE_FOR_OFF_ROUTE;
  boolean exceedsManeuverDistancesThreshold = validOffRouteDistanceTraveled
    && distancesAwayFromManeuver.size() >= 3;

  if (exceedsManeuverDistancesThreshold) {
    // User's moving away from maneuver position, thus offRoute.
    return true;
  }
  if (distancesAwayFromManeuver.isEmpty()) {
    distancesAwayFromManeuver.push((int) userDistanceToManeuver);
  } else if (userDistanceToManeuver > distancesAwayFromManeuver.peek()) {
    distancesAwayFromManeuver.push((int) userDistanceToManeuver);
  } else {
    // If we get a descending distance, reset the counter
    distancesAwayFromManeuver.clear();
  }
  return false;
}