@osmix/pbf 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +144 -0
  3. package/dist/blobs-to-blocks.d.ts +5 -0
  4. package/dist/blobs-to-blocks.js +21 -0
  5. package/dist/blocks-to-pbf.d.ts +16 -0
  6. package/dist/blocks-to-pbf.js +73 -0
  7. package/dist/index.d.ts +8 -0
  8. package/dist/index.js +8 -0
  9. package/dist/pbf-to-blobs.d.ts +6 -0
  10. package/dist/pbf-to-blobs.js +48 -0
  11. package/dist/pbf-to-blocks.d.ts +20 -0
  12. package/dist/pbf-to-blocks.js +53 -0
  13. package/dist/proto/fileformat.d.ts +26 -0
  14. package/dist/proto/fileformat.js +56 -0
  15. package/dist/proto/osmformat.d.ts +91 -0
  16. package/dist/proto/osmformat.js +458 -0
  17. package/dist/spec.d.ts +5 -0
  18. package/dist/spec.js +9 -0
  19. package/dist/utils.d.ts +27 -0
  20. package/dist/utils.js +92 -0
  21. package/package.json +49 -0
  22. package/src/blobs-to-blocks.ts +28 -0
  23. package/src/blocks-to-pbf.ts +98 -0
  24. package/src/index.ts +8 -0
  25. package/src/pbf-to-blobs.ts +56 -0
  26. package/src/pbf-to-blocks.ts +77 -0
  27. package/src/proto/fileformat.proto +68 -0
  28. package/src/proto/fileformat.ts +70 -0
  29. package/src/proto/osmformat.proto +262 -0
  30. package/src/proto/osmformat.ts +488 -0
  31. package/src/spec.ts +10 -0
  32. package/src/utils.ts +90 -0
  33. package/test/blobs-to-blocks.test.ts +73 -0
  34. package/test/helpers.ts +66 -0
  35. package/test/pbf-to-blobs.test.ts +85 -0
  36. package/test/read.bench.ts +42 -0
  37. package/test/read.test.ts +45 -0
  38. package/test/streams.test.ts +92 -0
  39. package/test/utils.bun.test.ts +327 -0
  40. package/test/utils.test.ts +56 -0
  41. package/test/utils.ts +65 -0
  42. package/test/verify-pbf-reading.bun.test.ts +39 -0
  43. package/test/write.test.ts +86 -0
  44. package/tsconfig.json +9 -0
  45. package/vitest.config.ts +7 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @osmix/pbf
