@loaders.gl/mvt 3.1.0-alpha.5 → 3.1.0-beta.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 (57) hide show
  1. package/dist/bundle.d.ts +1 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2277 -0
  4. package/dist/es5/lib/parse-mvt.js.map +1 -1
  5. package/dist/es5/mvt-loader.js +1 -1
  6. package/dist/es5/mvt-loader.js.map +1 -1
  7. package/dist/esm/lib/parse-mvt.js.map +1 -1
  8. package/dist/esm/mvt-loader.js +1 -1
  9. package/dist/esm/mvt-loader.js.map +1 -1
  10. package/dist/helpers/binary-util-functions.d.ts +1 -0
  11. package/dist/helpers/binary-util-functions.d.ts.map +1 -0
  12. package/dist/helpers/binary-util-functions.js +116 -0
  13. package/dist/helpers/mapbox-util-functions.d.ts +1 -0
  14. package/dist/helpers/mapbox-util-functions.d.ts.map +1 -0
  15. package/dist/helpers/mapbox-util-functions.js +82 -0
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +6 -0
  19. package/dist/lib/binary-vector-tile/features-to-binary.d.ts +1 -0
  20. package/dist/lib/binary-vector-tile/features-to-binary.d.ts.map +1 -0
  21. package/dist/lib/binary-vector-tile/features-to-binary.js +353 -0
  22. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +1 -0
  23. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -0
  24. package/dist/lib/binary-vector-tile/vector-tile-feature.js +159 -0
  25. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +1 -0
  26. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -0
  27. package/dist/lib/binary-vector-tile/vector-tile-layer.js +91 -0
  28. package/dist/lib/binary-vector-tile/vector-tile.d.ts +1 -0
  29. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -0
  30. package/dist/lib/binary-vector-tile/vector-tile.js +29 -0
  31. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +1 -0
  32. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -0
  33. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +170 -0
  34. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +1 -0
  35. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -0
  36. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +89 -0
  37. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +1 -0
  38. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -0
  39. package/dist/lib/mapbox-vector-tile/vector-tile.js +29 -0
  40. package/dist/lib/parse-mvt.d.ts +2 -1
  41. package/dist/lib/parse-mvt.d.ts.map +1 -0
  42. package/dist/lib/parse-mvt.js +142 -0
  43. package/dist/lib/types.d.ts +1 -0
  44. package/dist/lib/types.d.ts.map +1 -0
  45. package/dist/lib/types.js +2 -0
  46. package/dist/mvt-loader.d.ts +1 -0
  47. package/dist/mvt-loader.d.ts.map +1 -0
  48. package/dist/mvt-loader.js +45 -0
  49. package/dist/mvt-worker.js +2286 -3
  50. package/dist/workers/mvt-worker.d.ts +1 -0
  51. package/dist/workers/mvt-worker.d.ts.map +1 -0
  52. package/dist/workers/mvt-worker.js +5 -0
  53. package/package.json +6 -7
  54. package/src/lib/parse-mvt.ts +1 -1
  55. package/dist/dist.min.js +0 -4
  56. package/dist/dist.min.js.map +0 -1
  57. package/dist/mvt-worker.js.map +0 -1
