Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / networklocation / geodb_build / src / lib.rs
lib.rs70 lines · 2.0 KiBRaw
| 1 | //! Build `geocoder.db` from an OpenStreetMap `.osm.pbf`. |
| 2 | //! |
| 3 | //! Replaces the `osmium tags-filter | osmium export | geocoder_gen.cpp` chain, which needed |
| 4 | //! osmium, g++, OpenMP, simdjson and POSIX mmap — so WSL only — and wrote a 101 GB GeoJSONSeq |
| 5 | //! intermediate on the way. This reads the PBF natively through `osm_ingest` and holds no |
| 6 | //! intermediate at all. |
| 7 | //! |
| 8 | //! It also indexes far more: the old database held only objects carrying both |
| 9 | //! `addr:housenumber` and `addr:street`. See [`extract`] for what that left out and why it |
| 10 | //! mattered. |
| 11 | |
| 12 | #![deny(missing_docs)] |
| 13 | |
| 14 | pub mod codec; |
| 15 | pub mod extract; |
| 16 | pub mod extract_extra; |
| 17 | pub mod format; |
| 18 | pub mod progress; |
| 19 | pub mod write; |
| 20 | pub mod write_extra; |
| 21 | |
| 22 | /// Format a byte count for a status line. |
| 23 | pub fn human_bytes(n: u64) -> String { |
| 24 | const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"]; |
| 25 | let mut v = n as f64; |
| 26 | let mut u = 0; |
| 27 | while v >= 1024.0 && u + 1 < UNITS.len() { |
| 28 | v /= 1024.0; |
| 29 | u += 1; |
| 30 | } |
| 31 | if u == 0 { |
| 32 | format!("{n} B") |
| 33 | } else { |
| 34 | format!("{v:.2} {}", UNITS[u]) |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | /// Format a count with thousands separators, so a planet build's numbers are readable. |
| 39 | pub fn commas(n: usize) -> String { |
| 40 | let s = n.to_string(); |
| 41 | let mut out = String::with_capacity(s.len() + s.len() / 3); |
| 42 | for (i, c) in s.chars().enumerate() { |
| 43 | if i > 0 && (s.len() - i) % 3 == 0 { |
| 44 | out.push(','); |
| 45 | } |
| 46 | out.push(c); |
| 47 | } |
| 48 | out |
| 49 | } |
| 50 | |
| 51 | #[cfg(test)] |
| 52 | mod tests { |
| 53 | use super::*; |
| 54 | |
| 55 | #[test] |
| 56 | fn byte_counts_are_readable() { |
| 57 | assert_eq!(human_bytes(0), "0 B"); |
| 58 | assert_eq!(human_bytes(1023), "1023 B"); |
| 59 | assert_eq!(human_bytes(1024), "1.00 KiB"); |
| 60 | assert_eq!(human_bytes(5_368_709_120), "5.00 GiB"); |
| 61 | } |
| 62 | |
| 63 | #[test] |
| 64 | fn counts_are_grouped() { |
| 65 | assert_eq!(commas(0), "0"); |
| 66 | assert_eq!(commas(999), "999"); |
| 67 | assert_eq!(commas(1000), "1,000"); |
| 68 | assert_eq!(commas(260_800_000), "260,800,000"); |
| 69 | } |
| 70 | } |