From 2cff624ef29341962be5f5fb28ad1e8c8bbcd4da Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Tue, 6 Jun 2017 15:31:22 -0400 Subject: [PATCH 01/10] route step progress test finished --- .../v5/models/RouteStepProgressTest.java | 125 +++++++++++++++++- 1 file changed, 123 insertions(+), 2 deletions(-) diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java index bffbbba619a..57f4d20bc16 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java @@ -17,7 +17,9 @@ import junit.framework.Assert; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.mockito.Mockito; import static junit.framework.Assert.assertEquals; @@ -53,7 +55,7 @@ public void sanityTest() { } @Test - public void getStepDistance_equalsZeroOnOneCoordSteps() throws Exception { + public void getStepDistance_equalsZeroOnOneCoordSteps() { Gson gson = new Gson(); String body = readPath(DCMAPBOX_CHIPOLTLE); response = gson.fromJson(body, DirectionsResponse.class); @@ -111,6 +113,125 @@ public void getDistanceRemaining_equalsZeroAtEndOfStep() { Assert.assertEquals(0, routeStepProgress.getDistanceRemaining(), BaseTest.DELTA); } -} + @Test + public void getDistanceTraveled_equalsZeroAtBeginning() { + RouteStepProgress routeStepProgress + = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + Assert.assertEquals(0, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getDistanceTraveled_equalsCorrectValueAtIntervals() { + LineString lineString + = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); + double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + double stepSegments = 5; // meters + + // Chop the line in small pieces + for (double i = 0; i < stepDistance; i += stepSegments) { + Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); + + LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), + Point.fromCoordinates(route.getLegs().get(0).getSteps().get(1).getManeuver().asPosition()), lineString); + + double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + distance = stepDistance - distance; + + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + Assert.assertEquals(distance, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); + } + } + + @Test + public void getDistanceTraveled_equalsStepDistanceAtEndOfStep() { + LineString lineString + = LineString.fromPolyline(firstLeg.getSteps().get(3).getGeometry(), Constants.PRECISION_6); + double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + firstLeg.getSteps().get(4).getManeuver().asPosition()); + + Assert.assertEquals(stepDistance, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getFractionTraveled_equalsZeroAtBeginning() { + RouteStepProgress routeStepProgress + = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + + Assert.assertEquals(0, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); + } + + @Test + public void getFractionTraveled_equalsCorrectValueAtIntervals() { + LineString lineString + = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); + double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + double stepSegments = 5; // meters + + // Chop the line in small pieces + for (double i = 0; i < stepDistance; i += stepSegments) { + Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); + + LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), + Point.fromCoordinates(route.getLegs().get(0).getSteps().get(1).getManeuver().asPosition()), lineString); + + double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + float fractionRemaining = (float) ((stepDistance - distance) / stepDistance); + Assert.assertEquals(fractionRemaining, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); + } + } + + @Test + public void getFractionTraveled_equalsOneAtEndOfStep() { + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + firstLeg.getSteps().get(4).getManeuver().asPosition()); + + Assert.assertEquals(1.0, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); + } + + @Test + public void getDurationRemaining_equalsStepDurationAtBeginning() { + RouteStepProgress routeStepProgress + = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + + Assert.assertEquals(41, routeStepProgress.getDurationRemaining(), BaseTest.DELTA); + } + + @Test + public void getDurationRemaining_equalsCorrectValueAtIntervals() { + LineString lineString + = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); + double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + double stepSegments = 5; // meters + + // Chop the line in small pieces + for (double i = 0; i < stepDistance; i += stepSegments) { + Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); + + LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), + Point.fromCoordinates(route.getLegs().get(0).getSteps().get(1).getManeuver().asPosition()), lineString); + + double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + float fractionRemaining = (float) ((stepDistance - distance) / stepDistance); + Assert.assertEquals((1 - fractionRemaining) * firstStep.getDistance(), routeStepProgress.getDurationRemaining(), + BaseTest.DELTA); + } + } + + @Test + public void getDurationRemaining_equalsZeroAtEndOfStep() { + RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + firstLeg.getSteps().get(4).getManeuver().asPosition()); + Assert.assertEquals(0, routeStepProgress.getDurationRemaining(), BaseTest.DELTA); + } +} \ No newline at end of file From 436ab96ad310aa9191ab70b52111e9e7a0dd0388 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Tue, 6 Jun 2017 17:41:37 -0400 Subject: [PATCH 02/10] leg progress test added --- .../v5/models/RouteLegProgress.java | 60 ++++-- .../v5/models/RouteStepProgress.java | 4 +- .../v5/models/RouteLegProgressTest.java | 200 ++++++++++++++++++ .../v5/models/RouteStepProgressTest.java | 6 +- 4 files changed, 251 insertions(+), 19 deletions(-) create mode 100644 navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index db486305c8e..d266ea0a641 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -7,7 +7,15 @@ import com.mapbox.services.api.directions.v5.models.LegStep; import com.mapbox.services.api.directions.v5.models.RouteLeg; import com.mapbox.services.api.utils.turf.TurfConstants; +import com.mapbox.services.api.utils.turf.TurfMeasurement; +import com.mapbox.services.api.utils.turf.TurfMisc; +import com.mapbox.services.commons.geojson.LineString; +import com.mapbox.services.commons.geojson.Point; import com.mapbox.services.commons.models.Position; +import com.mapbox.services.commons.utils.PolylineUtils; + +import java.util.ArrayList; +import java.util.List; @Experimental public class RouteLegProgress { @@ -32,12 +40,19 @@ public RouteLegProgress(RouteLeg routeLeg, int stepIndex, Position userSnappedPo this.userSnappedPosition = userSnappedPosition; currentStepProgress = new RouteStepProgress(routeLeg, stepIndex, userSnappedPosition); - legDistance = RouteUtils.getDistanceToNextLeg( - routeLeg.getSteps().get(0).getManeuver().asPosition(), - routeLeg, - TurfConstants.UNIT_METERS, - Constants.PRECISION_6 - ); + // Decode the geometry + List coords = new ArrayList<>(); + for (LegStep step : routeLeg.getSteps()) { + coords.addAll(PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6)); + } + if (coords.size() > 1) { + LineString slicedLine = TurfMisc.lineSlice( + Point.fromCoordinates(routeLeg.getSteps().get(0).getManeuver().asPosition()), + Point.fromCoordinates(coords.get(coords.size() - 1)), + LineString.fromCoordinates(coords) + ); + legDistance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + } } /** @@ -81,9 +96,23 @@ public double getDistanceTraveled() { * @since 0.1.0 */ public double getDistanceRemaining() { - return RouteUtils.getDistanceToNextLeg( - userSnappedPosition, routeLeg, TurfConstants.UNIT_METERS, Constants.PRECISION_6 - ); + double distanceRemaining = 0; + + List coords = new ArrayList<>(); + for (LegStep step : routeLeg.getSteps()) { + coords.addAll(PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6)); + } + + + if (coords.size() > 1) { + LineString slicedLine = TurfMisc.lineSlice( + Point.fromCoordinates(userSnappedPosition), + Point.fromCoordinates(coords.get(coords.size() - 1)), + LineString.fromCoordinates(coords) + ); + distanceRemaining = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + } + return distanceRemaining; } /** @@ -92,8 +121,8 @@ public double getDistanceRemaining() { * @return {@code long} value representing the duration remaining till end of step, in unit seconds. * @since 0.1.0 */ - public long getDurationRemaining() { - return (long) ((1 - getFractionTraveled()) * routeLeg.getDuration()); + public double getDurationRemaining() { + return (1 - getFractionTraveled()) * routeLeg.getDuration(); } /** @@ -104,7 +133,12 @@ public long getDurationRemaining() { * @since 0.1.0 */ public float getFractionTraveled() { - return (float) (getDistanceTraveled() / legDistance); + float fractionRemaining = 1; + + if (legDistance > 0) { + fractionRemaining = (float) (getDistanceTraveled() / legDistance); + } + return fractionRemaining; } /** @@ -139,7 +173,7 @@ public LegStep getCurrentStep() { * @since 0.1.0 */ public LegStep getUpComingStep() { - if (routeLeg.getSteps().size() > getStepIndex()) { + if (routeLeg.getSteps().size() - 1 > getStepIndex()) { return routeLeg.getSteps().get(getStepIndex() + 1); } return null; diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java index 3abaf4fd144..833d21b1bc5 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java @@ -105,8 +105,8 @@ public float getFractionTraveled() { * @return {@code long} value representing the duration remaining till end of step, in unit seconds. * @since 0.1.0 */ - public long getDurationRemaining() { - return (long) ((1 - getFractionTraveled()) * step.getDuration()); + public double getDurationRemaining() { + return (1 - getFractionTraveled()) * step.getDuration(); } diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java new file mode 100644 index 00000000000..2f75e2a0fd7 --- /dev/null +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java @@ -0,0 +1,200 @@ +package com.mapbox.services.android.navigation.v5.models; + +import com.google.gson.Gson; +import com.mapbox.services.Constants; +import com.mapbox.services.android.navigation.v5.BaseTest; +import com.mapbox.services.api.directions.v5.models.DirectionsResponse; +import com.mapbox.services.api.directions.v5.models.DirectionsRoute; +import com.mapbox.services.api.directions.v5.models.LegStep; +import com.mapbox.services.api.directions.v5.models.RouteLeg; +import com.mapbox.services.api.utils.turf.TurfConstants; +import com.mapbox.services.api.utils.turf.TurfMeasurement; +import com.mapbox.services.commons.geojson.LineString; +import com.mapbox.services.commons.models.Position; + +import junit.framework.Assert; + +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mockito; + +public class RouteLegProgressTest extends BaseTest { + + // Fixtures + private static final String PRECISION_6 = "directions_v5_precision_6.json"; + + private DirectionsRoute route; + private RouteLeg firstLeg; + + @Before + public void setup() { + Gson gson = new Gson(); + String body = readPath(PRECISION_6); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); + route = response.getRoutes().get(0); + firstLeg = route.getLegs().get(0); + } + + @Test + public void sanityTest() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 0, Mockito.mock(Position.class)); + Assert.assertNotNull("should not be null", routeLegProgress); + } + + @Test + public void getUpComingStep_returnsNextStepInLeg() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + + // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged + Assert.assertTrue(routeLegProgress.getUpComingStep().getGeometry() + .startsWith("so{gfA~}xpgFzOyNnRoOdVqXzLmQbDiGhKqQ|Vie@`X{g@dkAw{B~NcXhPoWlRmXfSeW|U")); + + } + + @Test + public void getUpComingStep_returnsNull() { + int lastStepIndex = firstLeg.getSteps().size() - 1; + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, lastStepIndex, + firstLeg.getSteps().get(lastStepIndex - 2).getManeuver().asPosition()); + + Assert.assertNull(routeLegProgress.getUpComingStep()); + } + + @Test + public void getCurrentStep_returnsCurrentStep() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + + // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged + Assert.assertEquals( + firstLeg.getSteps().get(5).getGeometry(), routeLegProgress.getCurrentStep().getGeometry() + ); + Assert.assertNotSame( + firstLeg.getSteps().get(6).getGeometry(), routeLegProgress.getCurrentStep().getGeometry() + ); + } + + @Test + public void getPreviousStep_returnsPreviousStep() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + + // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged + Assert.assertEquals( + firstLeg.getSteps().get(4).getGeometry(), routeLegProgress.getPreviousStep().getGeometry() + ); + Assert.assertNotSame( + firstLeg.getSteps().get(5).getGeometry(), routeLegProgress.getPreviousStep().getGeometry() + ); + } + + @Test + public void getStepIndex_returnsCurrentStepIndex() { + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 3, + firstLeg.getSteps().get(4).getManeuver().asPosition()); + + Assert.assertEquals(3, routeLegProgress.getStepIndex(), BaseTest.DELTA); + } + + @Test + public void getFractionTraveled_equalsZeroAtBeginning() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + + Assert.assertEquals(0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); + } + + @Test + public void getFractionTraveled_equalsCorrectValueAtIntervals() { + double legDistance = 0; + for (LegStep step : firstLeg.getSteps()) { + LineString lineString + = LineString.fromPolyline(step.getGeometry(), Constants.PRECISION_6); + legDistance += TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + } + + double stepSegments = 5000; // meters + + // Chop the line in small pieces + LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); + for (double i = 0; i < legDistance; i += stepSegments) { + Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); + + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 0, position); + float fractionRemaining = (float) (routeLegProgress.getDistanceTraveled() / legDistance); + Assert.assertEquals(fractionRemaining, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); + } + } + + @Test + public void getFractionTraveled_equalsOneAtEndOfLeg() { + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); + + Assert.assertEquals(1.0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); + } + + @Test + public void getDistanceRemaining_equalsLegDistanceAtBeginning() { + double legDistance = 0; + for (LegStep step : firstLeg.getSteps()) { + LineString lineString + = LineString.fromPolyline(step.getGeometry(), Constants.PRECISION_6); + legDistance += TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + } + + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + + Assert.assertEquals(legDistance, routeLegProgress.getDistanceRemaining(), BaseTest.DELTA); + } + + @Test + public void getDistanceRemaining_equalsZeroAtEndOfLeg() { + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); + + Assert.assertEquals(0, routeLegProgress.getDistanceRemaining(), BaseTest.DELTA); + } + + @Test + public void getDistanceTraveled_equalsZeroAtBeginning() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + Assert.assertEquals(0, routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getDistanceTraveled_equalsLegDistanceAtEndOfLeg() { + double traveledLegDistance = 0; + for (int i = 0; i < 3; i++) { + LineString traveledLineString + = LineString.fromPolyline(firstLeg.getSteps().get(i).getGeometry(), Constants.PRECISION_6); + traveledLegDistance += TurfMeasurement.lineDistance(traveledLineString, TurfConstants.UNIT_METERS); + } + + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 4, + firstLeg.getSteps().get(3).getManeuver().asPosition()); + + Assert.assertEquals(traveledLegDistance, routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getDurationRemaining_equalsLegDurationAtBeginning() { + RouteLegProgress routeLegProgress + = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + + Assert.assertEquals(3535.2, routeLegProgress.getDurationRemaining(), BaseTest.DELTA); + } + + @Test + public void getDurationRemaining_equalsZeroAtEndOfLeg() { + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); + + Assert.assertEquals(0, routeLegProgress.getDurationRemaining(), BaseTest.DELTA); + } +} diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java index 57f4d20bc16..d1c28135d1b 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java @@ -17,9 +17,7 @@ import junit.framework.Assert; import org.junit.Before; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.mockito.Mockito; import static junit.framework.Assert.assertEquals; @@ -200,7 +198,7 @@ public void getDurationRemaining_equalsStepDurationAtBeginning() { RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); - Assert.assertEquals(41, routeStepProgress.getDurationRemaining(), BaseTest.DELTA); + Assert.assertEquals(41.5, routeStepProgress.getDurationRemaining(), BaseTest.DELTA); } @Test @@ -222,7 +220,7 @@ public void getDurationRemaining_equalsCorrectValueAtIntervals() { RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); float fractionRemaining = (float) ((stepDistance - distance) / stepDistance); - Assert.assertEquals((1 - fractionRemaining) * firstStep.getDistance(), routeStepProgress.getDurationRemaining(), + Assert.assertEquals((1 - fractionRemaining) * firstStep.getDuration(), routeStepProgress.getDurationRemaining(), BaseTest.DELTA); } } From 82cf0c747afc18479f9f777fe95849fbe26f6cf9 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Tue, 6 Jun 2017 17:46:43 -0400 Subject: [PATCH 03/10] cleaned up routeProgress --- .../android/navigation/v5/RouteProgress.java | 13 +++++++++---- .../navigation/v5/models/RouteLegProgress.java | 4 +++- .../navigation/v5/models/RouteStepProgress.java | 3 +++ 3 files changed, 15 insertions(+), 5 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java index cb5abe30a06..9b1433d17b4 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java @@ -8,8 +8,10 @@ import com.mapbox.services.api.directions.v5.models.DirectionsRoute; import com.mapbox.services.api.directions.v5.models.RouteLeg; import com.mapbox.services.api.utils.turf.TurfConstants; +import com.mapbox.services.api.utils.turf.TurfMeasurement; import com.mapbox.services.api.utils.turf.TurfMisc; import com.mapbox.services.commons.geojson.Feature; +import com.mapbox.services.commons.geojson.LineString; import com.mapbox.services.commons.geojson.Point; import com.mapbox.services.commons.models.Position; import com.mapbox.services.commons.utils.PolylineUtils; @@ -62,11 +64,14 @@ public class RouteProgress { private void initialize() { // Measure route from beginning to end. This is done since the directions API gives a different distance then the // one we measure using turf. - routeDistance = RouteUtils.getDistanceToEndOfRoute( - route.getLegs().get(0).getSteps().get(0).getManeuver().asPosition(), - route, - TurfConstants.UNIT_METERS + List coords = PolylineUtils.decode(route.getGeometry(), Constants.PRECISION_6); + + LineString slicedLine = TurfMisc.lineSlice( + Point.fromCoordinates(route.getLegs().get(0).getSteps().get(0).getManeuver().asPosition()), + Point.fromCoordinates(coords.get(coords.size() - 1)), + LineString.fromCoordinates(coords) ); + routeDistance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); } /** diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index d266ea0a641..371d131eaa9 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -2,7 +2,6 @@ import com.mapbox.services.Constants; import com.mapbox.services.Experimental; -import com.mapbox.services.android.navigation.v5.RouteUtils; import com.mapbox.services.api.ServicesException; import com.mapbox.services.api.directions.v5.models.LegStep; import com.mapbox.services.api.directions.v5.models.RouteLeg; @@ -39,7 +38,10 @@ public RouteLegProgress(RouteLeg routeLeg, int stepIndex, Position userSnappedPo this.stepIndex = stepIndex; this.userSnappedPosition = userSnappedPosition; currentStepProgress = new RouteStepProgress(routeLeg, stepIndex, userSnappedPosition); + initialize(); + } + private void initialize() { // Decode the geometry List coords = new ArrayList<>(); for (LegStep step : routeLeg.getSteps()) { diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java index 833d21b1bc5..41236a17477 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java @@ -34,7 +34,10 @@ public class RouteStepProgress { RouteStepProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { this.userSnappedPosition = userSnappedPosition; this.step = routeLeg.getSteps().get(stepIndex); + initialize(); + } + private void initialize() { // Decode the geometry List coords = PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6); From 571f31a51fc72b2bd2620b266a5c08176a35997b Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Wed, 7 Jun 2017 10:48:01 -0400 Subject: [PATCH 04/10] made routeProgress more testable and added test --- .../navigation/v5/NavigationEngine.java | 13 +- .../android/navigation/v5/RouteProgress.java | 75 ++++---- .../android/navigation/v5/SnapLocation.java | 44 +++-- .../navigation/v5/RouteProgressTest.java | 161 +++++++++++++++++- .../navigation/v5/SnapLocationTest.java | 4 +- 5 files changed, 236 insertions(+), 61 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java index 6a73efba8d1..e91e1f1bdc2 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java @@ -9,6 +9,7 @@ import com.mapbox.services.android.navigation.v5.listeners.OffRouteListener; import com.mapbox.services.android.navigation.v5.listeners.ProgressChangeListener; import com.mapbox.services.api.directions.v5.models.DirectionsRoute; +import com.mapbox.services.commons.models.Position; import java.util.concurrent.CopyOnWriteArrayList; import java.util.concurrent.TimeUnit; @@ -60,7 +61,9 @@ class NavigationEngine { void onLocationChanged(DirectionsRoute directionsRoute, Location location) { // if the previousRouteProgress is null, the route has just begun and one needs to be created if (previousRouteProgress == null) { - previousRouteProgress = new RouteProgress(directionsRoute, location, 0, 0, NavigationConstants.NONE_ALERT_LEVEL); + Position currentPosition = Position.fromCoordinates(location.getLongitude(), location.getLatitude()); + previousRouteProgress = new RouteProgress(directionsRoute, currentPosition, + 0, 0, NavigationConstants.NONE_ALERT_LEVEL); } if (!TextUtils.equals(directionsRoute.getGeometry(), previousRouteProgress.getRoute().getGeometry())) { @@ -81,8 +84,12 @@ void onLocationChanged(DirectionsRoute directionsRoute, Location location) { stepIndex = alertLevelState.getStepIndex(); legIndex = alertLevelState.getLegIndex(); + SnapLocation snapLocation = new SnapLocation(location, + previousRouteProgress.getCurrentLegProgress().getCurrentStep(), options); + // Create a new RouteProgress object using the latest user location - RouteProgress routeProgress = new RouteProgress(directionsRoute, location, legIndex, stepIndex, alertLevel); + RouteProgress routeProgress = new RouteProgress(directionsRoute, snapLocation.getUsersCurrentSnappedPosition(), + legIndex, stepIndex, alertLevel); // Determine if the user is off route or not UserOffRouteState userOffRouteState = new UserOffRouteState(location, routeProgress, options); @@ -90,8 +97,8 @@ void onLocationChanged(DirectionsRoute directionsRoute, Location location) { // Snap location to the route if they aren't off route and return the location object if (isSnapEnabled && !isUserOffRoute) { - SnapLocation snapLocation = new SnapLocation(location, routeProgress, options); location = snapLocation.getSnappedLocation(); + location.setBearing(snapLocation.snapUserBearing(routeProgress)); } notifyAlertLevelChange(routeProgress); diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java index 9b1433d17b4..50817cadbe8 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java @@ -1,7 +1,5 @@ package com.mapbox.services.android.navigation.v5; -import android.location.Location; - import com.mapbox.services.Constants; import com.mapbox.services.Experimental; import com.mapbox.services.android.navigation.v5.models.RouteLegProgress; @@ -10,7 +8,6 @@ import com.mapbox.services.api.utils.turf.TurfConstants; import com.mapbox.services.api.utils.turf.TurfMeasurement; import com.mapbox.services.api.utils.turf.TurfMisc; -import com.mapbox.services.commons.geojson.Feature; import com.mapbox.services.commons.geojson.LineString; import com.mapbox.services.commons.geojson.Point; import com.mapbox.services.commons.models.Position; @@ -35,29 +32,27 @@ public class RouteProgress { private RouteLegProgress currentLegProgress; private DirectionsRoute route; - private Location location; - private int LegIndex; + private Position userSnappedPosition; + private int legIndex; private int alertUserLevel; private double routeDistance; - private int stepIndex; /** * Constructor for the route routeProgress information. * - * @param route the {@link DirectionsRoute} being used for the navigation session. When a user is - * rerouted this route is updated. - * @param location the users location most recently used when creating this object. - * @param stepIndex an {@code integer} representing the current step index the user is on. - * @param alertUserLevel the most recently calculated alert level. + * @param route the {@link DirectionsRoute} being used for the navigation session. When a user is + * rerouted this route is updated. + * @param userSnappedPosition the users position most recently used when creating this object. + * @param stepIndex an {@code integer} representing the current step index the user is on. + * @param alertUserLevel the most recently calculated alert level. * @since 0.1.0 */ - RouteProgress(DirectionsRoute route, Location location, int legIndex, int stepIndex, int alertUserLevel) { + RouteProgress(DirectionsRoute route, Position userSnappedPosition, int legIndex, int stepIndex, int alertUserLevel) { this.route = route; this.alertUserLevel = alertUserLevel; - this.location = location; - this.LegIndex = legIndex; - this.stepIndex = stepIndex; - currentLegProgress = new RouteLegProgress(getCurrentLeg(), stepIndex, getUsersCurrentSnappedPosition()); + this.userSnappedPosition = userSnappedPosition; + this.legIndex = legIndex; + currentLegProgress = new RouteLegProgress(getCurrentLeg(), stepIndex, userSnappedPosition); initialize(); } @@ -91,7 +86,7 @@ public RouteLegProgress getCurrentLegProgress() { * @since 0.1.0 */ public int getLegIndex() { - return LegIndex; + return legIndex; } /** @@ -119,8 +114,8 @@ public double getDistanceTraveled() { * @return {@code long} value representing the duration remaining till end of route, in unit seconds. * @since 0.1.0 */ - public long getDurationRemaining() { - return (long) ((1 - getFractionTraveled()) * route.getDuration()); + public double getDurationRemaining() { + return (1 - getFractionTraveled()) * route.getDuration(); } /** @@ -131,7 +126,12 @@ public long getDurationRemaining() { * @since 0.1.0 */ public float getFractionTraveled() { - return (float) (getDistanceTraveled() / routeDistance); + float fractionRemaining = 1; + + if (routeDistance > 0) { + fractionRemaining = (float) (getDistanceTraveled() / routeDistance); + } + return fractionRemaining; } /** @@ -141,7 +141,21 @@ public float getFractionTraveled() { * @since 0.1.0 */ public double getDistanceRemaining() { - return RouteUtils.getDistanceToEndOfRoute(getUsersCurrentSnappedPosition(), route, TurfConstants.UNIT_METERS); + double distanceRemaining = 0; + + LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); + Position lastCoordinate + = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); + + if (routeDistance > 1) { + LineString slicedLine = TurfMisc.lineSlice( + Point.fromCoordinates(userSnappedPosition), + Point.fromCoordinates(lastCoordinate), + lineString + ); + distanceRemaining = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + } + return distanceRemaining; } /** @@ -163,23 +177,4 @@ public int getAlertUserLevel() { public DirectionsRoute getRoute() { return route; } - - /** - * Provides the users location snapped to the current route they are navigating on. - * - * @return {@link Position} object with coordinates snapping the user to the route. - * @since 0.1.0 - */ - public Position getUsersCurrentSnappedPosition() { - Point locationToPoint = Point.fromCoordinates(new double[] {location.getLongitude(), location.getLatitude()}); - String stepGeometry = route.getLegs().get(getLegIndex()).getSteps().get(stepIndex).getGeometry(); - - // Decode the geometry - List coords = PolylineUtils.decode(stepGeometry, Constants.PRECISION_6); - - // Uses Turf's pointOnLine, which takes a Point and a LineString to calculate the closest - // Point on the LineString. - Feature feature = TurfMisc.pointOnLine(locationToPoint, coords); - return ((Point) feature.getGeometry()).getCoordinates(); - } } diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java index 5b12132718c..fed7888a3c9 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java @@ -2,41 +2,65 @@ import android.location.Location; +import com.mapbox.services.Constants; import com.mapbox.services.android.telemetry.utils.MathUtils; +import com.mapbox.services.api.directions.v5.models.LegStep; import com.mapbox.services.api.utils.turf.TurfConstants; import com.mapbox.services.api.utils.turf.TurfMeasurement; +import com.mapbox.services.api.utils.turf.TurfMisc; +import com.mapbox.services.commons.geojson.Feature; import com.mapbox.services.commons.geojson.LineString; import com.mapbox.services.commons.geojson.Point; import com.mapbox.services.commons.models.Position; +import com.mapbox.services.commons.utils.PolylineUtils; + +import java.util.List; public class SnapLocation { private MapboxNavigationOptions options; - private RouteProgress routeProgress; + private LegStep currentStep; + private Location location; - SnapLocation(Location location, RouteProgress routeProgress, MapboxNavigationOptions options) { + SnapLocation(Location location, LegStep currentStep, MapboxNavigationOptions options) { this.location = location; - this.routeProgress = routeProgress; + this.currentStep = currentStep; this.options = options; } Location getSnappedLocation() { // Pass in the snapped location with all the other location data remaining intact for their use. - location.setLatitude(routeProgress.getUsersCurrentSnappedPosition().getLatitude()); - location.setLongitude(routeProgress.getUsersCurrentSnappedPosition().getLongitude()); - - location.setBearing(snapUserBearing()); - + location.setLatitude(getUsersCurrentSnappedPosition().getLatitude()); + location.setLongitude(getUsersCurrentSnappedPosition().getLongitude()); return location; } - private float snapUserBearing() { + /** + * Provides the users location snapped to the current route they are navigating on. + * + * @return {@link Position} object with coordinates snapping the user to the route. + * @since 0.1.0 + */ + Position getUsersCurrentSnappedPosition() { + Point locationToPoint = Point.fromCoordinates(new double[] {location.getLongitude(), location.getLatitude()}); + String stepGeometry = currentStep.getGeometry(); + + // Decode the geometry + List coords = PolylineUtils.decode(stepGeometry, Constants.PRECISION_6); + + // Uses Turf's pointOnLine, which takes a Point and a LineString to calculate the closest + // Point on the LineString. + Feature feature = TurfMisc.pointOnLine(locationToPoint, coords); + return ((Point) feature.getGeometry()).getCoordinates(); + } + + float snapUserBearing(RouteProgress routeProgress) { LineString lineString = LineString.fromPolyline(routeProgress.getRoute().getGeometry(), com.mapbox.services.Constants.PRECISION_6); Position newCoordinate; - newCoordinate = routeProgress.getUsersCurrentSnappedPosition(); + newCoordinate = getUsersCurrentSnappedPosition(); double userDistanceBuffer = location.getSpeed() * options.getDeadReckoningTimeInterval(); diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java index 596a9609ea3..83843f3bf3c 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java @@ -1,34 +1,181 @@ package com.mapbox.services.android.navigation.v5; -import android.location.Location; - import com.google.gson.Gson; +import com.mapbox.services.Constants; import com.mapbox.services.api.directions.v5.models.DirectionsResponse; import com.mapbox.services.api.directions.v5.models.DirectionsRoute; +import com.mapbox.services.api.directions.v5.models.RouteLeg; +import com.mapbox.services.api.utils.turf.TurfConstants; +import com.mapbox.services.api.utils.turf.TurfMeasurement; +import com.mapbox.services.commons.geojson.LineString; +import com.mapbox.services.commons.models.Position; + +import junit.framework.Assert; import org.junit.Before; import org.junit.Test; -import org.mockito.Mockito; import static junit.framework.Assert.assertNotNull; public class RouteProgressTest extends BaseTest { - private DirectionsResponse response; + // Fixtures + private static final String PRECISION_6 = "directions_v5_precision_6.json"; + private DirectionsRoute route; + private RouteLeg firstLeg; + private Position userSnappedPosition; @Before public void setup() { Gson gson = new Gson(); - String body = readPath("directions_v5.json"); - response = gson.fromJson(body, DirectionsResponse.class); + String body = readPath(PRECISION_6); + DirectionsResponse response = gson.fromJson(body, DirectionsResponse.class); route = response.getRoutes().get(0); + firstLeg = route.getLegs().get(0); + userSnappedPosition = firstLeg.getSteps().get(4).getManeuver().asPosition(); } @Test public void sanityTest() { - RouteProgress routeProgress = new RouteProgress(route, Mockito.mock(Location.class), 0, 0, 0); + RouteProgress routeProgress = new RouteProgress(route, userSnappedPosition, 0, 0, 0); assertNotNull("should not be null", routeProgress); } + @Test + public void getRoute_returnsDirectionsRoute() { + RouteProgress routeProgress + = new RouteProgress(route, userSnappedPosition, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + + Assert.assertEquals(route, routeProgress.getRoute()); + } + + @Test + public void getAlertLevel_returnsCorrectAlertLevel() { + RouteProgress routeProgress + = new RouteProgress(route, userSnappedPosition, 0, 0, NavigationConstants.LOW_ALERT_LEVEL); + + Assert.assertEquals(NavigationConstants.LOW_ALERT_LEVEL, routeProgress.getAlertUserLevel()); + Assert.assertNotSame(NavigationConstants.HIGH_ALERT_LEVEL, routeProgress.getAlertUserLevel()); + } + + @Test + public void getDistanceRemaining_equalsZeroAtEndOfRoute() { + RouteProgress routeProgress + = new RouteProgress( + route, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition(), + route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.LOW_ALERT_LEVEL); + + Assert.assertEquals(0, routeProgress.getDistanceRemaining(), DELTA); + } + + @Test + public void getDistanceRemaining_equalsRouteDistanceAtBeginning() { + LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); + double routeDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + RouteProgress routeProgress + = new RouteProgress( + route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.LOW_ALERT_LEVEL); + + Assert.assertEquals(routeDistance, routeProgress.getDistanceRemaining(), DELTA); + } + + @Test + public void getFractionTraveled_equalsZeroAtBeginning() { + RouteProgress routeProgress + = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + + Assert.assertEquals(0, routeProgress.getFractionTraveled(), DELTA); + } + + @Test + public void getFractionTraveled_equalsCorrectValueAtIntervals() { + LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); + double routeDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + double stepSegments = 500; // meters + + // Chop the line in small pieces + for (double i = 0; i < routeDistance; i += stepSegments) { + Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); + + RouteProgress routeProgress = new RouteProgress(route, position, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + float fractionRemaining = (float) (routeProgress.getDistanceTraveled() / routeDistance); + Assert.assertEquals(fractionRemaining, routeProgress.getFractionTraveled(), BaseTest.DELTA); + } + } + + @Test + public void getFractionTraveled_equalsOneAtEndOfRoute() { + Position lastCoordinate + = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); + + RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); + + Assert.assertEquals(1.0, routeProgress.getFractionTraveled(), DELTA); + } + + @Test + public void getDurationRemaining_equalsRouteDurationAtBeginning() { + RouteProgress routeProgress + = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + + Assert.assertEquals(3535.2, routeProgress.getDurationRemaining(), BaseTest.DELTA); + } + + @Test + public void getDurationRemaining_equalsZeroAtEndOfRoute() { + Position lastCoordinate + = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); + + RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); + + Assert.assertEquals(0, routeProgress.getDurationRemaining(), BaseTest.DELTA); + } + + @Test + public void getDistanceTraveled_equalsZeroAtBeginning() { + RouteProgress routeProgress + = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + Assert.assertEquals(0, routeProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getDistanceTraveled_equalsRouteDistanceAtEndOfRoute() { + LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); + double traveledRouteDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + + Position lastCoordinate + = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); + + RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); + + Assert.assertEquals(traveledRouteDistance, routeProgress.getDistanceTraveled(), BaseTest.DELTA); + } + + @Test + public void getCurrentLeg_returnsCurrentLeg() { + RouteProgress routeProgress + = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + + Assert.assertEquals(route.getLegs().get(0), routeProgress.getCurrentLeg()); + } + + @Test + public void getLegIndex_returnsCurrentLegIndex() { + RouteProgress routeProgress + = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + + Assert.assertEquals(0, routeProgress.getLegIndex()); + } } diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/SnapLocationTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/SnapLocationTest.java index a37f399958f..3d4ec596f0b 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/SnapLocationTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/SnapLocationTest.java @@ -2,6 +2,8 @@ import android.location.Location; +import com.mapbox.services.api.directions.v5.models.LegStep; + import org.junit.Test; import org.mockito.Mockito; @@ -11,7 +13,7 @@ public class SnapLocationTest extends BaseTest { @Test public void sanityTest() { - SnapLocation snapLocation = new SnapLocation(Mockito.mock(Location.class), Mockito.mock(RouteProgress.class), + SnapLocation snapLocation = new SnapLocation(Mockito.mock(Location.class), Mockito.mock(LegStep.class), new MapboxNavigationOptions()); assertNotNull("should not be null", snapLocation); } From 943c135a4eae33e7f6d51ffa839de9da7f3dba39 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Wed, 7 Jun 2017 10:49:11 -0400 Subject: [PATCH 05/10] added routeProgress annotations --- .../android/navigation/v5/models/RouteLegProgress.java | 4 +++- .../services/android/navigation/v5/RouteProgressTest.java | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index 371d131eaa9..a5ab873a434 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -1,5 +1,7 @@ package com.mapbox.services.android.navigation.v5.models; +import android.support.annotation.NonNull; + import com.mapbox.services.Constants; import com.mapbox.services.Experimental; import com.mapbox.services.api.ServicesException; @@ -33,7 +35,7 @@ public class RouteLegProgress { * @param userSnappedPosition the users snapped location when routeProgress was last updated. * @since 0.1.0 */ - public RouteLegProgress(RouteLeg routeLeg, int stepIndex, Position userSnappedPosition) { + public RouteLegProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { this.routeLeg = routeLeg; this.stepIndex = stepIndex; this.userSnappedPosition = userSnappedPosition; diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java index 83843f3bf3c..1397d89017d 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java @@ -178,4 +178,4 @@ public void getLegIndex_returnsCurrentLegIndex() { Assert.assertEquals(0, routeProgress.getLegIndex()); } -} +} \ No newline at end of file From b25d2b47f7b2a6ad57ec4e7dd2b0c242db0e239a Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Wed, 7 Jun 2017 17:28:28 -0400 Subject: [PATCH 06/10] use values directly from directions API --- .../android/navigation/v5/RouteProgress.java | 42 +++++++--------- .../v5/models/RouteLegProgress.java | 48 +++++++------------ .../v5/models/RouteStepProgress.java | 33 +++++-------- .../android/navigation/v5/BaseTest.java | 3 +- .../navigation/v5/RouteProgressTest.java | 17 ++----- .../v5/models/RouteLegProgressTest.java | 37 +++----------- .../v5/models/RouteStepProgressTest.java | 37 +++++++------- 7 files changed, 78 insertions(+), 139 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java index 50817cadbe8..1273df2e67e 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java @@ -35,7 +35,6 @@ public class RouteProgress { private Position userSnappedPosition; private int legIndex; private int alertUserLevel; - private double routeDistance; /** * Constructor for the route routeProgress information. @@ -53,20 +52,6 @@ public class RouteProgress { this.userSnappedPosition = userSnappedPosition; this.legIndex = legIndex; currentLegProgress = new RouteLegProgress(getCurrentLeg(), stepIndex, userSnappedPosition); - initialize(); - } - - private void initialize() { - // Measure route from beginning to end. This is done since the directions API gives a different distance then the - // one we measure using turf. - List coords = PolylineUtils.decode(route.getGeometry(), Constants.PRECISION_6); - - LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(route.getLegs().get(0).getSteps().get(0).getManeuver().asPosition()), - Point.fromCoordinates(coords.get(coords.size() - 1)), - LineString.fromCoordinates(coords) - ); - routeDistance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); } /** @@ -105,7 +90,11 @@ public RouteLeg getCurrentLeg() { * @since 0.1.0 */ public double getDistanceTraveled() { - return routeDistance - getDistanceRemaining(); + double distanceTraveled = route.getDistance() - getDistanceRemaining(); + if (distanceTraveled < 0) { + distanceTraveled = 0; + } + return distanceTraveled; } /** @@ -128,8 +117,8 @@ public double getDurationRemaining() { public float getFractionTraveled() { float fractionRemaining = 1; - if (routeDistance > 0) { - fractionRemaining = (float) (getDistanceTraveled() / routeDistance); + if (route.getDistance() > 0) { + fractionRemaining = (float) (getDistanceTraveled() / route.getDistance()); } return fractionRemaining; } @@ -143,17 +132,18 @@ public float getFractionTraveled() { public double getDistanceRemaining() { double distanceRemaining = 0; - LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); - Position lastCoordinate - = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); - - if (routeDistance > 1) { + List coords = PolylineUtils.decode(currentLegProgress.getCurrentStep().getGeometry(), + Constants.PRECISION_6); + if (coords.size() > 1) { LineString slicedLine = TurfMisc.lineSlice( Point.fromCoordinates(userSnappedPosition), - Point.fromCoordinates(lastCoordinate), - lineString + Point.fromCoordinates(coords.get(coords.size() - 1)), + LineString.fromCoordinates(coords) ); - distanceRemaining = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + distanceRemaining += TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + } + for (int i = currentLegProgress.getStepIndex() + 1; i < getCurrentLeg().getSteps().size(); i++) { + distanceRemaining += getCurrentLeg().getSteps().get(i).getDistance(); } return distanceRemaining; } diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index a5ab873a434..426fe2be2e5 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -25,7 +25,6 @@ public class RouteLegProgress { private int stepIndex; private Position userSnappedPosition; private RouteStepProgress currentStepProgress; - private double legDistance; /** * Constructor for the route leg routeProgress information. @@ -40,23 +39,6 @@ public RouteLegProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Posi this.stepIndex = stepIndex; this.userSnappedPosition = userSnappedPosition; currentStepProgress = new RouteStepProgress(routeLeg, stepIndex, userSnappedPosition); - initialize(); - } - - private void initialize() { - // Decode the geometry - List coords = new ArrayList<>(); - for (LegStep step : routeLeg.getSteps()) { - coords.addAll(PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6)); - } - if (coords.size() > 1) { - LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(routeLeg.getSteps().get(0).getManeuver().asPosition()), - Point.fromCoordinates(coords.get(coords.size() - 1)), - LineString.fromCoordinates(coords) - ); - legDistance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - } } /** @@ -90,7 +72,11 @@ public int getStepIndex() { * @since 0.1.0 */ public double getDistanceTraveled() { - return legDistance - getDistanceRemaining(); + double distanceTraveled = routeLeg.getDistance() - getDistanceRemaining(); + if (distanceTraveled < 0) { + distanceTraveled = 0; + } + return distanceTraveled; } /** @@ -102,20 +88,19 @@ public double getDistanceTraveled() { public double getDistanceRemaining() { double distanceRemaining = 0; - List coords = new ArrayList<>(); - for (LegStep step : routeLeg.getSteps()) { - coords.addAll(PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6)); - } - - + List coords = PolylineUtils.decode(getCurrentStep().getGeometry(), Constants.PRECISION_6); if (coords.size() > 1) { LineString slicedLine = TurfMisc.lineSlice( Point.fromCoordinates(userSnappedPosition), Point.fromCoordinates(coords.get(coords.size() - 1)), LineString.fromCoordinates(coords) ); - distanceRemaining = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + distanceRemaining += TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); + } + for (int i = stepIndex + 1; i < routeLeg.getSteps().size(); i++) { + distanceRemaining += routeLeg.getSteps().get(i).getDistance(); } + return distanceRemaining; } @@ -137,12 +122,15 @@ public double getDurationRemaining() { * @since 0.1.0 */ public float getFractionTraveled() { - float fractionRemaining = 1; + float fractionTraveled = 1; - if (legDistance > 0) { - fractionRemaining = (float) (getDistanceTraveled() / legDistance); + if (routeLeg.getDistance() > 0) { + fractionTraveled = (float) (getDistanceTraveled() / routeLeg.getDistance()); + if (fractionTraveled < 0) { + fractionTraveled = 0; + } } - return fractionRemaining; + return fractionTraveled; } /** diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java index 41236a17477..e716bd7a062 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java @@ -22,7 +22,6 @@ public class RouteStepProgress { private LegStep step; private Position userSnappedPosition; - private double stepDistance; /** * Constructor for the step progress. @@ -34,21 +33,6 @@ public class RouteStepProgress { RouteStepProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { this.userSnappedPosition = userSnappedPosition; this.step = routeLeg.getSteps().get(stepIndex); - initialize(); - } - - private void initialize() { - // Decode the geometry - List coords = PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6); - - if (coords.size() > 1) { - LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(step.getManeuver().asPosition()), - Point.fromCoordinates(coords.get(coords.size() - 1)), - LineString.fromCoordinates(coords) - ); - stepDistance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - } } /** @@ -59,7 +43,11 @@ private void initialize() { * @since 0.1.0 */ public double getDistanceTraveled() { - return stepDistance - getDistanceRemaining(); + double distanceTraveled = step.getDistance() - getDistanceRemaining(); + if (distanceTraveled < 0) { + distanceTraveled = 0; + } + return distanceTraveled; } /** @@ -94,12 +82,15 @@ public double getDistanceRemaining() { * @since 0.1.0 */ public float getFractionTraveled() { - float fractionRemaining = 1; + float fractionTraveled = 1; - if (stepDistance > 0) { - fractionRemaining = (float) (getDistanceTraveled() / stepDistance); + if (step.getDistance() > 0) { + fractionTraveled = (float) (getDistanceTraveled() / step.getDistance()); + if (fractionTraveled < 0) { + fractionTraveled = 0; + } } - return fractionRemaining; + return fractionTraveled; } /** diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/BaseTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/BaseTest.java index 8decdf088f9..007591881c0 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/BaseTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/BaseTest.java @@ -10,8 +10,9 @@ public class BaseTest { public static final double DELTA = 1E-10; - private static final String BASE_PATH = "/res/"; + public static final double LARGE_DELTA = 0.1; + private static final String BASE_PATH = "/res/"; private static final String LOG_TAG = BaseTest.class.getSimpleName(); protected String readPath(String path) { diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java index 1397d89017d..e4c11424d36 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java @@ -71,15 +71,12 @@ public void getDistanceRemaining_equalsZeroAtEndOfRoute() { @Test public void getDistanceRemaining_equalsRouteDistanceAtBeginning() { - LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); - double routeDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); - RouteProgress routeProgress = new RouteProgress( route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.LOW_ALERT_LEVEL); - Assert.assertEquals(routeDistance, routeProgress.getDistanceRemaining(), DELTA); + Assert.assertEquals(route.getDistance(), routeProgress.getDistanceRemaining(), LARGE_DELTA); } @Test @@ -88,22 +85,21 @@ public void getFractionTraveled_equalsZeroAtBeginning() { = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); - Assert.assertEquals(0, routeProgress.getFractionTraveled(), DELTA); + Assert.assertEquals(0, routeProgress.getFractionTraveled(), BaseTest.LARGE_DELTA); } @Test public void getFractionTraveled_equalsCorrectValueAtIntervals() { LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); - double routeDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); double stepSegments = 500; // meters // Chop the line in small pieces - for (double i = 0; i < routeDistance; i += stepSegments) { + for (double i = 0; i < route.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); RouteProgress routeProgress = new RouteProgress(route, position, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); - float fractionRemaining = (float) (routeProgress.getDistanceTraveled() / routeDistance); + float fractionRemaining = (float) (routeProgress.getDistanceTraveled() / route.getDistance()); Assert.assertEquals(fractionRemaining, routeProgress.getFractionTraveled(), BaseTest.DELTA); } } @@ -149,16 +145,13 @@ public void getDistanceTraveled_equalsZeroAtBeginning() { @Test public void getDistanceTraveled_equalsRouteDistanceAtEndOfRoute() { - LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); - double traveledRouteDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); - Position lastCoordinate = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); - Assert.assertEquals(traveledRouteDistance, routeProgress.getDistanceTraveled(), BaseTest.DELTA); + Assert.assertEquals(route.getDistance(), routeProgress.getDistanceTraveled(), BaseTest.DELTA); } @Test diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java index 2f75e2a0fd7..b8cfd0823e5 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java @@ -5,7 +5,6 @@ import com.mapbox.services.android.navigation.v5.BaseTest; import com.mapbox.services.api.directions.v5.models.DirectionsResponse; import com.mapbox.services.api.directions.v5.models.DirectionsRoute; -import com.mapbox.services.api.directions.v5.models.LegStep; import com.mapbox.services.api.directions.v5.models.RouteLeg; import com.mapbox.services.api.utils.turf.TurfConstants; import com.mapbox.services.api.utils.turf.TurfMeasurement; @@ -50,7 +49,6 @@ public void getUpComingStep_returnsNextStepInLeg() { // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged Assert.assertTrue(routeLegProgress.getUpComingStep().getGeometry() .startsWith("so{gfA~}xpgFzOyNnRoOdVqXzLmQbDiGhKqQ|Vie@`X{g@dkAw{B~NcXhPoWlRmXfSeW|U")); - } @Test @@ -104,27 +102,20 @@ public void getFractionTraveled_equalsZeroAtBeginning() { RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); - Assert.assertEquals(0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); + Assert.assertEquals(0.0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); } @Test public void getFractionTraveled_equalsCorrectValueAtIntervals() { - double legDistance = 0; - for (LegStep step : firstLeg.getSteps()) { - LineString lineString - = LineString.fromPolyline(step.getGeometry(), Constants.PRECISION_6); - legDistance += TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); - } - double stepSegments = 5000; // meters // Chop the line in small pieces LineString lineString = LineString.fromPolyline(route.getGeometry(), Constants.PRECISION_6); - for (double i = 0; i < legDistance; i += stepSegments) { + for (double i = 0; i < firstLeg.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 0, position); - float fractionRemaining = (float) (routeLegProgress.getDistanceTraveled() / legDistance); + float fractionRemaining = (float) (routeLegProgress.getDistanceTraveled() / firstLeg.getDistance()); Assert.assertEquals(fractionRemaining, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); } } @@ -139,17 +130,10 @@ public void getFractionTraveled_equalsOneAtEndOfLeg() { @Test public void getDistanceRemaining_equalsLegDistanceAtBeginning() { - double legDistance = 0; - for (LegStep step : firstLeg.getSteps()) { - LineString lineString - = LineString.fromPolyline(step.getGeometry(), Constants.PRECISION_6); - legDistance += TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); - } - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); - Assert.assertEquals(legDistance, routeLegProgress.getDistanceRemaining(), BaseTest.DELTA); + Assert.assertEquals(firstLeg.getDistance(), routeLegProgress.getDistanceRemaining(), BaseTest.LARGE_DELTA); } @Test @@ -169,17 +153,10 @@ public void getDistanceTraveled_equalsZeroAtBeginning() { @Test public void getDistanceTraveled_equalsLegDistanceAtEndOfLeg() { - double traveledLegDistance = 0; - for (int i = 0; i < 3; i++) { - LineString traveledLineString - = LineString.fromPolyline(firstLeg.getSteps().get(i).getGeometry(), Constants.PRECISION_6); - traveledLegDistance += TurfMeasurement.lineDistance(traveledLineString, TurfConstants.UNIT_METERS); - } - - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 4, - firstLeg.getSteps().get(3).getManeuver().asPosition()); + RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); - Assert.assertEquals(traveledLegDistance, routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); + Assert.assertEquals(firstLeg.getDistance(), routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); } @Test diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java index d1c28135d1b..69168b4d873 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java @@ -121,21 +121,22 @@ public void getDistanceTraveled_equalsZeroAtBeginning() { @Test public void getDistanceTraveled_equalsCorrectValueAtIntervals() { - LineString lineString - = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); - double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); + LineString lineString = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); double stepSegments = 5; // meters // Chop the line in small pieces - for (double i = 0; i < stepDistance; i += stepSegments) { + for (double i = 0; i < firstStep.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), Point.fromCoordinates(route.getLegs().get(0).getSteps().get(1).getManeuver().asPosition()), lineString); double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - distance = stepDistance - distance; + distance = firstStep.getDistance() - distance; + if (distance < 0) { + distance = 0; + } RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); Assert.assertEquals(distance, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); @@ -144,14 +145,11 @@ public void getDistanceTraveled_equalsCorrectValueAtIntervals() { @Test public void getDistanceTraveled_equalsStepDistanceAtEndOfStep() { - LineString lineString - = LineString.fromPolyline(firstLeg.getSteps().get(3).getGeometry(), Constants.PRECISION_6); - double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); - Assert.assertEquals(stepDistance, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); + Assert.assertEquals(firstLeg.getSteps().get(3).getDistance(), + routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); } @Test @@ -166,12 +164,11 @@ public void getFractionTraveled_equalsZeroAtBeginning() { public void getFractionTraveled_equalsCorrectValueAtIntervals() { LineString lineString = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); - double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); double stepSegments = 5; // meters // Chop the line in small pieces - for (double i = 0; i < stepDistance; i += stepSegments) { + for (double i = 0; i < firstStep.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), @@ -180,8 +177,11 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); - float fractionRemaining = (float) ((stepDistance - distance) / stepDistance); - Assert.assertEquals(fractionRemaining, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); + float fractionRemaining = (float) ((firstStep.getDistance() - distance) / firstStep.getDistance()); + if (fractionRemaining < 0) { + fractionRemaining = 0; + } + Assert.assertEquals(fractionRemaining, routeStepProgress.getFractionTraveled(), DELTA); } } @@ -205,12 +205,11 @@ public void getDurationRemaining_equalsStepDurationAtBeginning() { public void getDurationRemaining_equalsCorrectValueAtIntervals() { LineString lineString = LineString.fromPolyline(firstStep.getGeometry(), Constants.PRECISION_6); - double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); double stepSegments = 5; // meters // Chop the line in small pieces - for (double i = 0; i < stepDistance; i += stepSegments) { + for (double i = 0; i < firstStep.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); LineString slicedLine = TurfMisc.lineSlice(Point.fromCoordinates(position), @@ -219,9 +218,9 @@ public void getDurationRemaining_equalsCorrectValueAtIntervals() { double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); - float fractionRemaining = (float) ((stepDistance - distance) / stepDistance); - Assert.assertEquals((1 - fractionRemaining) * firstStep.getDuration(), routeStepProgress.getDurationRemaining(), - BaseTest.DELTA); + double fractionRemaining = (firstStep.getDistance() - distance) / firstStep.getDistance(); + Assert.assertEquals((1.0 - fractionRemaining) * firstStep.getDuration(), + routeStepProgress.getDurationRemaining(), BaseTest.LARGE_DELTA); } } From 0f751404e1770f8a8eb9b7f98c41655f65240f8e Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Wed, 7 Jun 2017 17:49:03 -0400 Subject: [PATCH 07/10] fixed snap bearing using routeUtils --- .../mapbox/services/android/navigation/v5/SnapLocation.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java index fed7888a3c9..3012d05574a 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/SnapLocation.java @@ -65,10 +65,7 @@ float snapUserBearing(RouteProgress routeProgress) { double userDistanceBuffer = location.getSpeed() * options.getDeadReckoningTimeInterval(); if (routeProgress.getDistanceTraveled() + userDistanceBuffer - > RouteUtils.getDistanceToEndOfRoute( - routeProgress.getRoute().getLegs().get(0).getSteps().get(0).getManeuver().asPosition(), - routeProgress.getRoute(), - TurfConstants.UNIT_METERS)) { + > routeProgress.getRoute().getDistance()) { // If the user is near the end of the route, take the remaining distance and divide by two userDistanceBuffer = routeProgress.getDistanceRemaining() / 2; } From 659b8060201621ffc8fc0be907d2e179a0ec4574 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Wed, 7 Jun 2017 18:00:07 -0400 Subject: [PATCH 08/10] removed unused import --- .../services/android/navigation/v5/models/RouteLegProgress.java | 1 - 1 file changed, 1 deletion(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index 426fe2be2e5..f65b76793a1 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -15,7 +15,6 @@ import com.mapbox.services.commons.models.Position; import com.mapbox.services.commons.utils.PolylineUtils; -import java.util.ArrayList; import java.util.List; @Experimental From 0af5c85c1a90a35b590a00b0102b085ecd341af1 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Thu, 8 Jun 2017 13:17:01 -0400 Subject: [PATCH 09/10] autoValue added --- navigation/build.gradle | 3 +- navigation/libandroid-navigation/build.gradle | 5 ++ .../navigation/v5/NavigationEngine.java | 4 +- .../android/navigation/v5/RouteProgress.java | 54 ++++++++++--------- .../v5/models/RouteLegProgress.java | 52 +++++++++--------- .../v5/models/RouteStepProgress.java | 28 +++++----- 6 files changed, 83 insertions(+), 63 deletions(-) diff --git a/navigation/build.gradle b/navigation/build.gradle index d0aa70430cc..0f5b68c4730 100644 --- a/navigation/build.gradle +++ b/navigation/build.gradle @@ -6,6 +6,7 @@ buildscript { } dependencies { classpath 'com.android.tools.build:gradle:2.3.2' + classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files @@ -20,4 +21,4 @@ allprojects { task clean(type: Delete) { delete rootProject.buildDir -} +} \ No newline at end of file diff --git a/navigation/libandroid-navigation/build.gradle b/navigation/libandroid-navigation/build.gradle index f73e329b15d..b1043cc8771 100644 --- a/navigation/libandroid-navigation/build.gradle +++ b/navigation/libandroid-navigation/build.gradle @@ -1,4 +1,5 @@ apply plugin: 'com.android.library' +apply plugin: 'com.neenbedankt.android-apt' android { compileSdkVersion 25 @@ -36,6 +37,10 @@ dependencies { // Logging compile 'com.jakewharton.timber:timber:4.5.1' + // AutoValues + provided 'com.google.auto.value:auto-value:1.4.1' + apt 'com.google.auto.value:auto-value:1.4.1' + // Testing testCompile 'junit:junit:4.12' testCompile 'org.hamcrest:hamcrest-junit:2.0.0.0' diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java index e91e1f1bdc2..43444cc8a2d 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java @@ -62,7 +62,7 @@ void onLocationChanged(DirectionsRoute directionsRoute, Location location) { // if the previousRouteProgress is null, the route has just begun and one needs to be created if (previousRouteProgress == null) { Position currentPosition = Position.fromCoordinates(location.getLongitude(), location.getLatitude()); - previousRouteProgress = new RouteProgress(directionsRoute, currentPosition, + previousRouteProgress = RouteProgress.create(directionsRoute, currentPosition, 0, 0, NavigationConstants.NONE_ALERT_LEVEL); } @@ -88,7 +88,7 @@ void onLocationChanged(DirectionsRoute directionsRoute, Location location) { previousRouteProgress.getCurrentLegProgress().getCurrentStep(), options); // Create a new RouteProgress object using the latest user location - RouteProgress routeProgress = new RouteProgress(directionsRoute, snapLocation.getUsersCurrentSnappedPosition(), + RouteProgress routeProgress = RouteProgress.create(directionsRoute, snapLocation.getUsersCurrentSnappedPosition(), legIndex, stepIndex, alertLevel); // Determine if the user is off route or not diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java index 1273df2e67e..1b850b8074b 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/RouteProgress.java @@ -1,5 +1,6 @@ package com.mapbox.services.android.navigation.v5; +import com.google.auto.value.AutoValue; import com.mapbox.services.Constants; import com.mapbox.services.Experimental; import com.mapbox.services.android.navigation.v5.models.RouteLegProgress; @@ -28,13 +29,18 @@ * @since 0.1.0 */ @Experimental -public class RouteProgress { +@AutoValue +public abstract class RouteProgress { - private RouteLegProgress currentLegProgress; - private DirectionsRoute route; - private Position userSnappedPosition; - private int legIndex; - private int alertUserLevel; + public abstract RouteLegProgress currentLegProgress(); + + public abstract DirectionsRoute route(); + + public abstract Position userSnappedPosition(); + + public abstract int legIndex(); + + public abstract int alertUserLevel(); /** * Constructor for the route routeProgress information. @@ -46,12 +52,12 @@ public class RouteProgress { * @param alertUserLevel the most recently calculated alert level. * @since 0.1.0 */ - RouteProgress(DirectionsRoute route, Position userSnappedPosition, int legIndex, int stepIndex, int alertUserLevel) { - this.route = route; - this.alertUserLevel = alertUserLevel; - this.userSnappedPosition = userSnappedPosition; - this.legIndex = legIndex; - currentLegProgress = new RouteLegProgress(getCurrentLeg(), stepIndex, userSnappedPosition); + public static RouteProgress create( + DirectionsRoute route, Position userSnappedPosition, int legIndex, int stepIndex, int alertUserLevel) { + RouteLegProgress routeLegProgress + = RouteLegProgress.create(route.getLegs().get(legIndex), stepIndex, userSnappedPosition); + return new AutoValue_RouteProgress( + routeLegProgress, route, userSnappedPosition, legIndex, alertUserLevel); } /** @@ -61,7 +67,7 @@ public class RouteProgress { * @since 0.1.0 */ public RouteLegProgress getCurrentLegProgress() { - return currentLegProgress; + return currentLegProgress(); } /** @@ -71,7 +77,7 @@ public RouteLegProgress getCurrentLegProgress() { * @since 0.1.0 */ public int getLegIndex() { - return legIndex; + return legIndex(); } /** @@ -80,7 +86,7 @@ public int getLegIndex() { * @return a {@link RouteLeg} the user is currently on. */ public RouteLeg getCurrentLeg() { - return route.getLegs().get(getLegIndex()); + return route().getLegs().get(getLegIndex()); } /** @@ -90,7 +96,7 @@ public RouteLeg getCurrentLeg() { * @since 0.1.0 */ public double getDistanceTraveled() { - double distanceTraveled = route.getDistance() - getDistanceRemaining(); + double distanceTraveled = route().getDistance() - getDistanceRemaining(); if (distanceTraveled < 0) { distanceTraveled = 0; } @@ -104,7 +110,7 @@ public double getDistanceTraveled() { * @since 0.1.0 */ public double getDurationRemaining() { - return (1 - getFractionTraveled()) * route.getDuration(); + return (1 - getFractionTraveled()) * route().getDuration(); } /** @@ -117,8 +123,8 @@ public double getDurationRemaining() { public float getFractionTraveled() { float fractionRemaining = 1; - if (route.getDistance() > 0) { - fractionRemaining = (float) (getDistanceTraveled() / route.getDistance()); + if (route().getDistance() > 0) { + fractionRemaining = (float) (getDistanceTraveled() / route().getDistance()); } return fractionRemaining; } @@ -132,17 +138,17 @@ public float getFractionTraveled() { public double getDistanceRemaining() { double distanceRemaining = 0; - List coords = PolylineUtils.decode(currentLegProgress.getCurrentStep().getGeometry(), + List coords = PolylineUtils.decode(currentLegProgress().getCurrentStep().getGeometry(), Constants.PRECISION_6); if (coords.size() > 1) { LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(userSnappedPosition), + Point.fromCoordinates(userSnappedPosition()), Point.fromCoordinates(coords.get(coords.size() - 1)), LineString.fromCoordinates(coords) ); distanceRemaining += TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); } - for (int i = currentLegProgress.getStepIndex() + 1; i < getCurrentLeg().getSteps().size(); i++) { + for (int i = currentLegProgress().getStepIndex() + 1; i < getCurrentLeg().getSteps().size(); i++) { distanceRemaining += getCurrentLeg().getSteps().get(i).getDistance(); } return distanceRemaining; @@ -155,7 +161,7 @@ public double getDistanceRemaining() { * @since 0.1.0 */ public int getAlertUserLevel() { - return alertUserLevel; + return alertUserLevel(); } /** @@ -165,6 +171,6 @@ public int getAlertUserLevel() { * @since 0.1.0 */ public DirectionsRoute getRoute() { - return route; + return route(); } } diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java index f65b76793a1..86ab4abae91 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgress.java @@ -2,6 +2,7 @@ import android.support.annotation.NonNull; +import com.google.auto.value.AutoValue; import com.mapbox.services.Constants; import com.mapbox.services.Experimental; import com.mapbox.services.api.ServicesException; @@ -18,12 +19,16 @@ import java.util.List; @Experimental -public class RouteLegProgress { +@AutoValue +public abstract class RouteLegProgress { - private RouteLeg routeLeg; - private int stepIndex; - private Position userSnappedPosition; - private RouteStepProgress currentStepProgress; + public abstract RouteLeg routeLeg(); + + public abstract int stepIndex(); + + public abstract Position userSnappedPosition(); + + public abstract RouteStepProgress currentStepProgress(); /** * Constructor for the route leg routeProgress information. @@ -33,11 +38,10 @@ public class RouteLegProgress { * @param userSnappedPosition the users snapped location when routeProgress was last updated. * @since 0.1.0 */ - public RouteLegProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { - this.routeLeg = routeLeg; - this.stepIndex = stepIndex; - this.userSnappedPosition = userSnappedPosition; - currentStepProgress = new RouteStepProgress(routeLeg, stepIndex, userSnappedPosition); + public static RouteLegProgress create( + @NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { + RouteStepProgress routeStepProgress = RouteStepProgress.create(routeLeg, stepIndex, userSnappedPosition); + return new AutoValue_RouteLegProgress(routeLeg, stepIndex, userSnappedPosition, routeStepProgress); } /** @@ -47,7 +51,7 @@ public RouteLegProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Posi * @since 0.1.0 */ public RouteStepProgress getCurrentStepProgress() { - return currentStepProgress; + return currentStepProgress(); } /** @@ -57,10 +61,10 @@ public RouteStepProgress getCurrentStepProgress() { * @since 0.1.0 */ public int getStepIndex() { - if (stepIndex < 0 || stepIndex > routeLeg.getSteps().size() - 1) { + if (stepIndex() < 0 || stepIndex() > routeLeg().getSteps().size() - 1) { throw new ServicesException("RouteProgress step index is outside its index limit."); } - return stepIndex; + return stepIndex(); } /** @@ -71,7 +75,7 @@ public int getStepIndex() { * @since 0.1.0 */ public double getDistanceTraveled() { - double distanceTraveled = routeLeg.getDistance() - getDistanceRemaining(); + double distanceTraveled = routeLeg().getDistance() - getDistanceRemaining(); if (distanceTraveled < 0) { distanceTraveled = 0; } @@ -90,14 +94,14 @@ public double getDistanceRemaining() { List coords = PolylineUtils.decode(getCurrentStep().getGeometry(), Constants.PRECISION_6); if (coords.size() > 1) { LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(userSnappedPosition), + Point.fromCoordinates(userSnappedPosition()), Point.fromCoordinates(coords.get(coords.size() - 1)), LineString.fromCoordinates(coords) ); distanceRemaining += TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); } - for (int i = stepIndex + 1; i < routeLeg.getSteps().size(); i++) { - distanceRemaining += routeLeg.getSteps().get(i).getDistance(); + for (int i = stepIndex() + 1; i < routeLeg().getSteps().size(); i++) { + distanceRemaining += routeLeg().getSteps().get(i).getDistance(); } return distanceRemaining; @@ -110,7 +114,7 @@ public double getDistanceRemaining() { * @since 0.1.0 */ public double getDurationRemaining() { - return (1 - getFractionTraveled()) * routeLeg.getDuration(); + return (1 - getFractionTraveled()) * routeLeg().getDuration(); } /** @@ -123,8 +127,8 @@ public double getDurationRemaining() { public float getFractionTraveled() { float fractionTraveled = 1; - if (routeLeg.getDistance() > 0) { - fractionTraveled = (float) (getDistanceTraveled() / routeLeg.getDistance()); + if (routeLeg().getDistance() > 0) { + fractionTraveled = (float) (getDistanceTraveled() / routeLeg().getDistance()); if (fractionTraveled < 0) { fractionTraveled = 0; } @@ -143,7 +147,7 @@ public LegStep getPreviousStep() { if (getStepIndex() == 0) { return null; } - return routeLeg.getSteps().get(getStepIndex() - 1); + return routeLeg().getSteps().get(getStepIndex() - 1); } /** @@ -153,7 +157,7 @@ public LegStep getPreviousStep() { * @since 0.1.0 */ public LegStep getCurrentStep() { - return routeLeg.getSteps().get(getStepIndex()); + return routeLeg().getSteps().get(getStepIndex()); } /** @@ -164,8 +168,8 @@ public LegStep getCurrentStep() { * @since 0.1.0 */ public LegStep getUpComingStep() { - if (routeLeg.getSteps().size() - 1 > getStepIndex()) { - return routeLeg.getSteps().get(getStepIndex() + 1); + if (routeLeg().getSteps().size() - 1 > getStepIndex()) { + return routeLeg().getSteps().get(getStepIndex() + 1); } return null; } diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java index e716bd7a062..ce66cd61e2b 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgress.java @@ -3,6 +3,7 @@ import android.support.annotation.NonNull; +import com.google.auto.value.AutoValue; import com.mapbox.services.Constants; import com.mapbox.services.Experimental; import com.mapbox.services.api.directions.v5.models.LegStep; @@ -18,10 +19,12 @@ import java.util.List; @Experimental -public class RouteStepProgress { +@AutoValue +public abstract class RouteStepProgress { - private LegStep step; - private Position userSnappedPosition; + public abstract LegStep step(); + + public abstract Position userSnappedPosition(); /** * Constructor for the step progress. @@ -30,9 +33,10 @@ public class RouteStepProgress { * @param userSnappedPosition the users snapped location when routeProgress was last updated. * @since 0.1.0 */ - RouteStepProgress(@NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { - this.userSnappedPosition = userSnappedPosition; - this.step = routeLeg.getSteps().get(stepIndex); + static RouteStepProgress create( + @NonNull RouteLeg routeLeg, int stepIndex, @NonNull Position userSnappedPosition) { + LegStep step = routeLeg.getSteps().get(stepIndex); + return new AutoValue_RouteStepProgress(step, userSnappedPosition); } /** @@ -43,7 +47,7 @@ public class RouteStepProgress { * @since 0.1.0 */ public double getDistanceTraveled() { - double distanceTraveled = step.getDistance() - getDistanceRemaining(); + double distanceTraveled = step().getDistance() - getDistanceRemaining(); if (distanceTraveled < 0) { distanceTraveled = 0; } @@ -61,11 +65,11 @@ public double getDistanceRemaining() { double distanceRemaining = 0; // Decode the geometry - List coords = PolylineUtils.decode(step.getGeometry(), Constants.PRECISION_6); + List coords = PolylineUtils.decode(step().getGeometry(), Constants.PRECISION_6); if (coords.size() > 1) { LineString slicedLine = TurfMisc.lineSlice( - Point.fromCoordinates(userSnappedPosition), + Point.fromCoordinates(userSnappedPosition()), Point.fromCoordinates(coords.get(coords.size() - 1)), LineString.fromCoordinates(coords) ); @@ -84,8 +88,8 @@ public double getDistanceRemaining() { public float getFractionTraveled() { float fractionTraveled = 1; - if (step.getDistance() > 0) { - fractionTraveled = (float) (getDistanceTraveled() / step.getDistance()); + if (step().getDistance() > 0) { + fractionTraveled = (float) (getDistanceTraveled() / step().getDistance()); if (fractionTraveled < 0) { fractionTraveled = 0; } @@ -100,7 +104,7 @@ public float getFractionTraveled() { * @since 0.1.0 */ public double getDurationRemaining() { - return (1 - getFractionTraveled()) * step.getDuration(); + return (1 - getFractionTraveled()) * step().getDuration(); } From b201b1fcfe965c76c6424d09b19a9b82ffaca830 Mon Sep 17 00:00:00 2001 From: Cameron Mace Date: Thu, 8 Jun 2017 13:36:13 -0400 Subject: [PATCH 10/10] fixed test --- .../navigation/v5/NavigationEngine.java | 2 +- .../navigation/v5/RouteProgressTest.java | 28 ++++++++--------- .../v5/models/RouteLegProgressTest.java | 30 +++++++++---------- .../v5/models/RouteStepProgressTest.java | 28 ++++++++--------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java index 43444cc8a2d..d21d6e249a0 100644 --- a/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java +++ b/navigation/libandroid-navigation/src/main/java/com/mapbox/services/android/navigation/v5/NavigationEngine.java @@ -87,7 +87,7 @@ void onLocationChanged(DirectionsRoute directionsRoute, Location location) { SnapLocation snapLocation = new SnapLocation(location, previousRouteProgress.getCurrentLegProgress().getCurrentStep(), options); - // Create a new RouteProgress object using the latest user location + // Create a RouteProgress.create object using the latest user location RouteProgress routeProgress = RouteProgress.create(directionsRoute, snapLocation.getUsersCurrentSnappedPosition(), legIndex, stepIndex, alertLevel); diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java index e4c11424d36..82008ab372f 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/RouteProgressTest.java @@ -38,14 +38,14 @@ public void setup() { @Test public void sanityTest() { - RouteProgress routeProgress = new RouteProgress(route, userSnappedPosition, 0, 0, 0); + RouteProgress routeProgress = RouteProgress.create(route, userSnappedPosition, 0, 0, 0); assertNotNull("should not be null", routeProgress); } @Test public void getRoute_returnsDirectionsRoute() { RouteProgress routeProgress - = new RouteProgress(route, userSnappedPosition, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + = RouteProgress.create(route, userSnappedPosition, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(route, routeProgress.getRoute()); } @@ -53,7 +53,7 @@ public void getRoute_returnsDirectionsRoute() { @Test public void getAlertLevel_returnsCorrectAlertLevel() { RouteProgress routeProgress - = new RouteProgress(route, userSnappedPosition, 0, 0, NavigationConstants.LOW_ALERT_LEVEL); + = RouteProgress.create(route, userSnappedPosition, 0, 0, NavigationConstants.LOW_ALERT_LEVEL); Assert.assertEquals(NavigationConstants.LOW_ALERT_LEVEL, routeProgress.getAlertUserLevel()); Assert.assertNotSame(NavigationConstants.HIGH_ALERT_LEVEL, routeProgress.getAlertUserLevel()); @@ -62,7 +62,7 @@ public void getAlertLevel_returnsCorrectAlertLevel() { @Test public void getDistanceRemaining_equalsZeroAtEndOfRoute() { RouteProgress routeProgress - = new RouteProgress( + = RouteProgress.create( route, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition(), route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.LOW_ALERT_LEVEL); @@ -72,7 +72,7 @@ public void getDistanceRemaining_equalsZeroAtEndOfRoute() { @Test public void getDistanceRemaining_equalsRouteDistanceAtBeginning() { RouteProgress routeProgress - = new RouteProgress( + = RouteProgress.create( route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.LOW_ALERT_LEVEL); @@ -82,7 +82,7 @@ public void getDistanceRemaining_equalsRouteDistanceAtBeginning() { @Test public void getFractionTraveled_equalsZeroAtBeginning() { RouteProgress routeProgress - = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + = RouteProgress.create(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(0, routeProgress.getFractionTraveled(), BaseTest.LARGE_DELTA); @@ -98,7 +98,7 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { for (double i = 0; i < route.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); - RouteProgress routeProgress = new RouteProgress(route, position, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); + RouteProgress routeProgress = RouteProgress.create(route, position, 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); float fractionRemaining = (float) (routeProgress.getDistanceTraveled() / route.getDistance()); Assert.assertEquals(fractionRemaining, routeProgress.getFractionTraveled(), BaseTest.DELTA); } @@ -109,7 +109,7 @@ public void getFractionTraveled_equalsOneAtEndOfRoute() { Position lastCoordinate = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); - RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + RouteProgress routeProgress = RouteProgress.create(route, lastCoordinate, route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); Assert.assertEquals(1.0, routeProgress.getFractionTraveled(), DELTA); @@ -118,7 +118,7 @@ public void getFractionTraveled_equalsOneAtEndOfRoute() { @Test public void getDurationRemaining_equalsRouteDurationAtBeginning() { RouteProgress routeProgress - = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + = RouteProgress.create(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(3535.2, routeProgress.getDurationRemaining(), BaseTest.DELTA); @@ -129,7 +129,7 @@ public void getDurationRemaining_equalsZeroAtEndOfRoute() { Position lastCoordinate = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); - RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + RouteProgress routeProgress = RouteProgress.create(route, lastCoordinate, route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); Assert.assertEquals(0, routeProgress.getDurationRemaining(), BaseTest.DELTA); @@ -138,7 +138,7 @@ public void getDurationRemaining_equalsZeroAtEndOfRoute() { @Test public void getDistanceTraveled_equalsZeroAtBeginning() { RouteProgress routeProgress - = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + = RouteProgress.create(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(0, routeProgress.getDistanceTraveled(), BaseTest.DELTA); } @@ -148,7 +148,7 @@ public void getDistanceTraveled_equalsRouteDistanceAtEndOfRoute() { Position lastCoordinate = route.getLegs().get(0).getSteps().get(route.getLegs().get(0).getSteps().size() - 1).getManeuver().asPosition(); - RouteProgress routeProgress = new RouteProgress(route, lastCoordinate, + RouteProgress routeProgress = RouteProgress.create(route, lastCoordinate, route.getLegs().size() - 1, firstLeg.getSteps().size() - 1, NavigationConstants.ARRIVE_ALERT_LEVEL); Assert.assertEquals(route.getDistance(), routeProgress.getDistanceTraveled(), BaseTest.DELTA); @@ -157,7 +157,7 @@ public void getDistanceTraveled_equalsRouteDistanceAtEndOfRoute() { @Test public void getCurrentLeg_returnsCurrentLeg() { RouteProgress routeProgress - = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + = RouteProgress.create(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(route.getLegs().get(0), routeProgress.getCurrentLeg()); @@ -166,7 +166,7 @@ public void getCurrentLeg_returnsCurrentLeg() { @Test public void getLegIndex_returnsCurrentLegIndex() { RouteProgress routeProgress - = new RouteProgress(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), + = RouteProgress.create(route, firstLeg.getSteps().get(0).getManeuver().asPosition(), 0, 0, NavigationConstants.DEPART_ALERT_LEVEL); Assert.assertEquals(0, routeProgress.getLegIndex()); diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java index b8cfd0823e5..821d2d38b2a 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteLegProgressTest.java @@ -37,14 +37,14 @@ public void setup() { @Test public void sanityTest() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 0, Mockito.mock(Position.class)); + = RouteLegProgress.create(firstLeg, 0, Mockito.mock(Position.class)); Assert.assertNotNull("should not be null", routeLegProgress); } @Test public void getUpComingStep_returnsNextStepInLeg() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged Assert.assertTrue(routeLegProgress.getUpComingStep().getGeometry() @@ -55,7 +55,7 @@ public void getUpComingStep_returnsNextStepInLeg() { public void getUpComingStep_returnsNull() { int lastStepIndex = firstLeg.getSteps().size() - 1; RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, lastStepIndex, + = RouteLegProgress.create(firstLeg, lastStepIndex, firstLeg.getSteps().get(lastStepIndex - 2).getManeuver().asPosition()); Assert.assertNull(routeLegProgress.getUpComingStep()); @@ -64,7 +64,7 @@ public void getUpComingStep_returnsNull() { @Test public void getCurrentStep_returnsCurrentStep() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged Assert.assertEquals( @@ -78,7 +78,7 @@ public void getCurrentStep_returnsCurrentStep() { @Test public void getPreviousStep_returnsPreviousStep() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); // TODO replace with equalsTo once https://github.com/mapbox/mapbox-java/pull/450 merged Assert.assertEquals( @@ -91,7 +91,7 @@ public void getPreviousStep_returnsPreviousStep() { @Test public void getStepIndex_returnsCurrentStepIndex() { - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 3, + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(3, routeLegProgress.getStepIndex(), BaseTest.DELTA); @@ -100,7 +100,7 @@ public void getStepIndex_returnsCurrentStepIndex() { @Test public void getFractionTraveled_equalsZeroAtBeginning() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); Assert.assertEquals(0.0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); } @@ -114,7 +114,7 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { for (double i = 0; i < firstLeg.getDistance(); i += stepSegments) { Position position = TurfMeasurement.along(lineString, i, TurfConstants.UNIT_METERS).getCoordinates(); - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, 0, position); + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, 0, position); float fractionRemaining = (float) (routeLegProgress.getDistanceTraveled() / firstLeg.getDistance()); Assert.assertEquals(fractionRemaining, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); } @@ -122,7 +122,7 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { @Test public void getFractionTraveled_equalsOneAtEndOfLeg() { - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, firstLeg.getSteps().size() - 1, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); Assert.assertEquals(1.0, routeLegProgress.getFractionTraveled(), BaseTest.DELTA); @@ -131,14 +131,14 @@ public void getFractionTraveled_equalsOneAtEndOfLeg() { @Test public void getDistanceRemaining_equalsLegDistanceAtBeginning() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); Assert.assertEquals(firstLeg.getDistance(), routeLegProgress.getDistanceRemaining(), BaseTest.LARGE_DELTA); } @Test public void getDistanceRemaining_equalsZeroAtEndOfLeg() { - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, firstLeg.getSteps().size() - 1, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); Assert.assertEquals(0, routeLegProgress.getDistanceRemaining(), BaseTest.DELTA); @@ -147,13 +147,13 @@ public void getDistanceRemaining_equalsZeroAtEndOfLeg() { @Test public void getDistanceTraveled_equalsZeroAtBeginning() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); Assert.assertEquals(0, routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); } @Test public void getDistanceTraveled_equalsLegDistanceAtEndOfLeg() { - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, firstLeg.getSteps().size() - 1, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); Assert.assertEquals(firstLeg.getDistance(), routeLegProgress.getDistanceTraveled(), BaseTest.DELTA); @@ -162,14 +162,14 @@ public void getDistanceTraveled_equalsLegDistanceAtEndOfLeg() { @Test public void getDurationRemaining_equalsLegDurationAtBeginning() { RouteLegProgress routeLegProgress - = new RouteLegProgress(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); + = RouteLegProgress.create(firstLeg, 0, firstLeg.getSteps().get(0).getManeuver().asPosition()); Assert.assertEquals(3535.2, routeLegProgress.getDurationRemaining(), BaseTest.DELTA); } @Test public void getDurationRemaining_equalsZeroAtEndOfLeg() { - RouteLegProgress routeLegProgress = new RouteLegProgress(firstLeg, firstLeg.getSteps().size() - 1, + RouteLegProgress routeLegProgress = RouteLegProgress.create(firstLeg, firstLeg.getSteps().size() - 1, firstLeg.getSteps().get(firstLeg.getSteps().size() - 1).getManeuver().asPosition()); Assert.assertEquals(0, routeLegProgress.getDurationRemaining(), BaseTest.DELTA); diff --git a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java index 69168b4d873..fc0016a993d 100644 --- a/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java +++ b/navigation/libandroid-navigation/src/test/java/com/mapbox/services/android/navigation/v5/models/RouteStepProgressTest.java @@ -48,7 +48,7 @@ public void setup() { @Test public void sanityTest() { RouteStepProgress routeStepProgress - = new RouteStepProgress(firstLeg, 0, Mockito.mock(Position.class)); + = RouteStepProgress.create(firstLeg, 0, Mockito.mock(Position.class)); assertNotNull("should not be null", routeStepProgress); } @@ -62,7 +62,7 @@ public void getStepDistance_equalsZeroOnOneCoordSteps() { int totalStepCountInLeg = lastLeg.getSteps().size() - 1; RouteStepProgress routeStepProgress = - new RouteStepProgress(lastLeg, totalStepCountInLeg, Mockito.mock(Position.class)); + RouteStepProgress.create(lastLeg, totalStepCountInLeg, Mockito.mock(Position.class)); assertNotNull("should not be null", routeStepProgress); assertEquals(1, routeStepProgress.getFractionTraveled(), DELTA); assertEquals(0, routeStepProgress.getDistanceRemaining(), DELTA); @@ -77,7 +77,7 @@ public void getDistanceRemaining_equalsStepDistanceAtBeginning() { double stepDistance = TurfMeasurement.lineDistance(lineString, TurfConstants.UNIT_METERS); RouteStepProgress routeStepProgress - = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteStepProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(stepDistance, routeStepProgress.getDistanceRemaining(), BaseTest.DELTA); } @@ -99,14 +99,14 @@ public void getDistanceRemaining_equalsCorrectValueAtIntervals() { double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 0, position); Assert.assertEquals(distance, routeStepProgress.getDistanceRemaining(), BaseTest.DELTA); } } @Test public void getDistanceRemaining_equalsZeroAtEndOfStep() { - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(0, routeStepProgress.getDistanceRemaining(), BaseTest.DELTA); @@ -115,7 +115,7 @@ public void getDistanceRemaining_equalsZeroAtEndOfStep() { @Test public void getDistanceTraveled_equalsZeroAtBeginning() { RouteStepProgress routeStepProgress - = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteStepProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(0, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); } @@ -138,14 +138,14 @@ public void getDistanceTraveled_equalsCorrectValueAtIntervals() { distance = 0; } - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 0, position); Assert.assertEquals(distance, routeStepProgress.getDistanceTraveled(), BaseTest.DELTA); } } @Test public void getDistanceTraveled_equalsStepDistanceAtEndOfStep() { - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(firstLeg.getSteps().get(3).getDistance(), @@ -155,7 +155,7 @@ public void getDistanceTraveled_equalsStepDistanceAtEndOfStep() { @Test public void getFractionTraveled_equalsZeroAtBeginning() { RouteStepProgress routeStepProgress - = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteStepProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(0, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); } @@ -176,7 +176,7 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 0, position); float fractionRemaining = (float) ((firstStep.getDistance() - distance) / firstStep.getDistance()); if (fractionRemaining < 0) { fractionRemaining = 0; @@ -187,7 +187,7 @@ public void getFractionTraveled_equalsCorrectValueAtIntervals() { @Test public void getFractionTraveled_equalsOneAtEndOfStep() { - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(1.0, routeStepProgress.getFractionTraveled(), BaseTest.DELTA); @@ -196,7 +196,7 @@ public void getFractionTraveled_equalsOneAtEndOfStep() { @Test public void getDurationRemaining_equalsStepDurationAtBeginning() { RouteStepProgress routeStepProgress - = new RouteStepProgress(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); + = RouteStepProgress.create(firstLeg, 5, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(41.5, routeStepProgress.getDurationRemaining(), BaseTest.DELTA); } @@ -217,7 +217,7 @@ public void getDurationRemaining_equalsCorrectValueAtIntervals() { double distance = TurfMeasurement.lineDistance(slicedLine, TurfConstants.UNIT_METERS); - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 0, position); + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 0, position); double fractionRemaining = (firstStep.getDistance() - distance) / firstStep.getDistance(); Assert.assertEquals((1.0 - fractionRemaining) * firstStep.getDuration(), routeStepProgress.getDurationRemaining(), BaseTest.LARGE_DELTA); @@ -226,7 +226,7 @@ public void getDurationRemaining_equalsCorrectValueAtIntervals() { @Test public void getDurationRemaining_equalsZeroAtEndOfStep() { - RouteStepProgress routeStepProgress = new RouteStepProgress(firstLeg, 3, + RouteStepProgress routeStepProgress = RouteStepProgress.create(firstLeg, 3, firstLeg.getSteps().get(4).getManeuver().asPosition()); Assert.assertEquals(0, routeStepProgress.getDurationRemaining(), BaseTest.DELTA);