Search in sources :

Example 6 with LineString

use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.

the class RouteStepProgressTest method distanceTraveled_equalsCorrectValueAtIntervals.

@Test
public void distanceTraveled_equalsCorrectValueAtIntervals() {
    LineString lineString = LineString.fromPolyline(firstStep.geometry(), Constants.PRECISION_6);
    // meters
    double stepSegments = 5;
    // Chop the line in small pieces
    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 distance = TurfMeasurement.length(slicedLine, TurfConstants.UNIT_METERS);
        distance = firstStep.distance() - distance;
        if (distance < 0) {
            distance = 0;
        }
        RouteProgress routeProgress = RouteProgress.builder().stepDistanceRemaining(firstLeg.steps().get(0).distance() - distance).legDistanceRemaining(firstLeg.distance()).distanceRemaining(route.distance()).directionsRoute(route).stepIndex(0).legIndex(0).build();
        RouteStepProgress routeStepProgress = routeProgress.currentLegProgress().currentStepProgress();
        assertEquals(distance, routeStepProgress.distanceTraveled(), BaseTest.DELTA);
    }
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Test(org.junit.Test) BaseTest(com.mapbox.services.android.navigation.v5.BaseTest)

Example 7 with LineString

use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.

the class NavigationHelper method stepDistanceRemaining.

/**
 * 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);
    // 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);
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) LegStep(com.mapbox.api.directions.v5.models.LegStep)

Example 8 with LineString

use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.

the class SnapToRoute method createUpcomingLineString.

@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;
}
Also used : LineString(com.mapbox.geojson.LineString) LineString(com.mapbox.geojson.LineString) Nullable(android.support.annotation.Nullable)

Example 9 with LineString

use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.

the class SnapToRoute method snapLocationBearing.

/**
 * Creates a snapped bearing for the snapped {@link Location}.
 * <p>
 * This is done by measuring 1 meter ahead of the current step distance traveled and
 * creating a {@link Point} with this distance using {@link TurfMeasurement#along(LineString, double, String)}.
 * <p>
 * If the step distance remaining is zero, the distance ahead is 1 meter into the upcoming step.
 * This way, an accurate bearing is upheld transitioning between steps.
 *
 * @param routeProgress for all current progress values
 * @return float bearing snapped to route
 */
private static float snapLocationBearing(RouteProgress routeProgress) {
    RouteLegProgress legProgress = routeProgress.currentLegProgress();
    RouteStepProgress stepProgress = legProgress.currentStepProgress();
    double distanceTraveled = stepProgress.distanceTraveled();
    double distanceRemaining = stepProgress.distanceRemaining();
    boolean distanceRemainingZero = distanceRemaining == 0;
    // Either want to measure our current step distance traveled + 1 or 1 meter into the upcoming step
    double distanceAhead = distanceRemainingZero ? 1 : distanceTraveled + 1;
    // Create the step linestring from the geometry
    LineString upcomingLineString = createUpcomingLineString(legProgress, distanceRemainingZero);
    LineString currentLineString = createCurrentLineString(legProgress);
    // Measure 1 meter ahead of the users current location, only if the distance remaining isn't zero
    Point futurePoint = createFuturePoint(distanceAhead, upcomingLineString, currentLineString);
    Point currentPoint = TurfMeasurement.along(currentLineString, distanceTraveled, TurfConstants.UNIT_METERS);
    // Get bearing and convert azimuth to degrees
    double azimuth = TurfMeasurement.bearing(currentPoint, futurePoint);
    return (float) MathUtils.wrap(azimuth, 0, 360);
}
Also used : RouteStepProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteStepProgress) LineString(com.mapbox.geojson.LineString) RouteLegProgress(com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress) Point(com.mapbox.geojson.Point)

Example 10 with LineString

use of com.mapbox.geojson.LineString in project mapbox-navigation-android by mapbox.

the class MeasurementUtils method userTrueDistanceFromStep.

/**
 * Calculates the distance between the users current raw {@link android.location.Location} object
 * to the closest {@link Point} in the {@link LegStep}.
 *
 * @param usersRawLocation {@link Point} the raw location where the user is currently located
 * @param step             {@link LegStep} to calculate the closest point on the step to our
 *                         predicted location
 * @return double in distance meters
 * @since 0.2.0
 */
public static double userTrueDistanceFromStep(Point usersRawLocation, LegStep step) {
    // Check that the leg step contains geometry.
    if (TextUtils.isEmpty(step.geometry())) {
        return 0;
    }
    // Get the lineString from the step geometry.
    LineString lineString = LineString.fromPolyline(step.geometry(), Constants.PRECISION_6);
    // the distance is obviously zero, so return 0 to avoid executing additional unnecessary code.
    if (lineString.coordinates().isEmpty() || usersRawLocation.equals(lineString.coordinates().get(0))) {
        return 0;
    }
    if (lineString.coordinates().size() == 1) {
        return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
    }
    Feature feature = TurfMisc.nearestPointOnLine(usersRawLocation, lineString.coordinates());
    Point snappedPoint = (Point) feature.geometry();
    if (snappedPoint == null) {
        return 0;
    }
    if (Double.isInfinite(snappedPoint.latitude()) || Double.isInfinite(snappedPoint.longitude())) {
        return TurfMeasurement.distance(usersRawLocation, lineString.coordinates().get(0), UNIT_METERS);
    }
    double distance = TurfMeasurement.distance(usersRawLocation, snappedPoint, UNIT_METERS);
    return Double.isNaN(distance) ? 0d : distance;
}
Also used : LineString(com.mapbox.geojson.LineString) Point(com.mapbox.geojson.Point) Feature(com.mapbox.geojson.Feature)

Aggregations

LineString (com.mapbox.geojson.LineString)19 Point (com.mapbox.geojson.Point)15 BaseTest (com.mapbox.services.android.navigation.v5.BaseTest)9 Test (org.junit.Test)9 LegStep (com.mapbox.api.directions.v5.models.LegStep)5 RouteProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress)5 Location (android.location.Location)2 Feature (com.mapbox.geojson.Feature)2 ArrayList (java.util.ArrayList)2 Handler (android.os.Handler)1 NonNull (android.support.annotation.NonNull)1 Nullable (android.support.annotation.Nullable)1 FeatureCollection (com.mapbox.geojson.FeatureCollection)1 Polygon (com.mapbox.geojson.Polygon)1 LatLng (com.mapbox.mapboxsdk.geometry.LatLng)1 LatLngBounds (com.mapbox.mapboxsdk.geometry.LatLngBounds)1 DataModel (com.mapbox.mapboxsdk.plugins.geojson.model.DataModel)1 MarkerData (com.mapbox.mapboxsdk.plugins.geojson.model.MarkerData)1 PolyData (com.mapbox.mapboxsdk.plugins.geojson.model.PolyData)1 RouteLegProgress (com.mapbox.services.android.navigation.v5.routeprogress.RouteLegProgress)1