@loaders.gl/mvt 4.3.2 → 4.4.0-alpha.2

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 (51) hide show
  1. package/dist/dist.dev.js +151 -93
  2. package/dist/dist.min.js +1 -1
  3. package/dist/index.cjs +74 -78
  4. package/dist/index.cjs.map +4 -4
  5. package/dist/index.d.ts +2 -2
  6. package/dist/index.d.ts.map +1 -1
  7. package/dist/index.js +0 -1
  8. package/dist/lib/parse-mvt.d.ts.map +1 -1
  9. package/dist/lib/pojo-parser/mvt-constants.d.ts +116 -0
  10. package/dist/lib/pojo-parser/mvt-constants.d.ts.map +1 -0
  11. package/dist/lib/pojo-parser/mvt-constants.js +126 -0
  12. package/dist/lib/pojo-parser/mvt-types.d.ts +17 -0
  13. package/dist/lib/pojo-parser/mvt-types.d.ts.map +1 -0
  14. package/dist/lib/pojo-parser/mvt-types.js +4 -0
  15. package/dist/lib/pojo-parser/parse-geometry-from-pbf.d.ts +77 -0
  16. package/dist/lib/pojo-parser/parse-geometry-from-pbf.d.ts.map +1 -0
  17. package/dist/lib/pojo-parser/parse-geometry-from-pbf.js +234 -0
  18. package/dist/lib/pojo-parser/parse-mvt-from-pbf.d.ts +25 -0
  19. package/dist/lib/pojo-parser/parse-mvt-from-pbf.d.ts.map +1 -0
  20. package/dist/lib/pojo-parser/parse-mvt-from-pbf.js +262 -0
  21. package/dist/lib/vector-tile/vector-tile-feature.d.ts +2 -1
  22. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -1
  23. package/dist/lib/vector-tile/vector-tile-layer.d.ts +1 -1
  24. package/dist/lib/vector-tile/vector-tile-layer.d.ts.map +1 -1
  25. package/dist/mvt-format.d.ts +12 -0
  26. package/dist/mvt-format.d.ts.map +1 -0
  27. package/dist/mvt-format.js +20 -0
  28. package/dist/mvt-loader.d.ts +12 -12
  29. package/dist/mvt-loader.d.ts.map +1 -1
  30. package/dist/mvt-loader.js +3 -13
  31. package/dist/mvt-source.d.ts +22 -24
  32. package/dist/mvt-source.d.ts.map +1 -1
  33. package/dist/mvt-source.js +14 -23
  34. package/dist/mvt-worker.js +20 -15
  35. package/dist/table-tile-source.d.ts +38 -38
  36. package/dist/table-tile-source.d.ts.map +1 -1
  37. package/dist/table-tile-source.js +54 -53
  38. package/dist/tilejson-loader.js +1 -1
  39. package/package.json +7 -7
  40. package/src/index.ts +2 -2
  41. package/src/lib/parse-mvt.ts +2 -8
  42. package/src/lib/pojo-parser/mvt-constants.ts +135 -0
  43. package/src/lib/pojo-parser/mvt-types.ts +22 -0
  44. package/src/lib/pojo-parser/parse-geometry-from-pbf.ts +285 -0
  45. package/src/lib/pojo-parser/parse-mvt-from-pbf.ts +310 -0
  46. package/src/lib/vector-tile/vector-tile-feature.ts +2 -6
  47. package/src/lib/vector-tile/vector-tile-layer.ts +1 -1
  48. package/src/mvt-format.ts +23 -0
  49. package/src/mvt-loader.ts +2 -13
  50. package/src/mvt-source.ts +33 -38
  51. package/src/table-tile-source.ts +116 -96
