1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
use std::collections::BTreeSet;
use enumset::EnumSetType;
use serde::{Deserialize, Serialize};
use geom::Duration;
pub use self::engine::CreateEngine;
pub use self::pathfinder::Pathfinder;
pub use self::v1::{Path, PathRequest, PathStep};
pub use self::v2::{PathStepV2, PathV2};
pub use self::vehicles::vehicle_cost;
pub use self::walking::WalkingNode;
use crate::{osm, Lane, LaneID, LaneType, Map, MovementID, Road, RoadID, TurnType};
mod engine;
mod node_map;
mod pathfinder;
pub mod uber_turns;
mod v1;
mod v2;
mod vehicles;
mod walking;
#[derive(Debug, Serialize, Deserialize, PartialOrd, Ord, EnumSetType)]
pub enum PathConstraints {
Pedestrian,
Car,
Bike,
Bus,
Train,
}
impl PathConstraints {
pub fn all() -> Vec<PathConstraints> {
vec![
PathConstraints::Pedestrian,
PathConstraints::Car,
PathConstraints::Bike,
PathConstraints::Bus,
PathConstraints::Train,
]
}
pub fn from_lt(lt: LaneType) -> PathConstraints {
match lt {
LaneType::Sidewalk | LaneType::Shoulder => PathConstraints::Pedestrian,
LaneType::Driving => PathConstraints::Car,
LaneType::Biking => PathConstraints::Bike,
LaneType::Bus => PathConstraints::Bus,
LaneType::LightRail => PathConstraints::Train,
_ => panic!("PathConstraints::from_lt({:?}) doesn't make sense", lt),
}
}
pub fn can_use(self, lane: &Lane, map: &Map) -> bool {
let result = match self {
PathConstraints::Pedestrian => {
return lane.is_walkable();
}
PathConstraints::Car => lane.is_driving(),
PathConstraints::Bike => {
if lane.is_biking() {
true
} else if lane.is_driving() || (lane.is_bus() && map.config.bikes_can_use_bus_lanes)
{
let road = map.get_r(lane.id.road);
!road.osm_tags.is("bicycle", "no")
&& !road
.osm_tags
.is_any(osm::HIGHWAY, vec!["motorway", "motorway_link"])
} else {
false
}
}
PathConstraints::Bus => {
return lane.is_driving() || lane.is_bus();
}
PathConstraints::Train => {
return lane.is_light_rail();
}
};
if result {
return true;
}
if lane.is_bus() {
if let Some(types) =
lane.get_lane_level_turn_restrictions(map.get_r(lane.id.road), true)
{
if types.contains(&TurnType::Right) || types.contains(&TurnType::Left) {
return true;
}
}
}
false
}
pub fn can_use_road(self, road: &Road, map: &Map) -> bool {
road.lanes.iter().any(|lane| self.can_use(lane, map))
}
pub(crate) fn filter_lanes(self, mut choices: Vec<LaneID>, map: &Map) -> Vec<LaneID> {
choices.retain(|l| self.can_use(map.get_l(*l), map));
if self == PathConstraints::Bike {
let just_bike_lanes: Vec<LaneID> = choices
.iter()
.copied()
.filter(|l| map.get_l(*l).is_biking())
.collect();
if !just_bike_lanes.is_empty() {
return just_bike_lanes;
}
}
choices
}
}
pub(crate) fn zone_cost(mvmnt: MovementID, constraints: PathConstraints, map: &Map) -> Duration {
if map
.get_r(mvmnt.from.road)
.access_restrictions
.allow_through_traffic
.contains(constraints)
&& !map
.get_r(mvmnt.to.road)
.access_restrictions
.allow_through_traffic
.contains(constraints)
{
Duration::hours(3)
} else {
Duration::ZERO
}
}
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct RoutingParams {
pub unprotected_turn_penalty: Duration,
pub bike_lane_penalty: f64,
pub bus_lane_penalty: f64,
pub driving_lane_penalty: f64,
pub avoid_steep_incline_penalty: f64,
pub avoid_high_stress: f64,
#[serde(skip_serializing, skip_deserializing)]
pub main_road_penalty: f64,
#[serde(skip_serializing, skip_deserializing)]
pub avoid_roads: BTreeSet<RoadID>,
#[serde(skip_serializing, skip_deserializing)]
pub avoid_movements_between: BTreeSet<(RoadID, RoadID)>,
}
impl Default for RoutingParams {
fn default() -> Self {
Self {
unprotected_turn_penalty: Duration::const_seconds(30.0),
bike_lane_penalty: 1.0,
bus_lane_penalty: 1.1,
driving_lane_penalty: 1.5,
avoid_steep_incline_penalty: 1.0,
avoid_high_stress: 1.0,
main_road_penalty: 1.0,
avoid_roads: BTreeSet::new(),
avoid_movements_between: BTreeSet::new(),
}
}
}
pub fn round(cost: Duration) -> usize {
(cost.inner_seconds().round() as usize).max(1)
}
pub fn unround(cost: usize) -> Duration {
Duration::seconds(cost as f64)
}