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
use std::collections::HashMap;
use abstutil::{Counter, Timer};
use geom::{Distance, HashablePt2D, Pt2D};
use map_model::raw::{OriginalRoad, RawIntersection, RawMap};
use map_model::{osm, Amenity, IntersectionType};
use crate::extract::OsmExtract;
pub fn split_up_roads(
map: &mut RawMap,
mut input: OsmExtract,
timer: &mut Timer,
) -> (Vec<(Pt2D, Amenity)>, HashMap<HashablePt2D, OriginalRoad>) {
timer.start("splitting up roads");
let mut pt_to_intersection: HashMap<HashablePt2D, osm::NodeID> = HashMap::new();
let mut counts_per_pt = Counter::new();
for (_, r) in &input.roads {
for (idx, raw_pt) in r.center_points.iter().enumerate() {
let pt = raw_pt.to_hashable();
let count = counts_per_pt.inc(pt);
if count == 2 || idx == 0 || idx == r.center_points.len() - 1 {
if !pt_to_intersection.contains_key(&pt) {
let id = input.osm_node_ids[&pt];
pt_to_intersection.insert(pt, id);
}
}
}
}
for (pt, id) in &pt_to_intersection {
map.intersections.insert(
*id,
RawIntersection {
point: pt.to_pt2d(),
intersection_type: if input.traffic_signals.remove(pt).is_some() {
IntersectionType::TrafficSignal
} else {
IntersectionType::StopSign
},
elevation: Distance::ZERO,
},
);
}
let mut pt_to_road: HashMap<HashablePt2D, OriginalRoad> = HashMap::new();
timer.start_iter("split roads", input.roads.len());
for (osm_way_id, orig_road) in &input.roads {
timer.next();
let mut r = orig_road.clone();
let mut pts = Vec::new();
let endpt1 = pt_to_intersection[&orig_road.center_points[0].to_hashable()];
let endpt2 = pt_to_intersection[&orig_road.center_points.last().unwrap().to_hashable()];
let mut i1 = endpt1;
for pt in &orig_road.center_points {
pts.push(*pt);
if pts.len() == 1 {
continue;
}
if let Some(i2) = pt_to_intersection.get(&pt.to_hashable()) {
if i1 == endpt1 {
r.osm_tags
.insert(osm::ENDPT_BACK.to_string(), "true".to_string());
}
if *i2 == endpt2 {
r.osm_tags
.insert(osm::ENDPT_FWD.to_string(), "true".to_string());
}
let id = OriginalRoad {
osm_way_id: *osm_way_id,
i1,
i2: *i2,
};
for pt in &pts {
pt_to_road.insert(pt.to_hashable(), id);
}
r.center_points = dedupe_angles(std::mem::replace(&mut pts, Vec::new()));
map.roads.insert(id, r.clone());
r.osm_tags.remove(osm::ENDPT_FWD);
r.osm_tags.remove(osm::ENDPT_BACK);
i1 = *i2;
pts.push(*pt);
}
}
assert!(pts.len() == 1);
}
let mut restrictions = Vec::new();
for (restriction, from_osm, via_osm, to_osm) in input.simple_turn_restrictions {
let roads = map.roads_per_intersection(via_osm);
if let (Some(from), Some(to)) = (
roads.iter().find(|r| r.osm_way_id == from_osm),
roads.iter().find(|r| r.osm_way_id == to_osm),
) {
restrictions.push((*from, restriction, *to));
}
}
for (from, rt, to) in restrictions {
map.roads
.get_mut(&from)
.unwrap()
.turn_restrictions
.push((rt, to));
}
let mut complicated_restrictions = Vec::new();
for (rel_osm, from_osm, via_osm, to_osm) in input.complicated_turn_restrictions {
let via_candidates: Vec<OriginalRoad> = map
.roads
.keys()
.filter(|r| r.osm_way_id == via_osm)
.cloned()
.collect();
if via_candidates.len() != 1 {
timer.warn(format!(
"Couldn't resolve turn restriction from way {} to way {} via way {}. Candidate \
roads for via: {:?}. See {}",
from_osm, to_osm, via_osm, via_candidates, rel_osm
));
continue;
}
let via = via_candidates[0];
let maybe_from = map
.roads_per_intersection(via.i1)
.into_iter()
.chain(map.roads_per_intersection(via.i2).into_iter())
.find(|r| r.osm_way_id == from_osm);
let maybe_to = map
.roads_per_intersection(via.i1)
.into_iter()
.chain(map.roads_per_intersection(via.i2).into_iter())
.find(|r| r.osm_way_id == to_osm);
match (maybe_from, maybe_to) {
(Some(from), Some(to)) => {
complicated_restrictions.push((from, via, to));
}
_ => {
timer.warn(format!(
"Couldn't resolve turn restriction from {} to {} via {:?}",
from_osm, to_osm, via
));
}
}
}
for (from, via, to) in complicated_restrictions {
map.roads
.get_mut(&from)
.unwrap()
.complicated_turn_restrictions
.push((via, to));
}
timer.start("match traffic signals to intersections");
let mut pt_to_road: HashMap<HashablePt2D, OriginalRoad> = HashMap::new();
for (id, r) in &map.roads {
for (idx, pt) in r.center_points.iter().enumerate() {
if idx != 0 && idx != r.center_points.len() - 1 {
pt_to_road.insert(pt.to_hashable(), *id);
}
}
}
for (pt, forwards) in input.traffic_signals {
if let Some(r) = pt_to_road.get(&pt) {
if !map.roads[r].osm_tags.is(osm::HIGHWAY, "construction") {
let i = if forwards { r.i2 } else { r.i1 };
map.intersections.get_mut(&i).unwrap().intersection_type =
IntersectionType::TrafficSignal;
}
}
}
timer.stop("match traffic signals to intersections");
timer.stop("splitting up roads");
(input.amenities, pt_to_road)
}
fn dedupe_angles(pts: Vec<Pt2D>) -> Vec<Pt2D> {
let mut result = Vec::new();
for (idx, pt) in pts.into_iter().enumerate() {
let l = result.len();
if idx == 0 || idx == 1 {
result.push(pt);
} else if result[l - 2]
.angle_to(result[l - 1])
.approx_eq(result[l - 1].angle_to(pt), 0.1)
{
result.pop();
result.push(pt);
} else {
result.push(pt);
}
}
result
}