Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apple Annotation parsing #287

Merged
merged 20 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
e7463fb
Feat: annotation parsing on iOS
Archdoog Oct 7, 2024
437f997
Feat: annotation parsing on iOS
Archdoog Oct 7, 2024
ae47cc1
Feat: annotation parsing on iOS
Archdoog Oct 7, 2024
61bfb33
Finalized annotation processing for speed limit
Archdoog Oct 27, 2024
8ea598d
Finalized annotation processing for speed limit
Archdoog Oct 27, 2024
16aa02c
Merge branch 'main' of github.com:stadiamaps/ferrostar into feat/appl…
Archdoog Oct 27, 2024
6291f5d
Finalized annotation processing for speed limit
Archdoog Oct 27, 2024
eca8d45
Finalized annotation processing for speed limit
Archdoog Oct 27, 2024
0245214
Applied swiftformat
Archdoog Oct 27, 2024
2eb6a50
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
d8f4378
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
bf253d1
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
674e2eb
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
4c93a77
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
0b62778
removed no longer needed cargo maxspeed mps tests
Archdoog Oct 27, 2024
dd35901
Apply suggestions from code review
Archdoog Oct 28, 2024
694923f
Merge branch 'main' of github.com:stadiamaps/ferrostar into feat/appl…
Archdoog Oct 28, 2024
de5a702
Improvements and testing from PR reivew
Archdoog Oct 28, 2024
76d5780
Improvements and testing from PR reivew
Archdoog Oct 28, 2024
e806cd2
Improvements and testing from PR reivew
Archdoog Oct 29, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apple/DemoApp/Demo/DemoNavigationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
styleURL: style,
camera: $camera,
navigationState: ferrostarCore.state,
calculateSpeedLimit: getSpeedLimit,
onTapExit: { stopNavigation() },
makeMapContent: {
let source = ShapeSource(identifier: "userLocation") {
Expand Down Expand Up @@ -148,6 +149,7 @@
.task {
await getRoutes()
}
.environment(\.navigationFormatterCollection, <#T##value: V##V#>)

Check failure on line 152 in apple/DemoApp/Demo/DemoNavigationView.swift

View workflow job for this annotation

GitHub Actions / build-demo (iOS Demo)

editor placeholder in source file
}
}

Expand Down Expand Up @@ -219,6 +221,13 @@
return "±\(Int(userLocation.horizontalAccuracy))m accuracy"
}

func getSpeedLimit(_ navigationState: NavigationState?) -> Measurement<UnitSpeed>? {
guard let annotation = try? navigationState?.currentAnnotation(as: ValhallaOsrmAnnotation.self) else {
return nil
}
return annotation.speedLimit?.measurementValue
}

private func preventAutoLock() {
UIApplication.shared.isIdleTimerDisabled = true
}
Expand Down
20 changes: 20 additions & 0 deletions apple/Sources/FerrostarCore/Models/Annotations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

/// A Valhalla OSRM flavored annotations object.
public struct ValhallaOsrmAnnotation: Decodable {
enum CodingKeys: String, CodingKey {
case speedLimit = "maxspeed"
case speed
case distance
case duration
}

/// The speed limit for the current line segment.
public let speedLimit: MaxSpeed?

public let speed: Double?

public let distance: Double?

public let duration: Double?
}
71 changes: 71 additions & 0 deletions apple/Sources/FerrostarCore/Models/MaxSpeed.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import Foundation

