-
Notifications
You must be signed in to change notification settings - Fork 319
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Dynamic reroute tolerance method (#428)
* adds validation utils class * Added initial logic that adjust the tolerance value when the users close to an intersection * moved turf nearest to MAS and fixed classes * nit extra line
- Loading branch information
Cameron Mace
authored
Oct 27, 2017
1 parent
0f646c9
commit 6af3488
Showing
11 changed files
with
362 additions
and
4,379 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...igation/src/main/java/com/mapbox/services/android/navigation/v5/utils/ToleranceUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.mapbox.services.android.navigation.v5.utils; | ||
|
||
import com.mapbox.directions.v5.models.StepIntersection; | ||
import com.mapbox.geojson.Point; | ||
import com.mapbox.services.android.navigation.v5.navigation.NavigationConstants; | ||
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; | ||
import com.mapbox.turf.TurfClassification; | ||
import com.mapbox.turf.TurfConstants; | ||
import com.mapbox.turf.TurfMeasurement; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public final class ToleranceUtils { | ||
|
||
private ToleranceUtils() { | ||
// Utils class therefore, shouldn't be initialized. | ||
} | ||
|
||
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.nearest(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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
98 changes: 98 additions & 0 deletions
98
...ion/src/test/java/com/mapbox/services/android/navigation/v5/utils/ToleranceUtilsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package com.mapbox.services.android.navigation.v5.utils; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.mapbox.directions.v5.DirectionsAdapterFactory; | ||
import com.mapbox.directions.v5.models.DirectionsResponse; | ||
import com.mapbox.geojson.LineString; | ||
import com.mapbox.geojson.Point; | ||
import com.mapbox.geojson.utils.PolylineUtils; | ||
import com.mapbox.services.android.navigation.v5.BaseTest; | ||
import com.mapbox.services.android.navigation.v5.routeprogress.RouteProgress; | ||
import com.mapbox.services.constants.Constants; | ||
import com.mapbox.turf.TurfConstants; | ||
import com.mapbox.turf.TurfMeasurement; | ||
|
||
import org.junit.Before; | ||
import org.junit.Ignore; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static com.mapbox.services.constants.Constants.PRECISION_6; | ||
import static junit.framework.Assert.assertEquals; | ||
|
||
public class ToleranceUtilsTest extends BaseTest { | ||
|
||
private static final String DIRECTIONS_FIXTURE = "single_intersection.json"; | ||
|
||
private DirectionsResponse response; | ||
|
||
@Before | ||
public void setUp() throws Exception { | ||
Gson gson = new GsonBuilder() | ||
.registerTypeAdapterFactory(DirectionsAdapterFactory.create()).create(); | ||
String json = loadJsonFixture(DIRECTIONS_FIXTURE); | ||
response = gson.fromJson(json, DirectionsResponse.class); | ||
} | ||
|
||
@Test | ||
public void dynamicRerouteDistanceTolerance_userFarAwayFromIntersection() throws Exception { | ||
RouteProgress routeProgress = RouteProgress.builder() | ||
.directionsRoute(response.routes().get(0)) | ||
.legDistanceRemaining(0) | ||
.distanceRemaining(0) | ||
.stepIndex(0) | ||
.legIndex(0) | ||
.build(); | ||
|
||
// Get a point on the route step which isn't close to an intersection. | ||
List<Point> stepPoints = PolylineUtils.decode(response.routes().get(0).geometry(), PRECISION_6); | ||
Point midPoint = TurfMeasurement.midpoint(stepPoints.get(0), stepPoints.get(1)); | ||
|
||
double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(midPoint, routeProgress); | ||
|
||
assertEquals(50.0, tolerance, DELTA); | ||
} | ||
|
||
|
||
@Test | ||
public void dynamicRerouteDistanceTolerance_userCloseToIntersection() throws Exception { | ||
RouteProgress routeProgress = RouteProgress.builder() | ||
.directionsRoute(response.routes().get(0)) | ||
.legDistanceRemaining(0) | ||
.distanceRemaining(0) | ||
.stepIndex(0) | ||
.legIndex(0) | ||
.build(); | ||
|
||
double distanceToIntersection = response.routes().get(0).distance() - 39; | ||
LineString lineString = LineString.fromPolyline(response.routes().get(0).geometry(), Constants.PRECISION_6); | ||
Point closePoint | ||
= TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS); | ||
|
||
double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress); | ||
|
||
assertEquals(25.0, tolerance, DELTA); | ||
} | ||
|
||
@Test | ||
public void dynamicRerouteDistanceTolerance_userJustPastTheIntersection() throws Exception { | ||
RouteProgress routeProgress = RouteProgress.builder() | ||
.directionsRoute(response.routes().get(0)) | ||
.legDistanceRemaining(0) | ||
.distanceRemaining(0) | ||
.stepIndex(0) | ||
.legIndex(0) | ||
.build(); | ||
|
||
double distanceToIntersection = response.routes().get(0).distance(); | ||
LineString lineString = LineString.fromPolyline(response.routes().get(0).geometry(), Constants.PRECISION_6); | ||
Point closePoint | ||
= TurfMeasurement.along(lineString, distanceToIntersection, TurfConstants.UNIT_METERS); | ||
|
||
double tolerance = ToleranceUtils.dynamicRerouteDistanceTolerance(closePoint, routeProgress); | ||
|
||
assertEquals(25.0, tolerance, DELTA); | ||
} | ||
} |
Oops, something went wrong.