mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 00:20:07 +00:00
89ad4ec56a
The PBF reader streams the extract and refuses a varint longer than ten bytes; the block offsets are read as the plain int64 the format defines. Region reads queue without loose promises: a region queued while another is read is still picked up, removing a region calls its read off, a shutdown stops the read under way and the next boot queues it again and clears the download folder. The admin list shows how far a read has come. Geofabrik is asked only on its own servers, redirects included, and an outage of its list answers 502 instead of an unknown region. Nested ids such as us/texas are accepted. A region being read again or after a failed read still counts with the data it holds, a region without a boundary spans its objects, and an object two overlapping regions hold is answered once. Stored geometry, tags and boundaries go through the JSON column declarations. An obstacle is related to the route both ways, so a long weir passed in its middle and a route inside an area count. Tests cover the reader with extracts written in the test, the tag rules, the extraction and lock grouping, the geo helpers, the Geofabrik client, the region queue, the read side, both controllers, the job, the repositories and the HTTP routes; the domain gets its coverage entry.
157 lines
5.2 KiB
TypeScript
157 lines
5.2 KiB
TypeScript
/**
|
|
* A small OpenStreetMap PBF writer for the paddle tests: just enough of the
|
|
* format (blob framing, raw and zlib blobs, a header block, string tables,
|
|
* plain and dense nodes, ways) to build an extract in the test itself, so
|
|
* no binary fixture is checked in. The field numbers are the ones of
|
|
* https://wiki.openstreetmap.org/wiki/PBF_Format.
|
|
*/
|
|
import { deflateSync } from 'node:zlib';
|
|
|
|
export interface FixtureNode {
|
|
id: number;
|
|
lat: number;
|
|
lon: number;
|
|
tags?: Record<string, string>;
|
|
}
|
|
|
|
export interface FixtureWay {
|
|
id: number;
|
|
refs: number[];
|
|
tags?: Record<string, string>;
|
|
}
|
|
|
|
export function varint(value: number): Buffer {
|
|
const bytes: number[] = [];
|
|
let rest = value;
|
|
while (rest >= 0x80) {
|
|
bytes.push((rest % 128) | 0x80);
|
|
rest = Math.floor(rest / 128);
|
|
}
|
|
bytes.push(rest);
|
|
return Buffer.from(bytes);
|
|
}
|
|
|
|
const zigzag = (value: number): number => (value >= 0 ? value * 2 : -value * 2 - 1);
|
|
|
|
export function key(field: number, wire: number): Buffer {
|
|
return varint(field * 8 + wire);
|
|
}
|
|
|
|
export function bytesField(field: number, payload: Buffer): Buffer {
|
|
return Buffer.concat([key(field, 2), varint(payload.length), payload]);
|
|
}
|
|
|
|
export function varintField(field: number, value: number): Buffer {
|
|
return Buffer.concat([key(field, 0), varint(value)]);
|
|
}
|
|
|
|
function packed(field: number, values: number[], signed: boolean): Buffer {
|
|
return bytesField(field, Buffer.concat(values.map((value) => varint(signed ? zigzag(value) : value))));
|
|
}
|
|
|
|
/** Deltas of a run of numbers, as the dense and way encodings store them. */
|
|
function deltas(values: number[]): number[] {
|
|
return values.map((value, index) => value - (values[index - 1] ?? 0));
|
|
}
|
|
|
|
/** One data block: a string table and one group of dense nodes, plain nodes and ways. */
|
|
export function dataBlock(content: { dense?: FixtureNode[]; nodes?: FixtureNode[]; ways?: FixtureWay[] }): Buffer {
|
|
const strings = [''];
|
|
const index = (text: string): number => {
|
|
const at = strings.indexOf(text);
|
|
if (at >= 0) return at;
|
|
strings.push(text);
|
|
return strings.length - 1;
|
|
};
|
|
const toUnits = (deg: number) => Math.round(deg * 1e7);
|
|
const groups: Buffer[] = [];
|
|
|
|
if (content.dense?.length) {
|
|
const nodes = content.dense;
|
|
const keysVals = nodes.flatMap((node) => [
|
|
...Object.entries(node.tags ?? {}).flatMap(([k, v]) => [index(k), index(v)]),
|
|
0,
|
|
]);
|
|
const dense = Buffer.concat([
|
|
packed(1, deltas(nodes.map((node) => node.id)), true),
|
|
packed(8, deltas(nodes.map((node) => toUnits(node.lat))), true),
|
|
packed(9, deltas(nodes.map((node) => toUnits(node.lon))), true),
|
|
packed(10, keysVals, false),
|
|
]);
|
|
groups.push(bytesField(2, dense));
|
|
}
|
|
for (const node of content.nodes ?? []) {
|
|
const tags = Object.entries(node.tags ?? {});
|
|
const body = Buffer.concat([
|
|
Buffer.concat([key(1, 0), varint(zigzag(node.id))]),
|
|
packed(
|
|
2,
|
|
tags.map(([k]) => index(k)),
|
|
false,
|
|
),
|
|
packed(
|
|
3,
|
|
tags.map(([, v]) => index(v)),
|
|
false,
|
|
),
|
|
Buffer.concat([key(8, 0), varint(zigzag(toUnits(node.lat)))]),
|
|
Buffer.concat([key(9, 0), varint(zigzag(toUnits(node.lon)))]),
|
|
]);
|
|
groups.push(bytesField(1, body));
|
|
}
|
|
for (const way of content.ways ?? []) {
|
|
const tags = Object.entries(way.tags ?? {});
|
|
const body = Buffer.concat([
|
|
varintField(1, way.id),
|
|
packed(
|
|
2,
|
|
tags.map(([k]) => index(k)),
|
|
false,
|
|
),
|
|
packed(
|
|
3,
|
|
tags.map(([, v]) => index(v)),
|
|
false,
|
|
),
|
|
packed(8, deltas(way.refs), true),
|
|
]);
|
|
groups.push(bytesField(3, body));
|
|
}
|
|
|
|
const table = Buffer.concat(strings.map((text) => bytesField(1, Buffer.from(text, 'utf8'))));
|
|
return Buffer.concat([bytesField(1, table), ...groups.map((group) => bytesField(2, group))]);
|
|
}
|
|
|
|
/** A header block, with the replication timestamp when one is given. */
|
|
export function headerBlock(timestamp?: number): Buffer {
|
|
const required = bytesField(4, Buffer.from('OsmSchema-V0.6'));
|
|
return timestamp === undefined ? required : Buffer.concat([required, varintField(32, timestamp)]);
|
|
}
|
|
|
|
/** A blob of `type` around `block`, framed as the file holds it: length, header, blob. */
|
|
export function framedBlob(
|
|
type: 'OSMHeader' | 'OSMData' | string,
|
|
block: Buffer,
|
|
compression: 'zlib' | 'raw' = 'zlib',
|
|
): Buffer {
|
|
const blob =
|
|
compression === 'zlib'
|
|
? Buffer.concat([varintField(2, block.length), bytesField(3, deflateSync(block))])
|
|
: bytesField(1, block);
|
|
const header = Buffer.concat([bytesField(1, Buffer.from(type, 'utf8')), varintField(3, blob.length)]);
|
|
const length = Buffer.alloc(4);
|
|
length.writeUInt32BE(header.length, 0);
|
|
return Buffer.concat([length, header, blob]);
|
|
}
|
|
|
|
/** A whole extract: the header block, then one data block per entry. */
|
|
export function pbfFile(
|
|
blocks: Array<{ dense?: FixtureNode[]; nodes?: FixtureNode[]; ways?: FixtureWay[] }>,
|
|
options: { timestamp?: number; compression?: 'zlib' | 'raw' } = {},
|
|
): Buffer {
|
|
return Buffer.concat([
|
|
framedBlob('OSMHeader', headerBlock(options.timestamp), options.compression),
|
|
...blocks.map((block) => framedBlob('OSMData', dataBlock(block), options.compression)),
|
|
]);
|
|
}
|