Skip to content

Commit 1816756

Browse files
committed
Include exit number in off-ramp instructions
Added TokenType.exitCode. Renamed TokenType.exit to exitIndex for clarity. Use the exit and exit_destination instructions if an exit code is present.
1 parent 21e625c commit 1816756

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

OSRMTextInstructions/OSRMTextInstructions.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,11 @@ public class OSRMInstructionFormatter: Formatter {
212212
// Decide which instruction string to use
213213
// Destination takes precedence over name
214214
var instruction: String
215-
if let _ = step.destinations, let obj = instructionObject["destination"] {
215+
if let _ = step.destinations, let _ = step.exitCodes?.first, let obj = instructionObject["exit_destination"] {
216+
instruction = obj
217+
} else if let _ = step.destinations, let obj = instructionObject["destination"] {
218+
instruction = obj
219+
} else if let _ = step.exitCodes?.first, let obj = instructionObject["exit"] {
216220
instruction = obj
217221
} else if !wayName.isEmpty, let obj = instructionObject["name"] {
218222
instruction = obj
@@ -225,10 +229,11 @@ public class OSRMInstructionFormatter: Formatter {
225229
if let legIndex = legIndex, let numberOfLegs = numberOfLegs, legIndex != numberOfLegs - 1 {
226230
nthWaypoint = ordinalFormatter.string(from: (legIndex + 1) as NSNumber)
227231
}
232+
let exitCode = step.exitCodes?.first ?? ""
228233
let destination = step.destinations?.first ?? ""
229-
var exit: String = ""
234+
var exitOrdinal: String = ""
230235
if let exitIndex = step.exitIndex, exitIndex <= 10 {
231-
exit = ordinalFormatter.string(from: exitIndex as NSNumber)!
236+
exitOrdinal = ordinalFormatter.string(from: exitIndex as NSNumber)!
232237
}
233238
let modifierConstants = constants["modifier"] as! [String: String]
234239
let modifierConstant = modifierConstants[modifier ?? "straight"]!
@@ -260,7 +265,8 @@ public class OSRMInstructionFormatter: Formatter {
260265
switch tokenType {
261266
case .wayName: replacement = wayName
262267
case .destination: replacement = destination
263-
case .exit: replacement = exit
268+
case .exitCode: replacement = exitCode
269+
case .exitIndex: replacement = exitOrdinal
264270
case .rotaryName: replacement = rotaryName
265271
case .laneInstruction: replacement = laneInstruction ?? ""
266272
case .modifier: replacement = modifierConstant

OSRMTextInstructions/TokenType.swift

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ public enum TokenType: Int, CustomStringConvertible {
66
case wayName
77
case destination
88
case rotaryName
9-
case exit
9+
case exitCode
10+
case exitIndex
1011
case laneInstruction
1112
case modifier
1213
case direction
@@ -21,8 +22,10 @@ public enum TokenType: Int, CustomStringConvertible {
2122
type = .destination
2223
case "rotary_name":
2324
type = .rotaryName
25+
case "exit":
26+
type = .exitCode
2427
case "exit_number":
25-
type = .exit
28+
type = .exitIndex
2629
case "lane_instruction":
2730
type = .laneInstruction
2831
case "modifier":
@@ -45,7 +48,9 @@ public enum TokenType: Int, CustomStringConvertible {
4548
return "destination"
4649
case .rotaryName:
4750
return "rotary_name"
48-
case .exit:
51+
case .exitCode:
52+
return "exit"
53+
case .exitIndex:
4954
return "exit_number"
5055
case .laneInstruction:
5156
return "lane_instruction"

OSRMTextInstructionsTests/OSRMTextInstructionsTests.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ class OSRMTextInstructionsTests: XCTestCase {
8686
if let ref = jsonStep["ref"] {
8787
step["ref"] = ref
8888
}
89+
if let exits = jsonStep["exits"] {
90+
step["exits"] = exits
91+
}
8992
if let destinations = jsonStep["destinations"] {
9093
step["destinations"] = destinations
9194
}

0 commit comments

Comments
 (0)