@@ -0,0 +1,353 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.TEST_EXPORTS = exports.featuresToBinary = void 0;
4
+ /* eslint-disable indent */
5
+ const polygon_1 = require("@math.gl/polygon");
6
+ /**
7
+ * Convert binary features to flat binary arrays. Similar to
8
+ * `geojsonToBinary` helper function, except that it expects
9
+ * a binary representation of the feature data, which enables
10
+ * 2X-3X speed increase in parse speed, compared to using
11
+ * geoJSON. See `binary-vector-tile/VectorTileFeature` for
12
+ * data format detais
13
+ *
14
+ * @param features
15
+ * @param firstPassData
16
+ * @param options
17
+ * @returns filled arrays
18
+ */
19
+ function featuresToBinary(features, firstPassData, options) {
20
+ return fillArrays(features, firstPassData, {
21
+ numericPropKeys: options ? options.numericPropKeys : extractNumericPropKeys(features),
22
+ PositionDataType: options ? options.PositionDataType : Float32Array
23
+ });
24
+ }
25
+ exports.featuresToBinary = featuresToBinary;
26
+ exports.TEST_EXPORTS = {
27
+ extractNumericPropKeys,
28
+ fillArrays
29
+ };
30
+ /**
31
+ * Extracts properties that are always numeric
32
+ *
33
+ * @param features
34
+ * @returns object with numeric keys
35
+ */
36
+ function extractNumericPropKeys(features) {
37
+ const numericPropKeys = {};
38
+ for (const feature of features) {
39
+ if (feature.properties) {
40
+ for (const key in feature.properties) {
41
+ // If property has not been seen before, or if property has been numeric
42
+ // in all previous features, check if numeric in this feature
43
+ // If not numeric, false is stored to prevent rechecking in the future
44
+ const numericSoFar = numericPropKeys[key];
45
+ if (numericSoFar || numericSoFar === undefined) {
46
+ const val = feature.properties[key];
47
+ numericPropKeys[key] = isNumeric(val);
48
+ }
49
+ }
50
+ }
51
+ }
52
+ return Object.keys(numericPropKeys).filter((k) => numericPropKeys[k]);
53
+ }
54
+ /**
55
+ * Fills coordinates into pre-allocated typed arrays
56
+ *
57
+ * @param features
58
+ * @param firstPassData
59
+ * @param options
60
+ * @returns an accessor object with value and size keys
61
+ */
62
+ // eslint-disable-next-line complexity
63
+ function fillArrays(features, firstPassData, options) {
64
+ const { pointPositionsCount, pointFeaturesCount, linePositionsCount, linePathsCount, lineFeaturesCount, polygonPositionsCount, polygonObjectsCount, polygonRingsCount, polygonFeaturesCount } = firstPassData;
65
+ const { numericPropKeys, PositionDataType = Float32Array } = options;
66
+ const hasGlobalId = features[0] && 'id' in features[0];
67
+ const coordLength = 2;
68
+ const GlobalFeatureIdsDataType = features.length > 65535 ? Uint32Array : Uint16Array;
69
+ const points = {
70
+ positions: new PositionDataType(pointPositionsCount * coordLength),
71
+ globalFeatureIds: new GlobalFeatureIdsDataType(pointPositionsCount),
72
+ featureIds: pointFeaturesCount > 65535
73
+ ? new Uint32Array(pointPositionsCount)
74
+ : new Uint16Array(pointPositionsCount),
75
+ numericProps: {},
76
+ properties: [],
77
+ fields: []
78
+ };
79
+ const lines = {
80
+ pathIndices: linePositionsCount > 65535
81
+ ? new Uint32Array(linePathsCount + 1)
82
+ : new Uint16Array(linePathsCount + 1),
83
+ positions: new PositionDataType(linePositionsCount * coordLength),
84
+ globalFeatureIds: new GlobalFeatureIdsDataType(linePositionsCount),
85
+ featureIds: lineFeaturesCount > 65535
86
+ ? new Uint32Array(linePositionsCount)
87
+ : new Uint16Array(linePositionsCount),
88
+ numericProps: {},
89
+ properties: [],
90
+ fields: []
91
+ };
92
+ const polygons = {
93
+ polygonIndices: polygonPositionsCount > 65535
94
+ ? new Uint32Array(polygonObjectsCount + 1)
95
+ : new Uint16Array(polygonObjectsCount + 1),
96
+ primitivePolygonIndices: polygonPositionsCount > 65535
97
+ ? new Uint32Array(polygonRingsCount + 1)
98
+ : new Uint16Array(polygonRingsCount + 1),
99
+ positions: new PositionDataType(polygonPositionsCount * coordLength),
100
+ triangles: [],
101
+ globalFeatureIds: new GlobalFeatureIdsDataType(polygonPositionsCount),
102
+ featureIds: polygonFeaturesCount > 65535
103
+ ? new Uint32Array(polygonPositionsCount)
104
+ : new Uint16Array(polygonPositionsCount),
105
+ numericProps: {},
106
+ properties: [],
107
+ fields: []
108
+ };
109
+ // Instantiate numeric properties arrays; one value per vertex
110
+ for (const object of [points, lines, polygons]) {
111
+ for (const propName of numericPropKeys) {
112
+ // If property has been numeric in all previous features in which the property existed, check
113
+ // if numeric in this feature
114
+ object.numericProps[propName] = new Float32Array(object.positions.length / coordLength);
115
+ }
116
+ }
117
+ // Set last element of path/polygon indices as positions length
118
+ lines.pathIndices[linePathsCount] = linePositionsCount;
119
+ polygons.polygonIndices[polygonObjectsCount] = polygonPositionsCount;
120
+ polygons.primitivePolygonIndices[polygonRingsCount] = polygonPositionsCount;
121
+ const indexMap = {
122
+ pointPosition: 0,
123
+ pointFeature: 0,
124
+ linePosition: 0,
125
+ linePath: 0,
126
+ lineFeature: 0,
127
+ polygonPosition: 0,
128
+ polygonObject: 0,
129
+ polygonRing: 0,
130
+ polygonFeature: 0,
131
+ feature: 0
132
+ };
133
+ for (const feature of features) {
134
+ const geometry = feature.geometry;
135
+ const properties = feature.properties || {};
136
+ switch (geometry.type) {
137
+ case 'Point':
138
+ case 'MultiPoint':
139
+ handlePoint(geometry, points, indexMap, coordLength, properties);
140
+ points.properties.push(keepStringProperties(properties, numericPropKeys));
141
+ if (hasGlobalId) {
142
+ points.fields.push({ id: feature.id });
143
+ }
144
+ indexMap.pointFeature++;
145
+ break;
146
+ case 'LineString':
147
+ case 'MultiLineString':
148
+ handleLineString(geometry, lines, indexMap, coordLength, properties);
149
+ lines.properties.push(keepStringProperties(properties, numericPropKeys));
150
+ if (hasGlobalId) {
151
+ lines.fields.push({ id: feature.id });
152
+ }
153
+ indexMap.lineFeature++;
154
+ break;
155
+ case 'Polygon':
156
+ case 'MultiPolygon':
157
+ handlePolygon(geometry, polygons, indexMap, coordLength, properties);
158
+ polygons.properties.push(keepStringProperties(properties, numericPropKeys));
159
+ if (hasGlobalId) {
160
+ polygons.fields.push({ id: feature.id });
161
+ }
162
+ indexMap.polygonFeature++;
163
+ break;
164
+ default:
165
+ throw new Error('Invalid geometry type');
166
+ }
167
+ indexMap.feature++;
168
+ }
169
+ // Wrap each array in an accessor object with value and size keys
170
+ return makeAccessorObjects(points, lines, polygons, coordLength);
171
+ }
172
+ /**
173
+ * Fills (Multi)Point coordinates into points object of arrays
174
+ *
175
+ * @param geometry
176
+ * @param points
177
+ * @param indexMap
178
+ * @param coordLength
179
+ * @param properties
180
+ */
181
+ function handlePoint(geometry, points, indexMap, coordLength, properties) {
182
+ points.positions.set(geometry.data, indexMap.pointPosition * coordLength);
183
+ const nPositions = geometry.data.length / coordLength;
184
+ fillNumericProperties(points, properties, indexMap.pointPosition, nPositions);
185
+ points.globalFeatureIds.fill(indexMap.feature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
186
+ points.featureIds.fill(indexMap.pointFeature, indexMap.pointPosition, indexMap.pointPosition + nPositions);
187
+ indexMap.pointPosition += nPositions;
188
+ }
189
+ /**
190
+ * Fills (Multi)LineString coordinates into lines object of arrays
191
+ *
192
+ * @param geometry
193
+ * @param lines
194
+ * @param indexMap
195
+ * @param coordLength
196
+ * @param properties
197
+ */
198
+ function handleLineString(geometry, lines, indexMap, coordLength, properties) {
199
+ lines.positions.set(geometry.data, indexMap.linePosition * coordLength);
200
+ const nPositions = geometry.data.length / coordLength;
201
+ fillNumericProperties(lines, properties, indexMap.linePosition, nPositions);
202
+ lines.globalFeatureIds.fill(indexMap.feature, indexMap.linePosition, indexMap.linePosition + nPositions);
203
+ lines.featureIds.fill(indexMap.lineFeature, indexMap.linePosition, indexMap.linePosition + nPositions);
204
+ for (let i = 0, il = geometry.lines.length; i < il; ++i) {
205
+ // Extract range of data we are working with, defined by start
206
+ // and end indices (these index into the geometry.data array)
207
+ const start = geometry.lines[i];
208
+ const end = i === il - 1
209
+ ? geometry.data.length // last line, so read to end of data
210
+ : geometry.lines[i + 1]; // start index for next line
211
+ lines.pathIndices[indexMap.linePath++] = indexMap.linePosition;
212
+ indexMap.linePosition += (end - start) / coordLength;
213
+ }
214
+ }
215
+ /**
216
+ * Fills (Multi)Polygon coordinates into polygons object of arrays
217
+ *
218
+ * @param geometry
219
+ * @param polygons
220
+ * @param indexMap
221
+ * @param coordLength
222
+ * @param properties
223
+ */
224
+ function handlePolygon(geometry, polygons, indexMap, coordLength, properties) {
225
+ polygons.positions.set(geometry.data, indexMap.polygonPosition * coordLength);
226
+ const nPositions = geometry.data.length / coordLength;
227
+ fillNumericProperties(polygons, properties, indexMap.polygonPosition, nPositions);
228
+ polygons.globalFeatureIds.fill(indexMap.feature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
229
+ polygons.featureIds.fill(indexMap.polygonFeature, indexMap.polygonPosition, indexMap.polygonPosition + nPositions);
230
+ // Unlike Point & LineString geometry.lines is a 2D array
231
+ for (let l = 0, ll = geometry.lines.length; l < ll; ++l) {
232
+ const startPosition = indexMap.polygonPosition;
233
+ polygons.polygonIndices[indexMap.polygonObject++] = startPosition;
234
+ const areas = geometry.areas[l];
235
+ const lines = geometry.lines[l];
236
+ const nextLines = geometry.lines[l + 1];
237
+ for (let i = 0, il = lines.length; i < il; ++i) {
238
+ const start = lines[i];
239
+ const end = i === il - 1
240
+ ? // last line, so either read to:
241
+ nextLines === undefined
242
+ ? geometry.data.length // end of data (no next lines)
243
+ : nextLines[0] // start of first line in nextLines
244
+ : lines[i + 1]; // start index for next line
245
+ polygons.primitivePolygonIndices[indexMap.polygonRing++] = indexMap.polygonPosition;
246
+ indexMap.polygonPosition += (end - start) / coordLength;
247
+ }
248
+ const endPosition = indexMap.polygonPosition;
249
+ triangulatePolygon(polygons, areas, lines, { startPosition, endPosition, coordLength });
250
+ }
251
+ }
252
+ /**
253
+ * Triangulate polygon using earcut
254
+ *
255
+ * @param polygons
256
+ * @param areas
257
+ * @param lines
258
+ * @param param3
259
+ */
260
+ function triangulatePolygon(polygons, areas, lines, { startPosition, endPosition, coordLength }) {
261
+ const start = startPosition * coordLength;
262
+ const end = endPosition * coordLength;
263
+ // Extract positions and holes for just this polygon
264
+ const polygonPositions = polygons.positions.subarray(start, end);
265
+ // Holes are referenced relative to outer polygon
266
+ const offset = lines[0];
267
+ const holes = lines.slice(1).map((n) => (n - offset) / coordLength);
268
+ // Compute triangulation
269
+ const indices = (0, polygon_1.earcut)(polygonPositions, holes, coordLength, areas);
270
+ // Indices returned by triangulation are relative to start
271
+ // of polygon, so we need to offset
272
+ for (let t = 0, tl = indices.length; t < tl; ++t) {
273
+ polygons.triangles.push(startPosition + indices[t]);
274
+ }
275
+ }
276
+ /**
277
+ * Wrap each array in an accessor object with value and size keys
278
+ *
279
+ * @param points
280
+ * @param lines
281
+ * @param polygons
282
+ * @param coordLength
283
+ * @returns object
284
+ */
285
+ function makeAccessorObjects(points, lines, polygons, coordLength) {
286
+ const returnObj = {
287
+ points: {
288
+ ...points,
289
+ positions: { value: points.positions, size: coordLength },
290
+ globalFeatureIds: { value: points.globalFeatureIds, size: 1 },
291
+ featureIds: { value: points.featureIds, size: 1 }
292
+ },
293
+ lines: {
294
+ ...lines,
295
+ pathIndices: { value: lines.pathIndices, size: 1 },
296
+ positions: { value: lines.positions, size: coordLength },
297
+ globalFeatureIds: { value: lines.globalFeatureIds, size: 1 },
298
+ featureIds: { value: lines.featureIds, size: 1 }
299
+ },
300
+ polygons: {
301
+ ...polygons,
302
+ polygonIndices: { value: polygons.polygonIndices, size: 1 },
303
+ primitivePolygonIndices: { value: polygons.primitivePolygonIndices, size: 1 },
304
+ positions: { value: polygons.positions, size: coordLength },
305
+ triangles: { value: new Uint32Array(polygons.triangles), size: 1 },
306
+ globalFeatureIds: { value: polygons.globalFeatureIds, size: 1 },
307
+ featureIds: { value: polygons.featureIds, size: 1 }
308
+ }
309
+ };
310
+ for (const geomType in returnObj) {
311
+ for (const numericProp in returnObj[geomType].numericProps) {
312
+ returnObj[geomType].numericProps[numericProp] = {
313
+ value: returnObj[geomType].numericProps[numericProp],
314
+ size: 1
315
+ };
316
+ }
317
+ }
318
+ return returnObj;
319
+ }
320
+ /**
321
+ * Add numeric properties to object
322
+ *
323
+ * @param object
324
+ * @param properties
325
+ * @param index
326
+ * @param length
327
+ */
328
+ function fillNumericProperties(object, properties, index, length) {
329
+ for (const numericPropName in object.numericProps) {
330
+ if (numericPropName in properties) {
331
+ object.numericProps[numericPropName].fill(properties[numericPropName], index, index + length);
332
+ }
333
+ }
334
+ }
335
+ /**
336
+ * Keep string properties in object
337
+ *
338
+ * @param properties
339
+ * @param numericKeys
340
+ * @returns object
341
+ */
342
+ function keepStringProperties(properties, numericKeys) {
343
+ const props = {};
344
+ for (const key in properties) {
345
+ if (!numericKeys.includes(key)) {
346
+ props[key] = properties[key];
347
+ }
348
+ }
349
+ return props;
350
+ }
351
+ function isNumeric(x) {
352
+ return Number.isFinite(x);
353
+ }
@@ -33,3 +33,4 @@ export default class VectorTileFeature {
33
33
  extent: any;
34
34
  }) => void)): MvtBinaryCoordinates;
