SPB Git

Repository Public BSD-3-Clause

ORIXGRAPH_FORMAT.md 13.7 KB 286 lines markdown

1# `.orixgraph` — ORIX Maps routing graph, format 1.023One file per region (`routing.orixgraph` inside a region package). It is written by `RoutingGraphWriter` and read by4`RoutingGraph`, both in `Packages/ORIXRouting`; no other code knows the layout.56Design goals:78- used in place through `mmap` (`NSData(contentsOf:options:.alwaysMapped)`): fixed-width records, no decoding pass,9  no per-query allocation proportional to the graph;10- compact enough to be downloaded to a phone;11- safe: a truncated or corrupted file is rejected with a typed error or, at worst, read without ever leaving the mapping.1213All integers are **little-endian**. Offsets are absolute byte offsets in the file. Sections start on 8-byte boundaries14(zero padding between them); records inside a section are packed, readers must not assume alignment.1516## Header (64 bytes)1718| Offset | Size | Field | Value |19|---:|---:|---|---|20| 0 | 8 | magic | ASCII `ORIXGRPH` |21| 8 | 2 | version major | 1 |22| 10 | 2 | version minor | 0 |23| 12 | 4 | header size | 64 |24| 16 | 4 | section count | 13 in this version (1…64) |25| 20 | 4 | section entry size | 32 |26| 24 | 8 | file size | total size of the file in bytes |27| 32 | 8 | flags | 0 (reserved) |28| 40 | 24 | reserved | 0 |2930The section table starts at `header size` and holds `section count` entries of `section entry size` bytes:3132| Offset | Size | Field |33|---:|---:|---|34| 0 | 4 | section id: four ASCII characters, first character in the low byte (`"NODE"`, `"ADJ "`…) |35| 4 | 4 | element size in bytes |36| 8 | 8 | offset of the section |37| 16 | 8 | length of the section in bytes = element size × element count |38| 24 | 8 | element count |3940### Versioning rules4142- A reader refuses any file whose **major** version is not its own (`RoutingGraphError.unsupportedVersion`).43- A **minor** revision may add sections, append fields to the header or to the section entries (their sizes are in the44  header) and use reserved bits. A reader ignores section ids it does not know and reads only the first 64 / 32 bytes45  of the header and of each entry. Anything that changes the meaning of an existing field is a new major version.46- The file size in the header must equal the real size: a shorter file is `truncated`, a longer one `malformedHeader`.4748## Identifiers4950- **Node**: index into `NODE` (0 … nodeCount − 1). A node is an intersection: an OSM node shared by several routable51  ways, the end of a way, a barrier, or the via node of a turn restriction. The other OSM nodes only shape edges.52- **Edge**: index into `EDGE`. An edge is undirected in storage; node A is the end met first along the OSM way.53- **Directed edge**: `edge << 1 | direction`, direction 0 = A → B ("forward"), 1 = B → A ("backward"). The twin of a54  directed edge is `id ^ 1`. The search engine labels directed edges, which makes turn restrictions and turn costs exact.5556When built with `reorderSpatially` (the builder always does) nodes are numbered along a Morton curve and edges are57sorted by (node A, node B), so that a search touches neighbouring records and label pages.5859## Sections6061| Id | Element | Count | Content |62|---|---:|---|---|63| `META` | 1 | bytes | metadata, UTF-8 JSON |64| `NODE` | 16 | nodeCount + 1 | node records, the last one is a sentinel |65| `NCMP` | 6 | nodeCount | connectivity words, one `u16` per mode |66| `ADJ ` | 4 | 2 × edgeCount | directed edge ids leaving each node |67| `EDGE` | 28 | edgeCount | edge records |68| `WAY ` | 16 | wayCount | OSM way id, name, reference |69| `GEOM` | 1 | bytes | intermediate points of edges |70| `STRO` | 4 | stringCount + 1 | string offsets into `STRB` |71| `STRB` | 1 | bytes | UTF-8 string bytes |72| `RSTR` | 16 | restrictionCount | turn restrictions, sorted by via node |73| `CMPS` | 4 | Σ ranked components | node count of each ranked component |74| `GRDK` | 8 | non-empty cells + 1 | spatial index keys, the last one is a sentinel |75| `GRDI` | 4 | index entries | spatial index items (edge indices) |7677All thirteen sections are required, even when empty.7879### `NODE` (16 bytes)8081| Offset | Type | Field |82|---:|---|---|83| 0 | i32 | latitude, E7 (1e-7 degree) |84| 4 | i32 | longitude, E7 |85| 8 | u32 | index in `ADJ ` of the first directed edge leaving the node |86| 12 | u32 | flags |8788The adjacency list of node *v* is `ADJ[NODE[v].first ..< NODE[v + 1].first]`; record `nodeCount` is the sentinel that89closes the last list (its `first` equals the `ADJ ` count).9091Node flags:9293| Bit | Meaning |94|---:|---|95| 0 | barrier a car cannot pass |96| 1 | barrier a bicycle cannot pass |97| 2 | barrier a pedestrian cannot pass |98| 3 | traffic signal |99| 4 | the node is the via node of at least one record of `RSTR` (set by the writer) |100101A route may start or end on an edge next to a barrier but never goes *through* a node that blocks its mode.102103### `ADJ ` (4 bytes)104105One `u32` directed edge id per edge end: `edge << 1` in the list of the edge's node A, `edge << 1 | 1` in the list of106its node B. Every incident edge is listed whatever its access, so the list also gives the edges *entering* a node107(the twins) and the number of roads meeting there.108109### `EDGE` (28 bytes)110111| Offset | Type | Field |112|---:|---|---|113| 0 | u32 | node A |114| 4 | u32 | node B |115| 8 | u32 | length in decimetres, rounded up, measured along the geometry on the sphere |116| 12 | u32 | offset of the shape in `GEOM`, `0xFFFFFFFF` when the edge is a straight line between its nodes |117| 16 | u32 | index in `WAY ` |118| 20 | u8 | road class |119| 21 | u8 | access bits |120| 22 | u16 | speed in 0.1 km/h, 0 when not tagged |121| 24 | u16 | flags |122| 26 | u8 | bearing when leaving A towards B, in 1/256 of a turn clockwise from north |123| 27 | u8 | bearing when arriving at B |124125The length is never smaller than the great-circle distance between the two nodes: the A* heuristic relies on it.126127Road classes: 0 motorway, 1 trunk, 2 primary, 3 secondary, 4 tertiary, 5 unclassified, 6 residential, 7 living street,1288 service, 9 track, 10 pedestrian, 11 footway, 12 path, 13 cycleway, 14 steps, 15 ferry, 16 road of unknown class.129`*_link` ways use the class of their road plus the *link* flag.130131Access bits (set = the mode may travel in that direction):132133| Bit | Meaning | Bit | Meaning |134|---:|---|---:|---|135| 0 | car A → B | 1 | car B → A |136| 2 | bicycle A → B | 3 | bicycle B → A |137| 4 | pedestrian A → B | 5 | pedestrian B → A |138139One-way rules (`oneway`, `oneway:bicycle`, `junction=roundabout`, motorway default, contraflow cycle lanes) are140entirely expressed by these bits.141142Edge flags:143144| Bits | Meaning |145|---:|---|146| 0–1 | surface: 0 unknown, 1 paved, 2 unpaved |147| 2 | toll |148| 3 | ferry |149| 4 | tunnel |150| 5 | bridge |151| 6 | roundabout |152| 7 | link (ramp, slip road) |153| 8 | the speed field is **tagged** (`maxspeed`, or a ferry `duration`); clear = the speed must be **assumed** from the class |154| 9 | a traffic signal lies on an intermediate point of the edge |155| 10 / 11 / 12 | restricted access (destination, private, customers, delivery) for car / bicycle / pedestrian |156| 13 | a cycle lane or track is tagged on the road |157| 14 | minor service road (driveway, parking aisle, drive-through, emergency access) |158| 15 | reserved |159160For a ferry the speed is its length divided by its tagged `duration`; without a duration the field is 0 and the161profile's default ferry speed applies (and is reported as assumed).162163### `WAY ` (16 bytes)164165| Offset | Type | Field |166|---:|---|---|167| 0 | i64 | OpenStreetMap way id (debugging, and matching restrictions at build time) |168| 8 | u32 | name, index in the string table (0 = none) |169| 12 | u32 | reference (`ref`), index in the string table (0 = none) |170171Consecutive edges cut from one OSM way share a record.172173### `GEOM`174175At the shape offset of an edge: a varint count *n* (1 … 2^20) followed by *n* pairs of zigzag varints176(Δlatitude, Δlongitude) in E7 units, each relative to the previous point, the first relative to node A. Varints are177the protobuf base-128 encoding. The full polyline of the edge is node A, the *n* points, node B.178179### `STRO` / `STRB`180181String *i* is the UTF-8 bytes `STRB[STRO[i] ..< STRO[i + 1]]`. String 0 is empty and means "none". Strings are182deduplicated; invalid UTF-8 is replaced when decoded, never trusted.183184### `RSTR` (16 bytes)185186| Offset | Type | Field |187|---:|---|---|188| 0 | u32 | via node |189| 4 | u32 | from edge (incident to the via node) |190| 8 | u32 | to edge (incident to the via node) |191| 12 | u8 | kind: 0 = the turn from → via → to is forbidden (`no_*`), 1 = coming from `from`, only `to` may be taken (`only_*`) |192| 13 | u8 | modes: bit 0 car, bit 1 bicycle |193| 14 | u16 | reserved |194195Records are sorted by via node (then from, to, kind) and found by binary search; node flag bit 4 tells the engine196whether a node has any. Several mandatory records with the same from edge are alternatives. `no_u_turn` is a197forbidden turn whose from and to edges are the same. Restrictions whose via member is a way are **not** stored (the198builder counts them in the metadata).199200### `NCMP` / `CMPS` — connectivity201202For each node and each mode (car, bicycle, pedestrian, in this order) one `u16`:203204- bits 0–14: rank of the node's **weakly connected component** for the mode — 1 is the largest component, 0 means the205  node is not usable by the mode (no usable edge, or the node is a barrier for it), `0x7FFF` means "one of the206  components too small to be ranked" (only 32 766 components are ranked per mode);207- bit 15: the node is **not** in the largest strongly connected component of its weak component (a one-way trap or a208  fragment reachable in one direction only).209210Components are computed on nodes: an edge joins its two ends when the mode may use it in at least one direction and211neither end is a barrier for the mode. Turn restrictions are ignored.212213`CMPS` holds the node counts of the ranked components: first those of the car, then bicycle, then pedestrian; the214number per mode is in the metadata (`rankedComponentCounts`).215216Use by the engine: two positions whose ranked components differ cannot be connected — the router answers "no217connection" without searching; snapping prefers edges whose two ends are in the main strongly connected part of a218component of at least 30 nodes.219220### `GRDK` / `GRDI` — spatial index221222A uniform grid in degrees described in the metadata (`grid`): origin (south-west corner, E7), cell height and width223(E7), number of columns and rows. Cell key = `row × columns + column`.224225- `GRDK`: one record per **non-empty** cell, sorted by key: `u32` key, `u32` index of its first item in `GRDI`.226  A final sentinel record (key `0xFFFFFFFF`) carries the total item count.227- `GRDI`: `u32` edge indices, grouped by cell. An edge is listed in every cell its geometry crosses (sampled every228  quarter of a cell; a query widens its box by a quarter of a cell to compensate).229230The builder uses cells of about 500 m. A cell is found by binary search in `GRDK`.231232### `META` — metadata (JSON object)233234| Key | Content |235|---|---|236| `formatMajor`, `formatMinor` | copy of the header version |237| `regionID` | identifier of the region (`ca-qc`) |238| `boundingBox` | area covered: `minLat`, `minLon`, `maxLat`, `maxLon` in degrees |239| `osmTimestamp` | ISO 8601 date of the OpenStreetMap data, absent when the extract does not record it |240| `buildDate` | ISO 8601 date of the build |241| `attribution` | `© OpenStreetMap contributors, ODbL 1.0` |242| `generator` | program that wrote the file |243| `nodeCount`, `edgeCount`, `wayCount`, `restrictionCount`, `stringCount` | must match the sections |244| `grid` | `originLatE7`, `originLonE7`, `cellLatE7`, `cellLonE7`, `columns`, `rows` |245| `rankedComponentCounts` | three integers (car, bicycle, pedestrian) |246| `maxTaggedSpeedKmh` | highest tagged road speed in the file (the heuristic needs an upper bound) |247| `maxFerrySpeedKmh`, `ferryEdgeCount` | same for ferries |248| `buildCounters` | free counters of the builder: OSM entities read, restrictions applied / skipped by reason… |249250## Limits251252| | Limit |253|---|---|254| nodes | 2^32 − 2 |255| edges | 2^31 − 2 (a directed edge id is 31 bits; one value is reserved by the engine) |256| `GEOM`, `STRB` | 4 GiB each |257| points per edge shape | 2^20 |258| grid cells | 2^32 − 2 |259| ranked components per mode | 32 766 |260| edge length | 2^32 − 1 dm (429 000 km) |261| tagged speed | 6 553.5 km/h |262263## Validation by the reader264265`RoutingGraph(url:validation:)` always checks, in constant time: magic, major version, header and entry sizes,266section count, declared file size, that every section lies inside the file, that the element size of each known267section is the expected one and that `length = element size × count`, that all required sections exist, the268metadata (JSON, counts equal to the sections, grid dimensions, bounding box, component counts) and the adjacency269sentinel.270271With `.full` (the default) it also walks every record once: adjacency offsets increasing, coordinates in range, node272and way indices of every edge, road class and surface values, every shape decodable inside `GEOM` and inside the273coordinate ranges, every adjacency entry pointing to an edge that really starts at that node, string indices and274offsets, restriction indices and order, grid keys strictly increasing with increasing item offsets, grid items.275Measured on the province graph (2.47 M edges, 194 MB, file in the page cache): 0.05 s.276277With `.structure` the per-record pass is skipped (useful when the package layer has already verified a checksum).278The accessors stay index-checked in both modes: an out-of-range index yields an empty or neutral value (an edge279nobody can use, an empty list), never a read outside the mapping. This is exercised by the fuzz test280(`FormatTests.fuzzedFilesNeverCrash`).281282## What the format does not hold283284Elevation, lane counts, turn lanes, exit numbers and destination signs, `maxspeed` per direction or per time,285conditional access, live or historical traffic, restrictions through via ways, public transport.286
Files main
Menu

Theme