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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
use std::collections::BTreeSet;
use std::fmt;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use abstutil::Tags;
use geom::{Distance, Line, PolyLine, Polygon, Pt2D};
use crate::{
osm, DirectedRoadID, Direction, DrivingSide, IntersectionID, Map, MapConfig, Road, RoadID,
RoadSideID, SideOfRoad, TransitStopID, TurnType,
};
pub const PARKING_LOT_SPOT_LENGTH: Distance = Distance::const_meters(6.4);
pub const NORMAL_LANE_THICKNESS: Distance = Distance::const_meters(2.5);
const SERVICE_ROAD_LANE_THICKNESS: Distance = Distance::const_meters(1.5);
pub const SIDEWALK_THICKNESS: Distance = Distance::const_meters(1.5);
const SHOULDER_THICKNESS: Distance = Distance::const_meters(0.5);
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct LaneID {
pub road: RoadID,
pub offset: usize,
}
impl fmt::Display for LaneID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Lane #{}", self.encode_u32())
}
}
impl LaneID {
pub fn encode_u32(self) -> u32 {
let road = self.road.0 << 5;
(road | self.offset) as u32
}
pub fn decode_u32(x: u32) -> LaneID {
let road = RoadID((x >> 5) as usize);
let offset = (x & (1 + 2 + 4 + 8 + 16)) as usize;
LaneID { road, offset }
}
pub fn dummy() -> LaneID {
LaneID {
road: RoadID(0),
offset: 0,
}
}
}
impl Serialize for LaneID {
fn serialize<S>(&self, s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
self.encode_u32().serialize(s)
}
}
impl<'de> Deserialize<'de> for LaneID {
fn deserialize<D>(d: D) -> Result<LaneID, D::Error>
where
D: Deserializer<'de>,
{
let x = <u32>::deserialize(d)?;
Ok(LaneID::decode_u32(x))
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum LaneType {
Driving,
Parking,
Sidewalk,
Shoulder,
Biking,
Bus,
SharedLeftTurn,
Construction,
LightRail,
Buffer(BufferType),
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub enum BufferType {
Stripes,
FlexPosts,
Planters,
JerseyBarrier,
Curb,
}
impl LaneType {
pub fn is_for_moving_vehicles(self) -> bool {
match self {
LaneType::Driving => true,
LaneType::Biking => true,
LaneType::Bus => true,
LaneType::Parking => false,
LaneType::Sidewalk => false,
LaneType::Shoulder => false,
LaneType::SharedLeftTurn => false,
LaneType::Construction => false,
LaneType::LightRail => true,
LaneType::Buffer(_) => false,
}
}
pub fn supports_any_movement(self) -> bool {
match self {
LaneType::Driving => true,
LaneType::Biking => true,
LaneType::Bus => true,
LaneType::Parking => false,
LaneType::Sidewalk => true,
LaneType::Shoulder => true,
LaneType::SharedLeftTurn => false,
LaneType::Construction => false,
LaneType::LightRail => true,
LaneType::Buffer(_) => false,
}
}
pub fn is_walkable(self) -> bool {
self == LaneType::Sidewalk || self == LaneType::Shoulder
}
pub fn describe(self) -> &'static str {
match self {
LaneType::Driving => "a general-purpose driving lane",
LaneType::Biking => "a protected bike lane",
LaneType::Bus => "a bus-only lane",
LaneType::Parking => "an on-street parking lane",
LaneType::Sidewalk => "a sidewalk",
LaneType::Shoulder => "a shoulder",
LaneType::SharedLeftTurn => "a shared left-turn lane",
LaneType::Construction => "a lane that's closed for construction",
LaneType::LightRail => "a light rail track",
LaneType::Buffer(BufferType::Stripes) => "striped pavement",
LaneType::Buffer(BufferType::FlexPosts) => "flex post barriers",
LaneType::Buffer(BufferType::Planters) => "planter barriers",
LaneType::Buffer(BufferType::JerseyBarrier) => "a Jersey barrier",
LaneType::Buffer(BufferType::Curb) => "a raised curb",
}
}
pub fn short_name(self) -> &'static str {
match self {
LaneType::Driving => "driving lane",
LaneType::Biking => "bike lane",
LaneType::Bus => "bus lane",
LaneType::Parking => "parking lane",
LaneType::Sidewalk => "sidewalk",
LaneType::Shoulder => "shoulder",
LaneType::SharedLeftTurn => "left-turn lane",
LaneType::Construction => "construction",
LaneType::LightRail => "light rail track",
LaneType::Buffer(BufferType::Stripes) => "stripes",
LaneType::Buffer(BufferType::FlexPosts) => "flex posts",
LaneType::Buffer(BufferType::Planters) => "planters",
LaneType::Buffer(BufferType::JerseyBarrier) => "Jersey barrier",
LaneType::Buffer(BufferType::Curb) => "curb",
}
}
pub fn from_short_name(x: &str) -> Option<LaneType> {
match x {
"driving lane" => Some(LaneType::Driving),
"bike lane" => Some(LaneType::Biking),
"bus lane" => Some(LaneType::Bus),
"parking lane" => Some(LaneType::Parking),
"sidewalk" => Some(LaneType::Sidewalk),
"shoulder" => Some(LaneType::Shoulder),
"left-turn lane" => Some(LaneType::SharedLeftTurn),
"construction" => Some(LaneType::Construction),
"light rail track" => Some(LaneType::LightRail),
"stripes" => Some(LaneType::Buffer(BufferType::Stripes)),
"flex posts" => Some(LaneType::Buffer(BufferType::FlexPosts)),
"planters" => Some(LaneType::Buffer(BufferType::Planters)),
"Jersey barrier" => Some(LaneType::Buffer(BufferType::JerseyBarrier)),
"curb" => Some(LaneType::Buffer(BufferType::Curb)),
_ => None,
}
}
pub fn to_char(self) -> char {
match self {
LaneType::Driving => 'd',
LaneType::Biking => 'b',
LaneType::Bus => 'B',
LaneType::Parking => 'p',
LaneType::Sidewalk => 's',
LaneType::Shoulder => 'S',
LaneType::SharedLeftTurn => 'C',
LaneType::Construction => 'x',
LaneType::LightRail => 'l',
LaneType::Buffer(_) => '|',
}
}
pub fn from_char(x: char) -> LaneType {
match x {
'd' => LaneType::Driving,
'b' => LaneType::Biking,
'B' => LaneType::Bus,
'p' => LaneType::Parking,
's' => LaneType::Sidewalk,
'S' => LaneType::Shoulder,
'C' => LaneType::SharedLeftTurn,
'x' => LaneType::Construction,
'l' => LaneType::LightRail,
'|' => LaneType::Buffer(BufferType::FlexPosts),
_ => panic!("from_char({}) undefined", x),
}
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Lane {
pub id: LaneID,
pub lane_type: LaneType,
pub lane_center_pts: PolyLine,
pub width: Distance,
pub dir: Direction,
pub src_i: IntersectionID,
pub dst_i: IntersectionID,
pub transit_stops: BTreeSet<TransitStopID>,
pub driving_blackhole: bool,
pub biking_blackhole: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct LaneSpec {
pub lt: LaneType,
pub dir: Direction,
pub width: Distance,
}
impl Lane {
pub fn first_pt(&self) -> Pt2D {
self.lane_center_pts.first_pt()
}
pub fn last_pt(&self) -> Pt2D {
self.lane_center_pts.last_pt()
}
pub fn first_line(&self) -> Line {
self.lane_center_pts.first_line()
}
pub fn last_line(&self) -> Line {
self.lane_center_pts.last_line()
}
pub fn endpoint(&self, i: IntersectionID) -> Pt2D {
if i == self.src_i {
self.first_pt()
} else if i == self.dst_i {
self.last_pt()
} else {
panic!("{} isn't an endpoint of {}", i, self.id);
}
}
pub fn end_line(&self, i: IntersectionID) -> Line {
if i == self.src_i {
self.first_line().reversed()
} else if i == self.dst_i {
self.last_line()
} else {
panic!("{} isn't an endpoint of {}", i, self.id);
}
}
pub fn dist_along_of_point(&self, pt: Pt2D) -> Option<Distance> {
self.lane_center_pts
.dist_along_of_point(pt)
.map(|(dist, _)| dist)
}
pub fn length(&self) -> Distance {
self.lane_center_pts.length()
}
pub fn intersections(&self) -> Vec<IntersectionID> {
vec![self.src_i, self.dst_i]
}
pub fn number_parking_spots(&self, cfg: &MapConfig) -> usize {
assert_eq!(self.lane_type, LaneType::Parking);
let spots = (self.length() / cfg.street_parking_spot_length).floor() - 2.0;
if spots >= 1.0 {
spots as usize
} else {
0
}
}
pub fn is_driving(&self) -> bool {
self.lane_type == LaneType::Driving
}
pub fn is_biking(&self) -> bool {
self.lane_type == LaneType::Biking
}
pub fn is_bus(&self) -> bool {
self.lane_type == LaneType::Bus
}
pub fn is_walkable(&self) -> bool {
self.lane_type.is_walkable()
}
pub fn is_sidewalk(&self) -> bool {
self.lane_type == LaneType::Sidewalk
}
pub fn is_shoulder(&self) -> bool {
self.lane_type == LaneType::Shoulder
}
pub fn is_parking(&self) -> bool {
self.lane_type == LaneType::Parking
}
pub fn is_light_rail(&self) -> bool {
self.lane_type == LaneType::LightRail
}
pub fn get_directed_parent(&self) -> DirectedRoadID {
DirectedRoadID {
road: self.id.road,
dir: self.dir,
}
}
pub fn get_nearest_side_of_road(&self, map: &Map) -> RoadSideID {
let side = match (self.dir, map.get_config().driving_side) {
(Direction::Fwd, DrivingSide::Right) => SideOfRoad::Right,
(Direction::Back, DrivingSide::Right) => SideOfRoad::Left,
(Direction::Fwd, DrivingSide::Left) => SideOfRoad::Left,
(Direction::Back, DrivingSide::Left) => SideOfRoad::Right,
};
RoadSideID {
road: self.id.road,
side,
}
}
pub fn get_lane_level_turn_restrictions(
&self,
road: &Road,
force_bus: bool,
) -> Option<BTreeSet<TurnType>> {
if !self.is_driving() && (!force_bus || !self.is_bus()) {
return None;
}
let all = if self.dir == Direction::Fwd && road.osm_tags.contains_key(osm::ENDPT_FWD) {
road.osm_tags
.get("turn:lanes:forward")
.or_else(|| road.osm_tags.get("turn:lanes"))?
} else if self.dir == Direction::Back && road.osm_tags.contains_key(osm::ENDPT_BACK) {
road.osm_tags.get("turn:lanes:backward")?
} else {
return None;
};
let parts: Vec<&str> = all.split('|').collect();
let lanes: Vec<LaneID> = road
.children(self.dir)
.into_iter()
.filter(|(_, lt)| *lt == LaneType::Driving || *lt == LaneType::Bus)
.map(|(id, _)| id)
.collect();
if parts.len() != lanes.len() {
warn!("{}'s turn restrictions don't match the lanes", road.orig_id);
return None;
}
let part = parts[lanes.iter().position(|l| *l == self.id)?];
if part == "yes" || part == "psv" || part == "bus" {
return None;
}
if part.is_empty() || part == "none" {
let all_explicit_types: BTreeSet<TurnType> = parts
.iter()
.flat_map(|part| part.split(';').flat_map(parse_turn_type_from_osm))
.collect();
let mut implied = BTreeSet::new();
implied.insert(TurnType::Straight);
for tt in [TurnType::Left, TurnType::Right] {
if !all_explicit_types.contains(&tt) {
implied.insert(tt);
}
}
return Some(implied);
}
Some(part.split(';').flat_map(parse_turn_type_from_osm).collect())
}
pub fn common_endpoint(&self, other: &Lane) -> CommonEndpoint {
CommonEndpoint::new((self.src_i, self.dst_i), (other.src_i, other.dst_i))
}
pub fn get_thick_polygon(&self) -> Polygon {
self.lane_center_pts.make_polygons(self.width)
}
}
pub enum CommonEndpoint {
One(IntersectionID),
Both,
None,
}
impl CommonEndpoint {
pub fn new(
obj1: (IntersectionID, IntersectionID),
obj2: (IntersectionID, IntersectionID),
) -> CommonEndpoint {
#![allow(clippy::suspicious_operation_groupings)]
let src = obj1.0 == obj2.0 || obj1.0 == obj2.1;
let dst = obj1.1 == obj2.0 || obj1.1 == obj2.1;
if src && dst {
return CommonEndpoint::Both;
}
if src {
return CommonEndpoint::One(obj1.0);
}
if dst {
return CommonEndpoint::One(obj1.1);
}
CommonEndpoint::None
}
}
impl LaneSpec {
pub fn typical_lane_widths(lt: LaneType, tags: &Tags) -> Vec<(Distance, &'static str)> {
match lt {
LaneType::Driving => {
let mut choices = vec![
(Distance::feet(8.0), "narrow"),
(SERVICE_ROAD_LANE_THICKNESS, "alley"),
(Distance::feet(10.0), "typical"),
(Distance::feet(12.0), "highway"),
];
if tags.is(osm::HIGHWAY, "service") || tags.is("narrow", "yes") {
choices.swap(1, 0);
}
choices
}
LaneType::Biking => vec![
(Distance::meters(2.0), "standard"),
(Distance::meters(1.5), "absolute minimum"),
],
LaneType::Bus => vec![
(Distance::feet(12.0), "normal"),
(Distance::feet(10.0), "minimum"),
],
LaneType::Parking => {
let mut choices = vec![
(Distance::feet(7.0), "narrow"),
(SERVICE_ROAD_LANE_THICKNESS, "alley"),
(Distance::feet(9.0), "wide"),
(Distance::feet(15.0), "loading zone"),
];
if tags.is(osm::HIGHWAY, "service") || tags.is("narrow", "yes") {
choices.swap(1, 0);
}
choices
}
LaneType::SharedLeftTurn => vec![(NORMAL_LANE_THICKNESS, "default")],
LaneType::Construction => vec![(NORMAL_LANE_THICKNESS, "default")],
LaneType::LightRail => vec![(NORMAL_LANE_THICKNESS, "default")],
LaneType::Sidewalk => vec![
(SIDEWALK_THICKNESS, "default"),
(Distance::feet(6.0), "wide"),
],
LaneType::Shoulder => vec![(SHOULDER_THICKNESS, "default")],
LaneType::Buffer(BufferType::Stripes) => vec![(Distance::meters(1.5), "default")],
LaneType::Buffer(BufferType::FlexPosts) => {
vec![(Distance::meters(1.5), "default")]
}
LaneType::Buffer(BufferType::Planters) => {
vec![(Distance::meters(2.0), "default")]
}
LaneType::Buffer(BufferType::JerseyBarrier) => {
vec![(Distance::meters(1.5), "default")]
}
LaneType::Buffer(BufferType::Curb) => vec![(Distance::meters(0.5), "default")],
}
}
pub fn assemble_ltr(
mut fwd_side: Vec<LaneSpec>,
mut back_side: Vec<LaneSpec>,
driving_side: DrivingSide,
) -> Vec<LaneSpec> {
match driving_side {
DrivingSide::Right => {
back_side.reverse();
back_side.extend(fwd_side);
back_side
}
DrivingSide::Left => {
fwd_side.reverse();
fwd_side.extend(back_side);
fwd_side
}
}
}
}
fn parse_turn_type_from_osm(x: &str) -> Vec<TurnType> {
match x {
"left" => vec![TurnType::Left],
"right" => vec![TurnType::Right],
"through" => vec![TurnType::Straight],
"slight_right" | "slight right" | "merge_to_right" | "sharp_right" => {
vec![TurnType::Straight, TurnType::Right]
}
"slight_left" | "slight left" | "merge_to_left" | "sharp_left" => {
vec![TurnType::Straight, TurnType::Left]
}
"reverse" => vec![TurnType::UTurn],
"none" | "" => vec![],
_ => {
warn!("Unknown turn restriction {}", x);
vec![]
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_lane_id_encoding() {
let l = LaneID {
road: RoadID(42),
offset: 3,
};
assert_eq!(l, LaneID::decode_u32(l.encode_u32()));
}
}