35
35
  }
36
+ //# sourceMappingURL=vector-tile-feature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-tile-feature.d.ts","sourceRoot":"","sources":["../../../src/lib/binary-vector-tile/vector-tile-feature.ts"],"names":[],"mappings":"AAEA,OAAO,QAAQ,MAAM,KAAK,CAAC;AAC3B,OAAO,EAAC,oBAAoB,EAAE,iBAAiB,EAAE,kBAAkB,EAAC,MAAM,UAAU,CAAC;AACrF,OAAO,EAAC,aAAa,EAAuB,MAAM,qCAAqC,CAAC;AAWxF,eAAO,MAAM,YAAY;;CAExB,CAAC;AAEF,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,UAAU,EAAE;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;KAAC,CAAC;IAC5D,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,IAAI,EAAE,QAAQ,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAC9C,cAAc,EAAE,kBAAkB,CAAC;IACnC,MAAM,KAAK,KAAK,aAEf;gBAIC,GAAG,EAAE,QAAQ,EACb,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,GAAG,EACX,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,EAC5C,aAAa,EAAE,kBAAkB;IAmBnC,YAAY,IAAI,iBAAiB;IAqDjC;;;;OAIG;IACH,oBAAoB,CAAC,SAAS,KAAA;IA4E9B,mBAAmB,CACjB,OAAO,EAAE;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE;QAAC,MAAM,EAAE,GAAG,CAAA;KAAC,KAAK,IAAI,CAAC,GAC9F,oBAAoB;CAMxB"}
@@ -0,0 +1,159 @@
1
+ "use strict";
2
+ // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ exports.TEST_EXPORTS = void 0;
5
+ const binary_util_functions_1 = require("../../helpers/binary-util-functions");
6
+ // Reduce GC by reusing variables
7
+ let endPos;
8
+ let cmd;
9
+ let cmdLen;
10
+ let length;
11
+ let x;
12
+ let y;
13
+ let i;
14
+ exports.TEST_EXPORTS = {
15
+ classifyRings: binary_util_functions_1.classifyRings
16
+ };
17
+ class VectorTileFeature {
18
+ // eslint-disable-next-line max-params
19
+ constructor(pbf, end, extent, keys, values, firstPassData) {
20
+ // Public
21
+ this.properties = {};
22
+ this.extent = extent;
23
+ this.type = 0;
24
+ this.id = null;
25
+ // Private
26
+ this._pbf = pbf;
27
+ this._geometry = -1;
28
+ this._keys = keys;
29
+ this._values = values;
30
+ this._firstPassData = firstPassData;
31
+ pbf.readFields(binary_util_functions_1.readFeature, this, end);
32
+ }
33
+ static get types() {
34
+ return ['Unknown', 'Point', 'LineString', 'Polygon'];
35
+ }
36
+ // eslint-disable-next-line complexity, max-statements
37
+ loadGeometry() {
38
+ const pbf = this._pbf;
39
+ pbf.pos = this._geometry;
40
+ endPos = pbf.readVarint() + pbf.pos;
41
+ cmd = 1;
42
+ length = 0;
43
+ x = 0;
44
+ y = 0;
45
+ i = 0;
46
+ // Note: I attempted to replace the `data` array with a
47
+ // Float32Array, but performance was worse, both using
48
+ // `set()` and direct index access. Also, we cannot
49
+ // know how large the buffer should be, so it would
50
+ // increase memory usage
51
+ const lines = []; // Indices where lines start
52
+ const data = []; // Flat array of coordinate data
53
+ while (pbf.pos < endPos) {
54
+ if (length <= 0) {
55
+ cmdLen = pbf.readVarint();
56
+ cmd = cmdLen & 0x7;
57
+ length = cmdLen >> 3;
58
+ }
59
+ length--;
60
+ if (cmd === 1 || cmd === 2) {
61
+ x += pbf.readSVarint();
62
+ y += pbf.readSVarint();
63
+ if (cmd === 1) {
64
+ // New line
65
+ lines.push(i);
66
+ }
67
+ data.push(x, y);
68
+ i += 2;
69
+ }
70
+ else if (cmd === 7) {
71
+ // Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
72
+ if (i > 0) {
73
+ const start = lines[lines.length - 1]; // start index of polygon
74
+ data.push(data[start], data[start + 1]); // closePolygon
75
+ i += 2;
76
+ }
77
+ }
78
+ else {
79
+ throw new Error(`unknown command ${cmd}`);
80
+ }
81
+ }
82
+ return { data, lines };
83
+ }
84
+ /**
85
+ *
86
+ * @param transform
87
+ * @returns result
88
+ */
89
+ _toBinaryCoordinates(transform) {
90
+ // Expands the protobuf data to an intermediate `lines`
91
+ // data format, which maps closely to the binary data buffers.
92
+ // It is similar to GeoJSON, but rather than storing the coordinates
93
+ // in multidimensional arrays, we have a 1D `data` with all the
94
+ // coordinates, and then index into this using the `lines`
95
+ // parameter, e.g.
96
+ //
97
+ // geometry: {
98
+ // type: 'Point', data: [1,2], lines: [0]
99
+ // }
100
+ // geometry: {
101
+ // type: 'LineString', data: [1,2,3,4,...], lines: [0]
102
+ // }
103
+ // geometry: {
104
+ // type: 'Polygon', data: [1,2,3,4,...], lines: [[0, 2]]
105
+ // }
106
+ // Thus the lines member lets us look up the relevant range
107
+ // from the data array.
108
+ // The Multi* versions of the above types share the same data
109
+ // structure, just with multiple elements in the lines array
110
+ let geom = this.loadGeometry();
111
+ // Apply the supplied transformation to data
112
+ transform(geom.data, this);
113
+ const coordLength = 2;
114
+ // eslint-disable-next-line default-case
115
+ switch (this.type) {
116
+ case 1: // Point
117
+ this._firstPassData.pointFeaturesCount++;
118
+ this._firstPassData.pointPositionsCount += geom.lines.length;
119
+ break;
120
+ case 2: // LineString
121
+ this._firstPassData.lineFeaturesCount++;
122
+ this._firstPassData.linePathsCount += geom.lines.length;
123
+ this._firstPassData.linePositionsCount += geom.data.length / coordLength;
124
+ break;
125
+ case 3: // Polygon
126
+ const classified = (0, binary_util_functions_1.classifyRings)(geom);
127
+ // Unlike Point & LineString geom.lines is a 2D array, thanks
128
+ // to the classifyRings method
129
+ this._firstPassData.polygonFeaturesCount++;
130
+ this._firstPassData.polygonObjectsCount += classified.lines.length;
131
+ for (const lines of classified.lines) {
132
+ this._firstPassData.polygonRingsCount += lines.length;
133
+ }
134
+ this._firstPassData.polygonPositionsCount += classified.data.length / coordLength;
135
+ geom = classified;
136
+ break;
137
+ }
138
+ geom.type = VectorTileFeature.types[this.type];
139
+ if (geom.lines.length > 1) {
140
+ geom.type = `Multi${geom.type}`;
141
+ }
142
+ const result = {
143
+ type: 'Feature',
144
+ geometry: geom,
145
+ properties: this.properties
146
+ };
147
+ if (this.id !== null) {
148
+ result.id = this.id;
149
+ }
150
+ return result;
151
+ }
152
+ toBinaryCoordinates(options) {
153
+ if (typeof options === 'function') {
154
+ return this._toBinaryCoordinates(options);
155
+ }
156
+ return this._toBinaryCoordinates(binary_util_functions_1.project);
157
+ }
158
+ }
159
+ exports.default = VectorTileFeature;
@@ -20,3 +20,4 @@ export default class VectorTileLayer {
20
20
  */
21
21
  feature(i: number, firstPassData: MvtFirstPassedData): VectorTileFeature;
22
22
  }