/// The OSRM formatted MaxSpeed. This is a custom field used by some API's like Mapbox,
/// Valhalla with OSRM json output, etc.
///
/// For more information see:
/// - https://wiki.openstreetmap.org/wiki/Key:maxspeed
/// - https://docs.mapbox.com/api/navigation/directions/#route-leg-object (search for `max_speed`)
/// - https://valhalla.github.io/valhalla/speeds/#assignment-of-speeds-to-roadways
public enum MaxSpeed: Decodable {
public enum Units: String, Decodable {
case kilometersPerHour = "km/h"
case milesPerHour = "mph"
case knots // "knots" are an option in core OSRM docs, though unsure if they're ever used in this context.
Archdoog marked this conversation as resolved.
Show resolved Hide resolved
}

/// There is no speed limit (it's unlimited, e.g. German Autobahn)
case none
Archdoog marked this conversation as resolved.
Show resolved Hide resolved

/// The speed limit is not known.
case unknown

/// The speed limit is a known value and unit (this may be localized depending on the API).
case speed(Double, unit: Units)

enum CodingKeys: CodingKey {
case none
case unknown
case speed
case unit
}

public init(from decoder: any Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)

if let none = try container.decodeIfPresent(Bool.self, forKey: .none) {

Check warning on line 36 in apple/Sources/FerrostarCore/Models/MaxSpeed.swift

View workflow job for this annotation

GitHub Actions / test (FerrostarCore-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

value 'none' was defined but never used; consider replacing with boolean test
// The speed configuration is `{none: true}` for unlimited.
self = .none
} else if let unknown = try container.decodeIfPresent(Bool.self, forKey: .unknown) {

Check warning on line 39 in apple/Sources/FerrostarCore/Models/MaxSpeed.swift

View workflow job for this annotation

GitHub Actions / test (FerrostarCore-Package, platform=iOS Simulator,name=iPhone 15,OS=17.2)

value 'unknown' was defined but never used; consider replacing with boolean test
// The speed configuration is `{unknown: true}` for unknown.
self = .unknown
} else if let value = try container.decodeIfPresent(Double.self, forKey: .speed),
let unit = try container.decodeIfPresent(Units.self, forKey: .unit)
{
// The speed is a known value with units. Some API's may localize, others only support a single unit.
self = .speed(value, unit: unit)
} else {
throw DecodingError.dataCorrupted(.init(
codingPath: decoder.codingPath,
debugDescription: "Invalid MaxSpeed, see docstrings for reference links"
))
}
}

/// The MaxSpeed as a measurement
public var measurementValue: Measurement<UnitSpeed>? {
switch self {
case .none: nil
case .unknown: nil
case let .speed(value, unit):
switch unit {
case .kilometersPerHour:
.init(value: value, unit: .kilometersPerHour)
case .milesPerHour:
.init(value: value, unit: .milesPerHour)
case .knots:
.init(value: value, unit: .knots)
}
}
}
}
18 changes: 18 additions & 0 deletions apple/Sources/FerrostarCore/NavigationState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,29 @@ public struct NavigationState: Hashable {
return remainingSteps
}

/// The current geometry segment's annotations in a JSON string.
///
/// A segment is the line between two coordinates on the geometry.
public var currentAnnotationJSON: String? {
guard case let .navigating(_, _, _, _, _, _, _, _, annotationJson: annotationJson) = tripState else {
return nil
}

return annotationJson
}

/// The current geometry segment's annotations.
///
/// A segment is the line between two coordinates on the geometry.
///
/// - Parameter type: The type to decode the annotation json string to.
/// - Returns: The decoded type.
public func currentAnnotation<T: Decodable>(as type: T.Type) throws -> T? {
Archdoog marked this conversation as resolved.
Show resolved Hide resolved
// TODO: This ideally goes away if we can convert the annotationJson to bytes.
guard let data = currentAnnotationJSON?.data(using: .utf8) else {
return nil
}

return try JSONDecoder().decode(type, from: data)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn
public var midLeading: (() -> AnyView)?
public var bottomTrailing: (() -> AnyView)?

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

public var minimumSafeAreaInsets: EdgeInsets
Expand All @@ -46,12 +49,14 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn
camera: Binding<MapViewCamera>,
navigationCamera: MapViewCamera = .automotiveNavigation(),
navigationState: NavigationState?,
calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)? = nil,
minimumSafeAreaInsets: EdgeInsets = EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16),
onTapExit: (() -> Void)? = nil,
@MapViewContentBuilder makeMapContent: () -> [StyleLayerDefinition] = { [] }
) {
self.styleURL = styleURL
self.navigationState = navigationState
self.calculateSpeedLimit = calculateSpeedLimit
self.minimumSafeAreaInsets = minimumSafeAreaInsets
self.onTapExit = onTapExit

Expand Down Expand Up @@ -83,7 +88,7 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn
case .landscapeLeft, .landscapeRight:
LandscapeNavigationOverlayView(
navigationState: navigationState,
speedLimit: nil,
speedLimit: speedLimit,
showZoom: true,
onZoomIn: { camera.incrementZoom(by: 1) },
onZoomOut: { camera.incrementZoom(by: -1) },
Expand All @@ -103,7 +108,7 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn
default:
PortraitNavigationOverlayView(
navigationState: navigationState,
speedLimit: nil,
speedLimit: speedLimit,
showZoom: true,
onZoomIn: { camera.incrementZoom(by: 1) },
onZoomOut: { camera.incrementZoom(by: -1) },
Expand All @@ -128,6 +133,9 @@ public struct DynamicallyOrientingNavigationView: View, CustomizableNavigatingIn
) { _ in
orientation = UIDevice.current.orientation
}
.onChange(of: navigationState) { value in
speedLimit = calculateSpeedLimit?(value)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ public struct LandscapeNavigationView: View, CustomizableNavigatingInnerGridView
public var midLeading: (() -> AnyView)?
public var bottomTrailing: (() -> AnyView)?

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

public var minimumSafeAreaInsets: EdgeInsets
Expand All @@ -46,12 +49,14 @@ public struct LandscapeNavigationView: View, CustomizableNavigatingInnerGridView
camera: Binding<MapViewCamera>,
navigationCamera: MapViewCamera = .automotiveNavigation(),
navigationState: NavigationState?,
calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)? = nil,
minimumSafeAreaInsets: EdgeInsets = EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16),
onTapExit: (() -> Void)? = nil,
@MapViewContentBuilder makeMapContent: () -> [StyleLayerDefinition] = { [] }
) {
self.styleURL = styleURL
self.navigationState = navigationState
self.calculateSpeedLimit = calculateSpeedLimit
self.minimumSafeAreaInsets = minimumSafeAreaInsets
self.onTapExit = onTapExit

Expand All @@ -77,7 +82,7 @@ public struct LandscapeNavigationView: View, CustomizableNavigatingInnerGridView

LandscapeNavigationOverlayView(
navigationState: navigationState,
speedLimit: nil,
speedLimit: speedLimit,
showZoom: true,
onZoomIn: { camera.incrementZoom(by: 1) },
onZoomOut: { camera.incrementZoom(by: -1) },
Expand All @@ -96,6 +101,9 @@ public struct LandscapeNavigationView: View, CustomizableNavigatingInnerGridView
}.complementSafeAreaInsets(parentGeometry: geometry, minimumInsets: minimumSafeAreaInsets)
}
}
.onChange(of: navigationState) { value in
speedLimit = calculateSpeedLimit?(value)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ struct PortraitNavigationOverlayView: View, CustomizableNavigatingInnerGridView
onTapExit: onTapExit
)
}
}.padding(.top, instructionsViewSizeWhenNotExpanded.height)
}
.padding(.top, instructionsViewSizeWhenNotExpanded.height + 16)

