Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / library / map / src / main / rust / tests / geometry_query.rs
geometry_query.rs105 lines · 3.6 KiBRaw
| 1 | #![allow(dead_code)] |
| 2 | //! The geometry query against a synthetic `.mamaps` archive, through |
| 3 | //! the same file reader + codec the device uses: bbox filtering, caps |
| 4 | //! and empty-region honesty at the archive level (the pure extraction |
| 5 | //! maths is unit-tested in `tile::geometry_query`). |
| 6 | |
| 7 | use std::path::PathBuf; |
| 8 | use std::sync::Arc; |
| 9 | |
| 10 | use map_renderer::tile::geometry_query::{ |
| 11 | query_geometry, GeometryQuery, KIND_ALL, KIND_BUILDING, KIND_ROAD, KIND_WATER, |
| 12 | }; |
| 13 | use map_renderer::tile::source::FileRangeReader; |
| 14 | use tilecodec::mamaps::read::{LeafCache, MamapsArchive}; |
| 15 | |
| 16 | #[path = "real_archive/fixture.rs"] |
| 17 | mod fixture; |
| 18 | |
| 19 | fn open(path: &std::path::Path) -> MamapsArchive<FileRangeReader> { |
| 20 | MamapsArchive::open_with_cache( |
| 21 | FileRangeReader::open(path).expect("FileRangeReader::open"), |
| 22 | Arc::new(LeafCache::new()), |
| 23 | ) |
| 24 | .expect("archive opens") |
| 25 | } |
| 26 | |
| 27 | /// A z14 archive over San Francisco holding the rich fixture body |
| 28 | /// (road, lake, building) in the tile containing the city centre. |
| 29 | fn sf_archive(tag: &str) -> (PathBuf, (u32, u32)) { |
| 30 | let (x, y) = fixture::tile_of(37.7749, -122.4194, 14); |
| 31 | let path = fixture::temp_path(tag); |
| 32 | fixture::write_archive(&path, &[(14, x, y, fixture::rich_body(0))]); |
| 33 | (path, (x, y)) |
| 34 | } |
| 35 | |
| 36 | fn sf_query() -> GeometryQuery { |
| 37 | GeometryQuery { |
| 38 | west: -122.43, |
| 39 | south: 37.76, |
| 40 | east: -122.41, |
| 41 | north: 37.79, |
| 42 | kinds_mask: KIND_ALL, |
| 43 | max_features: 256, |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | #[test] |
| 48 | fn query_reads_roads_water_and_buildings_from_a_real_archive() { |
| 49 | let (path, _) = sf_archive("geometry-sf"); |
| 50 | let archive = open(&path); |
| 51 | assert_eq!(archive.header.max_zoom, 14); |
| 52 | let out = query_geometry(&archive, &sf_query()); |
| 53 | assert!(out.covered_tiles >= 1, "the SF tile was decoded"); |
| 54 | assert_eq!(out.failed_tiles, 0); |
| 55 | let kinds: Vec<u32> = out.features.iter().map(|f| f.kind).collect(); |
| 56 | assert!(kinds.contains(&KIND_ROAD), "roads came back: {kinds:?}"); |
| 57 | assert!(kinds.contains(&KIND_WATER), "the lake came back: {kinds:?}"); |
| 58 | assert!(kinds.contains(&KIND_BUILDING), "buildings came back: {kinds:?}"); |
| 59 | for f in &out.features { |
| 60 | for &(lon, lat) in &f.points { |
| 61 | assert!((-123.0..-122.0).contains(&lon), "lon {lon}"); |
| 62 | assert!((37.0..38.5).contains(&lat), "lat {lat}"); |
| 63 | } |
| 64 | } |
| 65 | let _ = std::fs::remove_file(&path); |
| 66 | } |
| 67 | |
| 68 | #[test] |
| 69 | fn query_over_empty_ground_is_covered_by_nothing_and_honest() { |
| 70 | let (path, _) = sf_archive("geometry-empty"); |
| 71 | let archive = open(&path); |
| 72 | // The Pacific, far from the one stored tile: the archive holds no |
| 73 | // tile there, so nothing is covered and nothing is invented — and |
| 74 | // that is not a failure. |
| 75 | let out = query_geometry( |
| 76 | &archive, |
| 77 | &GeometryQuery { |
| 78 | west: -150.0, |
| 79 | south: 30.0, |
| 80 | east: -149.0, |
| 81 | north: 31.0, |
| 82 | kinds_mask: KIND_ALL, |
| 83 | max_features: 256, |
| 84 | }, |
| 85 | ); |
| 86 | assert!(out.features.is_empty()); |
| 87 | assert_eq!(out.covered_tiles, 0); |
| 88 | assert_eq!(out.failed_tiles, 0); |
| 89 | assert!(out.total_tiles >= 1); |
| 90 | let _ = std::fs::remove_file(&path); |
| 91 | } |
| 92 | |
| 93 | #[test] |
| 94 | fn kind_mask_and_feature_cap_are_respected() { |
| 95 | let (path, _) = sf_archive("geometry-caps"); |
| 96 | let archive = open(&path); |
| 97 | let water_only = |
| 98 | query_geometry(&archive, &GeometryQuery { kinds_mask: KIND_WATER, ..sf_query() }); |
| 99 | assert!(!water_only.features.is_empty()); |
| 100 | assert!(water_only.features.iter().all(|f| f.kind == KIND_WATER)); |
| 101 | let capped = query_geometry(&archive, &GeometryQuery { max_features: 1, ..sf_query() }); |
| 102 | assert_eq!(capped.features.len(), 1); |
| 103 | assert!(capped.truncated); |
| 104 | let _ = std::fs::remove_file(&path); |
| 105 | } |