@@ -0,0 +1,135 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright vis.gl contributors
4
+
5
+ /**
6
+ * MVT spec constants
7
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
8
+ */
9
+ export enum TileInfo {
10
+ /** repeated Layer */
11
+ layers = 3
12
+ }
13
+
14
+ /**
15
+ * MVT spec constants
16
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
17
+ * @note Layers are described in section 4.1 of the specification
18
+ */
19
+ export enum LayerInfo {
20
+ /**
21
+ * Any compliant implementation must first read the version
22
+ * number encoded in this message and choose the correct
23
+ * implementation for this version number before proceeding to
24
+ * decode other parts of this message.
25
+ * required uint32 [ default = 1 ];
26
+ */
27
+ version = 15,
28
+
29
+ /** PBF: required string */
30
+ name = 1,
31
+
32
+ /** The actual features in this tile.
33
+ * PBF: repeated Feature
34
+ */
35
+ features = 2,
36
+
37
+ /**
38
+ * Dictionary encoding for keys
39
+ * PBF: repeated string
40
+ */
41
+ keys = 3,
42
+
43
+ /**
44
+ * Dictionary encoding for values
45
+ * PBF: repeated Value
46
+ */
47
+ values = 4,
48
+
49
+ /**
50
+ * Although this is an "optional" field it is required by the specification.
51
+ * See https://github.com/mapbox/vector-tile-spec/issues/47
52
+ * PBF: optional uint32 [ default = 4096 ];
53
+ */
54
+ extent = 5
55
+
56
+ // extensions 16 to max;
57
+ }
58
+
59
+ /**
60
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
61
+ * Features are described in section 4.2 of the specification
62
+ */
63
+ export enum FeatureInfo {
64
+ /** optional uint64 [ default = 0 ]; */
65
+ id = 1,
66
+
67
+ /**
68
+ * Tags of this feature are encoded as repeated pairs of integers.
69
+ * A detailed description of tags is located in sections 4.2 and 4.4 of the specification
70
+ * repeated uint32 [ packed = true ];
71
+ */
72
+ tags = 2,
73
+
74
+ /**
75
+ * The type of geometry stored in this feature.
76
+ * GeomType [ default = UNKNOWN ];
77
+ */
78
+ type = 3,
79
+
80
+ /**
81
+ * Contains a stream of commands and parameters (vertices).
82
+ * A detailed description on geometry encoding is located in
83
+ * section 4.3 of the specification.
84
+ * repeated uint32 [ packed = true ];
85
+ */
86
+ geometry = 4
87
+ }
88
+
89
+ /**
90
+ * GeomType is described in section 4.3.4 of the specification
91
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
92
+ * */
93
+ export enum GeometryType {
94
+ UNKNOWN = 0,
95
+ POINT = 1,
96
+ LINESTRING = 2,
97
+ POLYGON = 3
98
+ }
99
+
100
+ /**
101
+ * Variant type encoding
102
+ * The use of values is described in section 4.1 of the specification
103
+ * @note Exactly one of these values must be present in a valid message
104
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
105
+ */
106
+ export enum PropertyType {
107
+ /** string */
108
+ string_value = 1, //
109
+ /** float */
110
+ float_value = 2,
111
+ /** double */
112
+ double_value = 3,
113
+ /** int64 */
114
+ int_value = 4,
115
+ /** uint64 */
116
+ uint_value = 5,
117
+ /** sint64 */
118
+ sint_value = 6,
119
+ /** bool */
120
+ bool_value = 7
121
+ // extensions 8 to max;
122
+ }
123
+
124
+ /**
125
+ * "Turtle graphics" style geometry commands
126
+ * @see https://github.com/mapbox/vector-tile-spec/blob/master/2.1/README.md
127
+ */
128
+ export enum Command {
129
+ /** 2 Parameters: dX, dY */
130
+ MoveTo = 1,
131
+ /** 2 Parameters dX, dY */
132
+ LineTo = 2,
133
+ /** No parameters */
134
+ ClosePath = 7
135
+ }
@@ -0,0 +1,22 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright vis.gl contributors
4
+
5
+ import type {Schema, FlatIndexedGeometry} from '@loaders.gl/schema';
6
+
7
+ export type MVTTile = {
8
+ layers: Record<string, MVTLayer>;
9
+ };
10
+
11
+ export type MVTLayer = {
12
+ version: number;
13
+ name: string;
14
+ extent: number;
15
+ length: number;
16
+ schema: Schema;
17
+ columns: Record<string, (string | number | boolean | null)[]>;
18
+ idColumn: number[];
19
+ boundingBoxColumn: [number, number, number, number][];
20
+ geometryTypeColumn: number[];
21
+ geometryColumn: FlatIndexedGeometry[];
22
+ };
@@ -0,0 +1,285 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright vis.gl contributors
4
+
5
+ // This code is inspired by https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
6
+
7
+ import type {
8
+ // Feature,
9
+ // FlatFeature,
10
+ // FlatGeometry,
11
+ FlatIndexedGeometry
12
+ // GeojsonGeometryInfo
13
+ } from '@loaders.gl/schema';
14
+
15
+ import Protobuf from 'pbf';
16
+
17
+ import * as MVT from './mvt-constants';
18
+
19
+ // import {
20
+ // classifyRings,
21
+ // classifyRingsFlat,
22
+ // projectToLngLat,
23
+ // projectToLngLatFlat,
24
+ // convertToLocalCoordinates,
25
+ // convertToLocalCoordinatesFlat
26
+ // } from '../utils/geometry-utils';
27
+
28
+ type GeometryCommand = {type: 'moveto' | 'lineto' | 'closepath'; x: number; y: number};
29
+
30
+ export function* makeMVTGeometryCommandIterator(pbf: Protobuf): IterableIterator<GeometryCommand> {
31
+ const endPos = pbf.readVarint() + pbf.pos;
32
+ let cmd = 1;
33
+ let cmdLen: number;
34
+ let length = 0;
35
+
36
+ // Note: We reuse and return the same command object to avoid creating new objects
37
+ const command: GeometryCommand = {type: 'closepath', x: 0, y: 0};
38
+
39
+ while (pbf.pos < endPos) {
40
+ if (length <= 0) {
41
+ cmdLen = pbf.readVarint();
42
+ cmd = cmdLen & 0x7;
43
+ length = cmdLen >> 3;
44
+ }
45
+
46
+ length--;
47
+
48
+ switch (cmd as MVT.Command) {
49
+ case MVT.Command.MoveTo:
50
+ command.type = 'moveto';
51
+ command.x += pbf.readSVarint();
52
+ command.y += pbf.readSVarint();
53
+ yield command;
54
+ break;
55
+
56
+ case MVT.Command.LineTo:
57
+ command.type = 'lineto';
58
+ command.x += pbf.readSVarint();
59
+ command.y += pbf.readSVarint();
60
+ yield command;
61
+ break;
62
+
63
+ case MVT.Command.ClosePath:
64
+ command.type = 'moveto';
65
+ yield command;
66
+ break;
67
+
68
+ default:
69
+ throw new Error(`unknown command ${cmd}`);
70
+ }
71
+ }
72
+ }
73
+
74
+ /**
75
+ * Creates a bounding box from the feature geometry at the current position
76
+ * @todo - Reparses the entire PBF geometry - replace with a generic bounding box calculation on parsed data?
77
+ */
78
+ export function readBoundingBoxFromPBF(pbf: Protobuf): [number, number, number, number] {
79
+ let x1 = Infinity;
80
+ let x2 = -Infinity;
81
+ let y1 = Infinity;
82
+ let y2 = -Infinity;
83
+
84
+ for (const command of makeMVTGeometryCommandIterator(pbf)) {
85
+ switch (command.type) {
86
+ case 'moveto':
87
+ case 'lineto':
88
+ if (command.x < x1) x1 = command.x;
89
+ if (command.x > x2) x2 = command.x;
90
+ if (command.y < y1) y1 = command.y;
91
+ if (command.y > y2) y2 = command.y;
92
+ break;
93
+
94
+ case 'closepath':
95
+ default:
96
+ // ignore
97
+ }
98
+ }
99
+
100
+ return [x1, y1, x2, y2];
101
+ }
102
+
103
+ /**
104
+ * Extract intermediate Flat GeoJSON
105
+ * data format, which maps closely to the binary data buffers.
106
+ * It is similar to GeoJSON, but rather than storing the coordinates
107
+ * in multidimensional arrays, we have a 1D `data` with all the
108
+ * coordinates, and then index into this using the `indices`
109
+ * parameter, e.g.
110
+ *
111
+ * geometry: {
112
+ * type: 'Point', data: [1,2], indices: [0]
113
+ * }
114
+ * geometry: {
115
+ * type: 'LineString', data: [1,2,3,4,...], indices: [0]
116
+ * }
117
+ * geometry: {
118
+ * type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
119
+ * }
120
+ * Thus the indices member lets us look up the relevant range
121
+ * from the data array.
122
+ * The Multi* versions of the above types share the same data
123
+ * structure, just with multiple elements in the indices array
124
+ */
125
+ // eslint-disable-next-line complexity, max-statements
126
+ export function loadFlatGeometryFromPBF(pbf: Protobuf): FlatIndexedGeometry {
127
+ // Note: I attempted to replace the `data` array with a
128
+ // Float32Array, but performance was worse, both using
129
+ // `set()` and direct index access. Also, we cannot
130
+ // know how large the buffer should be, so it would
131
+ // increase memory usage
132
+ const indices: number[] = []; // Indices where geometries start
133
+ const data: number[] = []; // Flat array of coordinate data
134
+ let i = 0;
135
+
136
+ for (const command of makeMVTGeometryCommandIterator(pbf)) {
137
+ switch (command.type) {
138
+ case 'moveto':
139
+ // New line
140
+ indices.push(i);
141
+ data.push(command.x, command.y);
142
+ i += 2;
143
+ break;
144
+
145
+ case 'lineto':
146
+ data.push(command.x, command.y);
147
+ i += 2;
148
+ break;
149
+
150
+ case 'closepath':
151
+ // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
152
+ if (i > 0) {
153
+ const start = indices[indices.length - 1]; // start index of polygon
154
+ data.push(data[start], data[start + 1]); // closePolygon
155
+ i += 2;
156
+ }
157
+ break;
158
+
159
+ default:
160
+ // just for eslint
161
+ }
162
+ }
163
+
164
+ return {data, indices};
165
+ }
166
+
167
+ /**
168
+ * Load a GeoJSON style Geometry from the raw PBF
169
+ *
170
+ function readGeoJSONGeometryFromPBF(pbf): number[][][] {
171
+ const lines: number[][][] = [];
172
+ let line: number[][] | undefined;
173
+
174
+ for (const command of makeMVTGeometryCommandIterator(pbf)) {
175
+ switch (command.type) {
176
+ case 'moveto':
177
+ // moveTo
178
+ if (line) lines.push(line);
179
+ line = [];
180
+ if (line) line.push([command.x, command.y]);
181
+ break;
182
+
183
+ case 'lineto':
184
+ if (line) line.push([command.x, command.y]);
185
+ break;
186
+
187
+ case 'closepath':
188
+ // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
189
+ if (line) {
190
+ line.push(line[0].slice()); // closePolygon
191
+ }
192
+ break;
193
+
194
+ default:
195
+ // just for eslint
196
+ }
197
+ }
198
+
199
+ if (line) {
200
+ lines.push(line);
201
+ }
202
+
203
+ return lines;
204
+ }
205
+ */
206
+
207
+ // export class VectorTileFeature implements MVTFeature {
208
+ // properties: Record<string, string | number | boolean | null>;
209
+ // extent: any;
210
+ // type: number;
211
+ // id: number | null;
212
+ // _pbf: Protobuf;
213
+ // _geometryPos: number;
214
+ // _keys: string[];
215
+ // _values: (string | number | boolean | null)[];
216
+ // _geometryInfo: GeojsonGeometryInfo;
217
+
218
+ // static types = MVT_GEOMETRY_TYPES;
219
+
220
+ // // eslint-disable-next-line max-params
221
+ // constructor(
222
+ // pbf: Protobuf,
223
+ // end: number,
224
+ // extent: any,
225
+ // keys: string[],
226
+ // values: (string | number | boolean | null)[],
227
+ // geometryInfo?: GeojsonGeometryInfo
228
+ // ) {
229
+ // // Public
230
+ // this.properties = {};
231
+ // this.extent = extent;
232
+ // this.type = 0;
233
+ // this.id = null;
234
+
235
+ // // Private
236
+ // this._pbf = pbf;
237
+ // this._geometryPos = -1;
238
+ // this._keys = keys;
239
+ // this._values = values;
240
+
241
+ // // Only used by binary tiles
242
+ // this._geometryInfo = geometryInfo!;
243
+
244
+ // pbf.readFields(readFeatureFromPBF, this, end);
245
+ // }
246
+
247
+ // toGeoJSONFeature(
248
+ // coordinates: 'wgs84' | 'local',
249
+ // tileIndex?: {x: number; y: number; z: number}
250
+ // ): Feature {
251
+ // return readGeoJSONFeatureFromPBF(this, coordinates, this.extent, tileIndex);
252
+ // }
253
+
254
+ // /**
255
+ // *
256
+ // * @param options
257
+ // * @returns
258
+ // */
259
+ // toBinaryFeature(
260
+ // coordinates: 'wgs84' | 'local',
261
+ // tileIndex?: {x: number; y: number; z: number}
262
+ // ): FlatFeature {
263
+ // return readFlatFeatureFromPBF(this, coordinates, this.extent, tileIndex);
264
+ // }
265
+
266
+ // /** Read a bounding box from the feature */
267
+ // bbox() {
268
+ // this._pbf.pos = this._geometryPos;
269
+ // return readBoundingBoxFromPBF(this._pbf);
270
+ // }
271
+
272
+ // // Geometry helpers
273
+
274
+ // /** Parses protobuf data to nested "GeoJSON like" coordinates array */
275
+ // loadGeometry(): number[][][] {
276
+ // this._pbf.pos = this._geometryPos;
277
+ // return readGeoJSONGeometryFromPBF(this._pbf);
278
+ // }
279
+
280
+ // /** Parses protobuf data to an intermediate "Flat GeoJSON" data format */
281
+ // loadFlatGeometry(): FlatIndexedGeometry {
282
+ // this._pbf.pos = this._geometryPos;
283
+ // return loadFlatGeometryFromPBF(this._pbf);
284
+ // }
285
+ // }