if case .navigating = navigationState?.tripState,
let visualInstruction = navigationState?.currentVisualInstruction,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ public struct PortraitNavigationView: View, CustomizableNavigatingInnerGridView
@Binding var camera: MapViewCamera
let navigationCamera: MapViewCamera

var calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)?
@State var speedLimit: Measurement<UnitSpeed>?

var onTapExit: (() -> Void)?

/// Create a portrait navigation view. This view is optimized for display on a portrait screen where the
Expand All @@ -47,12 +50,14 @@ public struct PortraitNavigationView: View, CustomizableNavigatingInnerGridView
camera: Binding<MapViewCamera>,
navigationCamera: MapViewCamera = .automotiveNavigation(),
navigationState: NavigationState?,
calculateSpeedLimit: ((NavigationState?) -> Measurement<UnitSpeed>?)? = nil,
minimumSafeAreaInsets: EdgeInsets = EdgeInsets(top: 16, leading: 16, bottom: 16, trailing: 16),
onTapExit: (() -> Void)? = nil,
@MapViewContentBuilder makeMapContent: () -> [StyleLayerDefinition] = { [] }
) {
self.styleURL = styleURL
self.navigationState = navigationState
self.calculateSpeedLimit = calculateSpeedLimit
self.minimumSafeAreaInsets = minimumSafeAreaInsets
self.onTapExit = onTapExit

Expand All @@ -79,7 +84,7 @@ public struct PortraitNavigationView: View, CustomizableNavigatingInnerGridView

PortraitNavigationOverlayView(
navigationState: navigationState,
speedLimit: nil,
speedLimit: speedLimit,
showZoom: true,
onZoomIn: { camera.incrementZoom(by: 1) },
onZoomOut: { camera.incrementZoom(by: -1) },
Expand All @@ -98,6 +103,9 @@ public struct PortraitNavigationView: View, CustomizableNavigatingInnerGridView
}.complementSafeAreaInsets(parentGeometry: geometry, minimumInsets: minimumSafeAreaInsets)
}
}
.onChange(of: navigationState) { value in
speedLimit = calculateSpeedLimit?(value)
}
}
}

Expand Down
18 changes: 3 additions & 15 deletions common/ferrostar/src/routing_adapters/osrm/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,31 +84,19 @@ pub enum MaxSpeedUnits {
KilometersPerHour,
#[serde(rename = "mph")]
MilesPerHour,
#[serde(rename = "knots")]
Knots,
}

/// The local posted speed limit between a pair of coordinates.
#[derive(Debug, Clone)]
#[allow(dead_code)] // TODO: https://github.com/stadiamaps/ferrostar/issues/271
pub enum MaxSpeed {
None { none: bool },
Unknown { unknown: bool },
Known { speed: f64, unit: MaxSpeedUnits },
}

#[allow(dead_code)] // TODO: https://github.com/stadiamaps/ferrostar/issues/271
impl MaxSpeed {
/// Get the max speed as meters per second.
pub fn get_as_meters_per_second(&self) -> Option<f64> {
match self {
MaxSpeed::Known { speed, unit } => match unit {
MaxSpeedUnits::KilometersPerHour => Some(speed * 0.27778),
MaxSpeedUnits::MilesPerHour => Some(speed * 0.44704),
},
#[allow(unused)]
MaxSpeed::Unknown { unknown } => None,
}
}
}

#[derive(Deserialize, Debug)]
pub struct RouteStep {
/// The distance from the start of the current maneuver to the following step, in meters.
Expand Down
Loading