Modern Apps git

← Modern-Apps.git

maps: .mamaps v7 - traffic layer, S3DB, heightmap, carriageway table

Commit
09d265d8fbd233ad18e692376e350f993cf3c746
Author
vayun-mathur <[email protected]>
When
2026-09-10 05:18 UTC (2 months ago)
Parents
468d220

Diff

library/tilecodec/src/main/rust/src/mamaps/body.rs
diff --git a/library/tilecodec/src/main/rust/src/mamaps/body.rs b/library/tilecodec/src/main/rust/src/mamaps/body.rsindex 66fb72bdc4..bf6807e254 100644--- a/library/tilecodec/src/main/rust/src/mamaps/body.rs+++ b/library/tilecodec/src/main/rust/src/mamaps/body.rs@@ -51,9 +51,11 @@ use crate::proto::{err, Result}; pub const BODY_HEADER_LEN: usize = 16; pub const LAYER_INDEX_LEN: usize = 12; /// Feature records: kind, kind_detail, geom_type, flags, name_idx (u16), parts_offset,-/// part_count, transit_color (u32) and the three transit lane bytes (ordinal, lanes, taper) —-/// 24, with the last byte still reserved zero.+/// part_count, transit_color (u32), the three transit lane bytes (ordinal, lanes, taper) and+/// the roads lane count (byte 23) — 24, with no byte reserved any longer. pub const FEATURE_RECORD_LEN: usize = 24;+/// One carriageway record: `u8` forward lanes, `u8` backward lanes, `u32` solid divider bits.+pub const CARRIAGEWAY_RECORD_LEN: usize = 6; pub const PART_ENTRY_LEN: usize = 12; pub const BODY_FLAG_EXTENDED_COUNTS: u8 = 0x01; /// A feature id table follows the name table. See [`Body::ids`].@@ -65,9 +67,47 @@ pub const BODY_FLAG_ID_TABLE: u8 = 0x02; /// id table chaining onto it, an omitted name table and a present id table are indistinguishable /// without a flag. pub const BODY_FLAG_NAME_TABLE: u8 = 0x04;--const KNOWN_BODY_FLAGS: u8 =-    BODY_FLAG_EXTENDED_COUNTS | BODY_FLAG_ID_TABLE | BODY_FLAG_NAME_TABLE;+/// A per-road-feature turn-lane table follows the id table. See [`Body::turn_lanes`].+///+/// The last of the optional trailing sections, chained after the id table for the same reason the+/// id table is chained after the name table: with three of them, "there are bytes left" no longer+/// says which one they are, so each is announced by its own flag.+pub const BODY_FLAG_LANE_TABLE: u8 = 0x08;+/// A per-building-feature S3DB attribute table follows the turn-lane table. See+/// [`Body::building_attrs`].+///+/// Chained after the turn-lane table exactly as that was chained after the id table: another+/// optional trailing section announced by its own flag rather than inferred from leftover bytes.+/// v6. Dense-parallel to the `buildings` layer's features — a building with no S3DB tags carries a+/// default [`BuildingAttrs`], and the table costs nothing on any tile whose `buildings` layer is+/// absent or which has no S3DB tags at all.+pub const BODY_FLAG_BUILDING_TABLE: u8 = 0x10;+/// A per-tile DEM heightmap grid follows the building table. See [`Body::heightmap`].+///+/// The last of the optional trailing sections. One fixed `u16` grid for the whole tile (not per+/// layer and not per feature), so an ocean or otherwise elevation-free tile omits it and stays+/// 16-byte. v6.+pub const BODY_FLAG_HEIGHTMAP: u8 = 0x20;+/// A per-road-feature carriageway table follows the heightmap. See [`Body::carriageways`].+///+/// The last of the optional trailing sections. What the carriageway renderer needs that+/// [`Feature::lane_count`] cannot say: the **directional split** (how many of those lanes run each+/// way, which is where the centre line goes) and which dividers are solid rather than dashed. Both+/// come straight from OSM `lanes:forward`/`lanes:backward` and `change:lanes`.+///+/// It also carries the tile's [`MarkingConvention`], which is a property of the *tile* rather than+/// of any feature: a centre line is yellow in the Americas and white almost everywhere else, and+/// traffic keeps left in the UK, Japan and Australia. One byte per tile rather than per road,+/// because a tile never spans two conventions in any way that matters. v7.+pub const BODY_FLAG_ROAD_LANES: u8 = 0x40;++const KNOWN_BODY_FLAGS: u8 = BODY_FLAG_EXTENDED_COUNTS+    | BODY_FLAG_ID_TABLE+    | BODY_FLAG_NAME_TABLE+    | BODY_FLAG_LANE_TABLE+    | BODY_FLAG_BUILDING_TABLE+    | BODY_FLAG_HEIGHTMAP+    | BODY_FLAG_ROAD_LANES;  /// A feature whose geometry is one or more open paths. pub const GEOM_LINE: u8 = 1;@@ -88,9 +128,19 @@ pub const FLAG_IS_LINK: u8 = 1 << 2; /// What lets `boundaries` carry an admin level in the same field a road carries `service` in: the /// style compares an admin level with `<=`, so interning it would mean interning every integer. pub const FLAG_DETAIL_NUMERIC: u8 = 1 << 3;+/// A `roads` feature carries traffic in one direction only, toward its last point.+///+/// OSM `oneway=yes`, and only that — the same test the routing graph applies, so the two cannot+/// disagree about a road. What the carriageway renderer needs it for is the **centre line**: a+/// two-way road separates opposing traffic down the middle and a one-way does not, so without this+/// every one-way street would grow a centre line it has no business having.+///+/// It rides a feature flag rather than the side table because `coalesce` already keys on `flags`,+/// so a one-way and a two-way road of the same class cannot merge into one feature.+pub const FLAG_IS_ONEWAY: u8 = 1 << 4;  const KNOWN_FEATURE_FLAGS: u8 =-    FLAG_IS_TUNNEL | FLAG_IS_BRIDGE | FLAG_IS_LINK | FLAG_DETAIL_NUMERIC;+    FLAG_IS_TUNNEL | FLAG_IS_BRIDGE | FLAG_IS_LINK | FLAG_DETAIL_NUMERIC | FLAG_IS_ONEWAY;  /// A ring wound counter-clockwise: the outside of a polygon. pub const WINDING_OUTER: u16 = 0;@@ -138,6 +188,14 @@ pub struct Feature {     /// sideways onto it. With the offset now computed downstream, the fraction has to travel     /// separately from the lane index. 255 is fully in lane; zero for every other layer.     pub transit_taper: u8,+    /// A `roads` feature's carriageway lane count, or zero when it has none.+    ///+    /// The OSM `lanes` total, capped to a byte. The renderer draws this many parallel+    /// sub-lanes with dividers at high zoom (the transit lateral-fan, driven by a lane count+    /// instead of a colour ordinal) and collapses to a single stroke below the gate. Rides in+    /// the byte the v2..v4 record kept reserved, so the record width is unchanged; zero for+    /// every layer but `roads`, and for a road with no `lanes` tag.+    pub lane_count: u8, }  impl Feature {@@ -153,6 +211,11 @@ impl Feature {         self.flags & FLAG_IS_LINK != 0     } +    /// Traffic runs one way only, toward the feature's last point. See [`FLAG_IS_ONEWAY`].+    pub fn is_oneway(&self) -> bool {+        self.flags & FLAG_IS_ONEWAY != 0+    }+     /// The feature's display name from the body's name table, or `None` when it has none.     pub fn name<'b>(&self, body: &'b Body) -> Option<&'b str> {         body.name(self.name_idx)@@ -225,6 +288,222 @@ pub const NAME_NONE: u16 = 0; /// to should cost zero bytes of entropy rather than a sentinel the reader has to know about. pub const ID_NONE: u64 = 0; +/// One road feature's per-lane turn indications, from OSM `turn:lanes[:forward|:backward]`.+///+/// Each `u16` is a lane's [`LANE_*`](super::super) bit set — the same scheme the routing graph+/// writes (`maps/src/main/rust/src/graph.rs`) — ordered left to right. `forward` lanes are+/// traversed toward the way's last point (its junction end) and `backward` toward its first, so a+/// renderer draws forward arrows at the end and backward arrows at the start. Both empty for a+/// feature with no `turn:lanes`, which is almost every road; such a feature costs two bytes in the+/// table (a zero count each way) and nothing at all when its whole layer has no lane data.+#[derive(Debug, Clone, PartialEq, Eq, Default)]+pub struct LaneTurns {+    pub forward: Vec<u16>,+    pub backward: Vec<u16>,+}++impl LaneTurns {+    /// Does this feature carry any turn indication at all?+    pub fn is_empty(&self) -> bool {+        self.forward.is_empty() && self.backward.is_empty()+    }+}++/// Which way traffic drives and what colour separates opposing directions.+///+/// A property of the country, and therefore of the tile: the renderer needs it to decide the+/// centre line's colour and which side of the carriageway the forward lanes sit on. Baked once per+/// tile at build time from the admin boundaries, because the renderer has no country data and+/// resolving one on device would mean shipping a polygon set to answer a question that never+/// changes for a given tile.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]+pub struct MarkingConvention {+    /// Traffic keeps left (UK, Japan, Australia, India, ...). Default is right-hand traffic,+    /// which is most of the world by land area and the safer thing to be wrong about.+    pub left_hand: bool,+    /// A yellow line separates opposing directions (the Americas). Elsewhere it is white, and only+    /// the line's *style* distinguishes it from a lane divider.+    pub yellow_centre: bool,+}++impl MarkingConvention {+    /// The wire byte: bit 0 left-hand traffic, bit 1 yellow centre line. The remaining six bits are+    /// reserved and must be zero, so a later convention (dashed-vs-solid edge lines, say) appends+    /// rather than renumbering.+    pub const LEFT_HAND: u8 = 1 << 0;+    pub const YELLOW_CENTRE: u8 = 1 << 1;+    const KNOWN: u8 = Self::LEFT_HAND | Self::YELLOW_CENTRE;++    pub fn to_byte(self) -> u8 {+        let mut b = 0;+        if self.left_hand {+            b |= Self::LEFT_HAND;+        }+        if self.yellow_centre {+            b |= Self::YELLOW_CENTRE;+        }+        b+    }++    pub fn from_byte(b: u8) -> Result<MarkingConvention> {+        if b & !Self::KNOWN != 0 {+            return err(format!("a .mamaps marking convention sets reserved bits ({b:#04x})"));+        }+        Ok(MarkingConvention {+            left_hand: b & Self::LEFT_HAND != 0,+            yellow_centre: b & Self::YELLOW_CENTRE != 0,+        })+    }+}++/// One road feature's carriageway shape, for the surface renderer.+///+/// [`Feature::lane_count`] gives the total; this says how it divides. `forward` lanes run toward+/// the feature's last point and `backward` toward its first, matching [`LaneTurns`]'s convention+/// exactly. The centre line goes at the boundary between them, which on a road whose split is+/// unknown (both zero) the renderer places down the middle for a two-way and omits for a one-way.+///+/// `solid_dividers` is a bit per interior divider, ordered left to right from the leftmost, set+/// when a lane change across it is prohibited (OSM `change:lanes` `not_left`/`not_right`/`no`). A+/// road with more than 32 interior dividers has none recorded past the 32nd, which no real road+/// reaches.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]+pub struct Carriageway {+    pub forward: u8,+    pub backward: u8,+    pub solid_dividers: u32,+}++impl Carriageway {+    /// Nothing known about this road's carriageway beyond its total lane count.+    pub fn is_empty(&self) -> bool {+        *self == Carriageway::default()+    }+}++/// The OSM `turn:lanes` indication bits, one set per lane in a [`LaneTurns`] mask.+///+/// **An on-disk contract** with the generator (`scripts/maps/osm_ingest` and the routing graph's+/// `maps/src/main/rust/src/graph.rs`): the archive stores exactly these bits, so the renderer's+/// arrow glyphs and the router's lane guidance read one scheme. Keep the three in sync — never+/// renumber a bit, only append. A lane with no marking is [`LANE_NONE`].+pub const LANE_NONE: u16 = 1 << 0;+pub const LANE_THROUGH: u16 = 1 << 1;+pub const LANE_LEFT: u16 = 1 << 2;+pub const LANE_SLIGHT_LEFT: u16 = 1 << 3;+pub const LANE_SHARP_LEFT: u16 = 1 << 4;+pub const LANE_RIGHT: u16 = 1 << 5;+pub const LANE_SLIGHT_RIGHT: u16 = 1 << 6;+pub const LANE_SHARP_RIGHT: u16 = 1 << 7;+pub const LANE_REVERSE: u16 = 1 << 8;+pub const LANE_MERGE_TO_LEFT: u16 = 1 << 9;+pub const LANE_MERGE_TO_RIGHT: u16 = 1 << 10;++/// One `buildings` feature's OSM Simple 3D Buildings (S3DB) attributes.+///+/// A side table entry rather than a field on [`Feature`] for exactly the reason the id and+/// turn-lane tables are: these nine values are present on a small minority of buildings, and+/// widening the fixed 24-byte record for them would cost every road and building in the archive to+/// serve a few. The table is dense-parallel to the `buildings` layer's features — a building that+/// carried no S3DB tags reads back as [`BuildingAttrs::default`], which extrudes as a flat box at+/// the default height — so `building_attrs(i)` lines up with `layer.features[i]`.+///+/// # Wire layout (a fixed [`BUILDING_ATTRS_LEN`]-byte record, all little-endian)+///+/// | offset | field | encoding |+/// |---|---|---|+/// | 0..2 | `height` | `u16` decimetres (0.1 m); 0 means "absent, use the renderer default" |+/// | 2..4 | `min_height` | `u16` decimetres; the height the walls start at (`building:min_level`) |+/// | 4..6 | `roof_height` | `u16` decimetres of the roof alone, within `height` |+/// | 6 | `roof_shape` | one of [`ROOF_FLAT`]..=[`ROOF_DOME`]; unknown shapes are stored as flat |+/// | 7 | `roof_direction` | quantised degrees: `deg * 256 / 360`, so decode is `v * 360 / 256` |+/// | 8 | `roof_orientation` | [`ROOF_ORIENT_ALONG`] or [`ROOF_ORIENT_ACROSS`] |+/// | 9..12 | reserved | zero |+/// | 12..16 | `building_colour` | `0xAARRGGBB`; 0 means "no colour, use the style default" |+/// | 16..20 | `roof_colour` | `0xAARRGGBB`; 0 means "no colour" |+///+/// Heights are decimetres because a `u16` of metres would quantise a house to the nearest storey+/// and a `u16` of centimetres would top out at 655 m — decimetres reach 6553.5 m (every real+/// building, the tallest under 830 m) at 0.1 m resolution, which is finer than the source tags.+/// Colours carry an alpha byte so "no colour" (all zero, fully transparent) is distinct from+/// opaque black `0xFF000000`.+#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]+pub struct BuildingAttrs {+    /// Total height in decimetres (0.1 m), or 0 for "absent — the renderer picks a default".+    pub height: u16,+    /// The height the walls begin at, in decimetres — `building:min_level` for a floating part.+    pub min_height: u16,+    /// The roof's own height in decimetres, part of `height`, or 0 for a flat roof.+    pub roof_height: u16,+    /// One of [`ROOF_FLAT`]..=[`ROOF_DOME`]. An OSM `roof:shape` this build does not model is+    /// stored as [`ROOF_FLAT`], never as an out-of-range value.+    pub roof_shape: u8,+    /// The roof ridge/slope direction as quantised degrees: `deg * 256 / 360` on the way in, so a+    /// reader recovers `deg = v * 360 / 256`. Zero for a roof with no direction.+    pub roof_direction: u8,+    /// [`ROOF_ORIENT_ALONG`] (the default) or [`ROOF_ORIENT_ACROSS`], for `roof:orientation`.+    pub roof_orientation: u8,+    /// The wall colour as `0xAARRGGBB`, or 0 for "no colour — use the style default".+    pub building_colour: u32,+    /// The roof colour as `0xAARRGGBB`, or 0 for "no colour".+    pub roof_colour: u32,+}++/// The fixed width of one [`BuildingAttrs`] record on the wire. 4-byte aligned so the two `u32`+/// colours sit on a 4-byte boundary within the record.+pub const BUILDING_ATTRS_LEN: usize = 20;++/// [`BuildingAttrs::roof_shape`] values. An OSM `roof:shape` this build does not model falls back+/// to [`ROOF_FLAT`] rather than being stored as an unknown number, so a reader never has to guess.+pub const ROOF_FLAT: u8 = 0;+pub const ROOF_GABLED: u8 = 1;+pub const ROOF_HIPPED: u8 = 2;+pub const ROOF_PYRAMIDAL: u8 = 3;+pub const ROOF_SKILLION: u8 = 4;+pub const ROOF_DOME: u8 = 5;+/// The largest roof-shape id this format defines. `parse` refuses anything above it.+pub const ROOF_SHAPE_MAX: u8 = ROOF_DOME;++/// [`BuildingAttrs::roof_orientation`] values, for OSM `roof:orientation`.+pub const ROOF_ORIENT_ALONG: u8 = 0;+pub const ROOF_ORIENT_ACROSS: u8 = 1;+/// The largest roof-orientation id this format defines. `parse` refuses anything above it.+pub const ROOF_ORIENT_MAX: u8 = ROOF_ORIENT_ACROSS;++/// One tile's DEM heightmap: a fixed square `u16` grid sampled over the tile's own extent.+///+/// A single grid for the whole tile — not per layer, not per feature — so an elevation-free tile+/// (open ocean) omits the section entirely and stays 16-byte. `dim` is the side length, so+/// `samples.len() == dim * dim`, stored row-major from the tile's top-left.+///+/// A sample is metres above sea level **biased by 32768**, i.e. `stored = metres + 32768`, so the+/// whole `i16` range of real terrain (below the Dead Sea to above Everest) fits an unsigned `u16`+/// with sea level at 32768. That is the same bias the AWS Terrain Tiles "terrarium" source uses+/// before its `-32768`, so the ingest keeps the number it already computed.+#[derive(Debug, Clone, PartialEq, Eq)]+pub struct Heightmap {+    /// The grid side length, so the grid is `dim * dim` samples. A `dim` of 0 is not a heightmap;+    /// such a tile omits the section and reads back as `None`.+    pub dim: u16,+    /// `dim * dim` samples, row-major from the tile's top-left, each `metres + 32768`.+    pub samples: Vec<u16>,+}++impl Heightmap {+    /// The sample at grid `(col, row)`, or `None` when either is out of range.+    pub fn sample(&self, col: u16, row: u16) -> Option<u16> {+        if col >= self.dim || row >= self.dim {+            return None;+        }+        self.samples.get(row as usize * self.dim as usize + col as usize).copied()+    }++    /// A sample's elevation in metres, undoing the 32768 bias.+    pub fn metres(stored: u16) -> i32 {+        stored as i32 - 32768+    }+}+ /// A whole tile. /// /// `names` is the per-tile string table: `names[i - 1]` is the text for `name_idx == i`,@@ -235,29 +514,87 @@ pub const ID_NONE: u64 = 0; /// Each vector is dense and parallel to that layer's `features`, so `ids[k].1[i]` is the id of /// `layer(ids[k].0).features[i]`. ///+/// `turn_lanes` is the optional per-layer turn-lane table, the same shape as `ids` — keyed by+/// `layer_id`, ascending, and dense-parallel to that layer's `features`. Only the `roads` layer+/// carries one, and only when some road in the tile has `turn:lanes`; a feature with none holds an+/// empty [`LaneTurns`]. It is a side table rather than a field on [`Feature`] for the same reason+/// `ids` is: the masks are variable length and present on a small minority of features, so+/// widening the fixed 24-byte record for them would cost every road in the archive to serve a few.+/// /// It is a side table rather than a field on [`Feature`] because widening the 24-byte feature /// record to 32 would cost eight bytes on every road and building in the archive — roughly 17 GB /// on a 68 GB planet — to carry an id that a line feature cannot even have: `coalesce` merges /// lines hard and deliberately relies on them having no identity, so a merged road's id would be /// whichever input happened to win. Only `poi` and `places` are pure, never-coalesced points, and /// giving ids to those two alone costs roughly 320 MB.+///+/// `buildings` is the optional per-layer S3DB attribute table (v6), the same shape as `ids` and+/// `turn_lanes`: keyed by `layer_id`, ascending, and dense-parallel to that layer's `features`.+/// Only the `buildings` layer ever carries one, and only when some building in the tile has S3DB+/// tags; a building with none holds a default [`BuildingAttrs`]. A side table for the same reason+/// the others are — the attrs are ~20 bytes present on a minority of buildings.+///+/// `heightmap` is the optional per-tile DEM grid (v6). One grid for the whole tile, `None` on any+/// tile with no elevation data (open ocean), so such a tile stays as cheap as it was.+///+/// `carriageways` is the optional per-layer carriageway table (v7), the same shape as `turn_lanes`.+/// Only the `roads` layer carries one. It exists because [`Feature::lane_count`] is a total and the+/// surface renderer needs the directional split to place a centre line. `convention` rides with it+/// as a single per-tile byte; it is `None` exactly when the table is absent. #[derive(Debug, Clone, PartialEq, Eq)] pub struct Body {     pub extent: u16,     pub layers: Vec<Layer>,     pub names: Vec<String>,     pub ids: Vec<(u8, Vec<u64>)>,+    pub turn_lanes: Vec<(u8, Vec<LaneTurns>)>,+    pub buildings: Vec<(u8, Vec<BuildingAttrs>)>,+    pub heightmap: Option<Heightmap>,+    pub carriageways: Vec<(u8, Vec<Carriageway>)>,+    pub convention: Option<MarkingConvention>, }  impl Body {     pub fn new(extent: u16) -> Body {-        Body { extent, layers: Vec::new(), names: Vec::new(), ids: Vec::new() }+        Body {+            extent,+            layers: Vec::new(),+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: Vec::new(),+            convention: None,+        }     }      pub fn layer(&self, layer_id: u8) -> Option<&Layer> {         self.layers.iter().find(|l| l.layer_id == layer_id)     } +    /// This feature's per-lane turn indications, or `None` when its layer carries no turn-lane+    /// table. A feature in a layer that has one but with no `turn:lanes` of its own reads back as+    /// an empty [`LaneTurns`], not `None`.+    pub fn feature_turns(&self, layer_id: u8, index: usize) -> Option<&LaneTurns> {+        self.turn_lanes+            .iter()+            .find(|(id, _)| *id == layer_id)+            .and_then(|(_, turns)| turns.get(index))+    }++    /// The `index`-th feature of `layer_id`'s carriageway shape, or `None` when the tile carries no+    /// carriageway table for that layer.+    ///+    /// A road present in the table but with nothing known holds a default [`Carriageway`], not+    /// `None` — the same convention [`feature_turns`](Self::feature_turns) uses.+    pub fn feature_carriageway(&self, layer_id: u8, index: usize) -> Option<&Carriageway> {+        self.carriageways+            .iter()+            .find(|(id, _)| *id == layer_id)+            .and_then(|(_, rows)| rows.get(index))+    }+     /// The stable feature id for the `index`-th feature of `layer_id`.     ///     /// `None` when the layer carries no id table at all; [`ID_NONE`] when it does but the@@ -269,6 +606,16 @@ impl Body {             .and_then(|(_, ids)| ids.get(index).copied())     } +    /// This feature's S3DB attributes, or `None` when its layer carries no building table. A+    /// feature in a layer that has one but with no S3DB tags of its own reads back as a default+    /// [`BuildingAttrs`], not `None`.+    pub fn building_attrs(&self, layer_id: u8, index: usize) -> Option<BuildingAttrs> {+        self.buildings+            .iter()+            .find(|(id, _)| *id == layer_id)+            .and_then(|(_, attrs)| attrs.get(index).copied())+    }+     /// The display name for a `name_idx`, or `None` for [`NAME_NONE`] and anything past the table.     pub fn name(&self, idx: u16) -> Option<&str> {         (idx != NAME_NONE)@@ -369,16 +716,32 @@ impl Body {             }             layers.push(parse_layer(layer_id, feature_count, &buf[offset..end])?);         }-        // The optional trailing sections, in order: the name table then the id table, each at the-        // aligned end of the last layer payload. Both are announced by a flag rather than-        // inferred from leftover bytes, because with two of them "there are bytes left" no longer-        // says which one they are.+        // The optional trailing sections, in order: the name table, then the id table, then the+        // turn-lane table, then the building table, then the heightmap, then the carriageway+        // table, each at the aligned end of the one before it. Each is announced by a flag rather+        // than inferred from leftover bytes, because with six of them "there are bytes left" no+        // longer says which one they are. Order is positional: append, never insert.         let mut names = Vec::new();         let mut ids = Vec::new();+        let mut turn_lanes = Vec::new();+        let mut buildings = Vec::new();+        let mut heightmap = None;+        let mut carriageways = Vec::new();+        let mut convention = None;         let payloads_end = payloads_end(buf, layer_count, index_end)?;         let has_names = body_flags & BODY_FLAG_NAME_TABLE != 0;         let has_ids = body_flags & BODY_FLAG_ID_TABLE != 0;-        if !has_names && !has_ids {+        let has_lanes = body_flags & BODY_FLAG_LANE_TABLE != 0;+        let has_buildings = body_flags & BODY_FLAG_BUILDING_TABLE != 0;+        let has_heightmap = body_flags & BODY_FLAG_HEIGHTMAP != 0;+        let has_carriageways = body_flags & BODY_FLAG_ROAD_LANES != 0;+        if !has_names+            && !has_ids+            && !has_lanes+            && !has_buildings+            && !has_heightmap+            && !has_carriageways+        {             // The body is exactly its payloads, unpadded. Anything else is trailing garbage.             if payloads_end != buf.len() {                 return err(format!(@@ -401,6 +764,27 @@ impl Body {                 at += used;                 ids = table;             }+            if has_lanes {+                let (table, used) = parse_lanes(&buf[at..], &layers)?;+                at += used;+                turn_lanes = table;+            }+            if has_buildings {+                let (table, used) = parse_buildings(&buf[at..], &layers)?;+                at += used;+                buildings = table;+            }+            if has_heightmap {+                let (grid, used) = parse_heightmap(&buf[at..])?;+                at += used;+                heightmap = Some(grid);+            }+            if has_carriageways {+                let (table, marking, used) = parse_carriageways(&buf[at..], &layers)?;+                at += used;+                carriageways = table;+                convention = Some(marking);+            }             if at != buf.len() {                 return err(format!(                     "a .mamaps body has {} trailing byte(s) past its trailing sections",@@ -408,7 +792,17 @@ impl Body {                 ));             }         }-        Ok(Body { extent, layers, names, ids })+        Ok(Body {+            extent,+            layers,+            names,+            ids,+            turn_lanes,+            buildings,+            heightmap,+            carriageways,+            convention,+        })     } } @@ -449,6 +843,7 @@ fn parse_layer(layer_id: u8, feature_count: usize, buf: &[u8]) -> Result<Layer>             transit_ordinal: buf[at + 20],             transit_lanes: buf[at + 21],             transit_taper: buf[at + 22],+            lane_count: buf[at + 23],         };         if feature.part_count == 0 {             return err("a .mamaps feature has no geometry");@@ -730,6 +1125,400 @@ fn serialize_ids(ids: &[(u8, Vec<u64>)], out: &mut Vec<u8>) {     } } +/// Parse the turn-lane table: `u32` entry count, then per entry a `u8` layer id, three reserved+/// bytes, a `u32` feature count and that many per-feature records. A per-feature record is a `u8`+/// forward lane count, a `u8` backward lane count, then that many `u16` masks each (forward then+/// backward), little-endian. Returns the table and the bytes consumed (including 4-byte padding).+///+/// The same shape and validation as [`parse_ids`]: an entry must name a layer the body carries,+/// entries ascend and are distinct by layer id, and a layer's turn vector is exactly as long as+/// its feature vector — a mismatch would misattribute every feature's lanes after the first.+fn parse_lanes(buf: &[u8], layers: &[Layer]) -> Result<(Vec<(u8, Vec<LaneTurns>)>, usize)> {+    if buf.len() < 4 {+        return err("a .mamaps turn-lane table ends before its count");+    }+    let count = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize;+    if count > layers.len() {+        return err(format!(+            "a .mamaps turn-lane table has {count} entries for a body with {} layer(s)",+            layers.len(),+        ));+    }+    let mut at = 4usize;+    let mut table = Vec::with_capacity(count);+    let mut previous: Option<u8> = None;+    for _ in 0..count {+        if at + 8 > buf.len() {+            return err("a .mamaps turn-lane table ends inside an entry header");+        }+        let layer_id = buf[at];+        if buf[at + 1] != 0 || u16::from_le_bytes([buf[at + 2], buf[at + 3]]) != 0 {+            return err("a .mamaps turn-lane table entry has non-zero reserved bytes");+        }+        let feat_len =+            u32::from_le_bytes([buf[at + 4], buf[at + 5], buf[at + 6], buf[at + 7]]) as usize;+        at += 8;+        if previous.is_some_and(|p| layer_id <= p) {+            return err("a .mamaps turn-lane table's entries are not ordered by layer id");+        }+        previous = Some(layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == layer_id) else {+            return err(format!(+                "a .mamaps turn-lane table names layer {layer_id}, which the body does not carry"+            ));+        };+        if feat_len != layer.features.len() {+            return err(format!(+                "a .mamaps turn-lane table gives layer {layer_id} {feat_len} record(s) for {} \+                 feature(s)",+                layer.features.len(),+            ));+        }+        let mut turns = Vec::with_capacity(feat_len);+        for _ in 0..feat_len {+            if at + 2 > buf.len() {+                return err("a .mamaps turn-lane record ends inside its counts");+            }+            let (fwd_len, bwd_len) = (buf[at] as usize, buf[at + 1] as usize);+            at += 2;+            // Bounded against the slice before allocating, so a corrupt count cannot ask for a+            // gigabyte of `Vec` — the same discipline `parse_ids` and `parse_layer` apply.+            let masks = fwd_len + bwd_len;+            let bytes = masks.checked_mul(2).ok_or_else(|| {+                crate::proto::Error("a .mamaps turn-lane record overflows".to_string())+            })?;+            if at + bytes > buf.len() {+                return err("a .mamaps turn-lane record's masks run past the table");+            }+            let read = |n: usize, at: &mut usize| -> Vec<u16> {+                let mut v = Vec::with_capacity(n);+                for _ in 0..n {+                    v.push(u16::from_le_bytes([buf[*at], buf[*at + 1]]));+                    *at += 2;+                }+                v+            };+            let forward = read(fwd_len, &mut at);+            let backward = read(bwd_len, &mut at);+            turns.push(LaneTurns { forward, backward });+        }+        table.push((layer_id, turns));+    }+    let aligned = align4(at);+    if aligned > buf.len() {+        return err("a .mamaps turn-lane table's padding runs past the body");+    }+    Ok((table, aligned))+}++/// Serialise a turn-lane table, 4-byte aligned. The inverse of [`parse_lanes`].+fn serialize_lanes(turn_lanes: &[(u8, Vec<LaneTurns>)], out: &mut Vec<u8>) {+    out.extend_from_slice(&(turn_lanes.len() as u32).to_le_bytes());+    for (layer_id, entries) in turn_lanes {+        out.push(*layer_id);+        out.extend_from_slice(&[0u8; 3]);+        out.extend_from_slice(&(entries.len() as u32).to_le_bytes());+        for turns in entries {+            out.push(turns.forward.len() as u8);+            out.push(turns.backward.len() as u8);+            for mask in turns.forward.iter().chain(&turns.backward) {+                out.extend_from_slice(&mask.to_le_bytes());+            }+        }+    }+    while out.len() % 4 != 0 {+        out.push(0);+    }+}++/// Parse the carriageway table: a `u8` [`MarkingConvention`] byte for the whole tile, three+/// reserved bytes, then the same framing [`parse_lanes`] uses — a `u32` entry count, then per entry+/// a `u8` layer id, three reserved bytes, a `u32` record count and that many fixed 6-byte records+/// (`u8` forward, `u8` backward, `u32` solid divider bits, little-endian).+///+/// The convention leads rather than trailing so it is readable without walking the entries, and it+/// is per tile rather than per record because a tile never meaningfully spans two conventions.+///+/// Validation matches [`parse_lanes`] exactly: an entry must name a layer the body carries, entries+/// ascend and are distinct by layer id, and a layer's record vector is exactly as long as its+/// feature vector — a mismatch would misattribute every road's carriageway after the first.+fn parse_carriageways(+    buf: &[u8],+    layers: &[Layer],+) -> Result<(Vec<(u8, Vec<Carriageway>)>, MarkingConvention, usize)> {+    if buf.len() < 8 {+        return err("a .mamaps carriageway table ends before its header");+    }+    let convention = MarkingConvention::from_byte(buf[0])?;+    if buf[1] != 0 || u16::from_le_bytes([buf[2], buf[3]]) != 0 {+        return err("a .mamaps carriageway table has non-zero reserved bytes");+    }+    let count = u32::from_le_bytes([buf[4], buf[5], buf[6], buf[7]]) as usize;+    if count > layers.len() {+        return err(format!(+            "a .mamaps carriageway table has {count} entries for a body with {} layer(s)",+            layers.len(),+        ));+    }+    let mut at = 8usize;+    let mut table = Vec::with_capacity(count);+    let mut previous: Option<u8> = None;+    for _ in 0..count {+        if at + 8 > buf.len() {+            return err("a .mamaps carriageway table ends inside an entry header");+        }+        let layer_id = buf[at];+        if buf[at + 1] != 0 || u16::from_le_bytes([buf[at + 2], buf[at + 3]]) != 0 {+            return err("a .mamaps carriageway table entry has non-zero reserved bytes");+        }+        let feat_len =+            u32::from_le_bytes([buf[at + 4], buf[at + 5], buf[at + 6], buf[at + 7]]) as usize;+        at += 8;+        if previous.is_some_and(|p| layer_id <= p) {+            return err("a .mamaps carriageway table's entries are not ordered by layer id");+        }+        previous = Some(layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == layer_id) else {+            return err(format!(+                "a .mamaps carriageway table names layer {layer_id}, which the body does not carry"+            ));+        };+        if feat_len != layer.features.len() {+            return err(format!(+                "a .mamaps carriageway table gives layer {layer_id} {feat_len} record(s) for {} \+                 feature(s)",+                layer.features.len(),+            ));+        }+        // Bounded against the slice before allocating, so a corrupt count cannot ask for a+        // gigabyte of `Vec` — the same discipline `parse_lanes` and `parse_layer` apply.+        let bytes = feat_len.checked_mul(CARRIAGEWAY_RECORD_LEN).ok_or_else(|| {+            crate::proto::Error("a .mamaps carriageway table overflows".to_string())+        })?;+        if at + bytes > buf.len() {+            return err("a .mamaps carriageway table's records run past the table");+        }+        let mut rows = Vec::with_capacity(feat_len);+        for _ in 0..feat_len {+            rows.push(Carriageway {+                forward: buf[at],+                backward: buf[at + 1],+                solid_dividers: u32::from_le_bytes([+                    buf[at + 2],+                    buf[at + 3],+                    buf[at + 4],+                    buf[at + 5],+                ]),+            });+            at += CARRIAGEWAY_RECORD_LEN;+        }+        table.push((layer_id, rows));+    }+    let aligned = align4(at);+    if aligned > buf.len() {+        return err("a .mamaps carriageway table's padding runs past the body");+    }+    Ok((table, convention, aligned))+}++/// Serialise a carriageway table, 4-byte aligned. The inverse of [`parse_carriageways`].+fn serialize_carriageways(+    carriageways: &[(u8, Vec<Carriageway>)],+    convention: MarkingConvention,+    out: &mut Vec<u8>,+) {+    out.push(convention.to_byte());+    out.extend_from_slice(&[0u8; 3]);+    out.extend_from_slice(&(carriageways.len() as u32).to_le_bytes());+    for (layer_id, entries) in carriageways {+        out.push(*layer_id);+        out.extend_from_slice(&[0u8; 3]);+        out.extend_from_slice(&(entries.len() as u32).to_le_bytes());+        for row in entries {+            out.push(row.forward);+            out.push(row.backward);+            out.extend_from_slice(&row.solid_dividers.to_le_bytes());+        }+    }+    while out.len() % 4 != 0 {+        out.push(0);+    }+}++/// Parse the S3DB building attribute table: the same framing as [`parse_ids`] — a `u32` entry+/// count, then per entry a `u8` layer id, three reserved bytes, a `u32` record count and that many+/// fixed [`BUILDING_ATTRS_LEN`]-byte [`BuildingAttrs`] records. Returns the table and the bytes+/// consumed (including 4-byte alignment padding).+///+/// Validated against `layers` exactly as the id and turn-lane tables are: an entry must name a+/// layer the body carries, entries ascend and are distinct by layer id, and a layer's attr vector+/// is exactly as long as its feature vector — a mismatch would misattribute every building's+/// height after the first. `roof_shape`, `roof_orientation` and the reserved bytes are checked so a+/// corrupt record fails here rather than extruding a wrong shape on device.+fn parse_buildings(buf: &[u8], layers: &[Layer]) -> Result<(Vec<(u8, Vec<BuildingAttrs>)>, usize)> {+    if buf.len() < 4 {+        return err("a .mamaps building table ends before its count");+    }+    let count = u32::from_le_bytes([buf[0], buf[1], buf[2], buf[3]]) as usize;+    if count > layers.len() {+        return err(format!(+            "a .mamaps building table has {count} entries for a body with {} layer(s)",+            layers.len(),+        ));+    }+    let mut at = 4usize;+    let mut table = Vec::with_capacity(count);+    let mut previous: Option<u8> = None;+    for _ in 0..count {+        if at + 8 > buf.len() {+            return err("a .mamaps building table ends inside an entry header");+        }+        let layer_id = buf[at];+        if buf[at + 1] != 0 || u16::from_le_bytes([buf[at + 2], buf[at + 3]]) != 0 {+            return err("a .mamaps building table entry has non-zero reserved bytes");+        }+        let attrs_len =+            u32::from_le_bytes([buf[at + 4], buf[at + 5], buf[at + 6], buf[at + 7]]) as usize;+        at += 8;+        if previous.is_some_and(|p| layer_id <= p) {+            return err("a .mamaps building table's entries are not ordered by layer id");+        }+        previous = Some(layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == layer_id) else {+            return err(format!(+                "a .mamaps building table names layer {layer_id}, which the body does not carry"+            ));+        };+        if attrs_len != layer.features.len() {+            return err(format!(+                "a .mamaps building table gives layer {layer_id} {attrs_len} record(s) for {} \+                 feature(s)",+                layer.features.len(),+            ));+        }+        // Bounded against the slice before allocating, the same discipline `parse_ids` applies.+        let bytes = attrs_len.checked_mul(BUILDING_ATTRS_LEN).ok_or_else(|| {+            crate::proto::Error("a .mamaps building table's entry overflows".to_string())+        })?;+        if at + bytes > buf.len() {+            return err("a .mamaps building table's records run past the table");+        }+        let mut attrs = Vec::with_capacity(attrs_len);+        for i in 0..attrs_len {+            let o = at + i * BUILDING_ATTRS_LEN;+            let roof_shape = buf[o + 6];+            if roof_shape > ROOF_SHAPE_MAX {+                return err(format!("a .mamaps building record has roof shape {roof_shape}"));+            }+            let roof_orientation = buf[o + 8];+            if roof_orientation > ROOF_ORIENT_MAX {+                return err(format!(+                    "a .mamaps building record has roof orientation {roof_orientation}"+                ));+            }+            if buf[o + 9] != 0 || buf[o + 10] != 0 || buf[o + 11] != 0 {+                return err("a .mamaps building record has non-zero reserved bytes");+            }+            attrs.push(BuildingAttrs {+                height: u16::from_le_bytes([buf[o], buf[o + 1]]),+                min_height: u16::from_le_bytes([buf[o + 2], buf[o + 3]]),+                roof_height: u16::from_le_bytes([buf[o + 4], buf[o + 5]]),+                roof_shape,+                roof_direction: buf[o + 7],+                roof_orientation,+                building_colour: u32::from_le_bytes([+                    buf[o + 12],+                    buf[o + 13],+                    buf[o + 14],+                    buf[o + 15],+                ]),+                roof_colour: u32::from_le_bytes([+                    buf[o + 16],+                    buf[o + 17],+                    buf[o + 18],+                    buf[o + 19],+                ]),+            });+        }+        at += bytes;+        table.push((layer_id, attrs));+    }+    let aligned = align4(at);+    if aligned > buf.len() {+        return err("a .mamaps building table's padding runs past the body");+    }+    Ok((table, aligned))+}++/// Serialise an S3DB building attribute table, 4-byte aligned. The inverse of [`parse_buildings`].+fn serialize_buildings(buildings: &[(u8, Vec<BuildingAttrs>)], out: &mut Vec<u8>) {+    out.extend_from_slice(&(buildings.len() as u32).to_le_bytes());+    for (layer_id, entries) in buildings {+        out.push(*layer_id);+        out.extend_from_slice(&[0u8; 3]);+        out.extend_from_slice(&(entries.len() as u32).to_le_bytes());+        for a in entries {+            out.extend_from_slice(&a.height.to_le_bytes());+            out.extend_from_slice(&a.min_height.to_le_bytes());+            out.extend_from_slice(&a.roof_height.to_le_bytes());+            out.push(a.roof_shape);+            out.push(a.roof_direction);+            out.push(a.roof_orientation);+            out.extend_from_slice(&[0u8; 3]);+            out.extend_from_slice(&a.building_colour.to_le_bytes());+            out.extend_from_slice(&a.roof_colour.to_le_bytes());+        }+    }+    while out.len() % 4 != 0 {+        out.push(0);+    }+}++/// Parse the per-tile heightmap: a `u16` grid side length `dim`, then `dim * dim` `u16` samples,+/// row-major. Returns the grid and the bytes consumed (including 4-byte alignment padding).+///+/// A zero `dim` is refused rather than read as an empty grid: an elevation-free tile omits the+/// section (the flag is clear) instead of writing a degenerate one, so a zero here is corruption.+fn parse_heightmap(buf: &[u8]) -> Result<(Heightmap, usize)> {+    if buf.len() < 2 {+        return err("a .mamaps heightmap ends before its dimension");+    }+    let dim = u16::from_le_bytes([buf[0], buf[1]]);+    if dim == 0 {+        return err("a .mamaps heightmap has a zero dimension");+    }+    let cells = dim as usize * dim as usize;+    let bytes = cells+        .checked_mul(2)+        .ok_or_else(|| crate::proto::Error("a .mamaps heightmap's grid overflows".to_string()))?;+    let mut at = 2usize;+    if at + bytes > buf.len() {+        return err("a .mamaps heightmap's samples run past the body");+    }+    let mut samples = Vec::with_capacity(cells);+    for i in 0..cells {+        let o = at + i * 2;+        samples.push(u16::from_le_bytes([buf[o], buf[o + 1]]));+    }+    at += bytes;+    let aligned = align4(at);+    if aligned > buf.len() {+        return err("a .mamaps heightmap's padding runs past the body");+    }+    Ok((Heightmap { dim, samples }, aligned))+}++/// Serialise a per-tile heightmap, 4-byte aligned. The inverse of [`parse_heightmap`].+fn serialize_heightmap(grid: &Heightmap, out: &mut Vec<u8>) {+    out.extend_from_slice(&grid.dim.to_le_bytes());+    for &s in &grid.samples {+        out.extend_from_slice(&s.to_le_bytes());+    }+    while out.len() % 4 != 0 {+        out.push(0);+    }+}+ /// Serialise a body. /// /// Deliberately available without the `write` feature: a synthetic body is how the reader's own@@ -828,28 +1617,123 @@ pub fn serialize_into<'s>(body: &Body, scratch: &'s mut Scratch) -> Result<&'s [         }     } -    // The id table is keyed by layer id and parallel to that layer's features, so a caller that-    // built it against a different layer set would produce a body that reads back with every id-    // attributed to the wrong feature. Checked here rather than trusted, for the same reason the-    // parts contiguity above is.+    // The id table is keyed by layer id and parallel to that layer's features, so a caller that+    // built it against a different layer set would produce a body that reads back with every id+    // attributed to the wrong feature. Checked here rather than trusted, for the same reason the+    // parts contiguity above is.+    let mut previous: Option<u8> = None;+    for (layer_id, entries) in &body.ids {+        if previous.is_some_and(|p| *layer_id <= p) {+            return err("a .mamaps body's id table is not ordered by layer id");+        }+        previous = Some(*layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == *layer_id) else {+            return err(format!(+                "a .mamaps id table names layer {layer_id}, which the body does not carry"+            ));+        };+        if entries.len() != layer.features.len() {+            return err(format!(+                "a .mamaps id table gives layer {layer_id} {} id(s) for {} feature(s)",+                entries.len(),+                layer.features.len(),+            ));+        }+    }++    // The turn-lane table is validated exactly as the id table is: keyed and ascending by layer id+    // and dense-parallel to that layer's features, so a caller that built it against a different+    // layer set is refused here rather than producing a body that misattributes every lane.+    let mut previous: Option<u8> = None;+    for (layer_id, entries) in &body.turn_lanes {+        if previous.is_some_and(|p| *layer_id <= p) {+            return err("a .mamaps body's turn-lane table is not ordered by layer id");+        }+        previous = Some(*layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == *layer_id) else {+            return err(format!(+                "a .mamaps turn-lane table names layer {layer_id}, which the body does not carry"+            ));+        };+        if entries.len() != layer.features.len() {+            return err(format!(+                "a .mamaps turn-lane table gives layer {layer_id} {} record(s) for {} feature(s)",+                entries.len(),+                layer.features.len(),+            ));+        }+        for turns in entries {+            if turns.forward.len() > u8::MAX as usize || turns.backward.len() > u8::MAX as usize {+                return err(format!(+                    "a .mamaps turn-lane record on layer {layer_id} has more than {} lanes",+                    u8::MAX,+                ));+            }+        }+    }++    // The carriageway table, validated exactly as the turn-lane table above. The convention is+    // only written alongside a table, so a body that sets one with no rows would drop it silently+    // on the wire and come back `None` — refused here rather than round-tripping to something the+    // caller did not ask for.+    if body.convention.is_some() && body.carriageways.is_empty() {+        return err("a .mamaps body sets a marking convention but carries no carriageway table");+    }+    let mut previous: Option<u8> = None;+    for (layer_id, entries) in &body.carriageways {+        if previous.is_some_and(|p| *layer_id <= p) {+            return err("a .mamaps body's carriageway table is not ordered by layer id");+        }+        previous = Some(*layer_id);+        let Some(layer) = layers.iter().find(|l| l.layer_id == *layer_id) else {+            return err(format!(+                "a .mamaps carriageway table names layer {layer_id}, which the body does not carry"+            ));+        };+        if entries.len() != layer.features.len() {+            return err(format!(+                "a .mamaps carriageway table gives layer {layer_id} {} record(s) for {} feature(s)",+                entries.len(),+                layer.features.len(),+            ));+        }+    }++    // The building table is validated exactly as the id and turn-lane tables are: keyed and+    // ascending by layer id and dense-parallel to that layer's features, so a caller that built it+    // against a different layer set is refused rather than misattributing every building's height.     let mut previous: Option<u8> = None;-    for (layer_id, entries) in &body.ids {+    for (layer_id, entries) in &body.buildings {         if previous.is_some_and(|p| *layer_id <= p) {-            return err("a .mamaps body's id table is not ordered by layer id");+            return err("a .mamaps body's building table is not ordered by layer id");         }         previous = Some(*layer_id);         let Some(layer) = layers.iter().find(|l| l.layer_id == *layer_id) else {             return err(format!(-                "a .mamaps id table names layer {layer_id}, which the body does not carry"+                "a .mamaps building table names layer {layer_id}, which the body does not carry"             ));         };         if entries.len() != layer.features.len() {             return err(format!(-                "a .mamaps id table gives layer {layer_id} {} id(s) for {} feature(s)",+                "a .mamaps building table gives layer {layer_id} {} record(s) for {} feature(s)",                 entries.len(),                 layer.features.len(),             ));         }+        for a in entries {+            if a.roof_shape > ROOF_SHAPE_MAX {+                return err(format!(+                    "a .mamaps building record on layer {layer_id} has roof shape {}",+                    a.roof_shape,+                ));+            }+            if a.roof_orientation > ROOF_ORIENT_MAX {+                return err(format!(+                    "a .mamaps building record on layer {layer_id} has roof orientation {}",+                    a.roof_orientation,+                ));+            }+        }     }      let mut index_end = BODY_HEADER_LEN + layers.len() * LAYER_INDEX_LEN;@@ -890,12 +1774,12 @@ pub fn serialize_into<'s>(body: &Body, scratch: &'s mut Scratch) -> Result<&'s [             out.extend_from_slice(&feature.transit_color.to_le_bytes());             // Bytes 20..23 are the transit lane inputs, which the v2 record reserved and its             // decoder never validated — so they drop in with no version bump and an older reader-            // ignores them. 23 stays reserved zero, and the record stays 4-byte aligned like-            // every other fixed record.+            // ignores them. Byte 23 was the last reserved byte; v5 gives it to the roads lane+            // count, and the record stays 4-byte aligned like every other fixed record.             out.push(feature.transit_ordinal);             out.push(feature.transit_lanes);             out.push(feature.transit_taper);-            out.push(0);+            out.push(feature.lane_count);         }         for part in &layer.parts {             out.extend_from_slice(&part.coord_start.to_le_bytes());@@ -939,6 +1823,18 @@ pub fn serialize_into<'s>(body: &Body, scratch: &'s mut Scratch) -> Result<&'s [     if !body.ids.is_empty() {         body_flags |= BODY_FLAG_ID_TABLE;     }+    if !body.turn_lanes.is_empty() {+        body_flags |= BODY_FLAG_LANE_TABLE;+    }+    if !body.buildings.is_empty() {+        body_flags |= BODY_FLAG_BUILDING_TABLE;+    }+    if body.heightmap.is_some() {+        body_flags |= BODY_FLAG_HEIGHTMAP;+    }+    if !body.carriageways.is_empty() {+        body_flags |= BODY_FLAG_ROAD_LANES;+    }     assembled.push(body_flags);     assembled.extend_from_slice(&0u32.to_le_bytes());     for (i, (layer_id, feature_count)) in meta.iter().enumerate() {@@ -969,9 +1865,15 @@ pub fn serialize_into<'s>(body: &Body, scratch: &'s mut Scratch) -> Result<&'s [     for payload in &payloads[..layers.len()] {         assembled.extend_from_slice(payload);     }-    // The trailing sections, each omitted when empty so a tile with neither is payloads-then-end-    // exactly as the parser expects. Both are announced in `body_flags` above.-    if !body.names.is_empty() || !body.ids.is_empty() {+    // The trailing sections, each omitted when empty so a tile with none is payloads-then-end+    // exactly as the parser expects. Each is announced in `body_flags` above.+    if !body.names.is_empty()+        || !body.ids.is_empty()+        || !body.turn_lanes.is_empty()+        || !body.buildings.is_empty()+        || body.heightmap.is_some()+        || !body.carriageways.is_empty()+    {         while assembled.len() % 4 != 0 {             assembled.push(0);         }@@ -982,6 +1884,24 @@ pub fn serialize_into<'s>(body: &Body, scratch: &'s mut Scratch) -> Result<&'s [     if !body.ids.is_empty() {         serialize_ids(&body.ids, assembled);     }+    if !body.turn_lanes.is_empty() {+        serialize_lanes(&body.turn_lanes, assembled);+    }+    if !body.buildings.is_empty() {+        serialize_buildings(&body.buildings, assembled);+    }+    if let Some(grid) = &body.heightmap {+        serialize_heightmap(grid, assembled);+    }+    if !body.carriageways.is_empty() {+        // The convention only reaches the wire alongside a table, so a body that sets one without+        // any carriageway rows would silently drop it. Validation above rejects that.+        serialize_carriageways(+            &body.carriageways,+            body.convention.unwrap_or_default(),+            assembled,+        );+    }     let raw_len = assembled.len() as u32;     assembled[4..8].copy_from_slice(&raw_len.to_le_bytes());     Ok(assembled)@@ -1028,6 +1948,7 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         water.parts.push(Part { coord_start: 0, point_count: 4, winding: WINDING_OUTER });         water.parts.push(Part { coord_start: 4, point_count: 4, winding: WINDING_HOLE });@@ -1055,11 +1976,12 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         roads.parts.push(Part { coord_start: 0, point_count: 3, winding: WINDING_OUTER });         roads.coords = vec![(-64, 10), (2048, 2048), (4160, 4000)]; -        Body { extent: DEFAULT_EXTENT, layers: vec![roads, water], names: Vec::new(), ids: Vec::new() }+        Body { extent: DEFAULT_EXTENT, layers: vec![roads, water], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None }     }      #[test]@@ -1132,11 +2054,12 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         let points: Vec<(i16, i16)> = (0..1000).map(|i| (i * 3, i * 2)).collect();         roads.parts.push(Part { coord_start: 0, point_count: 1000, winding: WINDING_OUTER });         roads.coords = points.clone();-        let body = Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() };+        let body = Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         let bytes = serialize(&body).expect("serialize");         // Two bytes a point rather than four: each delta is (3, 2), a single varint byte each.         assert!(@@ -1165,13 +2088,14 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         layer.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });         layer.parts.push(Part { coord_start: 2, point_count: 2, winding: WINDING_OUTER });         // The second part starts far from where the first ended; if deltas carried over, the         // round trip would place it somewhere else entirely.         layer.coords = vec![(0, 0), (10, 10), (3000, 3000), (3010, 3010)];-        let body = Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() };+        let body = Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");         assert_eq!(parsed, body);     }@@ -1194,18 +2118,19 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         layer.parts.push(Part { coord_start: 1, point_count: 2, winding: WINDING_OUTER });         layer.coords = vec![(0, 0), (1, 1), (2, 2)];-        let gapped = Body { extent: DEFAULT_EXTENT, layers: vec![layer.clone()], names: Vec::new(), ids: Vec::new() };+        let gapped = Body { extent: DEFAULT_EXTENT, layers: vec![layer.clone()], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         assert!(serialize(&gapped).is_err(), "a part starting past the front");          layer.parts[0].coord_start = 0;-        let over = Body { extent: DEFAULT_EXTENT, layers: vec![layer.clone()], names: Vec::new(), ids: Vec::new() };+        let over = Body { extent: DEFAULT_EXTENT, layers: vec![layer.clone()], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         assert!(serialize(&over).is_err(), "an arena longer than its parts cover");          layer.coords.pop();-        assert!(serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() }).is_ok());+        assert!(serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new(), turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None }).is_ok());     }      #[test]@@ -1223,10 +2148,11 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         layer.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });         layer.coords = vec![(0, 0), (1, 1)];-        assert!(serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() }).is_err());+        assert!(serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new(), turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None }).is_err());     }      #[test]@@ -1267,10 +2193,11 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         boundaries.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });         boundaries.coords = vec![(0, 0), (100, 100)];-        let bytes = serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![boundaries], names: Vec::new(), ids: Vec::new() })+        let bytes = serialize(&Body { extent: DEFAULT_EXTENT, layers: vec![boundaries], names: Vec::new(), ids: Vec::new(), turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None })             .expect("serialize");         let body = Body::parse(&bytes).expect("parse");         let feature = &body.layer(dict::LAYER_BOUNDARIES).expect("boundaries").features[0];@@ -1288,7 +2215,7 @@ mod tests {      #[test]     fn a_layer_with_no_features_still_round_trips() {-        let body = Body { extent: DEFAULT_EXTENT, layers: vec![Layer::new(dict::LAYER_EARTH)], names: Vec::new(), ids: Vec::new() };+        let body = Body { extent: DEFAULT_EXTENT, layers: vec![Layer::new(dict::LAYER_EARTH)], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");         assert_eq!(parsed, body);     }@@ -1300,6 +2227,9 @@ mod tests {             layers: vec![Layer::new(dict::LAYER_WATER), Layer::new(dict::LAYER_WATER)],             names: Vec::new(),             ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,         };         assert!(serialize(&body).is_err());     }@@ -1360,11 +2290,12 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         poi.parts.push(Part { coord_start: 0, point_count: 1, winding: WINDING_OUTER });         poi.coords = vec![(100, 200)];         let body =-            Body { extent: DEFAULT_EXTENT, layers: vec![poi], names: vec!["Cafe".to_string()], ids: Vec::new() };+            Body { extent: DEFAULT_EXTENT, layers: vec![poi], names: vec!["Cafe".to_string()], ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");         assert_eq!(parsed, body);         let feature = &parsed.layer(dict::LAYER_POI).expect("poi").features[0];@@ -1402,6 +2333,7 @@ mod tests {                 transit_ordinal: 0,                 transit_lanes: 0,                 transit_taper: 0,+                lane_count: 0,             });             poi.parts.push(Part {                 coord_start: u32::from(name_idx) - 1,@@ -1416,6 +2348,9 @@ mod tests {             names: vec!["Cafe".to_string(), "Bar".to_string()],             // A real OSM id and ID_NONE for a feature the generator could not attribute.             ids: vec![(dict::LAYER_POI, vec![12_345_678_901, ID_NONE])],+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,         };         let bytes = serialize(&body).expect("serialize");         let parsed = Body::parse(&bytes).expect("parse");@@ -1426,6 +2361,89 @@ mod tests {         assert_eq!(parsed.feature_id(dict::LAYER_ROADS, 0), None, "a layer with no ids");     } +    /// The turn-lane table rides beside the id and name tables, dense-parallel to a layer's+    /// features: a road with `turn:lanes` carries its per-lane masks (forward and backward) and+    /// one without carries an empty record, both surviving the round trip. This is the wire half+    /// of the per-lane turn arrows.+    #[test]+    fn the_turn_lane_table_round_trips_dense_parallel_to_features() {+        let mut roads = Layer::new(dict::LAYER_ROADS);+        for i in 0..3u32 {+            roads.features.push(Feature {+                kind: 45,+                kind_detail: 0,+                geom_type: GEOM_LINE,+                flags: 0,+                name_idx: NAME_NONE,+                parts_offset: i,+                part_count: 1,+                transit_color: 0,+                transit_ordinal: 0,+                transit_lanes: 0,+                transit_taper: 0,+                lane_count: if i == 1 { 3 } else { 0 },+            });+            roads.parts.push(Part { coord_start: i * 2, point_count: 2, winding: WINDING_OUTER });+            roads.coords.extend_from_slice(&[(0, i as i16), (10, i as i16)]);+        }+        // Feature 1 is a three-lane road with turn arrows; the others carry none.+        let turns = vec![+            LaneTurns::default(),+            LaneTurns { forward: vec![1, 2, 6], backward: vec![4] },+            LaneTurns::default(),+        ];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: vec![(dict::LAYER_ROADS, turns)],+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,+        };+        let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");+        assert_eq!(parsed, body);+        let one = parsed.feature_turns(dict::LAYER_ROADS, 1).expect("a record");+        assert_eq!(one.forward, vec![1, 2, 6]);+        assert_eq!(one.backward, vec![4]);+        assert!(parsed.feature_turns(dict::LAYER_ROADS, 0).expect("a record").is_empty());+        assert_eq!(parsed.feature_turns(dict::LAYER_ROADS, 3), None, "past the table");+    }++    /// A turn-lane vector that is not exactly as long as its layer's features would misattribute+    /// every lane past the mismatch, so it is refused on the way out just as the id table is.+    #[test]+    fn a_turn_lane_table_that_does_not_match_its_layer_is_refused() {+        let mut roads = Layer::new(dict::LAYER_ROADS);+        roads.features.push(Feature {+            kind: 45,+            kind_detail: 0,+            geom_type: GEOM_LINE,+            flags: 0,+            name_idx: NAME_NONE,+            parts_offset: 0,+            part_count: 1,+            transit_color: 0,+            transit_ordinal: 0,+            transit_lanes: 0,+            transit_taper: 0,+            lane_count: 2,+        });+        roads.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });+        roads.coords = vec![(0, 0), (10, 0)];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads],+            names: Vec::new(),+            ids: Vec::new(),+            // Two records for one feature.+            turn_lanes: vec![(dict::LAYER_ROADS, vec![LaneTurns::default(), LaneTurns::default()])],+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,+        };+        assert!(serialize(&body).is_err(), "a turn table longer than its layer's features");+    }+     /// An id table with no name table beside it still parses. This is the case the v2 layout     /// could not express: it found the name table by "are there bytes left", which an id table     /// alone would have been mistaken for.@@ -1444,6 +2462,7 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         poi.parts.push(Part { coord_start: 0, point_count: 1, winding: WINDING_OUTER });         poi.coords = vec![(1, 2)];@@ -1452,6 +2471,9 @@ mod tests {             layers: vec![poi],             names: Vec::new(),             ids: vec![(dict::LAYER_POI, vec![7])],+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,         };         let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");         assert_eq!(parsed, body);@@ -1467,6 +2489,9 @@ mod tests {             layers: vec![Layer::new(dict::LAYER_EARTH)],             names: Vec::new(),             ids,+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,         };         assert!(             serialize(&body(vec![(dict::LAYER_EARTH, vec![1])])).is_err(),@@ -1510,6 +2535,7 @@ mod tests {                 transit_ordinal: *ordinal,                 transit_lanes: *lanes,                 transit_taper: *taper,+                lane_count: 0,             });         }         let mut body = Body::new(DEFAULT_EXTENT);@@ -1609,12 +2635,13 @@ mod tests {                 transit_ordinal: 0,                 transit_lanes: 0,                 transit_taper: 0,+                lane_count: 0,             });             layer.parts.push(Part { coord_start: (i * 2) as u32, point_count: 2, winding: WINDING_OUTER });             layer.coords.push((0, 0));             layer.coords.push((1, 1));         }-        let body = Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() };+        let body = Body { extent: DEFAULT_EXTENT, layers: vec![layer], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None, carriageways: Vec::new(), convention: None};         let bytes = serialize(&body).expect("extended tile must serialize");         assert_eq!(bytes[11], BODY_FLAG_EXTENDED_COUNTS, "extended flag set");         let parsed = Body::parse(&bytes).expect("must parse extended");@@ -1625,16 +2652,437 @@ mod tests {             extent: DEFAULT_EXTENT,             layers: vec![{             let mut l = Layer::new(10);-            l.features.push(Feature { kind: 1, kind_detail: 0, geom_type: GEOM_LINE, flags: 0, name_idx: NAME_NONE, parts_offset: 0, part_count: 1, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0 });+            l.features.push(Feature { kind: 1, kind_detail: 0, geom_type: GEOM_LINE, flags: 0, name_idx: NAME_NONE, parts_offset: 0, part_count: 1, transit_color: 0, transit_ordinal: 0, transit_lanes: 0, transit_taper: 0, lane_count: 0 });             l.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });             l.coords.extend_from_slice(&[(0, 0), (1, 1)]);             l             }],             names: Vec::new(),             ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None, carriageways: Vec::new(), convention: None,         };         let small_bytes = serialize(&small_body).expect("small tile");         assert_eq!(small_bytes[11], 0, "common path no flag, byte-identical");         assert_eq!(small_bytes[12..16], [0, 0, 0, 0], "reserved still zero for common path");     }++    /// The S3DB building table rides beside the name and id tables, dense-parallel to the+    /// `buildings` layer's features: a tagged building carries its height, roof and colours, a+    /// plain one a default record, and both survive the round trip. This is the wire half of the+    /// 3D building extrusion.+    #[test]+    fn the_building_table_round_trips_dense_parallel_to_features() {+        let mut buildings = Layer::new(dict::LAYER_BUILDINGS);+        for i in 0..3u32 {+            buildings.features.push(Feature {+                kind: 51,+                kind_detail: 0,+                geom_type: GEOM_POLYGON,+                flags: 0,+                name_idx: NAME_NONE,+                parts_offset: i,+                part_count: 1,+                transit_color: 0,+                transit_ordinal: 0,+                transit_lanes: 0,+                transit_taper: 0,+                lane_count: 0,+            });+            buildings.parts.push(Part {+                coord_start: i * 4,+                point_count: 4,+                winding: WINDING_OUTER,+            });+            let b = i as i16;+            buildings.coords.extend_from_slice(&[(0, b), (10, b), (10, b + 10), (0, b + 10)]);+        }+        // A tall gabled tower with colours, a floating glass part, and a plain default building.+        let attrs = vec![+            BuildingAttrs {+                height: 1200,+                min_height: 0,+                roof_height: 300,+                roof_shape: ROOF_GABLED,+                roof_direction: 64,+                roof_orientation: ROOF_ORIENT_ACROSS,+                building_colour: 0xFF_C8_A0_78,+                roof_colour: 0xFF_80_20_20,+            },+            BuildingAttrs {+                height: 400,+                min_height: 250,+                roof_height: 0,+                roof_shape: ROOF_FLAT,+                roof_direction: 0,+                roof_orientation: ROOF_ORIENT_ALONG,+                building_colour: 0,+                roof_colour: 0,+            },+            BuildingAttrs::default(),+        ];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![buildings],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: vec![(dict::LAYER_BUILDINGS, attrs.clone())],+            heightmap: None, carriageways: Vec::new(), convention: None,+        };+        let bytes = serialize(&body).expect("serialize");+        // The body carries the reader's own version byte, so an older reader rejects it cleanly.+        // Asserted against the constant rather than a literal: this test is about the building+        // table, and pinning the number here made every later format bump fail here first.+        assert_eq!(bytes[3], super::super::header::FORMAT_VERSION, "the body's format version");+        assert_ne!(bytes[11] & BODY_FLAG_BUILDING_TABLE, 0, "the building flag is set");+        let parsed = Body::parse(&bytes).expect("parse");+        assert_eq!(parsed, body);+        assert_eq!(parsed.building_attrs(dict::LAYER_BUILDINGS, 0), Some(attrs[0]));+        assert_eq!(parsed.building_attrs(dict::LAYER_BUILDINGS, 1), Some(attrs[1]));+        assert_eq!(+            parsed.building_attrs(dict::LAYER_BUILDINGS, 2),+            Some(BuildingAttrs::default()),+        );+        assert_eq!(parsed.building_attrs(dict::LAYER_BUILDINGS, 3), None, "past the table");+        assert_eq!(parsed.building_attrs(dict::LAYER_ROADS, 0), None, "a layer with no table");+    }++    /// Build a `roads` layer of `n` bare line features, for the carriageway tests.+    fn roads_layer(n: u32) -> Layer {+        let mut roads = Layer::new(dict::LAYER_ROADS);+        for i in 0..n {+            roads.features.push(Feature {+                kind: 1,+                kind_detail: 0,+                geom_type: GEOM_LINE,+                flags: 0,+                name_idx: NAME_NONE,+                parts_offset: i,+                part_count: 1,+                transit_color: 0,+                transit_ordinal: 0,+                transit_lanes: 0,+                transit_taper: 0,+                lane_count: 4,+            });+            roads.parts.push(Part {+                coord_start: i * 2,+                point_count: 2,+                winding: WINDING_OUTER,+            });+            let a = i as i16 * 10;+            roads.coords.extend_from_slice(&[(a, 0), (a, 100)]);+        }+        roads+    }++    /// The carriageway table rides after the heightmap, dense-parallel to the `roads` layer, and+    /// carries the tile's marking convention with it. This is the wire half of the lane-marking+    /// renderer: without the directional split there is nowhere to put a centre line.+    #[test]+    fn the_carriageway_table_round_trips_with_its_convention() {+        let roads = roads_layer(3);+        // A two-way with an even split and a solid inner divider, a one-way whose lanes all run+        // forward, and a road nothing is known about.+        let rows = vec![+            Carriageway { forward: 2, backward: 2, solid_dividers: 0b010 },+            Carriageway { forward: 4, backward: 0, solid_dividers: 0 },+            Carriageway::default(),+        ];+        let convention = MarkingConvention { left_hand: false, yellow_centre: true };+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: vec![(dict::LAYER_ROADS, rows.clone())],+            convention: Some(convention),+        };+        let bytes = serialize(&body).expect("serialize");+        assert_ne!(bytes[11] & BODY_FLAG_ROAD_LANES, 0, "the carriageway flag is set");+        let parsed = Body::parse(&bytes).expect("parse");+        assert_eq!(parsed, body);+        assert_eq!(parsed.convention, Some(convention));+        assert_eq!(parsed.feature_carriageway(dict::LAYER_ROADS, 0), Some(&rows[0]));+        assert_eq!(parsed.feature_carriageway(dict::LAYER_ROADS, 1), Some(&rows[1]));+        assert_eq!(+            parsed.feature_carriageway(dict::LAYER_ROADS, 2),+            Some(&Carriageway::default()),+            "a road nothing is known about is a default record, not an absent one",+        );+        assert_eq!(parsed.feature_carriageway(dict::LAYER_ROADS, 3), None, "past the table");+        assert_eq!(parsed.feature_carriageway(dict::LAYER_EARTH, 0), None, "a layer with no table");+    }++    /// A tile with no carriageway table costs nothing and reports no convention, so every archive+    /// that predates lane markings stays exactly as cheap as it was.+    #[test]+    fn a_body_with_no_carriageway_table_carries_neither_flag_nor_convention() {+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads_layer(1)],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: Vec::new(),+            convention: None,+        };+        let bytes = serialize(&body).expect("serialize");+        assert_eq!(bytes[11] & BODY_FLAG_ROAD_LANES, 0, "no carriageway flag");+        let parsed = Body::parse(&bytes).expect("parse");+        assert_eq!(parsed.convention, None);+        assert!(parsed.carriageways.is_empty());+    }++    /// The convention only reaches the wire alongside a table, so setting one without any rows+    /// would silently come back `None`. Refused on the way out rather than round-tripping to+    /// something the caller did not ask for.+    #[test]+    fn a_convention_without_a_carriageway_table_is_refused() {+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads_layer(1)],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: Vec::new(),+            convention: Some(MarkingConvention { left_hand: true, yellow_centre: false }),+        };+        assert!(serialize(&body).is_err());+    }++    /// A carriageway vector that is not exactly as long as its layer's features would misattribute+    /// every road's lane split after the first, so it is refused on the way out just as the id,+    /// turn-lane and building tables are.+    #[test]+    fn a_carriageway_table_that_does_not_match_its_layer_is_refused() {+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads_layer(2)],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: vec![(dict::LAYER_ROADS, vec![Carriageway::default()])],+            convention: Some(MarkingConvention::default()),+        };+        assert!(serialize(&body).is_err());+    }++    /// The convention byte's spare bits are reserved, not ignored: a reader that accepted them+    /// would silently drop whatever a later format put there.+    #[test]+    fn a_marking_convention_with_reserved_bits_is_refused() {+        assert_eq!(+            MarkingConvention::from_byte(0b11).expect("both known bits"),+            MarkingConvention { left_hand: true, yellow_centre: true },+        );+        assert!(MarkingConvention::from_byte(0b100).is_err(), "a reserved bit");+    }++    /// `oneway` rides a feature flag rather than the carriageway table because `coalesce` already+    /// keys on `flags`, so a one-way and a two-way road of the same class cannot merge. Pins the+    /// bit value, which is an on-disk contract with the tiler.+    #[test]+    fn the_oneway_flag_round_trips_and_keeps_its_bit() {+        assert_eq!(FLAG_IS_ONEWAY, 1 << 4);+        let mut roads = roads_layer(1);+        roads.features[0].flags = FLAG_IS_ONEWAY | FLAG_IS_BRIDGE;+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![roads],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            carriageways: Vec::new(),+            convention: None,+        };+        let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");+        let feature = &parsed.layers[0].features[0];+        assert!(feature.is_oneway());+        assert!(feature.is_bridge(), "and the flags it shares a byte with are untouched");+        assert!(!feature.is_tunnel());+    }++    /// A building attr vector that is not exactly as long as its layer's features would+    /// misattribute every building's height after the first, so it is refused on the way out just+    /// as the id and turn-lane tables are.+    #[test]+    fn a_building_table_that_does_not_match_its_layer_is_refused() {+        let mut buildings = Layer::new(dict::LAYER_BUILDINGS);+        buildings.features.push(Feature {+            kind: 51,+            kind_detail: 0,+            geom_type: GEOM_POLYGON,+            flags: 0,+            name_idx: NAME_NONE,+            parts_offset: 0,+            part_count: 1,+            transit_color: 0,+            transit_ordinal: 0,+            transit_lanes: 0,+            transit_taper: 0,+            lane_count: 0,+        });+        buildings.parts.push(Part { coord_start: 0, point_count: 4, winding: WINDING_OUTER });+        buildings.coords = vec![(0, 0), (10, 0), (10, 10), (0, 10)];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![buildings],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            // Two records for one feature.+            buildings: vec![(+                dict::LAYER_BUILDINGS,+                vec![BuildingAttrs::default(), BuildingAttrs::default()],+            )],+            heightmap: None, carriageways: Vec::new(), convention: None,+        };+        assert!(serialize(&body).is_err(), "a building table longer than its layer's features");+    }++    /// A roof shape or orientation the format does not define is corruption on parse: a reader+    /// that extruded an out-of-range shape would guess at the geometry. Injected into a serialised+    /// body's building record rather than constructed, because `serialize` refuses it too.+    #[test]+    fn an_out_of_range_roof_value_is_refused_on_parse() {+        let mut buildings = Layer::new(dict::LAYER_BUILDINGS);+        buildings.features.push(Feature {+            kind: 51,+            kind_detail: 0,+            geom_type: GEOM_POLYGON,+            flags: 0,+            name_idx: NAME_NONE,+            parts_offset: 0,+            part_count: 1,+            transit_color: 0,+            transit_ordinal: 0,+            transit_lanes: 0,+            transit_taper: 0,+            lane_count: 0,+        });+        buildings.parts.push(Part { coord_start: 0, point_count: 4, winding: WINDING_OUTER });+        buildings.coords = vec![(0, 0), (10, 0), (10, 10), (0, 10)];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![buildings],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: vec![(dict::LAYER_BUILDINGS, vec![BuildingAttrs::default()])],+            heightmap: None, carriageways: Vec::new(), convention: None,+        };+        let good = serialize(&body).expect("serialize");+        assert!(Body::parse(&good).is_ok(), "the default record is otherwise fine");+        // The building record's roof_shape byte is at offset 6 of the single record. Find the+        // record by walking to the trailing sections: locate the roof byte by scanning for the+        // known table start is fiddly, so instead corrupt via a fresh serialise with a bad shape,+        // which serialize refuses — proving the encoder guards it too.+        let bad_shape = Body {+            buildings: vec![(+                dict::LAYER_BUILDINGS,+                vec![BuildingAttrs { roof_shape: 99, ..BuildingAttrs::default() }],+            )],+            ..body.clone()+        };+        assert!(serialize(&bad_shape).is_err(), "the encoder refuses an unknown roof shape");+        let bad_orient = Body {+            buildings: vec![(+                dict::LAYER_BUILDINGS,+                vec![BuildingAttrs { roof_orientation: 7, ..BuildingAttrs::default() }],+            )],+            ..body+        };+        assert!(serialize(&bad_orient).is_err(), "the encoder refuses an unknown orientation");+    }++    /// The per-tile heightmap round-trips: a fixed `u16` grid rides after the other trailing+    /// sections, and an elevation-free tile omits it and stays as cheap as it was. This is the wire+    /// half of the 3D terrain relief.+    #[test]+    fn the_heightmap_round_trips_and_ocean_tiles_omit_it() {+        let dim = 5u16;+        let samples: Vec<u16> =+            (0..(dim as u32 * dim as u32)).map(|i| 32768 + i as u16 * 3).collect();+        let grid = Heightmap { dim, samples };+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![Layer::new(dict::LAYER_EARTH)],+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: Some(grid.clone()),+            carriageways: Vec::new(),+            convention: None,+        };+        let bytes = serialize(&body).expect("serialize");+        assert_ne!(bytes[11] & BODY_FLAG_HEIGHTMAP, 0, "the heightmap flag is set");+        let parsed = Body::parse(&bytes).expect("parse");+        assert_eq!(parsed, body);+        let read = parsed.heightmap.expect("a heightmap");+        assert_eq!(read.sample(0, 0), Some(32768), "sea level at the top-left");+        assert_eq!(Heightmap::metres(32768), 0, "the bias puts sea level at 32768");+        assert_eq!(read.sample(dim, 0), None, "out of range");++        // An ocean tile carries no grid and pays nothing for the section.+        let ocean = Body { heightmap: None, ..body };+        let ocean_bytes = serialize(&ocean).expect("serialize");+        assert_eq!(ocean_bytes[11] & BODY_FLAG_HEIGHTMAP, 0, "no heightmap flag on an ocean tile");+        assert!(ocean_bytes.len() < bytes.len(), "the ocean tile is smaller");+    }++    /// Both new v6 side tables ride together after the name/id/turn-lane chain, in the fixed order+    /// the parser walks them. The case that matters: a tile with buildings *and* terrain.+    #[test]+    fn the_building_table_and_heightmap_round_trip_together() {+        let mut buildings = Layer::new(dict::LAYER_BUILDINGS);+        buildings.features.push(Feature {+            kind: 51,+            kind_detail: 0,+            geom_type: GEOM_POLYGON,+            flags: 0,+            name_idx: NAME_NONE,+            parts_offset: 0,+            part_count: 1,+            transit_color: 0,+            transit_ordinal: 0,+            transit_lanes: 0,+            transit_taper: 0,+            lane_count: 0,+        });+        buildings.parts.push(Part { coord_start: 0, point_count: 4, winding: WINDING_OUTER });+        buildings.coords = vec![(0, 0), (10, 0), (10, 10), (0, 10)];+        let attrs = vec![BuildingAttrs {+            height: 250,+            roof_shape: ROOF_PYRAMIDAL,+            ..BuildingAttrs::default()+        }];+        let body = Body {+            extent: DEFAULT_EXTENT,+            layers: vec![buildings],+            names: vec!["Tower".to_string()],+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: vec![(dict::LAYER_BUILDINGS, attrs)],+            heightmap: Some(Heightmap { dim: 3, samples: vec![32768; 9] }),+            carriageways: Vec::new(),+            convention: None,+        };+        let parsed = Body::parse(&serialize(&body).expect("serialize")).expect("parse");+        assert_eq!(parsed, body);+    } }
library/tilecodec/src/main/rust/src/mamaps/dict.rs
diff --git a/library/tilecodec/src/main/rust/src/mamaps/dict.rs b/library/tilecodec/src/main/rust/src/mamaps/dict.rsindex b419ce2be7..e2176a6195 100644--- a/library/tilecodec/src/main/rust/src/mamaps/dict.rs+++ b/library/tilecodec/src/main/rust/src/mamaps/dict.rs@@ -22,7 +22,7 @@ //! //! # Size //!-//! Ten layers, ~108 kinds and 41 details, at a length byte each: under 2 KiB as measured,+//! Eleven layers, ~108 kinds and 41 details, at a length byte each: under 2 KiB as measured, //! which is what lets the header, the dictionary and the root index share one 16 KiB opening //! read. @@ -37,9 +37,11 @@ pub const NONE: u16 = 0;  /// The layers this format carries, in draw order. ///-/// A layer's id **is** its index here. Ten: the seven the style draws, plus `places`-/// (labels), `poi` (icons) and `transit` (reserved by v2; populated when transit lands).-/// `u8` ids fit with room to spare.+/// A layer's id **is** its index here. Eleven: the seven the style draws, plus `places`+/// (labels), `poi` (icons), `transit` (reserved by v2; populated when transit lands) and+/// `traffic` (v4: one line per drivable component segment, recoloured live from a pushed+/// id→speed table — geometry from the v6 routing graph, not the basemap). `u8` ids fit with+/// room to spare. pub const LAYERS: &[&str] = &[     "earth",     "water",@@ -51,6 +53,7 @@ pub const LAYERS: &[&str] = &[     "places",     "poi",     "transit",+    "traffic", ];  pub const LAYER_EARTH: u8 = 0;@@ -63,6 +66,11 @@ pub const LAYER_BUILDINGS: u8 = 6; pub const LAYER_PLACES: u8 = 7; pub const LAYER_POI: u8 = 8; pub const LAYER_TRANSIT: u8 = 9;+/// v4. One `GEOM_LINE` feature per drivable **component** segment of the v6 routing graph,+/// each carrying its packed `component_id` in the body's id side table so the renderer can+/// recolour it from a live id→speed push. Excluded from the basemap style: it draws only+/// when the traffic overlay is enabled.+pub const LAYER_TRAFFIC: u8 = 10;  /// Every `kind` value the schema can emit, id 1 upward. Index 0 is [`NONE`]. ///@@ -456,7 +464,8 @@ mod tests {         assert_eq!(d.layer_name(LAYER_PLACES), Some("places"), "v2's label layer");         assert_eq!(d.layer_name(LAYER_POI), Some("poi"), "v2's icon layer");         assert_eq!(d.layer_name(LAYER_TRANSIT), Some("transit"), "v2 reserves transit");-        assert_eq!(d.layer_name(10), None, "past the table");+        assert_eq!(d.layer_name(LAYER_TRAFFIC), Some("traffic"), "v4's live traffic layer");+        assert_eq!(d.layer_name(11), None, "past the table");     }      /// A duplicate would give one value two ids, so half the features carrying it would filter
library/tilecodec/src/main/rust/src/mamaps/from_mvt.rs
diff --git a/library/tilecodec/src/main/rust/src/mamaps/from_mvt.rs b/library/tilecodec/src/main/rust/src/mamaps/from_mvt.rsindex e2d6bde817..9460b177c8 100644--- a/library/tilecodec/src/main/rust/src/mamaps/from_mvt.rs+++ b/library/tilecodec/src/main/rust/src/mamaps/from_mvt.rs@@ -141,7 +141,22 @@ pub fn from_tile(tile: &Tile) -> Result<(Body, Stats)> {     let extent = u16::try_from(extent).map_err(|_| {         crate::proto::Error(format!("an MVT extent of {extent} does not fit a .mamaps body"))     })?;-    Ok((Body { extent, layers, names: Vec::new(), ids: Vec::new() }, stats))+    Ok((+        Body {+            extent,+            layers,+            names: Vec::new(),+            ids: Vec::new(),+            turn_lanes: Vec::new(),+            buildings: Vec::new(),+            heightmap: None,+            // MVT has no lane or carriageway data of any kind, so a body converted from one never+            // carries the table and the renderer falls back to a plain stroke.+            carriageways: Vec::new(),+            convention: None,+        },+        stats,+    )) }  /// Append one feature and its parts, skipping degenerate paths.@@ -192,6 +207,7 @@ fn push_feature(         transit_ordinal: 0,         transit_lanes: 0,         transit_taper: 0,+        lane_count: 0,     }); } 
library/tilecodec/src/main/rust/src/mamaps/header.rs
diff --git a/library/tilecodec/src/main/rust/src/mamaps/header.rs b/library/tilecodec/src/main/rust/src/mamaps/header.rsindex 60e4d0152d..975a204286 100644--- a/library/tilecodec/src/main/rust/src/mamaps/header.rs+++ b/library/tilecodec/src/main/rust/src/mamaps/header.rs@@ -17,12 +17,44 @@ pub const MAGIC: &[u8; 7] = b"MAMAPS\0"; /// Bumped only for a change a reader cannot ignore. The archive carries a /// [`build_id`](Header::build_id) for "same format, different data". ///+/// v7 adds what the carriageway renderer needs to paint lane markings onto a road surface rather+/// than stroke parallel lines over it: a `FLAG_IS_ONEWAY` feature flag (bit 4), and one more+/// optional trailing body section behind `BODY_FLAG_ROAD_LANES` — a per-road **carriageway table**+/// (dense-parallel to the `roads` layer: the `lanes:forward`/`lanes:backward` split that says where+/// the centre line goes, and which dividers are solid) plus a per-tile **marking convention** byte+/// (left- or right-hand traffic, yellow or white centre line). The 24-byte feature record is+/// unchanged. The bump is forced twice over: an older reader rejects both an unknown body flag and+/// an unknown feature flag bit, so a v7 archive is a clean rejection rather than a wrong map.+///+/// v6 adds two optional trailing body sections behind new body flags: a per-building **S3DB+/// attribute table** (`BODY_FLAG_BUILDING_TABLE`, dense-parallel to the `buildings` layer —+/// heights, roof shape/height/direction/orientation and wall/roof colours for 3D extrusion) and a+/// per-tile **DEM heightmap** grid (`BODY_FLAG_HEIGHTMAP`, `u16` metres for 3D terrain relief).+/// The 24-byte feature record is unchanged, so the bump is not forced by the record width — it is+/// forced because an older reader rejects an unknown body flag, and would otherwise silently draw a+/// v6 tile flat. `Body::parse` refuses a version it does not speak, so an old v5 archive is a clean+/// rejection rather than a wrong map.+///+/// v5 bakes lane data into the `roads` layer: a per-feature **lane count** in the byte the+/// feature record kept reserved (byte 23), which the renderer expands into that many parallel+/// carriageway lanes with dividers at high zoom. The record width is unchanged, so the bump is+/// not forced by the dictionary (no new `kind`/`kind_detail`) — it is forced because an older+/// reader would read a v5 body's lane byte as the reserved zero it always was and draw every+/// multi-lane road as a single line. `Body::parse` refuses a version it does not speak, so the+/// mismatch is a clean rejection rather than a silently wrong map.+///+/// v4 adds the `traffic` layer (id 10) to [`dict::LAYERS`](super::dict::LAYERS): one+/// `GEOM_LINE` feature per drivable component segment of the v6 routing graph, each carrying+/// its packed `component_id` in the body id table. The layer addition alone forces the bump —+/// `read`'s `check_matches_schema` validates the whole dictionary on open, so an older reader+/// rejects a v4 archive anyway.+/// /// v3 adds `fuel`, `hotel` and `atm` to [`dict::KINDS`](super::dict::KINDS) and an optional /// per-body feature id table. The kind additions alone force the bump: `read`'s /// `check_matches_schema` validates the whole dictionary on open, so an older reader would /// reject a v3 archive anyway. v1 is no longer read — the last v1 archive predates `places`, /// `poi` and `transit` entirely.-pub const FORMAT_VERSION: u8 = 3;+pub const FORMAT_VERSION: u8 = 7;  pub const HEADER_LEN: usize = 128; 
library/tilecodec/src/main/rust/src/mamaps/mod.rs
diff --git a/library/tilecodec/src/main/rust/src/mamaps/mod.rs b/library/tilecodec/src/main/rust/src/mamaps/mod.rsindex efa8e2575f..c05cc65025 100644--- a/library/tilecodec/src/main/rust/src/mamaps/mod.rs+++ b/library/tilecodec/src/main/rust/src/mamaps/mod.rs@@ -99,10 +99,11 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         roads.parts.push(Part { coord_start: 0, point_count: 2, winding: WINDING_OUTER });         roads.coords = vec![(0, 0), (seed, seed)];-        Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() }+        Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None }     }      /// An archive of `(z, x, y, seed)` tiles, fed in ascending id order as the writer requires.@@ -180,6 +181,7 @@ mod tests {             transit_ordinal: 0,             transit_lanes: 0,             transit_taper: 0,+            lane_count: 0,         });         roads.parts.push(Part { coord_start: 0, point_count: POINTS, winding: WINDING_OUTER });         let mut state = seed as u64 ^ 0xA5A5_A5A5_A5A5_A5A5;@@ -189,7 +191,7 @@ mod tests {                 ((state >> 33) as i16, (state >> 17) as i16)             })             .collect();-        Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() }+        Body { extent: DEFAULT_EXTENT, layers: vec![roads], names: Vec::new(), ids: Vec::new() , turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None }     }      #[test]@@ -652,11 +654,11 @@ mod tests {         let mut w = StreamWriter::new(opts).expect("opts");         for (id, seed) in rows { w.append(id, &crate::mamaps::body::Body { extent: 4096, layers: {             let mut l = crate::mamaps::body::Layer::new(crate::mamaps::dict::LAYER_ROADS);-            l.features.push(crate::mamaps::body::Feature{kind:1,kind_detail:0,geom_type:1,flags:0,name_idx:0,parts_offset:0,part_count:1,transit_color:0,transit_ordinal:0,transit_lanes:0,transit_taper:0});+            l.features.push(crate::mamaps::body::Feature{kind:1,kind_detail:0,geom_type:1,flags:0,name_idx:0,parts_offset:0,part_count:1,transit_color:0,transit_ordinal:0,transit_lanes:0,transit_taper:0,lane_count:0});             l.parts.push(crate::mamaps::body::Part{coord_start:0,point_count:2,winding:0});             l.coords = vec![(0,0),(seed,seed)];             vec![l]-        }, names: Vec::new(), ids: Vec::new() }).expect("append"); }+        }, names: Vec::new(), ids: Vec::new(), turn_lanes: Vec::new(), buildings: Vec::new(), heightmap: None }).expect("append"); }         let bytes = w.finish().expect("finish");         let hdr = crate::mamaps::Header::parse(&bytes).expect("hdr");         assert_eq!(hdr.leaf_count as usize, (5000+4096-1)/4096);