23
+ //# sourceMappingURL=vector-tile-layer.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-tile-layer.d.ts","sourceRoot":"","sources":["../../../src/lib/binary-vector-tile/vector-tile-layer.ts"],"names":[],"mappings":"AAGA,OAAO,iBAAiB,MAAM,uBAAuB,CAAC;AACtD,OAAO,QAAQ,MAAM,KAAK,CAAC;AAC3B,OAAO,EAAC,kBAAkB,EAAC,MAAM,UAAU,CAAC;AAE5C,MAAM,CAAC,OAAO,OAAO,eAAe;IAClC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,IAAI,EAAE,QAAQ,CAAC;IACf,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAC9C,SAAS,EAAE,MAAM,EAAE,CAAC;gBACR,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,MAAM;IAkBtC;;;;;;OAMG;IACH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,aAAa,EAAE,kBAAkB,GAAG,iBAAiB;CAiBzE"}
@@ -0,0 +1,91 @@
1
+ "use strict";
2
+ /* eslint-disable indent */
3
+ // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
4
+ var __importDefault = (this && this.__importDefault) || function (mod) {
5
+ return (mod && mod.__esModule) ? mod : { "default": mod };
6
+ };
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ const vector_tile_feature_1 = __importDefault(require("./vector-tile-feature"));
9
+ class VectorTileLayer {
10
+ constructor(pbf, end) {
11
+ // Public
12
+ this.version = 1;
13
+ this.name = '';
14
+ this.extent = 4096;
15
+ this.length = 0;
16
+ // Private
17
+ this._pbf = pbf;
18
+ this._keys = [];
19
+ this._values = [];
20
+ this._features = [];
21
+ pbf.readFields(readLayer, this, end);
22
+ this.length = this._features.length;
23
+ }
24
+ /**
25
+ * return feature `i` from this layer as a `VectorTileFeature`
26
+ *
27
+ * @param index
28
+ * @param firstPassData
29
+ * @returns {VectorTileFeature}
30
+ */
31
+ feature(i, firstPassData) {
32
+ if (i < 0 || i >= this._features.length) {
33
+ throw new Error('feature index out of bounds');
34
+ }
35
+ this._pbf.pos = this._features[i];
36
+ const end = this._pbf.readVarint() + this._pbf.pos;
37
+ return new vector_tile_feature_1.default(this._pbf, end, this.extent, this._keys, this._values, firstPassData);
38
+ }
39
+ }
40
+ exports.default = VectorTileLayer;
41
+ /**
42
+ *
43
+ * @param tag
44
+ * @param layer
45
+ * @param pbf
46
+ */
47
+ function readLayer(tag, layer, pbf) {
48
+ if (layer && pbf) {
49
+ if (tag === 15)
50
+ layer.version = pbf.readVarint();
51
+ else if (tag === 1)
52
+ layer.name = pbf.readString();
53
+ else if (tag === 5)
54
+ layer.extent = pbf.readVarint();
55
+ else if (tag === 2)
56
+ layer._features.push(pbf.pos);
57
+ else if (tag === 3)
58
+ layer._keys.push(pbf.readString());
59
+ else if (tag === 4)
60
+ layer._values.push(readValueMessage(pbf));
61
+ }
62
+ }
63
+ /**
64
+ *
65
+ * @param pbf
66
+ * @returns value
67
+ */
68
+ function readValueMessage(pbf) {
69
+ let value = null;
70
+ const end = pbf.readVarint() + pbf.pos;
71
+ while (pbf.pos < end) {
72
+ const tag = pbf.readVarint() >> 3;
73
+ value =
74
+ tag === 1
75
+ ? pbf.readString()
76
+ : tag === 2
77
+ ? pbf.readFloat()
78
+ : tag === 3
79
+ ? pbf.readDouble()
80
+ : tag === 4
81
+ ? pbf.readVarint64()
82
+ : tag === 5
83
+ ? pbf.readVarint()
84
+ : tag === 6
85
+ ? pbf.readSVarint()
86
+ : tag === 7
87
+ ? pbf.readBoolean()
88
+ : null;
89
+ }
90
+ return value;
91
+ }
@@ -6,3 +6,4 @@ export default class VectorTile {
6
6
  };
