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
use anyhow::Result;
use geom::{PolyLine, Polygon, Pt2D, Ring};
use map_model::osm::{OsmID, RelationID, WayID};
use crate::reader::{Document, Relation};
pub fn get_multipolygon_members(
id: RelationID,
rel: &Relation,
doc: &Document,
) -> Vec<(WayID, Vec<Pt2D>)> {
let mut pts_per_way = Vec::new();
for (role, member) in &rel.members {
if let OsmID::Way(w) = member {
if role == "outer" {
pts_per_way.push((*w, doc.ways[w].pts.clone()));
} else {
println!("{} has unhandled member role {}, ignoring it", id, role);
}
}
}
pts_per_way
}
pub fn glue_multipolygon(
rel_id: RelationID,
mut pts_per_way: Vec<(WayID, Vec<Pt2D>)>,
boundary: Option<&Ring>,
) -> Vec<Polygon> {
let mut polygons: Vec<Polygon> = Vec::new();
pts_per_way.retain(|(_, pts)| {
if let Ok(ring) = Ring::new(pts.clone()) {
polygons.push(ring.to_polygon());
false
} else {
true
}
});
if pts_per_way.is_empty() {
return polygons;
}
let (_, mut result) = pts_per_way.pop().unwrap();
let mut reversed = false;
while !pts_per_way.is_empty() {
let glue_pt = *result.last().unwrap();
if let Some(idx) = pts_per_way
.iter()
.position(|(_, pts)| pts[0] == glue_pt || *pts.last().unwrap() == glue_pt)
{
let (_, mut append) = pts_per_way.remove(idx);
if append[0] != glue_pt {
append.reverse();
}
result.pop();
result.extend(append);
} else {
if reversed {
println!(
"Throwing away {} chunks from relation {}: {:?}",
pts_per_way.len(),
rel_id,
pts_per_way.iter().map(|(id, _)| *id).collect::<Vec<_>>()
);
break;
} else {
reversed = true;
result.reverse();
}
}
}
result.dedup();
if let Ok(ring) = Ring::new(result.clone()) {
polygons.push(ring.to_polygon());
return polygons;
}
if result.len() < 2 {
return Vec::new();
}
match PolyLine::new(result.clone()) {
Ok(pl) => {
if let Some(poly) = boundary.and_then(|boundary| glue_to_boundary(pl, boundary)) {
polygons.push(poly);
} else {
result.push(result[0]);
polygons.push(Ring::must_new(result).to_polygon());
}
}
Err(err) => {
error!("Really weird multipolygon {}: {}", rel_id, err);
}
}
polygons
}
fn glue_to_boundary(result_pl: PolyLine, boundary: &Ring) -> Option<Polygon> {
let hits = boundary.all_intersections(&result_pl);
if hits.len() != 2 {
return None;
}
let trimmed_result = result_pl.trim_to_endpts(hits[0], hits[1]);
let boundary_glue = boundary.get_shorter_slice_btwn(hits[0], hits[1]).unwrap();
let mut trimmed_pts = trimmed_result.points().clone();
if trimmed_result.last_pt() == boundary_glue.first_pt() {
trimmed_pts.pop();
trimmed_pts.extend(boundary_glue.into_points());
} else {
assert_eq!(trimmed_result.last_pt(), boundary_glue.last_pt());
trimmed_pts.pop();
trimmed_pts.extend(boundary_glue.reversed().into_points());
}
Some(Ring::must_new(trimmed_pts).to_polygon())
}
pub fn multipoly_geometry(rel_id: RelationID, rel: &Relation, doc: &Document) -> Result<Polygon> {
let mut outer: Vec<Vec<Pt2D>> = Vec::new();
let mut inner: Vec<Vec<Pt2D>> = Vec::new();
for (role, member) in &rel.members {
if let OsmID::Way(w) = member {
let mut deduped = doc.ways[w].pts.clone();
deduped.dedup();
if deduped.len() < 3 {
continue;
}
if role == "outer" {
outer.push(deduped);
} else if role == "inner" {
inner.push(deduped);
} else {
bail!("What's role {} for multipolygon {}?", role, rel_id);
}
}
}
if outer.len() == 0 || outer.len() > 1 && !inner.is_empty() {
bail!(
"Multipolygon {} has {} outer, {} inner. Huh?",
rel_id,
outer.len(),
inner.len()
);
}
if inner.is_empty() {
if outer.len() > 1 {
Ok(Polygon::union_all(
outer.into_iter().map(Polygon::buggy_new).collect(),
))
} else {
Ok(Polygon::buggy_new(outer.remove(0)))
}
} else {
let mut inner_rings = Vec::new();
for pts in inner {
inner_rings.push(Ring::new(pts)?);
}
Ok(Polygon::with_holes(
Ring::new(outer.pop().unwrap())?,
inner_rings,
))
}
}