//! Fill in the lane count of a junction stub from the road it interrupts. //! //! # The defect this exists for //! //! [`schema::roads::lane_count`](crate::schema::roads::lane_count) reads the OSM `lanes` tag and //! nothing else, and a way that does not carry one gets zero — which the renderer turns into //! `oneway ? 1 : 2`. That default is right for the untagged residential street it was written for. //! It is wrong for the twenty metres of asphalt between the two carriageways of a divided //! arterial, because a mapper who tagged `lanes=6` on the road either side very often left the //! short piece in the middle untagged. //! //! The result reaches a screen as an intersection that is *narrower than the roads entering it*. //! Measured at Broadway × Rollins Road in Burlingame, the case this was written for: Rollins is //! 6.1 painted lanes west of the junction and 4.1 east of it, and **2.0 lanes through it** — the //! narrowest pavement in the scene is the middle of the intersection. Way `417329335` is that //! stub: nineteen metres, no `lanes`, no `lanes:forward`, no `lanes:backward`, sitting between //! neighbours tagged 6 and 4. //! //! **This is not a defect in the extract.** [`crate::extract`] reads one OSM way to one feature //! and never splits a way, so there is no `lanes` tag being dropped anywhere. The data genuinely //! does not have one. //! //! # What this is, and what it is not //! //! **It is inference, and it must be read as inference.** Nothing here is tagged. What makes it //! legitimate is not that the guess is good but that *the thing it replaces is also a guess* — //! `oneway ? 1 : 2` is an invention with no more support in the data than this has, and it is a //! worse one, because it ignores the two tagged measurements sitting at either end of the way it //! is guessing about. Replacing a context-free invention with a context-sensitive one is the whole //! of the claim. It is not a claim that the answer is right. //! //! Nothing here ever overrides a tag. A way that carries `lanes` keeps it. //! //! # What counts as the same road //! //! A way inherits only when **every** one of these holds. Each was measured against the whole San //! Francisco peninsula extract — 27,287 drivable ways, of which 13,674 carry no `lanes` tag. //! //! * **It carries no `lanes` tag at all.** Never override data. //! * **It has a name**, and the donor has the same one. //! * **It is at most [`MAX_STUB_M`] long.** //! * **A donor at *both* ends**, each collinear within [`MAX_TURN_DEGREES`] and agreeing on //! one-wayness. //! * **The donor is itself tagged.** Not inherited — see "one hop" below. //! //! ## Why name *and* collinearity, rather than either //! //! | rule | candidates | of which the donor disagrees on one-wayness | //! |---|---|---| //! | same name only | 343 | 77 | //! | collinear only | 393 | 77 | //! | **both** | **290** | **40** | //! //! Neither alone is safe. Name alone joins a street to itself around a hard corner, which is a //! different carriageway. Collinearity alone joins a street to whatever runs straight on through //! the junction, and a road changing its name at a crossroads is ordinary. Requiring both is the //! intersection of the two, and the last column is the evidence that it is the *right* //! intersection rather than merely the smaller one: the disagreement rate on an independent //! attribute nobody selected for — one-wayness — halves. Pairs that were never the same //! carriageway are what that column counts, and requiring both conditions removes half of them. //! //! Matching on `ref` as well as `name` was measured and rejected: over the same extract it admits //! exactly one further way, a 257 m motorway spur, which [`MAX_STUB_M`] excludes anyway. //! //! ## Why one-wayness has to agree //! //! `lanes` counts **both** directions. A two-way `lanes=6` inherited onto a one-way stub paints //! six lanes running one way where the truth is about three, so the error is not a small //! misjudgement but a doubling, in the direction that reads as a broken map. This is a //! correctness condition and not a tuning knob; it costs 290 candidates down to 252. //! //! ## Why the minimum of the donors //! //! Over the same extract, at [`MAX_STUB_M`]: taking the minimum changes 113 ways by an average of //! +1.33 lanes; taking the maximum changes 135 by +1.82. Both fix the reported defect. The //! minimum is preferred because the two errors are not symmetric — too narrow reads as a cautious //! map and too wide reads as a wrong one. //! //! Note the gap between 156 ways that qualify and 113 that change: where the minimum lands on the //! value the default would have produced anyway, nothing is emitted. A rule that declines to speak //! unless both of its neighbours jointly justify it is behaving correctly, not under-reaching. //! //! ## Why a length bound, and why in metres //! //! Candidate lengths run to a long tail — median 33.5 m but p90 202.7 m and a maximum of 804 m. //! Unbounded, half a kilometre of El Camino Real quietly goes from the default 1 to 2, and 519 m //! of San Bruno Avenue West from 1 to 3. That is no longer a junction stub; it is a road, and a //! road acquiring a width from a neighbour it merely touches is the failure mode this bound //! exists to prevent. //! //! [`MAX_STUB_M`] is 50 because of what is being modelled rather than where a percentile falls. //! The subject is the pavement inside a junction, or the link between the two carriageways of a //! divided road: [`schema::junction`](crate::schema::junction) already takes an intersection to be //! about 14 m from its centre to its edge, so a box some 28 m across, and a divided-carriageway //! link runs 15–30 m. Fifty clears that comfortably and sits well below the p75 of 99 m, so it //! excludes the tail by construction. Relaxing 50 → 100 m buys 17 more changed ways; removing the //! bound entirely buys 49 and admits the two above. //! //! **Metres, not node count.** A node count looks like a proxy for a short way and is not one: way //! `23925587` in the same extract is a two-node way 497 m long. //! //! ## One hop, never a chain //! //! A donor must carry a real tag. An inherited count never becomes a donor, so a tagged way's //! width can travel exactly one way and no further — a property of the rule's shape rather than a //! limit someone has to remember to enforce. Chaining would also need iteration to a fixpoint //! inside a pass whose output must be byte-identical at every thread count. //! //! # What is deliberately *not* touched //! //! Only the lane **count** — the width the carriageway is painted. Not //! [`turn_masks`](crate::schema::roads::turn_masks) and not //! [`carriageway`](crate::schema::roads::carriageway). //! //! A count is a physical property of the road that plainly continues across a junction. A turn //! arrow and a directional split are surveys of a *particular* piece of asphalt, and they do not. //! There is also a concrete trap: `osm_ingest::tags::build_dir_lanes` pads its mask list to the //! lane count, so feeding an inferred count into the masks would grow the list and **draw turn //! arrows that no one tagged**. Ways with `turn:lanes` and no `lanes` do exist — way `417976725` //! on Rollins Road is one. Arrows stay strictly data-driven. //! //! # How it is computed //! //! A hash-partitioned external group-by, because the in-memory form does not survive contact with //! a planet build. Every named road way contributes one record per endpoint; on the peninsula //! extract that is tens of thousands, but scaled to a planet's ~70 M drivable ways it is of the //! order of 4 GB against a build already peaking near 7 GB. Pre-filtering by node count was //! measured and does not rescue it — it still holds ~1 GB while losing real candidates. //! //! So records go to [`PARTITIONS`] files, partitioned on the endpoint node id. Every way meeting //! at a node lands in the same file, which is the only grouping the rule needs, and each file is //! then sorted and grouped on its own. Peak memory is one partition rather than the whole planet. //! //! Recipients longer than [`MAX_STUB_M`] are dropped before they are ever written — an untagged //! way is never a donor, so a long untagged way cannot affect anything and need not be stored. //! That alone removes three quarters of the recipient records. //! //! Names are hashed to a `u64` rather than carried. A collision costs one wrong inheritance and //! cannot cost more than that: the collinearity and one-wayness conditions still have to pass, and //! the result is still bounded by [`MAX_STUB_M`] and by the minimum of the donors. use std::fs::File; use std::io::{BufWriter, Read, Write}; use std::path::{Path, PathBuf}; use osm_ingest::proto::{err, Error, Result}; use rayon::prelude::*; use tile_build::par; /// The longest way that may inherit, in metres. See the module docs for why 50 and why metres. pub const MAX_STUB_M: f64 = 50.0; /// How far from straight a donor may leave the shared node and still be the same road, in degrees. /// /// Zero is a perfect continuation. 45° was measured and rejected: it admits 17 more candidates and /// brings 17 more one-wayness disagreements with them, which is the whole of what it buys. pub const MAX_TURN_DEGREES: f64 = 30.0; /// How many files the endpoint records are partitioned across. /// /// Only peak memory depends on this — one partition is held at a time — so it wants to be large /// enough that a planet's records divide into pieces of tens of megabytes, and small enough that /// the open file handles and the write buffers behind them stay cheap. pub const PARTITIONS: usize = 256; /// Metres per degree of latitude. Spherical, matching [`crate::schema::junction`], and worth /// centimetres over the tens of metres this module measures. const METRES_PER_DEGREE: f64 = 111_320.0; /// One endpoint record, fixed width so a partition is read back without a varint decoder. const RECORD_LEN: usize = 30; /// Bit 0 of a record's flag byte: the way is `oneway=yes`. const FLAG_ONEWAY: u8 = 1 << 0; /// Bit 1: this record is the way's *last* node rather than its first. const FLAG_LAST_END: u8 = 1 << 1; /// One named road way offered to the join. /// /// `nodes` and `line` must be the same length: a way whose extract cut some of its nodes away has /// coordinates that no longer line up with its refs, and its neighbours are as likely to be /// missing as its geometry. Such a way is skipped rather than guessed at. pub struct Segment<'a> { pub id: i64, /// The OSM `name`. A way without one cannot take part. pub name: &'a str, /// The way's tagged lane count. Zero — no `lanes` tag — is what makes it a recipient; anything /// else makes it a donor. pub lanes: u8, pub oneway: bool, pub nodes: &'a [i64], /// The way's coordinates in lon/lat, parallel to `nodes`. pub line: &'a [(f64, f64)], } #[derive(Clone, Copy)] struct Record { node: i64, way: i64, name_hash: u64, /// Degrees clockwise from north, pointing *away* from `node` along the way. bearing: f32, lanes: u8, flags: u8, } impl Record { fn oneway(self) -> bool { self.flags & FLAG_ONEWAY != 0 } fn end(self) -> u8 { u8::from(self.flags & FLAG_LAST_END != 0) } fn write(self, out: &mut impl Write) -> Result<()> { let mut buf = [0u8; RECORD_LEN]; buf[0..8].copy_from_slice(&self.node.to_le_bytes()); buf[8..16].copy_from_slice(&self.way.to_le_bytes()); buf[16..24].copy_from_slice(&self.name_hash.to_le_bytes()); buf[24..28].copy_from_slice(&self.bearing.to_le_bytes()); buf[28] = self.lanes; buf[29] = self.flags; out.write_all(&buf).map_err(|e| Error(format!("cannot write a lanefill partition: {e}"))) } fn read(buf: &[u8]) -> Record { let at = |a: usize, b: usize| -> [u8; 8] { buf[a..b].try_into().expect("8 bytes") }; Record { node: i64::from_le_bytes(at(0, 8)), way: i64::from_le_bytes(at(8, 16)), name_hash: u64::from_le_bytes(at(16, 24)), bearing: f32::from_le_bytes(buf[24..28].try_into().expect("4 bytes")), lanes: buf[28], flags: buf[29], } } } /// A way's two endpoint records, built off the serial path. See [`Collector::push_prepared`]. pub struct Prepared([Record; 2]); /// What [`Collector::push`] writes for `segment`, or `None` where it writes nothing. /// /// Pure over the segment: the same checks, in the same order, with the same arithmetic as the /// inline body this was lifted out of, so a pool-built `Prepared` is bit-for-bit the record pair /// the serial path produced. pub fn prepare(segment: &Segment) -> Option { if segment.name.is_empty() || segment.nodes.len() < 2 { return None; } if segment.nodes.len() != segment.line.len() { return None; } let first = segment.nodes[0]; let last = segment.nodes[segment.nodes.len() - 1]; // A closed way has one endpoint, not two, so it can never have a donor at "both" ends and // is not a junction stub in any case. if first == last { return None; } // An untagged way is never a donor, so one too long to be a recipient cannot affect // anything. Dropping it here rather than at resolve time is what keeps the spill small. if segment.lanes == 0 && polyline_length_m(segment.line) > MAX_STUB_M { return None; } // The bearing leaving each end, skipping repeated coordinates: a duplicated vertex has no // direction, and the graph does contain them where a way was split at a coincident point. let head = leaving(segment.line, true)?; let tail = leaving(segment.line, false)?; let name_hash = hash_name(segment.name); let base = if segment.oneway { FLAG_ONEWAY } else { 0 }; let record = |node: i64, bearing: f64, end: u8| Record { node, way: segment.id, name_hash, bearing: bearing as f32, lanes: segment.lanes, flags: base | end, }; Some(Prepared([record(first, head, 0u8), record(last, tail, FLAG_LAST_END)])) } /// [`prepare`] for a way whose coordinates are read on demand: `coord(i)` is node `i`'s /// coordinate, valid for every `i < nodes.len()` (the caller has checked every node resolves -- /// `prepare`'s `nodes.len() == line.len()` test). /// /// The same decisions on the same arithmetic, reading only the coordinates they need: a donor /// (`lanes > 0`) needs only the bearings leaving its two ends, and a recipient's length test can /// stop as soon as the running sum passes [`MAX_STUB_M`] -- the sum of non-negative segments only /// grows, so a prefix over the limit means the whole way is. On the planet every coordinate read /// is a random read of an ~80 GB table, and lane inheritance read every coordinate of every named /// road (134 s); most of them are long and decided after a few nodes. pub fn prepare_lazy( id: i64, name: &str, lanes: u8, oneway: bool, nodes: &[i64], coord: impl Fn(usize) -> (f64, f64), ) -> Option { if name.is_empty() || nodes.len() < 2 { return None; } let first = nodes[0]; let last = nodes[nodes.len() - 1]; if first == last { return None; } if lanes == 0 { // `polyline_length_m`, summed in the same order, stopping once it is decided. let mut length = -0.0f64; let mut previous = coord(0); for i in 1..nodes.len() { let p = coord(i); let north = (p.1 - previous.1) * METRES_PER_DEGREE; let east = (p.0 - previous.0) * METRES_PER_DEGREE * previous.1.to_radians().cos(); length += (north * north + east * east).sqrt(); if length > MAX_STUB_M { return None; } previous = p; } } // `leaving`, scanning from each end for the first coordinate that differs. let origin = coord(0); let head = bearing(origin, (1..nodes.len()).map(&coord).find(|p| *p != origin)?); let origin = coord(nodes.len() - 1); let tail = bearing(origin, (0..nodes.len() - 1).rev().map(&coord).find(|p| *p != origin)?); let name_hash = hash_name(name); let base = if oneway { FLAG_ONEWAY } else { 0 }; let record = |node: i64, bearing: f64, end: u8| Record { node, way: id, name_hash, bearing: bearing as f32, lanes, flags: base | end, }; Some(Prepared([record(first, head, 0u8), record(last, tail, FLAG_LAST_END)])) } /// Accumulates endpoint records, then resolves them into the ways whose lane count changes. pub struct Collector { files: Vec>, paths: Vec, /// Whether the partition names were unlinked at creation (Unix); if not, `finish` and drop /// delete them. unlinked: bool, } include!("lanefill_s1.rs");