7
7
  constructor(pbf: Protobuf, end?: number);
8
8
  }
9
+ //# sourceMappingURL=vector-tile.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-tile.d.ts","sourceRoot":"","sources":["../../../src/lib/binary-vector-tile/vector-tile.ts"],"names":[],"mappings":"AAEA,OAAO,eAAe,MAAM,qBAAqB,CAAC;AAClD,OAAO,QAAQ,MAAM,KAAK,CAAC;AAE3B,MAAM,CAAC,OAAO,OAAO,UAAU;IAC7B,MAAM,EAAE;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,eAAe,CAAA;KAAC,CAAC;gBAC3B,GAAG,EAAE,QAAQ,EAAE,GAAG,CAAC,EAAE,MAAM;CAGxC"}
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ const vector_tile_layer_1 = __importDefault(require("./vector-tile-layer"));
8
+ class VectorTile {
9
+ constructor(pbf, end) {
10
+ this.layers = pbf.readFields(readTile, {}, end);
11
+ }
12
+ }
13
+ exports.default = VectorTile;
14
+ /**
15
+ *
16
+ * @param tag
17
+ * @param layers
18
+ * @param pbf
19
+ */
20
+ function readTile(tag, layers, pbf) {
21
+ if (tag === 3) {
22
+ if (pbf) {
23
+ const layer = new vector_tile_layer_1.default(pbf, pbf.readVarint() + pbf.pos);
24
+ if (layer.length && layers) {
25
+ layers[layer.name] = layer;
26
+ }
27
+ }
28
+ }
29
+ }
@@ -24,3 +24,4 @@ export default class VectorTileFeature {
24
24
  extent: any;
25
25
  }) => void)): MvtMapboxCoordinates;
