Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / mamaps_build / src / schema / buildings.rs
buildings.rs496 lines · 20.7 KiBRaw
| 1 | //! `buildings`: the densest layer in the schema, and the simplest to classify. |
| 2 | //! |
| 3 | //! Two kinds and almost no judgement — `building=*` is a building unless it says `no`. What makes it |
| 4 | //! the expensive layer is volume: a city tile holds thousands, and California holds millions. So the |
| 5 | //! style keeps it off below z14 and the schema does not carry it at all above that floor, which is |
| 6 | //! the same decision made twice for two different reasons — the style's is a draw-call cost, this |
| 7 | //! one is archive bytes. |
| 8 | //! |
| 9 | //! `building:part` is a separate kind because a 3D model tags the parts of a building separately |
| 10 | //! from its footprint, and drawing both means drawing the courtyard of a building twice. |
| 11 | |
| 12 | use tilecodec::mamaps::body::{ |
| 13 | BuildingAttrs, ROOF_DOME, ROOF_FLAT, ROOF_GABLED, ROOF_HIPPED, ROOF_ORIENT_ACROSS, |
| 14 | ROOF_ORIENT_ALONG, ROOF_PYRAMIDAL, ROOF_SKILLION, |
| 15 | }; |
| 16 | use tilecodec::mamaps::dict::LAYER_BUILDINGS; |
| 17 | |
| 18 | use super::{kind, Class, TagSource}; |
| 19 | |
| 20 | /// The pre-screen. |
| 21 | /// |
| 22 | /// A superset of the keys the rules and [`attrs`] read: `building`/`building:part` decide the |
| 23 | /// classification, and the rest carry the S3DB attributes [`attrs`] parses. Listing the attribute |
| 24 | /// keys keeps a building's height and roof tags through the `osmium tags-filter` extract step, so |
| 25 | /// they are still present when [`attrs`] runs — a building that passed the screen on `building` |
| 26 | /// alone would otherwise arrive stripped of everything the 3D extrusion needs. |
| 27 | pub const FILTERS: &[&str] = &[ |
| 28 | "building", |
| 29 | "building:part", |
| 30 | "height", |
| 31 | "building:levels", |
| 32 | "building:min_level", |
| 33 | "min_height", |
| 34 | "roof:shape", |
| 35 | "roof:height", |
| 36 | "roof:levels", |
| 37 | "roof:direction", |
| 38 | "roof:orientation", |
| 39 | "building:colour", |
| 40 | "roof:colour", |
| 41 | "building:material", |
| 42 | "roof:material", |
| 43 | ]; |
| 44 | |
| 45 | /// Every `kind` this module can emit. |
| 46 | #[cfg_attr(not(test), allow(dead_code))] |
| 47 | pub const KINDS: &[&str] = &["building", "building_part"]; |
| 48 | |
| 49 | /// The shallowest zoom a building is worth carrying at. |
| 50 | /// |
| 51 | /// Matches the style's own floor for the `buildings` layer, so the archive holds nothing the |
| 52 | /// renderer would not draw. The two are independent decisions that happen to agree, and |
| 53 | /// `the_schema_floor_matches_the_styles` in the crate root is what keeps them agreeing. |
| 54 | pub const MIN_ZOOM: u8 = 14; |
| 55 | |
| 56 | /// The smallest drawn building footprint worth carrying, in square pixels of a 256-unit tile. |
| 57 | /// |
| 58 | /// `2.0`: a footprint smaller than two display pixels at z14 (Phase C floors — sub-2px |
| 59 | /// sheds/garages). Converted by [`super::landtype::min_area_units`] to 512 square extent units |
| 60 | /// at extent 4096 (a ~23x23-unit square, ~13.5 m at z14) — a speck rather than detail, and more |
| 61 | /// aggressive than the floor the schema applies to the smallest z14 `landtype` kinds (`pitch`, |
| 62 | /// `playground`, `platform`). Whole-number because [`crate::store`] packs this field as a `u8` |
| 63 | /// and refuses anything fractional rather than rounding a threshold silently. Carried through |
| 64 | /// the existing `min_area_px` plumbing so [`crate::tiler`] needs no building branch for the |
| 65 | /// drop itself. |
| 66 | /// |
| 67 | /// The prototype at `analysis/mamaps_building_savings.py` estimates dropping the smallest ~15% |
| 68 | /// of footprints; this floor is at least that aggressive, on the same z14-only layer (9.76M |
| 69 | /// features, ~20% of the archive). |
| 70 | pub const MIN_AREA_PX: f64 = 2.0; |
| 71 | |
| 72 | pub fn classify(tags: &(impl TagSource + ?Sized)) -> Option<Class> { |
| 73 | // A part first: a way carrying both is a part of a larger footprint, and drawing it as a |
| 74 | // footprint too would double-paint the courtyard. |
| 75 | if tags.truthy("building:part") { |
| 76 | return Some(Class { min_area_px: MIN_AREA_PX, ..Class::area(LAYER_BUILDINGS, kind("building_part"), MIN_ZOOM) }); |
| 77 | } |
| 78 | if tags.truthy("building") { |
| 79 | return Some(Class { min_area_px: MIN_AREA_PX, ..Class::area(LAYER_BUILDINGS, kind("building"), MIN_ZOOM) }); |
| 80 | } |
| 81 | None |
| 82 | } |
| 83 | |
| 84 | /// Roughly the height of one storey, in metres. OSM's own default for `building:levels` when a |
| 85 | /// building carries no explicit `height`, and what the S3DB wiki suggests. Rooms are ~2.5 m but a |
| 86 | /// storey includes floor and ceiling, so 3 m is the conventional figure the renderer expects back. |
| 87 | const METRES_PER_LEVEL: f64 = 3.0; |
| 88 | |
| 89 | /// The S3DB attributes of a building, parsed from its tags — the build half of the 3D extrusion. |
| 90 | /// |
| 91 | /// Called by [`crate::extract`] on any feature the classifier put in [`LAYER_BUILDINGS`], for both |
| 92 | /// `building` footprints and `building:part` parts. Every field falls back gracefully: a building |
| 93 | /// with no S3DB tags at all yields [`BuildingAttrs::default`], which the renderer extrudes as a |
| 94 | /// flat box at its own default height. Heights are stored in decimetres (0.1 m); directions are |
| 95 | /// quantised to a byte; colours resolve a CSS/named colour or, failing that, a `*:material`. |
| 96 | pub fn attrs(tags: &(impl TagSource + ?Sized)) -> BuildingAttrs { |
| 97 | // `height` wins over `building:levels`; a building with neither leaves 0 for the renderer's |
| 98 | // own default rather than guessing a storey count. |
| 99 | let height = metres_dm(tags, "height") |
| 100 | .or_else(|| levels_dm(tags, "building:levels")) |
| 101 | .unwrap_or(0); |
| 102 | let min_height = metres_dm(tags, "min_height") |
| 103 | .or_else(|| levels_dm(tags, "building:min_level")) |
| 104 | .unwrap_or(0); |
| 105 | let roof_height = |
| 106 | metres_dm(tags, "roof:height").or_else(|| levels_dm(tags, "roof:levels")).unwrap_or(0); |
| 107 | let building_colour = tags |
| 108 | .get("building:colour") |
| 109 | .and_then(parse_colour) |
| 110 | .or_else(|| tags.get("building:material").and_then(material_colour)) |
| 111 | .unwrap_or(0); |
| 112 | let roof_colour = tags |
| 113 | .get("roof:colour") |
| 114 | .and_then(parse_colour) |
| 115 | .or_else(|| tags.get("roof:material").and_then(material_colour)) |
| 116 | .unwrap_or(0); |
| 117 | BuildingAttrs { |
| 118 | height, |
| 119 | min_height, |
| 120 | roof_height, |
| 121 | roof_shape: roof_shape(tags.get("roof:shape")), |
| 122 | roof_direction: roof_direction(tags.get("roof:direction")), |
| 123 | roof_orientation: match tags.get("roof:orientation") { |
| 124 | Some("across") => ROOF_ORIENT_ACROSS, |
| 125 | _ => ROOF_ORIENT_ALONG, |
| 126 | }, |
| 127 | building_colour, |
| 128 | roof_colour, |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// A metre value (`12`, `12.5`, `12 m`) as decimetres, clamped to what a `u16` holds (6553.5 m, |
| 133 | /// past the tallest building on earth). `None` when the tag is absent or not a number. |
| 134 | fn metres_dm(tags: &(impl TagSource + ?Sized), key: &str) -> Option<u16> { |
| 135 | let raw = tags.get(key)?; |
| 136 | let metres = parse_leading_f64(raw)?; |
| 137 | super::buildings_extra::dm_of(metres) |
| 138 | } |
| 139 | |
| 140 | /// A level count (`building:levels=3`) as a height in decimetres, at [`METRES_PER_LEVEL`] each. |
| 141 | fn levels_dm(tags: &(impl TagSource + ?Sized), key: &str) -> Option<u16> { |
| 142 | let levels = parse_leading_f64(tags.get(key)?)?; |
| 143 | super::buildings_extra::dm_of(levels * METRES_PER_LEVEL) |
| 144 | } |
| 145 | |
| 146 | /// The leading number of a value, ignoring a trailing unit like ` m`. OSM height tags are metres |
| 147 | /// by convention; a stray `ft` is rare enough to accept the bare number rather than convert. |
| 148 | fn parse_leading_f64(raw: &str) -> Option<f64> { |
| 149 | let trimmed = raw.trim(); |
| 150 | let end = trimmed |
| 151 | .find(|c: char| !(c.is_ascii_digit() || c == '.' || c == '-' || c == '+')) |
| 152 | .unwrap_or(trimmed.len()); |
| 153 | trimmed[..end].parse::<f64>().ok() |
| 154 | } |
| 155 | |
| 156 | /// An OSM `roof:shape` mapped to one of the six the renderer models. Anything else — round, |
| 157 | /// gambrel, mansard, sawtooth — falls back to flat, which is the plan's stated behaviour: a shape |
| 158 | /// the extruder cannot build is drawn as a flat cap rather than guessed at. |
| 159 | fn roof_shape(value: Option<&str>) -> u8 { |
| 160 | match value { |
| 161 | Some("gabled") => ROOF_GABLED, |
| 162 | Some("hipped") => ROOF_HIPPED, |
| 163 | Some("pyramidal") => ROOF_PYRAMIDAL, |
| 164 | Some("skillion") => ROOF_SKILLION, |
| 165 | Some("dome") => ROOF_DOME, |
| 166 | _ => ROOF_FLAT, |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | /// A `roof:direction` (degrees, or a compass point like `NE`) quantised to a byte: `deg * 256 / |
| 171 | /// 360`, which the renderer decodes as `v * 360 / 256`. Zero (north) for an absent or unparseable |
| 172 | /// value, which is the same default a flat roof carries anyway. |
| 173 | fn roof_direction(value: Option<&str>) -> u8 { |
| 174 | let Some(raw) = value else { return 0 }; |
| 175 | let degrees = compass_degrees(raw.trim()).or_else(|| parse_leading_f64(raw)).unwrap_or(0.0); |
| 176 | // Wrap into 0..360 before quantising, so 720 and -90 land where a compass would put them. |
| 177 | let wrapped = degrees.rem_euclid(360.0); |
| 178 | ((wrapped / 360.0 * 256.0).round() as i64).rem_euclid(256) as u8 |
| 179 | } |
| 180 | |
| 181 | /// The eight- and sixteen-point compass abbreviations OSM allows in a direction tag, in degrees. |
| 182 | fn compass_degrees(raw: &str) -> Option<f64> { |
| 183 | let d = match raw.to_ascii_uppercase().as_str() { |
| 184 | "N" => 0.0, |
| 185 | "NNE" => 22.5, |
| 186 | "NE" => 45.0, |
| 187 | "ENE" => 67.5, |
| 188 | "E" => 90.0, |
| 189 | "ESE" => 112.5, |
| 190 | "SE" => 135.0, |
| 191 | "SSE" => 157.5, |
| 192 | "S" => 180.0, |
| 193 | "SSW" => 202.5, |
| 194 | "SW" => 225.0, |
| 195 | "WSW" => 247.5, |
| 196 | "W" => 270.0, |
| 197 | "WNW" => 292.5, |
| 198 | "NW" => 315.0, |
| 199 | "NNW" => 337.5, |
| 200 | _ => return None, |
| 201 | }; |
| 202 | Some(d) |
| 203 | } |
| 204 | |
| 205 | /// A CSS/OSM colour — `#rgb`, `#rrggbb`, or a common colour name — as opaque `0xAARRGGBB`. |
| 206 | /// `None` for anything unrecognised, so the caller can fall back to a material. |
| 207 | fn parse_colour(raw: &str) -> Option<u32> { |
| 208 | let s = raw.trim(); |
| 209 | if let Some(hex) = s.strip_prefix('#') { |
| 210 | let rgb = match hex.len() { |
| 211 | 3 => { |
| 212 | let mut v = 0u32; |
| 213 | for c in hex.chars() { |
| 214 | let n = c.to_digit(16)?; |
| 215 | // Each nibble is doubled: #abc is #aabbcc. |
| 216 | v = (v << 8) | (n * 17); |
| 217 | } |
| 218 | v |
| 219 | } |
| 220 | 6 => u32::from_str_radix(hex, 16).ok()?, |
| 221 | _ => return None, |
| 222 | }; |
| 223 | return Some(0xFF00_0000 | rgb); |
| 224 | } |
| 225 | named_colour(&s.to_ascii_lowercase()).map(|rgb| 0xFF00_0000 | rgb) |
| 226 | } |
| 227 | |
| 228 | /// The common CSS/OSM colour names that actually appear on buildings, as `0xRRGGBB`. |
| 229 | /// |
| 230 | /// Not the full 147-name CSS set: the long tail never appears on a `building:colour`, and a name |
| 231 | /// this misses falls through to the material palette or to "no colour", both of which are fine. |
| 232 | fn named_colour(name: &str) -> Option<u32> { |
| 233 | let rgb = match name { |
| 234 | "white" => 0xFFFFFF, |
| 235 | "black" => 0x000000, |
| 236 | "gray" | "grey" => 0x808080, |
| 237 | "silver" => 0xC0C0C0, |
| 238 | "lightgray" | "lightgrey" => 0xD3D3D3, |
| 239 | "darkgray" | "darkgrey" => 0xA9A9A9, |
| 240 | "red" => 0xFF0000, |
| 241 | "darkred" => 0x8B0000, |
| 242 | "maroon" => 0x800000, |
| 243 | "brown" => 0xA52A2A, |
| 244 | "sienna" => 0xA0522D, |
| 245 | "orange" => 0xFFA500, |
| 246 | "yellow" => 0xFFFF00, |
| 247 | "gold" => 0xFFD700, |
| 248 | "beige" => 0xF5F5DC, |
| 249 | "cream" => 0xFFFDD0, |
| 250 | "tan" => 0xD2B48C, |
| 251 | "green" => 0x008000, |
| 252 | "darkgreen" => 0x006400, |
| 253 | "olive" => 0x808000, |
| 254 | "lightgreen" => 0x90EE90, |
| 255 | "blue" => 0x0000FF, |
| 256 | "lightblue" => 0xADD8E6, |
| 257 | "navy" => 0x000080, |
| 258 | "cyan" => 0x00FFFF, |
| 259 | "teal" => 0x008080, |
| 260 | "purple" => 0x800080, |
| 261 | "pink" => 0xFFC0CB, |
| 262 | "terracotta" => 0xE2725B, |
| 263 | _ => return None, |
| 264 | }; |
| 265 | Some(rgb) |
| 266 | } |
| 267 | |
| 268 | /// A `building:material` or `roof:material` mapped to a representative colour, as opaque |
| 269 | /// `0xAARRGGBB`. The palette a renderer falls back to when a building states its material but not |
| 270 | /// its colour, which is the common case on 3D-mapped cities. |
| 271 | fn material_colour(material: &str) -> Option<u32> { |
| 272 | let rgb = match material.trim().to_ascii_lowercase().as_str() { |
| 273 | "brick" | "brick_wall" => 0xB0_4A_39, |
| 274 | "concrete" | "cement_block" => 0xB0_B0_B0, |
| 275 | "glass" | "mirror" => 0xB8_D0_E0, |
| 276 | "wood" | "timber_framing" => 0x9B_6B_43, |
| 277 | "stone" | "sandstone" | "limestone" => 0xC8_BE_A8, |
| 278 | "marble" => 0xF0_EE_E6, |
| 279 | "metal" | "metal_sheet" | "steel" => 0xB4_B8_BC, |
| 280 | "copper" => 0x7A_9E_8E, |
| 281 | "plaster" | "stucco" | "render" => 0xE8_E0_D0, |
| 282 | "roof_tiles" | "tile" | "tiles" | "clay" => 0xB5_6A_4A, |
| 283 | "slate" => 0x50_54_5A, |
| 284 | "tar_paper" | "asphalt" | "bitumen" => 0x3A_3A_3E, |
| 285 | "grass" | "green_roof" => 0x6E_8B_3D, |
| 286 | "gravel" => 0x9A_94_88, |
| 287 | _ => return None, |
| 288 | }; |
| 289 | Some(0xFF00_0000 | rgb) |
| 290 | } |
| 291 | |
| 292 | #[cfg(test)] |
| 293 | mod tests { |
| 294 | use super::*; |
| 295 | use tilecodec::mamaps::dict; |
| 296 | |
| 297 | fn classify_tags(pairs: &[(&str, &str)]) -> Option<Class> { |
| 298 | super::classify(pairs) |
| 299 | } |
| 300 | |
| 301 | fn kind_name(class: &Class) -> &'static str { |
| 302 | dict::KINDS[class.kind as usize - 1] |
| 303 | } |
| 304 | |
| 305 | #[test] |
| 306 | fn any_building_value_but_no_is_a_building() { |
| 307 | // OSM tags a building's *type* in the same key, so the value is not a yes/no in practice: |
| 308 | // `building=house`, `building=church`, `building=yes` are all buildings. |
| 309 | for value in ["yes", "house", "church", "industrial", "retail", "1", "true"] { |
| 310 | let class = classify_tags(&[("building", value)]).expect(value); |
| 311 | assert_eq!(class.layer, dict::LAYER_BUILDINGS); |
| 312 | assert_eq!(kind_name(&class), "building"); |
| 313 | assert!(class.area, "a building is an area"); |
| 314 | assert_eq!(class.min_zoom, MIN_ZOOM); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | #[test] |
| 319 | fn building_no_is_not_a_building() { |
| 320 | // A real tag: `building=no` marks a way that looks like a building and is not. |
| 321 | assert!(classify_tags(&[("building", "no")]).is_none()); |
| 322 | assert!(classify_tags(&[("building", "false")]).is_none()); |
| 323 | assert!(classify_tags(&[("building", "0")]).is_none()); |
| 324 | } |
| 325 | |
| 326 | /// A 3D model tags parts separately from the footprint. Both are drawn, so they need different |
| 327 | /// kinds — otherwise a building with parts paints its courtyard twice. |
| 328 | #[test] |
| 329 | fn a_part_is_its_own_kind_and_wins_over_the_footprint() { |
| 330 | let part = classify_tags(&[("building:part", "yes")]).expect("part"); |
| 331 | assert_eq!(kind_name(&part), "building_part"); |
| 332 | let both = classify_tags(&[("building", "yes"), ("building:part", "yes")]).expect("both"); |
| 333 | assert_eq!(kind_name(&both), "building_part", "the part rule is asked first"); |
| 334 | } |
| 335 | |
| 336 | #[test] |
| 337 | fn nothing_else_is_a_building() { |
| 338 | for tags in [ |
| 339 | vec![("natural", "water")], |
| 340 | vec![("highway", "residential")], |
| 341 | vec![("building:levels", "3")], |
| 342 | vec![], |
| 343 | ] { |
| 344 | assert!(classify_tags(&tags).is_none(), "{tags:?} should not be a building"); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | /// The densest layer in the schema stays off the mid zooms. Without a floor, a z10 tile of a |
| 349 | /// city is megabytes of footprints nothing draws. |
| 350 | #[test] |
| 351 | fn buildings_are_not_carried_below_street_zoom() { |
| 352 | assert_eq!(MIN_ZOOM, 14); |
| 353 | let class = classify_tags(&[("building", "yes")]).expect("building"); |
| 354 | assert_eq!(class.min_zoom, 14); |
| 355 | } |
| 356 | |
| 357 | /// The densest z14 layer drops what no screen can show: below two display pixels a |
| 358 | /// footprint is a speck (a sub-2px shed or garage), not detail. Both kinds carry it — a part |
| 359 | /// is still a footprint — and it is a whole number because the store packs this field as a |
| 360 | /// `u8` and refuses fractions rather than rounding a threshold silently. |
| 361 | #[test] |
| 362 | fn buildings_carry_a_two_pixel_minimum_area() { |
| 363 | assert_eq!(MIN_AREA_PX, 2.0); |
| 364 | for tags in [vec![("building", "yes")], vec![("building:part", "yes")]] { |
| 365 | let class = classify_tags(&tags).expect("building"); |
| 366 | assert_eq!(class.min_area_px, MIN_AREA_PX, "{tags:?}"); |
| 367 | } |
| 368 | // Two display pixels, expressed in a 4096-unit tile: a ~23x23-unit square. |
| 369 | assert_eq!(crate::schema::landtype::min_area_units(MIN_AREA_PX, 4096), 512.0); |
| 370 | } |
| 371 | |
| 372 | fn attrs_of(pairs: &[(&str, &str)]) -> BuildingAttrs { |
| 373 | super::attrs(pairs) |
| 374 | } |
| 375 | |
| 376 | #[test] |
| 377 | fn a_plain_building_has_default_attrs() { |
| 378 | // No S3DB tags at all: a flat box at the renderer's own default height, no colours. |
| 379 | let a = attrs_of(&[("building", "yes")]); |
| 380 | assert_eq!(a, BuildingAttrs::default()); |
| 381 | assert_eq!(a.height, 0, "0 means the renderer picks a default"); |
| 382 | assert_eq!(a.roof_shape, ROOF_FLAT); |
| 383 | } |
| 384 | |
| 385 | #[test] |
| 386 | fn height_is_metres_in_decimetres_and_levels_are_the_fallback() { |
| 387 | // An explicit height wins and carries its fraction, stored as decimetres. |
| 388 | assert_eq!(attrs_of(&[("building", "yes"), ("height", "12.5")]).height, 125); |
| 389 | // A trailing unit is ignored — the number is metres by convention. |
| 390 | assert_eq!(attrs_of(&[("building", "yes"), ("height", "20 m")]).height, 200); |
| 391 | // Levels are the fallback at 3 m each, only when there is no explicit height. |
| 392 | assert_eq!(attrs_of(&[("building", "yes"), ("building:levels", "4")]).height, 120); |
| 393 | let both = attrs_of(&[("building", "yes"), ("height", "9"), ("building:levels", "4")]); |
| 394 | assert_eq!(both.height, 90, "an explicit height wins over the level count"); |
| 395 | } |
| 396 | |
| 397 | #[test] |
| 398 | fn min_height_and_roof_height_parse_with_their_level_fallbacks() { |
| 399 | let a = attrs_of(&[ |
| 400 | ("building:part", "yes"), |
| 401 | ("min_height", "8"), |
| 402 | ("roof:height", "3.5"), |
| 403 | ]); |
| 404 | assert_eq!(a.min_height, 80); |
| 405 | assert_eq!(a.roof_height, 35); |
| 406 | let levels = attrs_of(&[ |
| 407 | ("building:part", "yes"), |
| 408 | ("building:min_level", "2"), |
| 409 | ("roof:levels", "1"), |
| 410 | ]); |
| 411 | assert_eq!(levels.min_height, 60, "two levels up at 3 m each"); |
| 412 | assert_eq!(levels.roof_height, 30); |
| 413 | } |
| 414 | |
| 415 | #[test] |
| 416 | fn roof_shapes_map_and_unknown_falls_back_to_flat() { |
| 417 | for (tag, want) in [ |
| 418 | ("flat", ROOF_FLAT), |
| 419 | ("gabled", ROOF_GABLED), |
| 420 | ("hipped", ROOF_HIPPED), |
| 421 | ("pyramidal", ROOF_PYRAMIDAL), |
| 422 | ("skillion", ROOF_SKILLION), |
| 423 | ("dome", ROOF_DOME), |
| 424 | // Shapes the extruder does not model fall back to flat rather than guessing. |
| 425 | ("gambrel", ROOF_FLAT), |
| 426 | ("mansard", ROOF_FLAT), |
| 427 | ("round", ROOF_FLAT), |
| 428 | ] { |
| 429 | assert_eq!( |
| 430 | attrs_of(&[("building", "yes"), ("roof:shape", tag)]).roof_shape, |
| 431 | want, |
| 432 | "roof:shape={tag}", |
| 433 | ); |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | #[test] |
| 438 | fn roof_direction_takes_degrees_and_compass_points() { |
| 439 | // 90° east quantises to a quarter of 256. |
| 440 | assert_eq!(attrs_of(&[("building", "yes"), ("roof:direction", "90")]).roof_direction, 64); |
| 441 | assert_eq!(attrs_of(&[("building", "yes"), ("roof:direction", "E")]).roof_direction, 64); |
| 442 | assert_eq!(attrs_of(&[("building", "yes"), ("roof:direction", "N")]).roof_direction, 0); |
| 443 | // A value past 360 wraps like a compass rather than clamping. |
| 444 | assert_eq!(attrs_of(&[("building", "yes"), ("roof:direction", "450")]).roof_direction, 64); |
| 445 | assert_eq!( |
| 446 | attrs_of(&[("building", "yes"), ("roof:orientation", "across")]).roof_orientation, |
| 447 | ROOF_ORIENT_ACROSS, |
| 448 | ); |
| 449 | assert_eq!( |
| 450 | attrs_of(&[("building", "yes")]).roof_orientation, |
| 451 | ROOF_ORIENT_ALONG, |
| 452 | "the default orientation is along", |
| 453 | ); |
| 454 | } |
| 455 | |
| 456 | #[test] |
| 457 | fn colours_parse_hex_and_names_with_a_material_fallback() { |
| 458 | // Hex, short and long, opaque. |
| 459 | assert_eq!( |
| 460 | attrs_of(&[("building", "yes"), ("building:colour", "#ffcc00")]).building_colour, |
| 461 | 0xFF_FF_CC_00, |
| 462 | ); |
| 463 | assert_eq!( |
| 464 | attrs_of(&[("building", "yes"), ("building:colour", "#fc0")]).building_colour, |
| 465 | 0xFF_FF_CC_00, |
| 466 | "a three-digit hex doubles each nibble", |
| 467 | ); |
| 468 | // A named colour. |
| 469 | assert_eq!( |
| 470 | attrs_of(&[("building", "yes"), ("roof:colour", "red")]).roof_colour, |
| 471 | 0xFF_FF_00_00, |
| 472 | ); |
| 473 | // No colour, but a material: the palette fills in. |
| 474 | let brick = attrs_of(&[("building", "yes"), ("building:material", "brick")]); |
| 475 | assert_ne!(brick.building_colour, 0, "a material resolves to a palette colour"); |
| 476 | assert_eq!(brick.building_colour >> 24, 0xFF, "and it is opaque"); |
| 477 | // An explicit colour beats the material. |
| 478 | let both = attrs_of(&[ |
| 479 | ("building", "yes"), |
| 480 | ("building:colour", "white"), |
| 481 | ("building:material", "brick"), |
| 482 | ]); |
| 483 | assert_eq!(both.building_colour, 0xFF_FF_FF_FF, "the colour wins over the material"); |
| 484 | // No colour and no material: absent, so the style default is used. |
| 485 | assert_eq!(attrs_of(&[("building", "yes")]).building_colour, 0); |
| 486 | } |
| 487 | |
| 488 | #[test] |
| 489 | fn a_giant_height_is_clamped_rather_than_wrapping() { |
| 490 | // A vandalised height must not wrap a u16 into a short building. |
| 491 | let a = attrs_of(&[("building", "yes"), ("height", "999999")]); |
| 492 | assert_eq!(a.height, u16::MAX); |
| 493 | // A negative height is nonsense and drops to the default rather than a huge unsigned value. |
| 494 | assert_eq!(attrs_of(&[("building", "yes"), ("height", "-5")]).height, 0); |
| 495 | } |
| 496 | } |