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
use std::collections::BTreeMap;
use abstutil::Timer;
use geom::{PolyLine, Ring};
use raw_map::{osm, IntersectionType, OriginalRoad, RawMap};
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());
map.roads.retain(|_, 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,
);
}
map.buildings.retain(|_, 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");
}
timer.stop("clipping map to boundary");
}