26
26
  }
27
+ //# sourceMappingURL=vector-tile-feature.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"vector-tile-feature.d.ts","sourceRoot":"","sources":["../../../src/lib/mapbox-vector-tile/vector-tile-feature.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,KAAK,CAAC;AAC3B,OAAO,EAAC,oBAAoB,EAAE,iBAAiB,EAAC,MAAM,UAAU,CAAC;AAGjE,MAAM,CAAC,OAAO,OAAO,iBAAiB;IACpC,UAAU,EAAE;QAAC,CAAC,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAA;KAAC,CAAC;IAC5D,MAAM,EAAE,GAAG,CAAC;IACZ,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,GAAG,IAAI,CAAC;IAClB,IAAI,EAAE,QAAQ,CAAC;IACf,SAAS,EAAE,MAAM,CAAC;IAClB,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,OAAO,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE,CAAC;IAC9C,MAAM,KAAK,KAAK,aAEf;gBAGC,GAAG,EAAE,QAAQ,EACb,GAAG,EAAE,MAAM,EACX,MAAM,EAAE,GAAG,EACX,IAAI,EAAE,MAAM,EAAE,EACd,MAAM,EAAE,CAAC,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,IAAI,CAAC,EAAE;IAkB9C,YAAY,IAAI,iBAAiB;IA+CjC,IAAI;IAsCJ,UAAU,CAAC,SAAS,KAAA;IAuDpB,SAAS,CACP,OAAO,EAAE;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAC,GAAG,CAAC,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,OAAO,EAAE;QAAC,MAAM,EAAE,GAAG,CAAA;KAAC,KAAK,IAAI,CAAC,GAC9F,oBAAoB;CAmBxB"}