Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
dependencies ▾

Modern-Apps.git / scripts / maps / mamaps_build / src / tiler_part7_s1.rs

tiler_part7_s1.rs208 lines · 9.2 KiBRaw
1 /// **The v10 narrowed-record path, end to end.** Buildings (minimal
2 /// records, no names/details/transit/lanes), roads (roads records) and
3 /// landtype survive a v10 serialize/parse round trip with identical
4 /// geometry, and the S3DB attrs survive the sparse table — through the
5 /// same `Body` API the renderer reads. Bridge coverage until tilecodec's
6 /// own test target builds again.
7 #[test]
8 fn a_v10_body_round_trips_narrowed_records_and_sparse_buildings() {
9 use tilecodec::mamaps::body::{
10 serialize_into_v10, BuildingAttrs, Feature as BodyFeature, Layer as BodyLayer,
11 Part, Scratch, GEOM_POLYGON, ROOF_GABLED, ROOF_ORIENT_ACROSS, WINDING_OUTER,
12 };
13 let mut buildings = BodyLayer::new(dict::LAYER_BUILDINGS);
14 buildings.features.push(BodyFeature {
15 kind: 1,
16 kind_detail: 0,
17 geom_type: GEOM_POLYGON,
18 flags: 0,
19 name_idx: tilecodec::mamaps::body::NAME_NONE,
20 parts_offset: 0,
21 part_count: 1,
22 transit_color: 0,
23 transit_ordinal: 0,
24 transit_lanes: 0,
25 transit_taper: 0,
26 lane_count: 0,
27 });
28 buildings.parts.push(Part { coord_start: 0, point_count: 4, winding: WINDING_OUTER });
29 buildings.coords = vec![(0, 0), (100, 0), (100, 100), (0, 100)];
30 let attrs = BuildingAttrs {
31 height: 420,
32 min_height: 0,
33 roof_height: 0,
34 roof_shape: ROOF_GABLED,
35 roof_direction: 64,
36 roof_orientation: ROOF_ORIENT_ACROSS,
37 building_colour: 0xFF_C8_A0_78,
38 roof_colour: 0,
39 };
40 let body = Body {
41 extent: 4096,
42 layers: vec![buildings],
43 names: Vec::new(),
44 ids: Vec::new(),
45 turn_lanes: Vec::new(),
46 buildings: vec![(dict::LAYER_BUILDINGS, vec![attrs])],
47 heightmap: None,
48 carriageways: Vec::new(),
49 convention: None,
50 region_links: Vec::new(),
51 };
52 let mut scratch = Scratch::default();
53 let bytes = serialize_into_v10(&body, &mut scratch).expect("v10 serialize");
54 assert_eq!(bytes[3], tilecodec::mamaps::header::FORMAT_VERSION_V10);
55 let back = Body::parse(bytes).expect("v10 parse");
56 let layer = back.layer(dict::LAYER_BUILDINGS).expect("buildings layer");
57 assert_eq!(layer.features.len(), 1);
58 assert_eq!(layer.coords.len(), 4, "arena survives narrowing");
59 let got = back
60 .building_attrs(dict::LAYER_BUILDINGS, 0)
61 .expect("sparse table expands");
62 assert_eq!(got, attrs, "S3DB attrs survive sparse + narrowed");
63 }
64
65 /// **v10 sparse lanes/carriageways, end to end.** One masked road among
66 /// defaults (and one split carriageway among defaults) must survive the
67 /// v10 serialize/parse trip exactly. Bridge coverage until tilecodec's own
68 /// test target builds again.
69 #[test]
70 fn a_v10_body_round_trips_sparse_lanes_and_carriageways() {
71 use tilecodec::mamaps::body::{
72 serialize_into_v10, Carriageway, Feature as BodyFeature, LaneTurns,
73 Layer as BodyLayer, MarkingConvention, Part, Scratch, GEOM_LINE,
74 WINDING_OUTER,
75 };
76 let mut roads = BodyLayer::new(dict::LAYER_ROADS);
77 for i in 0..3u32 {
78 roads.features.push(BodyFeature {
79 kind: 1,
80 kind_detail: 0,
81 geom_type: GEOM_LINE,
82 flags: 0,
83 name_idx: tilecodec::mamaps::body::NAME_NONE,
84 parts_offset: i,
85 part_count: 1,
86 transit_color: 0,
87 transit_ordinal: 0,
88 transit_lanes: 0,
89 transit_taper: 0,
90 lane_count: 4,
91 });
92 roads.parts.push(Part {
93 coord_start: i * 2,
94 point_count: 2,
95 winding: WINDING_OUTER,
96 });
97 let a = i as i16 * 10;
98 roads.coords.extend_from_slice(&[(a, 0), (a, 100)]);
99 }
100 let turns = vec![
101 LaneTurns::default(),
102 LaneTurns { forward: vec![1, 2, 6], backward: vec![4] },
103 LaneTurns::default(),
104 ];
105 let rows = vec![
106 Carriageway { forward: 2, backward: 2, solid_dividers: 0b010 },
107 Carriageway::default(),
108 Carriageway::default(),
109 ];
110 let convention = MarkingConvention { left_hand: false, yellow_centre: true };
111 let body = Body {
112 extent: 4096,
113 layers: vec![roads],
114 names: Vec::new(),
115 ids: Vec::new(),
116 turn_lanes: vec![(dict::LAYER_ROADS, turns.clone())],
117 buildings: Vec::new(),
118 heightmap: None,
119 carriageways: vec![(dict::LAYER_ROADS, rows.clone())],
120 convention: Some(convention),
121 region_links: Vec::new(),
122 };
123 let mut scratch = Scratch::default();
124 let bytes = serialize_into_v10(&body, &mut scratch).expect("v10 serialize");
125 assert_eq!(bytes[3], tilecodec::mamaps::header::FORMAT_VERSION_V10);
126 let back = Body::parse(bytes).expect("v10 parse");
127 assert_eq!(back.feature_turns(dict::LAYER_ROADS, 1), Some(&turns[1]));
128 assert_eq!(back.feature_turns(dict::LAYER_ROADS, 0), Some(&LaneTurns::default()));
129 assert_eq!(back.feature_carriageway(dict::LAYER_ROADS, 0), Some(&rows[0]));
130 assert_eq!(
131 back.feature_carriageway(dict::LAYER_ROADS, 2),
132 Some(&Carriageway::default()),
133 );
134 assert_eq!(back.convention, Some(convention));
135 }
136
137 /// **The DEM heightmap path, end to end.** A synthetic per-tile grid handed to the tiler reaches
138 /// the archive on the tile it covers and reads back through [`tilecodec::mamaps::body::Heightmap::sample`]
139 /// — and a build with no DEM leaves every tile's heightmap unset, a valid v6 with no terrain
140 /// section.
141 #[test]
142 fn a_dem_grid_reaches_the_archive_and_reads_back_via_sample() {
143 use tilecodec::mamaps::body::Heightmap;
144 let (z, dim) = (14u8, 17u16);
145 let (tx, ty) = (2730u64, 6335u64);
146 // The tile's centre, so a small building lands squarely inside it (and so in exactly the
147 // tile the synthetic DEM covers).
148 let n = (1u64 << z) as f64;
149 let clon = (tx as f64 + 0.5) / n * 360.0 - 180.0;
150 let clat = {
151 let m = std::f64::consts::PI * (1.0 - 2.0 * (ty as f64 + 0.5) / n);
152 m.sinh().atan().to_degrees()
153 };
154 let building = || Feature {
155 class: Class::area(dict::LAYER_BUILDINGS, crate::schema::kind("building"), 14),
156 geometry: square(clon - 0.001, clat - 0.001, 0.001),
157 name: None,
158 id: tilecodec::mamaps::body::ID_NONE,
159 transit_color: 0,
160 transit_ordinal: 0,
161 transit_lanes: 0,
162 transit_taper: 0,
163 lane_count: 0,
164 turn_fwd: Vec::new(),
165 turn_bwd: Vec::new(),
166 building: None,
167 carriageway: tilecodec::mamaps::body::Carriageway::default(),
168 };
169 // A ramp grid: sample(col, row) = 32768 + row*10 + col, so a read-back names its cell.
170 let mut grid = Vec::with_capacity(dim as usize * dim as usize);
171 for row in 0..dim as usize {
172 for col in 0..dim as usize {
173 grid.push(32768u16 + (row * 10 + col) as u16);
174 }
175 }
176 let dem =
177 crate::dem::Dem::from_grids(z, dim, vec![(tile_id(z, tx, ty), grid.clone())]);
178 let with_dem = Settings { dem, ..settings(14, 14) };
179 let (bytes, _) = build(&spilled(&[building()]), &with_dem).expect("build with dem");
180 // v9: leaves compress by default, which the v8 byte cannot name.
181 assert_eq!(
182 bytes[7],
183 tilecodec::mamaps::header::FORMAT_VERSION_V9,
184 "the archive header carries the writer's own format version",
185 );
186 let entries = tilecodec::mamaps::read::read_all(&bytes).expect("read");
187 let mut saw = false;
188 for (id, _, body) in &entries {
189 let body = Body::parse(body).expect("parse");
190 if *id == tile_id(z, tx, ty) {
191 let hm = body.heightmap.expect("the covered tile carries a heightmap");
192 assert_eq!(hm.dim, dim);
193 assert_eq!(hm.samples, grid, "the grid reaches the archive verbatim at its own zoom");
194 assert_eq!(hm.sample(0, 0), Some(32768), "sea level at the top-left");
195 assert_eq!(hm.sample(4, 3), Some(32768 + 34));
196 assert_eq!(Heightmap::metres(hm.sample(4, 3).unwrap()), 34);
197 saw = true;
198 }
199 }
200 assert!(saw, "the building's tile should be emitted and carry the DEM grid");
201
202 // Without a DEM the same build is a valid v6 whose tiles carry no heightmap section.
203 let (plain, _) = build(&spilled(&[building()]), &settings(14, 14)).expect("build without dem");
204 for (_, _, body) in &tilecodec::mamaps::read::read_all(&plain).expect("read") {
205 let body = Body::parse(body).expect("parse");
206 assert!(body.heightmap.is_none(), "no DEM means no heightmap section");
207 }
208 }