Skip to content

Commit 2ba2ac0

Browse files
authored
improve validation if missing distance annotation (#215)
* improve validation to fix #213 * improve error message; fix test&fixture
1 parent 534334f commit 2ba2ac0

3 files changed

Lines changed: 20 additions & 0 deletions

File tree

maplibre-navigation-core/src/androidUnitTest/kotlin/org/maplibre/navigation/core/utils/ValidationUtilsTest.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import org.maplibre.navigation.core.models.RouteOptions
77
import java.io.IOException
88
import java.util.MissingFormatArgumentException
99
import kotlin.test.Test
10+
import kotlin.test.assertEquals
11+
import kotlin.test.assertFailsWith
1012

1113
class ValidationUtilsTest : BaseTest() {
1214

@@ -44,6 +46,16 @@ class ValidationUtilsTest : BaseTest() {
4446
ValidationUtils.validDirectionsRoute(routeWithFalseBannerInstructions, true)
4547
}
4648

49+
@Test
50+
fun validDirectionsRoute_isInvalidWithoutDistance() {
51+
val route = buildTestDirectionsRoute("directions_two_leg_route_without_distances.json");
52+
val exception = assertFailsWith<IllegalArgumentException> {
53+
ValidationUtils.validDirectionsRoute(route, false)
54+
}
55+
56+
assertEquals("Leg 0 has maxspeed annotation but missing distance annotation", exception.message)
57+
}
58+
4759
@Throws(IOException::class)
4860
private fun buildRouteWithNullInstructions(): DirectionsRoute {
4961
val route = buildTestDirectionsRoute()
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"routes":[{"weight_name":"auto","weight":33.207,"duration":27.475,"distance":183.091,"legs":[{"via_waypoints":[],"annotation":{"maxspeed":[{"speed":50,"unit":"km/h"},{"speed":50,"unit":"km/h"},{"speed":50,"unit":"km/h"}],"duration":[6.505,2.719,2.405]},"admins":[{"iso_3166_1_alpha3":"USA","iso_3166_1":"US"}],"weight":13.924,"duration":11.629,"steps":[{"bannerInstructions":[{"primary":{"components":[{"type":"text","text":"You will arrive at your destination"}],"type":"arrive","modifier":"straight","text":"You will arrive at your destination"},"distanceAlongGeometry":92.111},{"primary":{"components":[{"type":"text","text":"You have arrived at your destination"}],"type":"arrive","modifier":"straight","text":"You have arrived at your destination"},"distanceAlongGeometry":55.556}],"voiceInstructions":[{"ssmlAnnouncement":"<speak><amazon:effect name=\"drc\"><prosody rate=\"1.08\">Drive east on <say-as interpret-as=\"address\">South Orange Avenue</say-as>, <say-as interpret-as=\"address\">County Route 5 10</say-as>. Then, in 300 feet, You will arrive at your destination.</prosody></amazon:effect></speak>","announcement":"Drive east on South Orange Avenue, County Route 5 10. Then, in 300 feet, You will arrive at your destination.","distanceAlongGeometry":92.111}],"intersections":[{"entry":[true],"bearings":[90],"duration":6.505,"traffic_signal":true,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":0,"weight":7.806,"geometry_index":0,"location":[-74.220588,40.745062]},{"entry":[true,true,false],"in":2,"bearings":[23,89,270],"duration":2.719,"turn_duration":0.019,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":1,"weight":3.24,"geometry_index":1,"location":[-74.219986,40.745065]},{"bearings":[89,197,269],"entry":[true,true,false],"in":2,"turn_duration":0.007,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":0,"geometry_index":2,"location":[-74.219733,40.745067]}],"maneuver":{"type":"depart","instruction":"Drive east on South Orange Avenue/CR 510.","bearing_after":90,"bearing_before":0,"location":[-74.220588,40.745062]},"name":"South Orange Avenue","duration":11.629,"distance":92.111,"driving_side":"right","weight":13.924,"mode":"driving","ref":"CR 510","geometry":"ke{ulAvaaqlCEsd@CyNCyM"},{"bannerInstructions":[],"voiceInstructions":[],"intersections":[{"bearings":[269],"entry":[true],"in":0,"admin_index":0,"geometry_index":3,"location":[-74.219496,40.745069]}],"maneuver":{"type":"arrive","instruction":"You have arrived at your destination.","bearing_after":0,"bearing_before":89,"location":[-74.219496,40.745069]},"name":"South Orange Avenue","duration":0,"distance":0,"driving_side":"right","weight":0,"mode":"driving","ref":"CR 510","geometry":"ye{ulAn}~plC??"}],"distance":92.111,"summary":"South Orange Avenue"},{"via_waypoints":[],"annotation":{"speed":[8.4,7.4,5.6,4.2],"distance":[29.4,13.8,16.2,31.6],"duration":[3.482,1.874,2.887,7.603]},"admins":[{"iso_3166_1_alpha3":"USA","iso_3166_1":"US"}],"weight":19.283,"duration":15.846,"steps":[{"bannerInstructions":[{"primary":{"components":[{"type":"text","text":"Your destination will be on the right"}],"type":"arrive","modifier":"right","text":"Your destination will be on the right"},"distanceAlongGeometry":90.98},{"primary":{"components":[{"type":"text","text":"Your destination is on the right"}],"type":"arrive","modifier":"right","text":"Your destination is on the right"},"distanceAlongGeometry":55.556}],"voiceInstructions":[{"ssmlAnnouncement":"<speak><amazon:effect name=\"drc\"><prosody rate=\"1.08\">Drive east on <say-as interpret-as=\"address\">South Orange Avenue</say-as>, <say-as interpret-as=\"address\">County Route 5 10</say-as>. Then, in 300 feet, Your destination will be on the right.</prosody></amazon:effect></speak>","announcement":"Drive east on South Orange Avenue, County Route 5 10. Then, in 300 feet, Your destination will be on the right.","distanceAlongGeometry":90.98}],"intersections":[{"entry":[true],"bearings":[89],"duration":3.482,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":0,"weight":4.178,"geometry_index":0,"location":[-74.219496,40.745069]},{"entry":[true,true,false],"in":2,"bearings":[90,183,269],"duration":1.874,"turn_duration":0.007,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":0,"weight":2.287,"geometry_index":1,"location":[-74.219148,40.745071]},{"entry":[true,true,false],"in":2,"bearings":[24,90,270],"duration":2.887,"turn_duration":0.007,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":1,"weight":3.528,"geometry_index":2,"location":[-74.218984,40.745072]},{"bearings":[89,197,270],"entry":[true,false,false],"in":2,"turn_duration":0.019,"mapbox_streets_v8":{"class":"primary"},"is_urban":true,"admin_index":0,"out":0,"geometry_index":3,"location":[-74.218792,40.745073]}],"maneuver":{"type":"depart","instruction":"Drive east on South Orange Avenue/CR 510.","bearing_after":89,"bearing_before":0,"location":[-74.219496,40.745069]},"name":"South Orange Avenue","duration":15.846,"distance":90.98,"driving_side":"right","weight":19.283,"mode":"driving","ref":"CR 510","geometry":"ye{ulAn}~plCCwTAgIA_KEmV"},{"bannerInstructions":[],"voiceInstructions":[],"intersections":[{"bearings":[269],"entry":[true],"in":0,"admin_index":0,"geometry_index":4,"location":[-74.218417,40.745076]}],"maneuver":{"type":"arrive","instruction":"Your destination is on the right.","modifier":"right","bearing_after":0,"bearing_before":89,"location":[-74.218417,40.745076]},"name":"South Orange Avenue","duration":0,"distance":0,"driving_side":"right","weight":0,"mode":"driving","ref":"CR 510","geometry":"gf{ulA`z|plC??"}],"distance":90.98,"summary":"South Orange Avenue"}],"geometry":"ke{ulAvaaqlCEsd@CyNCyMCwTAgIA_KEmV","voiceLocale":"en-US"}],"waypoints":[{"distance":1.949,"name":"South Orange Avenue","location":[-74.220588,40.745062]},{"distance":0.514,"name":"South Orange Avenue","location":[-74.219496,40.745069]},{"distance":3.286,"name":"South Orange Avenue","location":[-74.218417,40.745076]}],"code":"Ok","uuid":"Rcua0Z2onGGvltk683mcg0F0YDvNIEbkaIdB-TS8pFok6GvgwYEnGg=="}

maplibre-navigation-core/src/commonMain/kotlin/org/maplibre/navigation/core/utils/ValidationUtils.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ object ValidationUtils {
1919
checkInvalidVoiceInstructions(routeOptions)
2020
checkInvalidBannerInstructions(routeOptions)
2121
}
22+
23+
for ((index, leg) in directionsRoute.legs.withIndex()) {
24+
val annotation = leg.annotation
25+
if (annotation?.maxSpeed != null && annotation.distance == null) {
26+
throw IllegalArgumentException("Leg $index has maxspeed annotation but missing distance annotation")
27+
}
28+
}
2229
}
2330

2431
private fun checkInvalidVoiceInstructions(routeOptions: RouteOptions) {

0 commit comments

Comments
 (0)