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
380
381
382
383
384
385
386
387
388
389
390
use std::collections::{BTreeMap, HashSet, VecDeque};
use std::fmt;
use serde::{Deserialize, Serialize};
use strum::IntoEnumIterator;
use strum_macros::{Display, EnumIter, EnumString};
use abstutil::{
deserialize_btreemap, deserialize_usize, serialize_btreemap, serialize_usize, Tags,
};
use geom::{Distance, PolyLine, Polygon, Pt2D};
use crate::{osm, LaneID, Map, PathConstraints, Position};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct BuildingID(
#[serde(
serialize_with = "serialize_usize",
deserialize_with = "deserialize_usize"
)]
pub usize,
);
impl fmt::Display for BuildingID {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "Building #{}", self.0)
}
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Building {
pub id: BuildingID,
pub polygon: Polygon,
pub levels: f64,
pub address: String,
pub name: Option<NamePerLanguage>,
pub orig_id: osm::OsmID,
pub label_center: Pt2D,
pub amenities: Vec<Amenity>,
pub bldg_type: BuildingType,
pub parking: OffstreetParking,
pub osm_tags: Tags,
pub sidewalk_pos: Position,
pub driveway_geom: PolyLine,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Amenity {
pub names: NamePerLanguage,
pub amenity_type: String,
pub osm_tags: Tags,
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
pub enum OffstreetParking {
PublicGarage(String, usize),
Private(usize, bool),
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum BuildingType {
Residential {
num_residents: usize,
num_housing_units: usize,
},
ResidentialCommercial(usize, usize),
Commercial(usize),
Empty,
}
impl BuildingType {
pub fn has_residents(&self) -> bool {
match self {
BuildingType::Residential { .. } | BuildingType::ResidentialCommercial(_, _) => true,
BuildingType::Commercial(_) | BuildingType::Empty => false,
}
}
}
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
pub struct NamePerLanguage(
#[serde(
serialize_with = "serialize_btreemap",
deserialize_with = "deserialize_btreemap"
)]
pub(crate) BTreeMap<Option<String>, String>,
);
impl NamePerLanguage {
pub fn get(&self, lang: Option<&String>) -> &String {
let lang = lang.cloned();
if let Some(name) = self.0.get(&lang) {
return name;
}
&self.0[&None]
}
pub fn new(tags: &Tags) -> Option<NamePerLanguage> {
let native_name = tags.get(osm::NAME)?;
let mut map = BTreeMap::new();
map.insert(None, native_name.to_string());
for (k, v) in tags.inner() {
if let Some(lang) = k.strip_prefix("name:") {
map.insert(Some(lang.to_string()), v.to_string());
}
}
Some(NamePerLanguage(map))
}
pub fn unnamed() -> NamePerLanguage {
let mut map = BTreeMap::new();
map.insert(None, "unnamed".to_string());
NamePerLanguage(map)
}
}
impl Building {
pub fn sidewalk(&self) -> LaneID {
self.sidewalk_pos.lane()
}
pub fn house_number(&self) -> Option<String> {
let num = self.address.split(' ').next().unwrap();
if num != "???" {
Some(num.to_string())
} else {
None
}
}
pub fn driving_connection(&self, map: &Map) -> Option<(Position, PolyLine)> {
let lane = map
.get_parent(self.sidewalk())
.find_closest_lane(self.sidewalk(), |l| PathConstraints::Car.can_use(l, map))?;
let pos = self
.sidewalk_pos
.equiv_pos(lane, map)
.buffer_dist(Distance::meters(7.0), map)?;
Some((pos, self.driveway_geom.clone().optionally_push(pos.pt(map))))
}
pub fn biking_connection(&self, map: &Map) -> Option<(Position, Position)> {
if let Some(pair) = sidewalk_to_bike(self.sidewalk_pos, map) {
return Some(pair);
}
let mut queue: VecDeque<LaneID> = VecDeque::new();
let mut visited: HashSet<LaneID> = HashSet::new();
queue.push_back(self.sidewalk());
loop {
if queue.is_empty() {
return None;
}
let l = queue.pop_front().unwrap();
if visited.contains(&l) {
continue;
}
visited.insert(l);
if let Some(pair) = sidewalk_to_bike(Position::new(l, map.get_l(l).length() / 2.0), map)
{
return Some(pair);
}
for (_, next) in map.get_next_turns_and_lanes(l) {
if !visited.contains(&next.id) {
queue.push_back(next.id);
}
}
}
}
pub fn num_parking_spots(&self) -> usize {
match self.parking {
OffstreetParking::PublicGarage(_, n) => n,
OffstreetParking::Private(n, _) => n,
}
}
pub fn has_amenity(&self, category: AmenityType) -> bool {
for amenity in &self.amenities {
if AmenityType::categorize(&amenity.amenity_type) == Some(category) {
return true;
}
}
false
}
}
fn sidewalk_to_bike(sidewalk_pos: Position, map: &Map) -> Option<(Position, Position)> {
let lane = map
.get_parent(sidewalk_pos.lane())
.find_closest_lane(sidewalk_pos.lane(), |l| {
!l.biking_blackhole && PathConstraints::Bike.can_use(l, map)
})?;
Some((sidewalk_pos.equiv_pos(lane, map), sidewalk_pos))
}
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, EnumString, Display, EnumIter)]
pub enum AmenityType {
Bank,
Bar,
Beauty,
Bike,
Cafe,
CarRepair,
CarShare,
Childcare,
ConvenienceStore,
Culture,
Exercise,
FastFood,
Food,
GreenSpace,
Hotel,
Laundry,
Library,
Medical,
Pet,
Playground,
Pool,
PostOffice,
Religious,
School,
Shopping,
Supermarket,
Tourism,
University,
}
impl AmenityType {
fn types(self) -> Vec<&'static str> {
match self {
AmenityType::Bank => vec!["bank"],
AmenityType::Bar => vec!["bar", "pub", "nightclub", "biergarten"],
AmenityType::Beauty => vec!["hairdresser", "beauty", "chemist", "cosmetics"],
AmenityType::Bike => vec!["bicycle"],
AmenityType::Cafe => vec!["cafe", "pastry", "coffee", "tea", "bakery"],
AmenityType::CarRepair => vec!["car_repair"],
AmenityType::CarShare => vec!["car_sharing"],
AmenityType::Childcare => vec!["childcare", "kindergarten"],
AmenityType::ConvenienceStore => vec!["convenience"],
AmenityType::Culture => vec!["arts_centre", "art", "cinema", "theatre"],
AmenityType::Exercise => vec!["fitness_centre", "sports_centre", "track", "pitch"],
AmenityType::FastFood => vec!["fast_food", "food_court"],
AmenityType::Food => vec![
"restaurant",
"farm",
"ice_cream",
"seafood",
"cheese",
"chocolate",
"deli",
"butcher",
"confectionery",
"beverages",
"alcohol",
],
AmenityType::GreenSpace => vec!["park", "garden", "nature_reserve"],
AmenityType::Hotel => vec!["hotel", "hostel", "guest_house", "motel"],
AmenityType::Laundry => vec!["dry_cleaning", "laundry", "tailor"],
AmenityType::Library => vec!["library"],
AmenityType::Medical => vec![
"clinic", "dentist", "hospital", "pharmacy", "doctors", "optician",
],
AmenityType::Pet => vec!["veterinary", "pet", "animal_boarding", "pet_grooming"],
AmenityType::Playground => vec!["playground"],
AmenityType::Pool => vec!["swimming_pool"],
AmenityType::PostOffice => vec!["post_office"],
AmenityType::Religious => vec!["place_of_worship", "religion"],
AmenityType::School => vec!["school"],
AmenityType::Shopping => vec![
"wholesale",
"bag",
"marketplace",
"second_hand",
"charity",
"clothes",
"lottery",
"shoes",
"mall",
"department_store",
"car",
"tailor",
"nutrition_supplements",
"watches",
"craft",
"fabric",
"kiosk",
"antiques",
"shoemaker",
"hardware",
"houseware",
"mobile_phone",
"photo",
"toys",
"bed",
"florist",
"electronics",
"fishing",
"garden_centre",
"frame",
"watchmaker",
"boutique",
"mobile_phone",
"party",
"car_parts",
"video",
"video_games",
"musical_instrument",
"music",
"baby_goods",
"doityourself",
"jewelry",
"variety_store",
"gift",
"carpet",
"perfumery",
"curtain",
"appliance",
"furniture",
"lighting",
"sewing",
"books",
"sports",
"travel_agency",
"interior_decoration",
"stationery",
"computer",
"tyres",
"newsagent",
"general",
],
AmenityType::Supermarket => vec!["supermarket", "greengrocer"],
AmenityType::Tourism => vec![
"gallery",
"museum",
"zoo",
"attraction",
"theme_park",
"aquarium",
],
AmenityType::University => vec!["college", "university"],
}
}
pub fn all() -> Vec<AmenityType> {
AmenityType::iter().collect()
}
pub fn categorize(a: &str) -> Option<AmenityType> {
for at in AmenityType::all() {
if at.types().contains(&a) {
return Some(at);
}
}
None
}
}