2
+
3
+ ## 0.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - Initial release
package/README.md ADDED
@@ -0,0 +1,144 @@
1
+ # @osmix/pbf
2
+
3
+ @osmix/pbf is a low-level TypeScript toolkit for reading and writing OpenStreetMap PBF data. It keeps the API surface close to the official protobuf schema, surfaces predictable types, and runs in Node 20+ and modern browsers through Web Streams and native compression primitives.
4
+
5
+ ## Highlights
6
+
7
+ - Parse headers and primitive blocks from `ArrayBufferLike`, async iterables, or Web `ReadableStream`s.
8
+ - Build streaming pipelines with `TransformStream` helpers instead of buffering entire files in memory.
9
+ - Serialize header and primitive blocks back to spec-compliant blobs with size guardrails baked in.
10
+ - Reuse generated protobuf types/readers so downstream tools can stay close to `osmformat.proto`.
11
+ - Utility helpers handle compression, concatenation, and big-endian encoding tuned for the PBF format.
12
+
13
+ ## Installation
14
+
15
+ ```sh
16
+ npm install @osmix/pbf
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ### Read an entire file
22
+
23
+ `readOsmPbf` accepts an `ArrayBufferLike`, async iterable, or Web `ReadableStream`. It returns the header block and an async generator of primitive blocks.
24
+
25
+ ```ts
26
+ import { readOsmPbf } from "@osmix/pbf"
27
+
28
+ const response = await fetch("/fixtures/monaco.pbf")
29
+ const { header, blocks } = await readOsmPbf(response.body)
30
+
31
+ console.log(header.required_features)
32
+
33
+ for await (const block of blocks) {
34
+ for (const group of block.primitivegroup) {
35
+ console.log(group.nodes?.length, group.ways.length, group.relations.length)
36
+ }
37
+ }
38
+ ```
39
+
40
+ ### Stream as you go
41
+
42
+ Use the streaming helpers when you do not want to materialize the whole file.
43
+
44
+ ```ts
45
+ import { OsmPbfBytesToBlocksTransformStream } from "@osmix/pbf"
46
+
47
+ const response = await fetch("/fixtures/monaco.pbf")
48
+
49
+ await response.body
50
+ .pipeThrough(new OsmPbfBytesToBlocksTransformStream())
51
+ .pipeTo(
52
+ new WritableStream({
53
+ write: (block) => {
54
+ if ("primitivegroup" in block) {
55
+ // Handle primitive data blocks.
56
+ return
57
+ }
58
+
59
+ console.log("Header bbox", block.bbox)
60
+ },
61
+ }),
62
+ )
63
+ ```
64
+
65
+ ### Write blocks back to PBF
66
+
67
+ Serialize individual blocks into blobs, or stream them directly to a writable target.
68
+
69
+ #### Buffer the result
70
+
71
+ ```ts
72
+ import { concatUint8, osmBlockToPbfBlobBytes, readOsmPbf } from "@osmix/pbf"
73
+
74
+ const response = await fetch("/fixtures/monaco.pbf")
75
+ const { header, blocks } = await readOsmPbf(response.body)
76
+
77
+ const chunks: Uint8Array[] = [await osmBlockToPbfBlobBytes(header)]
78
+ for await (const block of blocks) chunks.push(await osmBlockToPbfBlobBytes(block))
79
+
80
+ const fullFile = concatUint8(...chunks)
81
+
82
+ ```
83
+
84
+ #### Stream to a sink
85
+
86
+ Generators returned by `readOsmPbf` are single-use. Re-open the source (or buffer the blocks) if you want to stream the same dataset again.
87
+
88
+ ```ts
89
+ import { OsmBlocksToPbfBytesTransformStream, readOsmPbf } from "@osmix/pbf"
90
+
91
+ const response = await fetch("/fixtures/monaco.pbf")
92
+ const { header, blocks } = await readOsmPbf(response.body)
93
+
94
+ const upstream = new ReadableStream({
95
+ async start(controller) {
96
+ controller.enqueue(header)
97
+ for await (const block of blocks) controller.enqueue(block)
98
+ controller.close()
99
+ },
100
+ })
101
+
102
+ await upstream
103
+ .pipeThrough(new OsmBlocksToPbfBytesTransformStream())
104
+ .pipeTo(new WritableStream({ write: persistChunk }))
105
+ ```
106
+
107
+ `persistChunk` represents your storage layer (filesystem writes, uploads, IndexedDB, and so on). It receives `Uint8Array` pieces in the order they should be persisted.
108
+
109
+ ## API overview
110
+
111
+ - `readOsmPbf(data)` – Parses binary PBF data, returning `{ header, blocks }`. Throws if the first block is not an OSM header.
112
+ - `OsmPbfBytesToBlocksTransformStream` – Web `TransformStream` that emits the header once and then primitive blocks as they become available.
113
+ - `OsmBlocksToPbfBytesTransformStream` – Inverse transform that turns header/primitive blocks into PBF byte blobs while enforcing size limits.
114
+ - `createOsmPbfBlobGenerator()` – Returns a stateful generator that slices incoming bytes into compressed blob payloads (`Uint8Array`s), emitting the header blob first.
115
+ - `osmPbfBlobsToBlocksGenerator(blobs)` – Accepts a (async) generator of compressed blobs, decompresses them, and yields the header followed by primitive blocks.
116
+ - `osmBlockToPbfBlobBytes(block)` – Serializes a single header or primitive block, returning the BlobHeader length prefix and blob bytes as one `Uint8Array`.
117
+ - Utility exports: `toAsyncGenerator`, `compress`, `decompress`, `concatUint8`, `uint32BE`, and the size constants from `spec.ts`. Compression helpers detect Bun and fall back to Node's zlib bindings for compatibility.
118
+ - Generated protobuf helpers: `readHeaderBlock`, `writeHeaderBlock`, `readPrimitiveBlock`, `writePrimitiveBlock`, plus the associated TypeScript types (`OsmPbfBlock`, `OsmPbfHeaderBlock`, `OsmPbfBlob`, and friends).
119
+
120
+ ## See also
121
+
122
+ - [`@osmix/json`](../json/README.md) – Converts parsed blocks into ergonomic JSON or GeoJSON entities.
123
+ - [`@osmix/core`](../core/README.md) – Uses these readers/writers for ingest and export workflows.
124
+ - [`@osmix/change`](../change/README.md) – Builds on `@osmix/core` to generate change pipelines.
125
+
126
+ ## Environment and limitations
127
+
128
+ - Requires runtimes with Web Streams + `CompressionStream` / `DecompressionStream` support (modern browsers, Node 20+).
129
+ - Only `zlib_data` blobs are supported today; files containing `raw` or `lzma` payloads will throw.
130
+ - When working with Node `Readable` / `Writable` streams, adapt them to Web Streams (`stream/web`) before passing them to these helpers.
131
+
132
+ ### Memory usage guidance
133
+
134
+ - Prefer streaming transforms (`OsmPbfBytesToBlocksTransformStream` → `OsmBlocksToPbfBytesTransformStream`) for large extracts to avoid materializing entire files.
135
+ - Re-materializing full files (e.g., concatenating all blocks into one buffer) can require memory roughly proportional to input size plus transient compression buffers.
136
+ - In browsers, keep an eye on available heap limits (2–4 GB typical). Use chunked persistence (IndexedDB, file streams) rather than holding all bytes in RAM.
137
+
138
+ ## Development
139
+
140
+ - `bun run test packages/pbf`
141
+ - `bun run lint packages/pbf`
142
+ - `bun run typecheck packages/pbf`
143
+
144
+ Run `bun run check` at the repo root before publishing to ensure formatting, lint, and type coverage.
@@ -0,0 +1,5 @@
1
+ /**
2
+ * Decompresses raw OSM PBF blobs and yields typed header and primitive blocks.
3
+ * Expects the first blob to contain the file header and streams the rest as data blocks.
4
+ */
5
+ export declare function osmPbfBlobsToBlocksGenerator(blobs: AsyncGenerator<Uint8Array<ArrayBuffer>> | Generator<Uint8Array<ArrayBuffer>>): AsyncGenerator<import(".").OsmPbfHeaderBlock | import(".").OsmPbfBlock, void, unknown>;
@@ -0,0 +1,21 @@
1
+ import Pbf from "pbf";
2
+ import { readHeaderBlock, readPrimitiveBlock } from "./proto/osmformat";
3
+ import { decompress } from "./utils";
4
+ /**
5
+ * Decompresses raw OSM PBF blobs and yields typed header and primitive blocks.
6
+ * Expects the first blob to contain the file header and streams the rest as data blocks.
7
+ */
8
+ export async function* osmPbfBlobsToBlocksGenerator(blobs) {
9
+ let headerRead = false;
10
+ for await (const blob of blobs) {
11
+ const decompressedBlob = await decompress(blob);
12
+ const pbf = new Pbf(decompressedBlob);
13
+ if (!headerRead) {
14
+ headerRead = true;
15
+ yield readHeaderBlock(pbf);
16
+ }
17
+ else {
18
+ yield readPrimitiveBlock(pbf);
19
+ }
20
+ }
21
+ }
@@ -0,0 +1,16 @@
1
+ import type { OsmPbfBlock, OsmPbfHeaderBlock } from "./proto/osmformat";
2
+ /**
3
+ * Serializes a header or primitive block into a spec-compliant compressed blob.
4
+ * Automatically sets the blob type, compresses the payload, and enforces size guardrails.
5
+ * @param block - Parsed header or primitive block to encode.
6
+ * @returns BlobHeader length prefix + Blob bytes as a single Uint8Array.
7
+ */
8
+ export declare function osmBlockToPbfBlobBytes(block: OsmPbfBlock | OsmPbfHeaderBlock): Promise<Uint8Array<ArrayBufferLike>>;
9
+ /**
10
+ * Web `TransformStream` that converts OSM header/data blocks into PBF byte chunks.
11
+ * Throws if the header is not the first block and reuses `osmBlockToPbfBlobBytes` for encoding.
12
+ */
13
+ export declare class OsmBlocksToPbfBytesTransformStream extends TransformStream<OsmPbfHeaderBlock | OsmPbfBlock, Uint8Array> {
14
+ headerEnqueued: boolean;
15
+ constructor();
16
+ }
@@ -0,0 +1,73 @@
1
+ import Pbf from "pbf";
2
+ import { writeBlob, writeBlobHeader } from "./proto/fileformat";
3
+ import { writeHeaderBlock, writePrimitiveBlock } from "./proto/osmformat";
4
+ import { MAX_BLOB_SIZE_BYTES, MAX_HEADER_SIZE_BYTES, RECOMMENDED_BLOB_SIZE_BYTES, RECOMMENDED_HEADER_SIZE_BYTES, } from "./spec";
5
+ import { compress, concatUint8, uint32BE } from "./utils";
6
+ /**
7
+ * Serializes a header or primitive block into a spec-compliant compressed blob.
8
+ * Automatically sets the blob type, compresses the payload, and enforces size guardrails.
9
+ * @param block - Parsed header or primitive block to encode.
10
+ * @returns BlobHeader length prefix + Blob bytes as a single Uint8Array.
11
+ */
12
+ export async function osmBlockToPbfBlobBytes(block) {
13
+ const contentPbf = new Pbf();
14
+ let type;
15
+ if ("primitivegroup" in block) {
16
+ type = "OSMData";
17
+ writePrimitiveBlock(block, contentPbf);
18
+ }
19
+ else {
20
+ type = "OSMHeader";
21
+ writeHeaderBlock(block, contentPbf);
22
+ }
23
+ const contentData = contentPbf.finish();
24
+ const raw_size = contentData.length;
25
+ const compressedBuffer = await compress(contentData);
26
+ const blobPbf = new Pbf();
27
+ writeBlob({
28
+ raw_size,
29
+ zlib_data: compressedBuffer,
30
+ }, blobPbf);
31
+ const blob = blobPbf.finish();
32
+ const blobHeaderPbf = new Pbf();
33
+ writeBlobHeader({
34
+ type,
35
+ datasize: blob.length,
36
+ }, blobHeaderPbf);
37
+ const blobHeader = blobHeaderPbf.finish();
38
+ const blobHeaderSize = uint32BE(blobHeader.byteLength);
39
+ // Check the BlobHeader and Blob sizes, log error if over the recommended size, throw error if over the maximum size
40
+ if (blobHeader.byteLength > RECOMMENDED_HEADER_SIZE_BYTES) {
41
+ const sizeKiB = (blobHeader.byteLength / 1024).toFixed(2);
42
+ if (blobHeader.byteLength > MAX_HEADER_SIZE_BYTES) {
43
+ throw new Error(`BlobHeader is ${sizeKiB} KiB, the maximum size is 64KiB`);
44
+ }
45
+ console.warn(`BlobHeader is ${sizeKiB} KiB, the recommended size is 32KiB`);
46
+ }
47
+ if (blob.byteLength > RECOMMENDED_BLOB_SIZE_BYTES) {
48
+ const sizeMiB = (blob.byteLength / 1024 / 1024).toFixed(2);
49
+ if (blob.byteLength > MAX_BLOB_SIZE_BYTES) {
50
+ throw new Error(`Blob is ${sizeMiB} MiB, the maximum size is 32MiB`);
51
+ }
52
+ console.warn(`Blob is ${sizeMiB} MiB, the recommended size is 16MiB`);
53
+ }
54
+ return concatUint8(blobHeaderSize, blobHeader, blob);
55
+ }
56
+ /**
57
+ * Web `TransformStream` that converts OSM header/data blocks into PBF byte chunks.
58
+ * Throws if the header is not the first block and reuses `osmBlockToPbfBlobBytes` for encoding.
59
+ */
60
+ export class OsmBlocksToPbfBytesTransformStream extends TransformStream {
61
+ headerEnqueued = false;
62
+ constructor() {
63
+ super({
64
+ transform: async (block, controller) => {
65
+ if ("primitivegroup" in block && !this.headerEnqueued) {
66
+ throw Error("Header first in ReadableStream of blocks.");
67
+ }
68
+ this.headerEnqueued = true;
69
+ controller.enqueue(await osmBlockToPbfBlobBytes(block));
70
+ },
71
+ });
72
+ }
73
+ }
@@ -0,0 +1,8 @@
1
+ export * from "./blobs-to-blocks";
2
+ export * from "./blocks-to-pbf";
3
+ export * from "./pbf-to-blobs";
4
+ export * from "./pbf-to-blocks";
5
+ export * from "./proto/fileformat";
6
+ export * from "./proto/osmformat";
7
+ export * from "./spec";
8
+ export * from "./utils";
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from "./blobs-to-blocks";
2
+ export * from "./blocks-to-pbf";
3
+ export * from "./pbf-to-blobs";
4
+ export * from "./pbf-to-blocks";
5
+ export * from "./proto/fileformat";
6
+ export * from "./proto/osmformat";
7
+ export * from "./spec";
8
+ export * from "./utils";
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Creates a stateful parser that slices incoming bytes into compressed OSM PBF blobs.
3
+ * Works with buffers, iterables, or streams and yields `Uint8Array` payloads ready to decompress.
4
+ * The first blob represents the file header; subsequent blobs hold primitive data.
5
+ */
6
+ export declare function createOsmPbfBlobGenerator(): (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
@@ -0,0 +1,48 @@
1
+ import Pbf from "pbf";
2
+ import { HEADER_LENGTH_BYTES } from "./pbf-to-blocks";
3
+ import { readBlob, readBlobHeader, } from "./proto/fileformat";
4
+ /**
5
+ * Creates a stateful parser that slices incoming bytes into compressed OSM PBF blobs.
6
+ * Works with buffers, iterables, or streams and yields `Uint8Array` payloads ready to decompress.
7
+ * The first blob represents the file header; subsequent blobs hold primitive data.
8
+ */
9
+ export function createOsmPbfBlobGenerator() {
10
+ let pbf = new Pbf(new Uint8Array(0));
11
+ let state = "header-length";
12
+ let bytesNeeded = HEADER_LENGTH_BYTES;
13
+ let blobHeader = null;
14
+ /**
15
+ * Feed the parser with the next chunk of bytes and yield any complete compressed blobs.
16
+ */
17
+ return function* nextChunk(chunk) {
18
+ const currentBuffer = pbf.buf.slice(pbf.pos);
19
+ const tmpBuffer = new Uint8Array(currentBuffer.buffer.byteLength + chunk.byteLength);
20
+ tmpBuffer.set(currentBuffer.subarray(0));
21
+ tmpBuffer.set(new Uint8Array(chunk), currentBuffer.byteLength);
22
+ pbf = new Pbf(tmpBuffer);
23
+ while (pbf.pos + bytesNeeded <= pbf.length) {
24
+ if (state === "header-length") {
25
+ const dataView = new DataView(pbf.buf.buffer);
26
+ bytesNeeded = dataView.getInt32(pbf.pos, false); // network byte order
27
+ pbf.pos += HEADER_LENGTH_BYTES;
28
+ state = "header";
29
+ }
30
+ else if (state === "header") {
31
+ blobHeader = readBlobHeader(pbf, pbf.pos + bytesNeeded);
32
+ bytesNeeded = blobHeader.datasize;
33
+ state = "blob";
34
+ }
35
+ else if (state === "blob") {
36
+ if (blobHeader == null)
37
+ throw Error("Blob header has not been read");
38
+ const blob = readBlob(pbf, pbf.pos + bytesNeeded);
39
+ if (blob.zlib_data === undefined || blob.zlib_data.length === 0)
40
+ throw Error("Blob has no zlib data. Format is unsupported.");
41
+ yield blob.zlib_data;
42
+ state = "header-length";
43
+ bytesNeeded = HEADER_LENGTH_BYTES;
44
+ blobHeader = null;
45
+ }
46
+ }
47
+ };
48
+ }
@@ -0,0 +1,20 @@
1
+ import { type OsmPbfBlock, type OsmPbfHeaderBlock } from "./proto/osmformat";
2
+ import { type AsyncGeneratorValue } from "./utils";
3
+ export declare const HEADER_LENGTH_BYTES = 4;
4
+ /**
5
+ * Parses OSM PBF bytes from buffers, streams, or generators into header + block iterators.
6
+ * Returns the decoded header and a lazy async generator of primitive blocks.
7
+ */
8
+ export declare function readOsmPbf(data: AsyncGeneratorValue<ArrayBufferLike>): Promise<{
9
+ header: OsmPbfHeaderBlock;
10
+ blocks: AsyncGenerator<OsmPbfBlock>;
11
+ }>;
12
+ /**
13
+ * Web `TransformStream` that turns raw PBF byte chunks into OSM header/data blocks.
14
+ * Assumes the first decoded blob carries the header and emits it before any primitive blocks.
15
+ */
16
+ export declare class OsmPbfBytesToBlocksTransformStream extends TransformStream<ArrayBufferLike, OsmPbfHeaderBlock | OsmPbfBlock> {
17
+ generateBlobsFromChunk: (chunk: Uint8Array) => Generator<Uint8Array<ArrayBuffer>, void, unknown>;
18
+ header: OsmPbfHeaderBlock | null;
19
+ constructor();
20
+ }
@@ -0,0 +1,53 @@
1
+ import Pbf from "pbf";
2
+ import { osmPbfBlobsToBlocksGenerator } from "./blobs-to-blocks";
3
+ import { createOsmPbfBlobGenerator } from "./pbf-to-blobs";
4
+ import { readHeaderBlock, readPrimitiveBlock, } from "./proto/osmformat";
5
+ import { decompress, toAsyncGenerator } from "./utils";
6
+ export const HEADER_LENGTH_BYTES = 4;
7
+ /**
8
+ * Parses OSM PBF bytes from buffers, streams, or generators into header + block iterators.
9
+ * Returns the decoded header and a lazy async generator of primitive blocks.
10
+ */
11
+ export async function readOsmPbf(data) {
12
+ const generateBlobsFromChunk = createOsmPbfBlobGenerator();
13
+ const blocks = osmPbfBlobsToBlocksGenerator((async function* () {
14
+ for await (const chunk of toAsyncGenerator(data)) {
15
+ for await (const blob of generateBlobsFromChunk(new Uint8Array(chunk))) {
16
+ yield blob;
17
+ }
18
+ }
19
+ })());
20
+ const header = (await blocks.next()).value;
21
+ if (header == null || !("required_features" in header)) {
22
+ throw Error("OSM PBF header block not found");
23
+ }
24
+ return {
25
+ header,
26
+ blocks: blocks,
27
+ };
28
+ }
29
+ /**
30
+ * Web `TransformStream` that turns raw PBF byte chunks into OSM header/data blocks.
31
+ * Assumes the first decoded blob carries the header and emits it before any primitive blocks.
32
+ */
33
+ export class OsmPbfBytesToBlocksTransformStream extends TransformStream {
34
+ generateBlobsFromChunk = createOsmPbfBlobGenerator();
35
+ header = null;
36
+ constructor() {
37
+ super({
38
+ transform: async (bytesChunk, controller) => {
39
+ for await (const rawBlobs of this.generateBlobsFromChunk(new Uint8Array(bytesChunk))) {
40
+ const decompressed = await decompress(rawBlobs);
41
+ const pbf = new Pbf(decompressed);
42
+ if (this.header == null) {
43
+ this.header = readHeaderBlock(pbf);
44
+ controller.enqueue(this.header);
45
+ }
46
+ else {
47
+ controller.enqueue(readPrimitiveBlock(pbf));
48
+ }
49
+ }
50
+ },
51
+ });
52
+ }
53
+ }
@@ -0,0 +1,26 @@
1
+ import type Pbf from "pbf";
2
+ export type OsmPbfBlob = {
3
+ raw_size?: number;
4
+ raw?: Uint8Array;
5
+ zlib_data?: Uint8Array;
6
+ };
7
+ export type OsmPbfBlobHeader = {
8
+ type: "OSMHeader" | "OSMData";
9
+ datasize: number;
10
+ };
11
+ /**
12
+ * Reads an `OsmPbfBlob` message from the current position in the Pbf reader.
13
+ */
14
+ export declare function readBlob(pbf: Pbf, end?: number): OsmPbfBlob;
15
+ /**
16
+ * Writes an `OsmPbfBlob` message to the provided Pbf writer.
17
+ */
18
+ export declare function writeBlob(obj: OsmPbfBlob, pbf: Pbf): void;
19
+ /**
20
+ * Reads an `OsmPbfBlobHeader` from the current position in the Pbf reader.
21
+ */
22
+ export declare function readBlobHeader(pbf: Pbf, end?: number): OsmPbfBlobHeader;
23
+ /**
24
+ * Writes an `OsmPbfBlobHeader` message to the provided Pbf writer.
25
+ */
26
+ export declare function writeBlobHeader(obj: OsmPbfBlobHeader, pbf: Pbf): void;
@@ -0,0 +1,56 @@
1
+ /**
2
+ * Reads an `OsmPbfBlob` message from the current position in the Pbf reader.
3
+ */
4
+ export function readBlob(pbf, end) {
5
+ return pbf.readFields(readBlobField, {
6
+ raw_size: 0,
7
+ }, end);
8
+ }
9
+ /**
10
+ * Dispatches individual Blob fields based on their protobuf tag.
11
+ */
12
+ function readBlobField(tag, obj, pbf) {
13
+ if (tag === 2)
14
+ obj.raw_size = pbf.readVarint(true);
15
+ else if (tag === 1) {
16
+ obj.raw = pbf.readBytes();
17
+ }
18
+ else if (tag === 3) {
19
+ obj.zlib_data = pbf.readBytes();
20
+ }
21
+ }
22
+ /**
23
+ * Writes an `OsmPbfBlob` message to the provided Pbf writer.
24
+ */
25
+ export function writeBlob(obj, pbf) {
26
+ if (obj.raw_size)
27
+ pbf.writeVarintField(2, obj.raw_size);
28
+ if (obj.raw != null)
29
+ pbf.writeBytesField(1, obj.raw);
30
+ if (obj.zlib_data != null)
31
+ pbf.writeBytesField(3, obj.zlib_data);
32
+ }
33
+ /**
34
+ * Reads an `OsmPbfBlobHeader` from the current position in the Pbf reader.
35
+ */
36
+ export function readBlobHeader(pbf, end) {
37
+ return pbf.readFields(readBlobHeaderField, { type: "OSMData", datasize: 0 }, end);
38
+ }
39
+ /**
40
+ * Dispatches individual BlobHeader fields based on their protobuf tag.
41
+ */
42
+ function readBlobHeaderField(tag, obj, pbf) {
43
+ if (tag === 1)
44
+ obj.type = pbf.readString();
45
+ else if (tag === 3)
46
+ obj.datasize = pbf.readVarint(true);
47
+ }
48
+ /**
49
+ * Writes an `OsmPbfBlobHeader` message to the provided Pbf writer.
50
+ */
51
+ export function writeBlobHeader(obj, pbf) {
52
+ if (obj.type)
53
+ pbf.writeStringField(1, obj.type);
54
+ if (obj.datasize)
55
+ pbf.writeVarintField(3, obj.datasize);
56
+ }
@@ -0,0 +1,91 @@
1
+ import type Pbf from "pbf";
2
+ export type OsmPbfHeaderBBox = {
3
+ left: number;
4
+ right: number;
5
+ top: number;
6
+ bottom: number;
7
+ };
8
+ export type OsmPbfHeaderBlock = {
9
+ bbox?: OsmPbfHeaderBBox;
10
+ required_features: string[];
11
+ optional_features: string[];
12
+ writingprogram?: string;
13
+ source?: string;
14
+ osmosis_replication_timestamp?: number;
15
+ osmosis_replication_sequence_number?: number;
16
+ osmosis_replication_base_url?: string;
17
+ };
18
+ export interface OsmPbfBlockSettings {
19
+ granularity?: number;
20
+ lat_offset?: number;
21
+ lon_offset?: number;
22
+ date_granularity?: number;
23
+ }
24
+ export interface OsmPbfBlock extends OsmPbfBlockSettings {
25
+ stringtable: OsmPbfStringTable;
26
+ primitivegroup: OsmPbfGroup[];
27
+ }
28
+ export type OsmPbfGroup = {
29
+ nodes: OsmPbfNode[];
30
+ dense?: OsmPbfDenseNodes;
31
+ ways: OsmPbfWay[];
32
+ relations: OsmPbfRelation[];
33
+ };
34
+ export type OsmPbfStringTable = Uint8Array[];
35
+ export interface OsmPbfInfo {
36
+ version?: number;
37
+ timestamp?: number;
38
+ changeset?: number;
39
+ uid?: number;
40
+ user_sid?: number;
41
+ visible?: boolean;
42
+ }
43
+ export type OsmPbfDenseInfo = {
44
+ version: number[];
45
+ timestamp: number[];
46
+ changeset: number[];
47
+ uid: number[];
48
+ user_sid: number[];
49
+ visible: boolean[];
50
+ };
51
+ export interface OsmPbfPrimitive {
52
+ id: number;
53
+ keys: number[];
54
+ vals: number[];
55
+ info?: OsmPbfInfo;
56
+ }
57
+ export interface OsmPbfNode extends OsmPbfPrimitive {
58
+ lat: number;
59
+ lon: number;
60
+ }
61
+ export type OsmPbfDenseNodes = {
62
+ id: number[];
63
+ denseinfo?: OsmPbfDenseInfo;
64
+ lat: number[];
65
+ lon: number[];
66
+ keys_vals: number[];
67
+ };
68
+ export interface OsmPbfWay extends OsmPbfPrimitive {
69
+ refs: number[];
70
+ }
71
+ export interface OsmPbfRelation extends OsmPbfPrimitive {
72
+ roles_sid: number[];
73
+ memids: number[];
74
+ types: number[];
75
+ }
76
+ /**
77
+ * Reads an `OsmPbfHeaderBlock` message from the provided Pbf reader.
78
+ */
79
+ export declare function readHeaderBlock(pbf: Pbf, end?: number): OsmPbfHeaderBlock;
80
+ /**
81
+ * Serializes an `OsmPbfHeaderBlock` message into the Pbf writer.
82
+ */
83
+ export declare function writeHeaderBlock(obj: OsmPbfHeaderBlock, pbf: Pbf): void;
84
+ /**
85
+ * Reads a primitive block containing string tables and primitive groups.
86
+ */
87
+ export declare function readPrimitiveBlock(pbf: Pbf, end?: number): OsmPbfBlock;
88
+ /**
89
+ * Serializes a primitive block including its string table and primitive groups.
90
+ */
91
+ export declare function writePrimitiveBlock(obj: OsmPbfBlock, pbf: Pbf): void;