SPB Git

Repository Public BSD-3-Clause

ORIXGRAPH_FORMAT.md 13.7 KB

# .orixgraph — ORIX Maps routing graph, format 1.0

One file per region (routing.orixgraph inside a region package). It is written by RoutingGraphWriter and read by RoutingGraph, both in Packages/ORIXRouting; no other code knows the layout.

Design goals:

  • used in place through mmap (NSData(contentsOf:options:.alwaysMapped)): fixed-width records, no decoding pass, no per-query allocation proportional to the graph;
  • compact enough to be downloaded to a phone;
  • safe: a truncated or corrupted file is rejected with a typed error or, at worst, read without ever leaving the mapping.

All integers are little-endian. Offsets are absolute byte offsets in the file. Sections start on 8-byte boundaries (zero padding between them); records inside a section are packed, readers must not assume alignment.

# Header (64 bytes)

Offset Size Field Value
0 8 magic ASCII ORIXGRPH
8 2 version major 1
10 2 version minor 0
12 4 header size 64
16 4 section count 13 in this version (1…64)
20 4 section entry size 32
24 8 file size total size of the file in bytes
32 8 flags 0 (reserved)
40 24 reserved 0

The section table starts at header size and holds section count entries of section entry size bytes:

Offset Size Field
0 4 section id: four ASCII characters, first character in the low byte ("NODE", "ADJ "…)
4 4 element size in bytes
8 8 offset of the section
16 8 length of the section in bytes = element size × element count
24 8 element count

# Versioning rules

  • A reader refuses any file whose major version is not its own (RoutingGraphError.unsupportedVersion).
  • A minor revision may add sections, append fields to the header or to the section entries (their sizes are in the header) and use reserved bits. A reader ignores section ids it does not know and reads only the first 64 / 32 bytes of the header and of each entry. Anything that changes the meaning of an existing field is a new major version.
  • The file size in the header must equal the real size: a shorter file is truncated, a longer one malformedHeader.

# Identifiers

  • Node: index into NODE (0 … nodeCount − 1). A node is an intersection: an OSM node shared by several routable ways, the end of a way, a barrier, or the via node of a turn restriction. The other OSM nodes only shape edges.
  • Edge: index into EDGE. An edge is undirected in storage; node A is the end met first along the OSM way.
  • Directed edge: edge << 1 | direction, direction 0 = A → B ("forward"), 1 = B → A ("backward"). The twin of a directed edge is id ^ 1. The search engine labels directed edges, which makes turn restrictions and turn costs exact.

When built with reorderSpatially (the builder always does) nodes are numbered along a Morton curve and edges are sorted by (node A, node B), so that a search touches neighbouring records and label pages.

# Sections

Id Element Count Content
META 1 bytes metadata, UTF-8 JSON
NODE 16 nodeCount + 1 node records, the last one is a sentinel
NCMP 6 nodeCount connectivity words, one u16 per mode
ADJ 4 2 × edgeCount directed edge ids leaving each node
EDGE 28 edgeCount edge records
WAY 16 wayCount OSM way id, name, reference
GEOM 1 bytes intermediate points of edges
STRO 4 stringCount + 1 string offsets into STRB
STRB 1 bytes UTF-8 string bytes
RSTR 16 restrictionCount turn restrictions, sorted by via node
CMPS 4 Σ ranked components node count of each ranked component
GRDK 8 non-empty cells + 1 spatial index keys, the last one is a sentinel
GRDI 4 index entries spatial index items (edge indices)

All thirteen sections are required, even when empty.

# NODE (16 bytes)

Offset Type Field
0 i32 latitude, E7 (1e-7 degree)
4 i32 longitude, E7
8 u32 index in ADJ of the first directed edge leaving the node
12 u32 flags

The adjacency list of node v is ADJ[NODE[v].first ..< NODE[v + 1].first]; record nodeCount is the sentinel that closes the last list (its first equals the ADJ count).

Node flags:

Bit Meaning
0 barrier a car cannot pass
1 barrier a bicycle cannot pass
2 barrier a pedestrian cannot pass
3 traffic signal
4 the node is the via node of at least one record of RSTR (set by the writer)

