Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / scripts / maps / osm_ingest / src / testpbf_part2.rs
testpbf_part2.rs105 lines · 4.1 KiBRaw
| 1 | // ---- the POI admin-hierarchy fixture ---------------------------------------- |
| 2 | // |
| 3 | // A FIFTH fixture, for `poi_admin.bin`: three nested administrative squares |
| 4 | // (country > state > city), each its own `type=boundary` relation over one |
| 5 | // closed way, a fourth level-8 boundary ("Ghosttown") inside the state that no |
| 6 | // POI falls in, and three cafes — one in the city, one in the state outside the |
| 7 | // city, one in the country outside the state. |
| 8 | |
| 9 | /// `admin_level=2`, `name=Testlandia`, `name:en=Testland`. |
| 10 | pub const ADMIN_COUNTRY_ID: i64 = 901; |
| 11 | /// `admin_level=4`, `name=Teststate`. |
| 12 | pub const ADMIN_STATE_ID: i64 = 902; |
| 13 | /// `admin_level=8`, `name=Testcity`. |
| 14 | pub const ADMIN_CITY_ID: i64 = 903; |
| 15 | /// `admin_level=8`, `name=Ghosttown`: inside the state, reached by no POI. |
| 16 | pub const ADMIN_GHOST_ID: i64 = 904; |
| 17 | |
| 18 | /// `(way id, relation id, [south, west, north, east] in 1e-7 degrees)` per square. |
| 19 | const ADMIN_SQUARES: [(i64, i64, [i32; 4]); 4] = [ |
| 20 | (811, ADMIN_COUNTRY_ID, [400_000_000, -1_000_000_000, 410_000_000, -990_000_000]), |
| 21 | (812, ADMIN_STATE_ID, [402_000_000, -998_000_000, 408_000_000, -992_000_000]), |
| 22 | (813, ADMIN_CITY_ID, [404_000_000, -996_000_000, 406_000_000, -994_000_000]), |
| 23 | (814, ADMIN_GHOST_ID, [402_500_000, -997_500_000, 403_000_000, -997_000_000]), |
| 24 | ]; |
| 25 | |
| 26 | /// The three cafes: in the city, in the state only, in the country only. |
| 27 | const ADMIN_CAFES: [(i64, i32, i32, &str); 3] = [ |
| 28 | (821, 405_000_000, -995_000_000, "City Cafe"), |
| 29 | (822, 407_500_000, -993_000_000, "State Cafe"), |
| 30 | (823, 409_000_000, -991_000_000, "Country Cafe"), |
| 31 | ]; |
| 32 | |
| 33 | fn admin_block() -> Vec<u8> { |
| 34 | let mut st = StringTable::new(); |
| 35 | let k_amenity = st.id("amenity"); |
| 36 | let v_cafe = st.id("cafe"); |
| 37 | let k_name = st.id("name"); |
| 38 | let k_name_en = st.id("name:en"); |
| 39 | let k_type = st.id("type"); |
| 40 | let k_boundary = st.id("boundary"); // also the value of `type=*` |
| 41 | let v_admin = st.id("administrative"); |
| 42 | let k_admin_level = st.id("admin_level"); |
| 43 | let role_outer = st.id("outer"); |
| 44 | let levels = [st.id("2"), st.id("4"), st.id("8"), st.id("8")]; |
| 45 | let names = [st.id("Testlandia"), st.id("Teststate"), st.id("Testcity"), st.id("Ghosttown")]; |
| 46 | let v_testland = st.id("Testland"); |
| 47 | |
| 48 | // Ring corners: four nodes per square, ids way_id * 10 + 1..=4. |
| 49 | let mut nodes: Vec<FixtureNode> = Vec::new(); |
| 50 | for (way_id, _, [s, w, n, e]) in ADMIN_SQUARES { |
| 51 | for (k, (lat, lon)) in [(s, w), (s, e), (n, e), (n, w)].into_iter().enumerate() { |
| 52 | nodes.push((way_id * 10 + k as i64 + 1, lat, lon, Vec::new())); |
| 53 | } |
| 54 | } |
| 55 | for (id, lat, lon, name) in ADMIN_CAFES { |
| 56 | nodes.push((id, lat, lon, vec![(k_amenity, v_cafe), (k_name, st.id(name))])); |
| 57 | } |
| 58 | nodes.sort_by_key(|n| n.0); |
| 59 | let dense = dense_group(&nodes); |
| 60 | let ways: Vec<Vec<u8>> = ADMIN_SQUARES |
| 61 | .iter() |
| 62 | .map(|(way_id, _, _)| { |
| 63 | let c = |k: i64| way_id * 10 + k; |
| 64 | way(*way_id, &[], &[c(1), c(2), c(3), c(4), c(1)]) |
| 65 | }) |
| 66 | .collect(); |
| 67 | let rels: Vec<Vec<u8>> = ADMIN_SQUARES |
| 68 | .iter() |
| 69 | .enumerate() |
| 70 | .map(|(i, (way_id, rel_id, _))| { |
| 71 | let mut tags = vec![ |
| 72 | (k_type, k_boundary), |
| 73 | (k_boundary, v_admin), |
| 74 | (k_admin_level, levels[i]), |
| 75 | (k_name, names[i]), |
| 76 | ]; |
| 77 | if *rel_id == ADMIN_COUNTRY_ID { |
| 78 | tags.push((k_name_en, v_testland)); |
| 79 | } |
| 80 | relation(*rel_id, &tags, &[(*way_id, role_outer, 1)]) |
| 81 | }) |
| 82 | .collect(); |
| 83 | |
| 84 | let mut node_group = Vec::new(); |
| 85 | bytes_field(2, &dense, &mut node_group); |
| 86 | let mut way_group = Vec::new(); |
| 87 | for w in &ways { |
| 88 | bytes_field(3, w, &mut way_group); |
| 89 | } |
| 90 | let mut rel_group = Vec::new(); |
| 91 | for r in &rels { |
| 92 | bytes_field(4, r, &mut rel_group); |
| 93 | } |
| 94 | let mut block = Vec::new(); |
| 95 | bytes_field(1, &st.encode(), &mut block); |
| 96 | bytes_field(2, &node_group, &mut block); |
| 97 | bytes_field(2, &way_group, &mut block); |
| 98 | bytes_field(2, &rel_group, &mut block); |
| 99 | varint_field(17, 100, &mut block); // granularity |
| 100 | block |
| 101 | } |
| 102 | |
| 103 | pub fn write_admin_sample(tag: &str) -> (std::path::PathBuf, std::path::PathBuf) { |
| 104 | write_pbf(tag, &pbf_from_block(&admin_block())) |
| 105 | } |