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
use std::collections::{BTreeMap, BTreeSet};
use abstutil::{Tags, Timer};
use geom::{Bounds, Circle, Distance, PolyLine, Polygon, Pt2D};
use crate::{osm, InputRoad, IntersectionType, OriginalRoad, RawMap};
pub struct InitialMap {
pub roads: BTreeMap<OriginalRoad, Road>,
pub intersections: BTreeMap<osm::NodeID, Intersection>,
pub bounds: Bounds,
}
pub struct Road {
pub id: OriginalRoad,
pub src_i: osm::NodeID,
pub dst_i: osm::NodeID,
pub trimmed_center_pts: PolyLine,
pub half_width: Distance,
pub osm_tags: Tags,
}
impl Road {
pub fn new(map: &RawMap, id: OriginalRoad) -> Road {
let road = &map.roads[&id];
let (trimmed_center_pts, total_width) = road.untrimmed_road_geometry();
Road {
id,
src_i: id.i1,
dst_i: id.i2,
trimmed_center_pts,
half_width: total_width / 2.0,
osm_tags: road.osm_tags.clone(),
}
}
pub(crate) fn to_input_road(&self) -> InputRoad {
InputRoad {
id: self.id,
center_pts: self.trimmed_center_pts.clone(),
half_width: self.half_width,
osm_tags: self.osm_tags.clone(),
}
}
}
pub struct Intersection {
pub id: osm::NodeID,
pub polygon: Polygon,
pub roads: BTreeSet<OriginalRoad>,
pub intersection_type: IntersectionType,
pub elevation: Distance,
}
impl InitialMap {
pub fn new(raw: &RawMap, bounds: &Bounds, timer: &mut Timer) -> InitialMap {
let mut m = InitialMap {
roads: BTreeMap::new(),
intersections: BTreeMap::new(),
bounds: *bounds,
};
for (id, i) in &raw.intersections {
m.intersections.insert(
*id,
Intersection {
id: *id,
polygon: Circle::new(Pt2D::new(0.0, 0.0), Distance::meters(1.0)).to_polygon(),
roads: BTreeSet::new(),
intersection_type: i.intersection_type,
elevation: i.elevation,
},
);
}
for (id, road) in &raw.roads {
let id = *id;
if id.i1 == id.i2 {
panic!("There's a loop {}", id);
}
if PolyLine::new(road.osm_center_points.clone()).is_err() {
panic!("There's broken geom {}", id);
}
m.intersections.get_mut(&id.i1).unwrap().roads.insert(id);
m.intersections.get_mut(&id.i2).unwrap().roads.insert(id);
m.roads.insert(id, Road::new(raw, id));
}
let mut remove_dangling_nodes = Vec::new();
timer.start_iter("find each intersection polygon", m.intersections.len());
for i in m.intersections.values_mut() {
timer.next();
let input_roads = i
.roads
.iter()
.map(|r| m.roads[r].to_input_road())
.collect::<Vec<_>>();
match crate::intersection_polygon(
i.id,
input_roads,
&raw.intersections[&i.id].trim_roads_for_merging,
) {
Ok(results) => {
i.polygon = results.intersection_polygon;
for (r, (pl, _)) in results.trimmed_center_pts {
m.roads.get_mut(&r).unwrap().trimmed_center_pts = pl;
}
}
Err(err) => {
error!("Can't make intersection geometry for {}: {}", i.id, err);
if let Some(r) = i.roads.iter().next() {
let road = &m.roads[r];
let pt = if road.src_i == i.id {
road.trimmed_center_pts.first_pt()
} else {
road.trimmed_center_pts.last_pt()
};
i.polygon = Circle::new(pt, Distance::meters(3.0)).to_polygon();
i.intersection_type = IntersectionType::StopSign;
} else {
remove_dangling_nodes.push(i.id);
}
}
}
}
for i in remove_dangling_nodes {
m.intersections.remove(&i).unwrap();
}
let min_len = Distance::meters(5.0);
for i in m.intersections.values_mut() {
if i.intersection_type != IntersectionType::Border {
continue;
}
let r = m.roads.get_mut(i.roads.iter().next().unwrap()).unwrap();
if r.trimmed_center_pts.length() >= min_len {
continue;
}
if r.dst_i == i.id {
r.trimmed_center_pts = r.trimmed_center_pts.extend_to_length(min_len);
} else {
r.trimmed_center_pts = r
.trimmed_center_pts
.reversed()
.extend_to_length(min_len)
.reversed();
}
let input_roads = i
.roads
.iter()
.map(|r| m.roads[r].to_input_road())
.collect::<Vec<_>>();
let results = crate::intersection_polygon(
i.id,
input_roads,
&raw.intersections[&i.id].trim_roads_for_merging,
)
.unwrap();
i.polygon = results.intersection_polygon;
for (r, (pl, _)) in results.trimmed_center_pts {
m.roads.get_mut(&r).unwrap().trimmed_center_pts = pl;
}
info!(
"Shifted border {} out a bit to make the road a reasonable length",
i.id
);
}
m
}
}