forked from sharedstreets/sharedstreets-pbf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
100 lines (95 loc) · 3.14 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { Reader, util } from "protobufjs/minimal";
import {
SharedStreetsGeometry,
SharedStreetsIntersection,
SharedStreetsMetadata,
SharedStreetsReference,
} from "sharedstreets-types";
import { SharedStreetsProto } from "./proto";
export * from "./proto/index";
/**
* Geometry Pbf
*
* Parser for SharedStreets Geometry Pbf Buffers
*
* @param {Buffer} buffer Pbf Buffer
* @returns {Array<SharedStreetsGeometry>} An Array of SharedStreet Geometry
* @example
* const buffer = fs.readFileSync('z-x-y.geometry.pbf')
*
* const geoms = sharedstreetsPbf.geometry(buffer)
* geoms[0].id // => '81f666c5e1e4de0f7df4fbd793d909b2'
*/
export function geometry(buffer: Buffer | Uint8Array) {
return readBuffer<SharedStreetsGeometry>(buffer, SharedStreetsProto.SharedStreetsGeometry);
}
/**
* Intersection Pbf
*
* Parser for SharedStreets Intersection Pbf Buffers
*
* @param {Buffer} buffer Pbf Buffer
* @returns {Array<SharedStreetsIntersection>} An Array of SharedStreet Intersections
* @example
* const buffer = fs.readFileSync('z-x-y.intersection.pbf')
*
* const intersections = sharedstreetsPbf.intersection(buffer)
* intersections[0].id // => '8037a9444353cd7dd3f58d9a436f2537'
*/
export function intersection(buffer: Buffer | Uint8Array) {
return readBuffer<SharedStreetsIntersection>(buffer, SharedStreetsProto.SharedStreetsIntersection);
}
/**
* Reference Pbf
*
* Parser for SharedStreets Reference Pbf Buffers
*
* @param {Buffer} buffer Pbf Buffer
* @returns {Array<SharedStreetsReferencePbf>} An Array of SharedStreet References
* @example
* const buffer = fs.readFileSync('z-x-y.reference.pbf')
*
* const references = sharedstreetsPbf.reference(buffer)
* references[0].id // => '41d73e28819470745fa1f93dc46d82a9'
*/
export function reference(buffer: Buffer | Uint8Array) {
return readBuffer<SharedStreetsReference>(buffer, SharedStreetsProto.SharedStreetsReference);
}
/**
* Metadata Pbf
*
* Parser for SharedStreets Metadata Pbf Buffers
*
* @param {Buffer} buffer Pbf Buffer
* @returns {Array<SharedStreetsMetadata>} An Array of SharedStreet Metadatas
* @example
* const buffer = fs.readFileSync('z-x-y.metadata.pbf')
*
* const metadatas = sharedstreetsPbf.metadata(buffer)
* metadatas[0].geometryID // => '81f666c5e1e4de0f7df4fbd793d909b2'
*/
export function metadata(buffer: Buffer | Uint8Array) {
return readBuffer<SharedStreetsMetadata>(buffer, SharedStreetsProto.SharedStreetsMetadata);
}
/**
* Decode Delimited buffers using protobufjs
*
* @param {Buffer|Uint8Array} buffer Pbf Buffer
* @param {any} parser Protobufjs Parser
* @returns {Object[]} An Array of based on given Protobufjs Parser
* @example
* const parser = sharedstreetsPbf.SharedStreetsProto.GISMetadata;
* const results = sharedstreetsPbf.readBuffer(buffer, parser);
*/
export function readBuffer<T = any>(buffer: Buffer | Uint8Array, parser: any): T[] {
const results = [];
const reader = new Reader(buffer);
while (reader.pos < reader.len) {
const message = parser.decodeDelimited(reader);
const options = util.toJSONOptions;
options.defaults = true;
const json = parser.toObject(message, options);
results.push(json);
}
return results;
}