A route may start or end on an edge next to a barrier but never goes through a node that blocks its mode.

# ADJ (4 bytes)

One u32 directed edge id per edge end: edge << 1 in the list of the edge's node A, edge << 1 | 1 in the list of its node B. Every incident edge is listed whatever its access, so the list also gives the edges entering a node (the twins) and the number of roads meeting there.

# EDGE (28 bytes)

Offset Type Field
0 u32 node A
4 u32 node B
8 u32 length in decimetres, rounded up, measured along the geometry on the sphere
12 u32 offset of the shape in GEOM, 0xFFFFFFFF when the edge is a straight line between its nodes
16 u32 index in WAY
20 u8 road class
21 u8 access bits
22 u16 speed in 0.1 km/h, 0 when not tagged
24 u16 flags
26 u8 bearing when leaving A towards B, in 1/256 of a turn clockwise from north
27 u8 bearing when arriving at B

The length is never smaller than the great-circle distance between the two nodes: the A* heuristic relies on it.

Road classes: 0 motorway, 1 trunk, 2 primary, 3 secondary, 4 tertiary, 5 unclassified, 6 residential, 7 living street, 8 service, 9 track, 10 pedestrian, 11 footway, 12 path, 13 cycleway, 14 steps, 15 ferry, 16 road of unknown class. *_link ways use the class of their road plus the link flag.

Access bits (set = the mode may travel in that direction):

Bit Meaning Bit Meaning
0 car A → B 1 car B → A
2 bicycle A → B 3 bicycle B → A
4 pedestrian A → B 5 pedestrian B → A

One-way rules (oneway, oneway:bicycle, junction=roundabout, motorway default, contraflow cycle lanes) are entirely expressed by these bits.

Edge flags:

Bits Meaning
0–1 surface: 0 unknown, 1 paved, 2 unpaved
2 toll
3 ferry
4 tunnel
5 bridge
6 roundabout
7 link (ramp, slip road)
8 the speed field is tagged (maxspeed, or a ferry duration); clear = the speed must be assumed from the class
9 a traffic signal lies on an intermediate point of the edge
10 / 11 / 12 restricted access (destination, private, customers, delivery) for car / bicycle / pedestrian
13 a cycle lane or track is tagged on the road
14 minor service road (driveway, parking aisle, drive-through, emergency access)
15 reserved

For a ferry the speed is its length divided by its tagged duration; without a duration the field is 0 and the profile's default ferry speed applies (and is reported as assumed).

# WAY (16 bytes)

Offset Type Field
0 i64 OpenStreetMap way id (debugging, and matching restrictions at build time)
8 u32 name, index in the string table (0 = none)
12 u32 reference (ref), index in the string table (0 = none)

Consecutive edges cut from one OSM way share a record.

# GEOM

At the shape offset of an edge: a varint count n (1 … 2^20) followed by n pairs of zigzag varints (Δlatitude, Δlongitude) in E7 units, each relative to the previous point, the first relative to node A. Varints are the protobuf base-128 encoding. The full polyline of the edge is node A, the n points, node B.

# STRO / STRB

String i is the UTF-8 bytes STRB[STRO[i] ..< STRO[i + 1]]. String 0 is empty and means "none". Strings are deduplicated; invalid UTF-8 is replaced when decoded, never trusted.

# RSTR (16 bytes)

Offset Type Field
0 u32 via node
4 u32 from edge (incident to the via node)
8 u32 to edge (incident to the via node)
12 u8 kind: 0 = the turn from → via → to is forbidden (no_*), 1 = coming from from, only to may be taken (only_*)
13 u8 modes: bit 0 car, bit 1 bicycle
14 u16 reserved

Records are sorted by via node (then from, to, kind) and found by binary search; node flag bit 4 tells the engine whether a node has any. Several mandatory records with the same from edge are alternatives. no_u_turn is a forbidden turn whose from and to edges are the same. Restrictions whose via member is a way are not stored (the builder counts them in the metadata).

# NCMP / CMPS — connectivity

