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
use std::collections::BTreeMap;
use abstutil::{retain_btreemap, Timer};
use geom::{Distance, PolyLine, Ring};
use map_model::raw::{OriginalRoad, RawMap};
use map_model::{osm, IntersectionType};
pub fn clip_map(map: &mut RawMap, timer: &mut Timer) {
timer.start("clipping map to boundary");
let boundary_polygon = map.boundary_polygon.clone();
let boundary_ring = Ring::must_new(boundary_polygon.points().clone());
retain_btreemap(&mut map.roads, |_, r| {
let first_in = boundary_polygon.contains_pt(r.center_points[0]);
let last_in = boundary_polygon.contains_pt(*r.center_points.last().unwrap());
let light_rail_ok = if r.is_light_rail() {
r.center_points
.iter()
.any(|pt| boundary_polygon.contains_pt(*pt))
} else {
false
};
first_in || last_in || light_rail_ok
});
let mut extra_borders: BTreeMap<osm::NodeID, osm::NodeID> = BTreeMap::new();
let road_ids: Vec<OriginalRoad> = map.roads.keys().cloned().collect();
for id in road_ids {
let r = &map.roads[&id];
if map.boundary_polygon.contains_pt(r.center_points[0]) {
continue;
}
let mut move_i = id.i1;
let orig_id = id.i1;
if map
.roads
.keys()
.filter(|r2| r2.i1 == move_i || r2.i2 == move_i)
.count()
> 1
{
let copy = map.intersections[&move_i].clone();
move_i = map.new_osm_node_id(-1);
extra_borders.insert(orig_id, move_i);
map.intersections.insert(move_i, copy);
println!("Disconnecting {} from some other stuff (starting OOB)", id);
}
let i = map.intersections.get_mut(&move_i).unwrap();
i.intersection_type = IntersectionType::Border;
let mut mut_r = map.roads.remove(&id).unwrap();
let center = PolyLine::must_new(mut_r.center_points.clone());
let border_pt = boundary_ring.all_intersections(¢er)[0];
if let Some(pl) = center.reversed().get_slice_ending_at(border_pt) {
mut_r.center_points = pl.reversed().into_points();
} else {
panic!("{} interacts with border strangely", id);
}
i.point = mut_r.center_points[0];
map.roads.insert(
OriginalRoad {
osm_way_id: id.osm_way_id,
i1: move_i,
i2: id.i2,
},
mut_r,
);
}
let road_ids: Vec<OriginalRoad> = map.roads.keys().cloned().collect();
for id in road_ids {
let r = &map.roads[&id];
if map
.boundary_polygon
.contains_pt(*r.center_points.last().unwrap())
{
continue;
}
let mut move_i = id.i2;
let orig_id = id.i2;
if map
.roads
.keys()
.filter(|r2| r2.i1 == move_i || r2.i2 == move_i)
.count()
> 1
{
let copy = map.intersections[&move_i].clone();
move_i = map.new_osm_node_id(-1);
extra_borders.insert(orig_id, move_i);
map.intersections.insert(move_i, copy);
println!("Disconnecting {} from some other stuff (ending OOB)", id);
}
let i = map.intersections.get_mut(&move_i).unwrap();
i.intersection_type = IntersectionType::Border;
let mut mut_r = map.roads.remove(&id).unwrap();
let center = PolyLine::must_new(mut_r.center_points.clone());
let border_pt = boundary_ring.all_intersections(¢er.reversed())[0];
if let Some(pl) = center.get_slice_ending_at(border_pt) {
mut_r.center_points = pl.into_points();
} else {
panic!("{} interacts with border strangely", id);
}
i.point = *mut_r.center_points.last().unwrap();
map.roads.insert(
OriginalRoad {
osm_way_id: id.osm_way_id,
i1: id.i1,
i2: move_i,
},
mut_r,
);
}
retain_btreemap(&mut map.buildings, |_, b| {
b.polygon
.points()
.iter()
.all(|pt| boundary_polygon.contains_pt(*pt))
});
let mut result_areas = Vec::new();
for orig_area in map.areas.drain(..) {
for polygon in map.boundary_polygon.intersection(&orig_area.polygon) {
let mut area = orig_area.clone();
area.polygon = polygon;
result_areas.push(area);
}
}
map.areas = result_areas;
if map.roads.is_empty() {
panic!("There are no roads inside the clipping polygon");
}
let all_routes = map.bus_routes.drain(..).collect::<Vec<_>>();
for mut r in all_routes {
if r.stops[0].vehicle_pos == r.stops.last().unwrap().vehicle_pos {
map.bus_routes.push(r);
continue;
}
let mut borders: Vec<osm::NodeID> = Vec::new();
for (node, _) in &r.all_pts {
if let Some(i) = map.intersections.get(node) {
if i.intersection_type == IntersectionType::Border {
borders.push(*node);
}
}
if let Some(i) = extra_borders.get(node) {
borders.push(*i);
}
}
let start_i = map.closest_intersection(r.stops[0].vehicle_pos.1);
let end_i = map.closest_intersection(r.stops.last().unwrap().vehicle_pos.1);
let mut best_start: Option<(osm::NodeID, Distance)> = None;
let mut best_end: Option<(osm::NodeID, Distance)> = None;
for i in borders {
if let Some(d1) = map
.path_dist_to(i, start_i)
.or_else(|| map.path_dist_to(start_i, i))
{
if best_start.map(|(_, d2)| d1 < d2).unwrap_or(true) {
best_start = Some((i, d1));
}
}
if let Some(d1) = map.path_dist_to(end_i, i) {
if best_end.map(|(_, d2)| d1 < d2).unwrap_or(true) {
best_end = Some((i, d1));
}
}
}
match (best_start, best_end) {
(Some((i1, d1)), Some((i2, d2))) => {
if i1 == i2 {
if d1 < d2 {
r.border_start = Some(i1);
} else {
r.border_end = Some(i2);
}
} else {
r.border_start = Some(i1);
r.border_end = Some(i2);
}
}
(Some((i1, _)), None) => {
r.border_start = Some(i1);
}
(None, Some((i2, _))) => {
r.border_end = Some(i2);
}
(None, None) => {}
}
map.bus_routes.push(r);
}
timer.stop("clipping map to boundary");
}