Skip to content

Commit a7e4167

Browse files
author
Bobby Sudekum
authored
Merge pull request #28 from Project-OSRM/wayname-class
2 parents a4a2606 + 7c816f4 commit a7e4167

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

Cartfile.resolved

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
github "mapbox/MapboxDirections.swift" "v0.10.0"
1+
github "mapbox/MapboxDirections.swift" "v0.10.1"
22
github "raphaelmor/Polyline" "v4.1.1"

OSRMTextInstructions/OSRMTextInstructions.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,20 @@ public class OSRMInstructionFormatter: Formatter {
121121
typealias InstructionsByModifier = [String: InstructionsByToken]
122122

123123
override public func string(for obj: Any?) -> String? {
124-
return string(for: obj, legIndex: nil, numberOfLegs: nil, modifyValueByKey: nil)
124+
return string(for: obj, legIndex: nil, numberOfLegs: nil, roadClasses: nil, modifyValueByKey: nil)
125125
}
126126

127-
public func string(for obj: Any?, legIndex: Int?, numberOfLegs: Int?, modifyValueByKey: ((TokenType, String) -> String)?) -> String? {
127+
/**
128+
Creates an instruction given a step and options.
129+
130+
- parameter step:
131+
- parameter legIndex: Current leg index the user is currently on.
132+
- parameter numberOfLegs: Total number of `RouteLeg` for the given `Route`.
133+
- parameter roadClasses: Option set representing the classes of road for the `RouteStep`.
134+
- parameter modifyValueByKey: Allows for mutating the instruction at given parts of the instruction.
135+
- returns: An instruction as a `String`.
136+
*/
137+
public func string(for obj: Any?, legIndex: Int?, numberOfLegs: Int?, roadClasses: RoadClasses? = RoadClasses([]), modifyValueByKey: ((TokenType, String) -> String)?) -> String? {
128138
guard let step = obj as? RouteStep else {
129139
return nil
130140
}
@@ -179,9 +189,12 @@ public class OSRMInstructionFormatter: Formatter {
179189
// Set wayName
180190
let name = step.names?.first
181191
let ref = step.codes?.first
192+
let isMotorway = roadClasses?.contains(.motorway) ?? false
182193

183-
if let name = name, let ref = ref, name != ref {
194+
if let name = name, let ref = ref, name != ref, !isMotorway {
184195
wayName = modifyValueByKey != nil ? "\(modifyValueByKey!(.wayName, name)) (\(modifyValueByKey!(.wayName, ref)))" : "\(name) (\(ref))"
196+
} else if let ref = ref, isMotorway, let decimalRange = ref.rangeOfCharacter(from: .decimalDigits), !decimalRange.isEmpty {
197+
wayName = modifyValueByKey != nil ? "\(modifyValueByKey!(.wayName, ref))" : ref
185198
} else if name == nil, let ref = ref {
186199
wayName = modifyValueByKey != nil ? "\(modifyValueByKey!(.wayName, ref))" : ref
187200
} else {

OSRMTextInstructionsTests/OSRMTextInstructionsTests.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,17 @@ class OSRMTextInstructionsTests: XCTestCase {
3434
XCTAssert(false, "invalid json")
3535
return
3636
}
37-
let options = json["options"] as? [String: Int]
37+
let options = json["options"] as? [String: Any]
3838

3939
let step = RouteStep(json: json["step"] as! [String: Any])
40+
41+
var roadClasses: RoadClasses? = nil
42+
if let classes = options?["classes"] as? [String] {
43+
roadClasses = RoadClasses(descriptions: classes)
44+
}
4045

4146
// compile instruction
42-
let instruction = instructions.string(for: step, legIndex: options?["legIndex"], numberOfLegs: options?["legCount"], modifyValueByKey: nil)
47+
let instruction = instructions.string(for: step, legIndex: options?["legIndex"] as? Int, numberOfLegs: options?["legCount"] as? Int, roadClasses: roadClasses, modifyValueByKey: nil)
4348

4449
// check generated instruction against fixture
4550
XCTAssertEqual(

osrm-text-instructions

0 commit comments

Comments
 (0)