For each node and each mode (car, bicycle, pedestrian, in this order) one u16:

  • bits 0–14: rank of the node's weakly connected component for the mode — 1 is the largest component, 0 means the node is not usable by the mode (no usable edge, or the node is a barrier for it), 0x7FFF means "one of the components too small to be ranked" (only 32 766 components are ranked per mode);
  • bit 15: the node is not in the largest strongly connected component of its weak component (a one-way trap or a fragment reachable in one direction only).

Components are computed on nodes: an edge joins its two ends when the mode may use it in at least one direction and neither end is a barrier for the mode. Turn restrictions are ignored.

CMPS holds the node counts of the ranked components: first those of the car, then bicycle, then pedestrian; the number per mode is in the metadata (rankedComponentCounts).

Use by the engine: two positions whose ranked components differ cannot be connected — the router answers "no connection" without searching; snapping prefers edges whose two ends are in the main strongly connected part of a component of at least 30 nodes.

# GRDK / GRDI — spatial index

A uniform grid in degrees described in the metadata (grid): origin (south-west corner, E7), cell height and width (E7), number of columns and rows. Cell key = row × columns + column.

  • GRDK: one record per non-empty cell, sorted by key: u32 key, u32 index of its first item in GRDI. A final sentinel record (key 0xFFFFFFFF) carries the total item count.
  • GRDI: u32 edge indices, grouped by cell. An edge is listed in every cell its geometry crosses (sampled every quarter of a cell; a query widens its box by a quarter of a cell to compensate).

The builder uses cells of about 500 m. A cell is found by binary search in GRDK.

# META — metadata (JSON object)

Key Content
formatMajor, formatMinor copy of the header version
regionID identifier of the region (ca-qc)
boundingBox area covered: minLat, minLon, maxLat, maxLon in degrees
osmTimestamp ISO 8601 date of the OpenStreetMap data, absent when the extract does not record it
buildDate ISO 8601 date of the build
attribution © OpenStreetMap contributors, ODbL 1.0
generator program that wrote the file
nodeCount, edgeCount, wayCount, restrictionCount, stringCount must match the sections
grid originLatE7, originLonE7, cellLatE7, cellLonE7, columns, rows
rankedComponentCounts three integers (car, bicycle, pedestrian)
maxTaggedSpeedKmh highest tagged road speed in the file (the heuristic needs an upper bound)
maxFerrySpeedKmh, ferryEdgeCount same for ferries
buildCounters free counters of the builder: OSM entities read, restrictions applied / skipped by reason…

# Limits

Limit
nodes 2^32 − 2
edges 2^31 − 2 (a directed edge id is 31 bits; one value is reserved by the engine)
GEOM, STRB 4 GiB each
points per edge shape 2^20
grid cells 2^32 − 2
ranked components per mode 32 766
edge length 2^32 − 1 dm (429 000 km)
tagged speed 6 553.5 km/h

# Validation by the reader

RoutingGraph(url:validation:) always checks, in constant time: magic, major version, header and entry sizes, section count, declared file size, that every section lies inside the file, that the element size of each known section is the expected one and that length = element size × count, that all required sections exist, the metadata (JSON, counts equal to the sections, grid dimensions, bounding box, component counts) and the adjacency sentinel.

With .full (the default) it also walks every record once: adjacency offsets increasing, coordinates in range, node and way indices of every edge, road class and surface values, every shape decodable inside GEOM and inside the coordinate ranges, every adjacency entry pointing to an edge that really starts at that node, string indices and offsets, restriction indices and order, grid keys strictly increasing with increasing item offsets, grid items. Measured on the province graph (2.47 M edges, 194 MB, file in the page cache): 0.05 s.

With .structure the per-record pass is skipped (useful when the package layer has already verified a checksum). The accessors stay index-checked in both modes: an out-of-range index yields an empty or neutral value (an edge nobody can use, an empty list), never a read outside the mapping. This is exercised by the fuzz test (FormatTests.fuzzedFilesNeverCrash).

# What the format does not hold

Elevation, lane counts, turn lanes, exit numbers and destination signs, maxspeed per direction or per time, conditional access, live or historical traffic, restrictions through via ways, public transport.

Files main
Menu

Theme