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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use osm::{OsmID, RelationID, WayID};
use abstio::{CityName, MapName};
use abstutil::{Tags, Timer};
use geom::{Distance, FindClosest, Polygon, Pt2D, Ring};
use kml::{ExtraShape, ExtraShapes};
use raw_map::{Amenity, AreaType, RawArea, RawBuilding, RawMap, RawParkingLot};
use street_network::{osm, NamePerLanguage};
use crate::Options;
use import_streets::osm_reader::{get_multipolygon_members, glue_multipolygon, multipoly_geometry};
use import_streets::OsmExtract;
pub fn extract_osm(
map: &mut RawMap,
osm_input_path: &str,
clip_path: Option<String>,
opts: &Options,
timer: &mut Timer,
) -> (OsmExtract, Vec<(Pt2D, Amenity)>) {
let osm_xml = fs_err::read_to_string(osm_input_path).unwrap();
let mut doc =
import_streets::osm_reader::read(&osm_xml, &map.streets.gps_bounds, timer).unwrap();
for id in [380902156, 380902155, 568612970] {
if let Some(way) = doc.ways.get_mut(&WayID(id)) {
way.tags.remove("bicycle");
}
}
if let Some(way) = doc.ways.get_mut(&WayID(332355467)) {
way.tags.insert("junction", "intersection");
}
if let Some(way) = doc.ways.get_mut(&WayID(332060260)) {
way.tags.insert("sidewalk", "right");
}
if let Some(way) = doc.ways.get_mut(&WayID(332060236)) {
way.tags.insert("sidewalk", "right");
}
let infer_both_sidewalks_for_oneways = map.name.city == CityName::new("ch", "geneva");
if clip_path.is_none() {
map.streets.gps_bounds = doc.gps_bounds.clone();
map.streets.boundary_polygon = map.streets.gps_bounds.to_bounds().get_rectangle();
}
let mut out = OsmExtract::new();
let mut amenity_points = Vec::new();
timer.start_iter("processing OSM nodes", doc.nodes.len());
for (id, node) in &doc.nodes {
timer.next();
out.handle_node(*id, node);
for amenity in get_bldg_amenities(&node.tags) {
amenity_points.push((node.pt, amenity));
}
}
let mut extra_footways = ExtraShapes { shapes: Vec::new() };
let mut coastline_groups: Vec<(WayID, Vec<Pt2D>)> = Vec::new();
let mut memorial_areas: Vec<Polygon> = Vec::new();
let mut amenity_areas: Vec<(Polygon, Amenity)> = Vec::new();
timer.start_iter("processing OSM ways", doc.ways.len());
for (id, way) in &mut doc.ways {
timer.next();
let id = *id;
way.tags.insert(osm::OSM_WAY_ID, id.0.to_string());
if out.handle_way(id, &way, opts, infer_both_sidewalks_for_oneways) {
continue;
} else if way.tags.is(osm::HIGHWAY, "service") {
map.parking_aisles.push((id, way.pts.clone()));
} else if way
.tags
.is_any(osm::HIGHWAY, vec!["cycleway", "footway", "path"])
{
extra_footways.shapes.push(ExtraShape {
points: map.streets.gps_bounds.convert_back(&way.pts),
attributes: way.tags.inner().clone(),
});
} else if way.tags.is("natural", "coastline") && !way.tags.is("place", "island") {
coastline_groups.push((id, way.pts.clone()));
continue;
}
let mut deduped = way.pts.clone();
deduped.dedup();
let polygon = if let Ok(ring) = Ring::new(deduped) {
ring.into_polygon()
} else {
continue;
};
if is_bldg(&way.tags) {
map.buildings.insert(
OsmID::Way(id),
RawBuilding {
polygon,
public_garage_name: None,
num_parking_spots: 0,
amenities: get_bldg_amenities(&way.tags),
osm_tags: way.tags.clone(),
},
);
} else if let Some(at) = get_area_type(&way.tags) {
map.areas.push(RawArea {
area_type: at,
osm_id: OsmID::Way(id),
polygon,
osm_tags: way.tags.clone(),
});
} else if way.tags.is("amenity", "parking") {
map.parking_lots.push(RawParkingLot {
osm_id: OsmID::Way(id),
polygon,
osm_tags: way.tags.clone(),
});
} else if way.tags.is("historic", "memorial") {
memorial_areas.push(polygon);
} else if way.tags.contains_key("amenity") {
let amenity = Amenity {
names: NamePerLanguage::new(&way.tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity_type: way.tags.get("amenity").unwrap().clone(),
osm_tags: way.tags.clone(),
};
amenity_areas.push((polygon, amenity));
}
}
if map.name == MapName::seattle("huge_seattle") {
abstio::write_binary(map.name.city.input_path("footways.bin"), &extra_footways);
}
let boundary = map.streets.boundary_polygon.clone().into_ring();
for (id, rel) in &mut doc.relations {
rel.tags.insert(osm::OSM_REL_ID, id.0.to_string());
}
timer.start_iter("processing OSM relations", doc.relations.len());
for (id, rel) in &doc.relations {
timer.next();
let id = *id;
if out.handle_relation(id, rel) {
continue;
} else if let Some(area_type) = get_area_type(&rel.tags) {
if rel.tags.is("type", "multipolygon") {
for polygon in
glue_multipolygon(id, get_multipolygon_members(id, rel, &doc), Some(&boundary))
{
map.areas.push(RawArea {
area_type,
osm_id: OsmID::Relation(id),
polygon,
osm_tags: rel.tags.clone(),
});
}
}
} else if is_bldg(&rel.tags) {
match multipoly_geometry(id, rel, &doc) {
Ok(polygon) => {
map.buildings.insert(
OsmID::Relation(id),
RawBuilding {
polygon,
public_garage_name: None,
num_parking_spots: 0,
amenities: get_bldg_amenities(&rel.tags),
osm_tags: rel.tags.clone(),
},
);
}
Err(err) => println!("Skipping building {}: {}", id, err),
}
} else if rel.tags.is("amenity", "parking") {
for polygon in
glue_multipolygon(id, get_multipolygon_members(id, rel, &doc), Some(&boundary))
{
map.parking_lots.push(RawParkingLot {
osm_id: OsmID::Relation(id),
polygon,
osm_tags: rel.tags.clone(),
});
}
} else if rel.tags.is("type", "multipolygon") && rel.tags.contains_key("amenity") {
let amenity = Amenity {
names: NamePerLanguage::new(&rel.tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity_type: rel.tags.get("amenity").unwrap().clone(),
osm_tags: rel.tags.clone(),
};
for (role, member) in &rel.members {
if role != "outer" {
continue;
}
if let OsmID::Way(w) = member {
if let Ok(ring) = Ring::new(doc.ways[w].pts.clone()) {
amenity_areas.push((ring.into_polygon(), amenity.clone()));
}
}
}
}
}
println!("{} ways of coastline", coastline_groups.len());
for polygon in glue_multipolygon(RelationID(-1), coastline_groups, Some(&boundary)) {
let mut osm_tags = Tags::empty();
osm_tags.insert("water", "ocean");
map.areas.insert(
0,
RawArea {
area_type: AreaType::Water,
osm_id: OsmID::Relation(RelationID(-1)),
polygon,
osm_tags,
},
);
}
timer.start_iter("match buildings to memorial areas", memorial_areas.len());
for area in memorial_areas {
timer.next();
map.buildings
.retain(|_, b| !area.contains_pt(b.polygon.center()));
}
timer.start_iter("match buildings to amenity areas", amenity_areas.len());
for (poly, amenity) in amenity_areas {
timer.next();
for b in map.buildings.values_mut() {
if poly.contains_pt(b.polygon.center()) {
b.amenities.push(amenity.clone());
}
}
}
map.areas.sort_by_key(|a| match a.area_type {
AreaType::Island => 2,
AreaType::Water => 1,
_ => 0,
});
timer.start("find service roads crossing parking lots");
find_parking_aisles(map, &mut out.roads);
timer.stop("find service roads crossing parking lots");
(out, amenity_points)
}
fn is_bldg(tags: &Tags) -> bool {
tags.contains_key("building") && !tags.contains_key("abandoned:man_made")
}
fn get_bldg_amenities(tags: &Tags) -> Vec<Amenity> {
let mut amenities = Vec::new();
for key in ["amenity", "shop", "craft", "office", "tourism", "leisure"] {
if let Some(amenity) = tags.get(key) {
amenities.push(Amenity {
names: NamePerLanguage::new(tags).unwrap_or_else(NamePerLanguage::unnamed),
amenity_type: amenity.clone(),
osm_tags: tags.clone(),
});
}
}
amenities
}
fn get_area_type(tags: &Tags) -> Option<AreaType> {
if tags.is_any("leisure", vec!["garden", "park", "golf_course"]) {
return Some(AreaType::Park);
}
if tags.is_any("natural", vec!["wood", "scrub"]) {
return Some(AreaType::Park);
}
if tags.is_any(
"landuse",
vec![
"cemetery",
"flowerbed",
"forest",
"grass",
"meadow",
"recreation_ground",
"village_green",
],
) || tags.is("amenity", "graveyard")
{
return Some(AreaType::Park);
}
if tags.is("natural", "water") || tags.is("waterway", "riverbank") {
return Some(AreaType::Water);
}
if tags.is("place", "island") {
return Some(AreaType::Island);
}
if tags.is(osm::HIGHWAY, "pedestrian") {
return Some(AreaType::PedestrianPlaza);
}
None
}
fn find_parking_aisles(map: &mut RawMap, roads: &mut Vec<(WayID, Vec<Pt2D>, Tags)>) {
let mut closest: FindClosest<usize> = FindClosest::new(&map.streets.gps_bounds.to_bounds());
for (idx, lot) in map.parking_lots.iter().enumerate() {
closest.add(idx, lot.polygon.points());
}
let mut keep_roads = Vec::new();
let mut parking_aisles = Vec::new();
for (id, pts, osm_tags) in roads.drain(..) {
if !osm_tags.is(osm::HIGHWAY, "service") {
keep_roads.push((id, pts, osm_tags));
continue;
}
let candidates: Vec<usize> = closest
.all_close_pts(Pt2D::center(&pts), Distance::meters(500.0))
.into_iter()
.map(|(idx, _, _)| idx)
.collect();
if service_road_crosses_parking_lot(map, &pts, candidates) {
parking_aisles.push((id, pts));
} else {
keep_roads.push((id, pts, osm_tags));
}
}
roads.extend(keep_roads);
for (id, pts) in parking_aisles {
map.parking_aisles.push((id, pts));
}
}
fn service_road_crosses_parking_lot(map: &RawMap, pts: &[Pt2D], candidates: Vec<usize>) -> bool {
if let Ok((polylines, rings)) = Ring::split_points(pts) {
for pl in polylines {
for idx in &candidates {
if map.parking_lots[*idx].polygon.clip_polyline(&pl).is_some() {
return true;
}
}
}
for ring in rings {
for idx in &candidates {
if map.parking_lots[*idx].polygon.clip_ring(&ring).is_some() {
return true;
}
}
}
}
false
}