@loaders.gl/mvt 3.1.0 → 3.1.4
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.
- package/dist/bundle.js +5 -2281
- package/dist/dist.min.js +2289 -0
- package/dist/es5/helpers/binary-util-functions.js +8 -5
- package/dist/es5/helpers/binary-util-functions.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +33 -31
- package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +2 -2
- package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/es5/lib/parse-mvt.js +6 -5
- package/dist/es5/lib/parse-mvt.js.map +1 -1
- package/dist/es5/mvt-loader.js +1 -1
- package/dist/esm/helpers/binary-util-functions.js +8 -5
- package/dist/esm/helpers/binary-util-functions.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +31 -30
- package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +2 -2
- package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -1
- package/dist/esm/lib/parse-mvt.js +6 -5
- package/dist/esm/lib/parse-mvt.js.map +1 -1
- package/dist/esm/mvt-loader.js +1 -1
- package/dist/helpers/binary-util-functions.d.ts +2 -6
- package/dist/helpers/binary-util-functions.d.ts.map +1 -1
- package/dist/helpers/binary-util-functions.js +7 -5
- package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +12 -7
- package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-feature.js +32 -39
- package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +3 -3
- package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-layer.js +3 -3
- package/dist/lib/parse-mvt.d.ts +8 -76
- package/dist/lib/parse-mvt.d.ts.map +1 -1
- package/dist/lib/parse-mvt.js +6 -5
- package/dist/lib/types.d.ts +0 -68
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/mvt-worker.js +90 -89
- package/package.json +7 -6
- package/src/helpers/binary-util-functions.ts +9 -7
- package/src/lib/binary-vector-tile/vector-tile-feature.ts +36 -44
- package/src/lib/binary-vector-tile/vector-tile-layer.ts +4 -4
- package/src/lib/parse-mvt.ts +10 -8
- package/src/lib/types.ts +0 -75
- package/dist/es5/lib/binary-vector-tile/features-to-binary.js +0 -389
- package/dist/es5/lib/binary-vector-tile/features-to-binary.js.map +0 -1
- package/dist/esm/lib/binary-vector-tile/features-to-binary.js +0 -331
- package/dist/esm/lib/binary-vector-tile/features-to-binary.js.map +0 -1
- package/dist/lib/binary-vector-tile/features-to-binary.d.ts +0 -177
- package/dist/lib/binary-vector-tile/features-to-binary.d.ts.map +0 -1
- package/dist/lib/binary-vector-tile/features-to-binary.js +0 -358
- package/src/lib/binary-vector-tile/features-to-binary.ts +0 -527
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
|
|
2
2
|
|
|
3
3
|
import Protobuf from 'pbf';
|
|
4
|
-
import {
|
|
4
|
+
import {FlatFeature, FlatIndexedGeometry, GeojsonGeometryInfo} from '@loaders.gl/schema';
|
|
5
5
|
import {classifyRings, project, readFeature} from '../../helpers/binary-util-functions';
|
|
6
6
|
|
|
7
7
|
// Reduce GC by reusing variables
|
|
@@ -26,10 +26,7 @@ export default class VectorTileFeature {
|
|
|
26
26
|
_geometry: number;
|
|
27
27
|
_keys: string[];
|
|
28
28
|
_values: (string | number | boolean | null)[];
|
|
29
|
-
|
|
30
|
-
static get types() {
|
|
31
|
-
return ['Unknown', 'Point', 'LineString', 'Polygon'];
|
|
32
|
-
}
|
|
29
|
+
_geometryInfo: GeojsonGeometryInfo;
|
|
33
30
|
|
|
34
31
|
// eslint-disable-next-line max-params
|
|
35
32
|
constructor(
|
|
@@ -38,7 +35,7 @@ export default class VectorTileFeature {
|
|
|
38
35
|
extent: any,
|
|
39
36
|
keys: string[],
|
|
40
37
|
values: (string | number | boolean | null)[],
|
|
41
|
-
|
|
38
|
+
geometryInfo: GeojsonGeometryInfo
|
|
42
39
|
) {
|
|
43
40
|
// Public
|
|
44
41
|
this.properties = {};
|
|
@@ -51,13 +48,13 @@ export default class VectorTileFeature {
|
|
|
51
48
|
this._geometry = -1;
|
|
52
49
|
this._keys = keys;
|
|
53
50
|
this._values = values;
|
|
54
|
-
this.
|
|
51
|
+
this._geometryInfo = geometryInfo;
|
|
55
52
|
|
|
56
53
|
pbf.readFields(readFeature, this, end);
|
|
57
54
|
}
|
|
58
55
|
|
|
59
56
|
// eslint-disable-next-line complexity, max-statements
|
|
60
|
-
loadGeometry():
|
|
57
|
+
loadGeometry(): FlatIndexedGeometry {
|
|
61
58
|
const pbf = this._pbf;
|
|
62
59
|
pbf.pos = this._geometry;
|
|
63
60
|
|
|
@@ -73,7 +70,7 @@ export default class VectorTileFeature {
|
|
|
73
70
|
// `set()` and direct index access. Also, we cannot
|
|
74
71
|
// know how large the buffer should be, so it would
|
|
75
72
|
// increase memory usage
|
|
76
|
-
const
|
|
73
|
+
const indices: number[] = []; // Indices where geometries start
|
|
77
74
|
const data: number[] = []; // Flat array of coordinate data
|
|
78
75
|
|
|
79
76
|
while (pbf.pos < endPos) {
|
|
@@ -91,14 +88,14 @@ export default class VectorTileFeature {
|
|
|
91
88
|
|
|
92
89
|
if (cmd === 1) {
|
|
93
90
|
// New line
|
|
94
|
-
|
|
91
|
+
indices.push(i);
|
|
95
92
|
}
|
|
96
93
|
data.push(x, y);
|
|
97
94
|
i += 2;
|
|
98
95
|
} else if (cmd === 7) {
|
|
99
96
|
// Workaround for https://github.com/mapbox/mapnik-vector-tile/issues/90
|
|
100
97
|
if (i > 0) {
|
|
101
|
-
const start =
|
|
98
|
+
const start = indices[indices.length - 1]; // start index of polygon
|
|
102
99
|
data.push(data[start], data[start + 1]); // closePolygon
|
|
103
100
|
i += 2;
|
|
104
101
|
}
|
|
@@ -107,7 +104,7 @@ export default class VectorTileFeature {
|
|
|
107
104
|
}
|
|
108
105
|
}
|
|
109
106
|
|
|
110
|
-
return {data,
|
|
107
|
+
return {data, indices};
|
|
111
108
|
}
|
|
112
109
|
|
|
113
110
|
/**
|
|
@@ -116,27 +113,28 @@ export default class VectorTileFeature {
|
|
|
116
113
|
* @returns result
|
|
117
114
|
*/
|
|
118
115
|
_toBinaryCoordinates(transform) {
|
|
119
|
-
// Expands the protobuf data to an intermediate
|
|
116
|
+
// Expands the protobuf data to an intermediate Flat GeoJSON
|
|
120
117
|
// data format, which maps closely to the binary data buffers.
|
|
121
118
|
// It is similar to GeoJSON, but rather than storing the coordinates
|
|
122
119
|
// in multidimensional arrays, we have a 1D `data` with all the
|
|
123
|
-
// coordinates, and then index into this using the `
|
|
120
|
+
// coordinates, and then index into this using the `indices`
|
|
124
121
|
// parameter, e.g.
|
|
125
122
|
//
|
|
126
123
|
// geometry: {
|
|
127
|
-
// type: 'Point', data: [1,2],
|
|
124
|
+
// type: 'Point', data: [1,2], indices: [0]
|
|
128
125
|
// }
|
|
129
126
|
// geometry: {
|
|
130
|
-
// type: 'LineString', data: [1,2,3,4,...],
|
|
127
|
+
// type: 'LineString', data: [1,2,3,4,...], indices: [0]
|
|
131
128
|
// }
|
|
132
129
|
// geometry: {
|
|
133
|
-
// type: 'Polygon', data: [1,2,3,4,...],
|
|
130
|
+
// type: 'Polygon', data: [1,2,3,4,...], indices: [[0, 2]]
|
|
134
131
|
// }
|
|
135
|
-
// Thus the
|
|
132
|
+
// Thus the indices member lets us look up the relevant range
|
|
136
133
|
// from the data array.
|
|
137
134
|
// The Multi* versions of the above types share the same data
|
|
138
|
-
// structure, just with multiple elements in the
|
|
139
|
-
|
|
135
|
+
// structure, just with multiple elements in the indices array
|
|
136
|
+
const geom = this.loadGeometry();
|
|
137
|
+
let geometry;
|
|
140
138
|
|
|
141
139
|
// Apply the supplied transformation to data
|
|
142
140
|
transform(geom.data, this);
|
|
@@ -146,43 +144,37 @@ export default class VectorTileFeature {
|
|
|
146
144
|
// eslint-disable-next-line default-case
|
|
147
145
|
switch (this.type) {
|
|
148
146
|
case 1: // Point
|
|
149
|
-
this.
|
|
150
|
-
this.
|
|
147
|
+
this._geometryInfo.pointFeaturesCount++;
|
|
148
|
+
this._geometryInfo.pointPositionsCount += geom.indices.length;
|
|
149
|
+
geometry = {type: 'Point', ...geom};
|
|
151
150
|
break;
|
|
152
151
|
|
|
153
152
|
case 2: // LineString
|
|
154
|
-
this.
|
|
155
|
-
this.
|
|
156
|
-
this.
|
|
153
|
+
this._geometryInfo.lineFeaturesCount++;
|
|
154
|
+
this._geometryInfo.linePathsCount += geom.indices.length;
|
|
155
|
+
this._geometryInfo.linePositionsCount += geom.data.length / coordLength;
|
|
156
|
+
geometry = {type: 'LineString', ...geom};
|
|
157
157
|
break;
|
|
158
158
|
|
|
159
159
|
case 3: // Polygon
|
|
160
|
-
|
|
160
|
+
geometry = classifyRings(geom);
|
|
161
161
|
|
|
162
|
-
// Unlike Point & LineString geom.
|
|
162
|
+
// Unlike Point & LineString geom.indices is a 2D array, thanks
|
|
163
163
|
// to the classifyRings method
|
|
164
|
-
this.
|
|
165
|
-
this.
|
|
164
|
+
this._geometryInfo.polygonFeaturesCount++;
|
|
165
|
+
this._geometryInfo.polygonObjectsCount += geometry.indices.length;
|
|
166
166
|
|
|
167
|
-
for (const
|
|
168
|
-
this.
|
|
167
|
+
for (const indices of geometry.indices) {
|
|
168
|
+
this._geometryInfo.polygonRingsCount += indices.length;
|
|
169
169
|
}
|
|
170
|
-
this.
|
|
170
|
+
this._geometryInfo.polygonPositionsCount += geometry.data.length / coordLength;
|
|
171
171
|
|
|
172
|
-
geom = classified;
|
|
173
172
|
break;
|
|
173
|
+
default:
|
|
174
|
+
throw new Error(`Invalid geometry type: ${this.type}`);
|
|
174
175
|
}
|
|
175
176
|
|
|
176
|
-
|
|
177
|
-
if (geom.lines.length > 1) {
|
|
178
|
-
geom.type = `Multi${geom.type}`;
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
const result: MvtBinaryCoordinates = {
|
|
182
|
-
type: 'Feature',
|
|
183
|
-
geometry: geom,
|
|
184
|
-
properties: this.properties
|
|
185
|
-
};
|
|
177
|
+
const result: FlatFeature = {type: 'Feature', geometry, properties: this.properties};
|
|
186
178
|
|
|
187
179
|
if (this.id !== null) {
|
|
188
180
|
result.id = this.id;
|
|
@@ -193,7 +185,7 @@ export default class VectorTileFeature {
|
|
|
193
185
|
|
|
194
186
|
toBinaryCoordinates(
|
|
195
187
|
options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
|
|
196
|
-
):
|
|
188
|
+
): FlatFeature {
|
|
197
189
|
if (typeof options === 'function') {
|
|
198
190
|
return this._toBinaryCoordinates(options);
|
|
199
191
|
}
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
import VectorTileFeature from './vector-tile-feature';
|
|
5
5
|
import Protobuf from 'pbf';
|
|
6
|
-
import {
|
|
6
|
+
import {GeojsonGeometryInfo} from '@loaders.gl/schema';
|
|
7
7
|
|
|
8
8
|
export default class VectorTileLayer {
|
|
9
9
|
version: number;
|
|
@@ -36,10 +36,10 @@ export default class VectorTileLayer {
|
|
|
36
36
|
* return feature `i` from this layer as a `VectorTileFeature`
|
|
37
37
|
*
|
|
38
38
|
* @param index
|
|
39
|
-
* @param
|
|
39
|
+
* @param geometryInfo
|
|
40
40
|
* @returns {VectorTileFeature}
|
|
41
41
|
*/
|
|
42
|
-
feature(i: number,
|
|
42
|
+
feature(i: number, geometryInfo: GeojsonGeometryInfo): VectorTileFeature {
|
|
43
43
|
if (i < 0 || i >= this._features.length) {
|
|
44
44
|
throw new Error('feature index out of bounds');
|
|
45
45
|
}
|
|
@@ -53,7 +53,7 @@ export default class VectorTileLayer {
|
|
|
53
53
|
this.extent,
|
|
54
54
|
this._keys,
|
|
55
55
|
this._values,
|
|
56
|
-
|
|
56
|
+
geometryInfo
|
|
57
57
|
);
|
|
58
58
|
}
|
|
59
59
|
}
|
package/src/lib/parse-mvt.ts
CHANGED
|
@@ -2,9 +2,10 @@
|
|
|
2
2
|
import VectorTile from './mapbox-vector-tile/vector-tile';
|
|
3
3
|
import BinaryVectorTile from './binary-vector-tile/vector-tile';
|
|
4
4
|
|
|
5
|
-
import {
|
|
5
|
+
import {flatGeojsonToBinary} from '@loaders.gl/gis';
|
|
6
6
|
import Protobuf from 'pbf';
|
|
7
|
-
import {
|
|
7
|
+
import type {FlatFeature} from '@loaders.gl/schema';
|
|
8
|
+
import type {MvtMapboxCoordinates, MvtOptions} from '../lib/types';
|
|
8
9
|
import VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';
|
|
9
10
|
import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
|
|
10
11
|
import {LoaderOptions} from '@loaders.gl/loader-utils';
|
|
@@ -18,11 +19,12 @@ import {LoaderOptions} from '@loaders.gl/loader-utils';
|
|
|
18
19
|
*/
|
|
19
20
|
export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {
|
|
20
21
|
options = normalizeOptions(options);
|
|
21
|
-
const features: (
|
|
22
|
+
const features: (FlatFeature | MvtMapboxCoordinates)[] = [];
|
|
22
23
|
|
|
23
24
|
if (options) {
|
|
24
25
|
const binary = options.gis.format === 'binary';
|
|
25
|
-
const
|
|
26
|
+
const geometryInfo = {
|
|
27
|
+
coordLength: 2,
|
|
26
28
|
pointPositionsCount: 0,
|
|
27
29
|
pointFeaturesCount: 0,
|
|
28
30
|
linePositionsCount: 0,
|
|
@@ -53,7 +55,7 @@ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptio
|
|
|
53
55
|
}
|
|
54
56
|
|
|
55
57
|
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
56
|
-
const vectorTileFeature = vectorTileLayer.feature(i,
|
|
58
|
+
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
57
59
|
|
|
58
60
|
const decodedFeature = binary
|
|
59
61
|
? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)
|
|
@@ -64,7 +66,7 @@ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptio
|
|
|
64
66
|
}
|
|
65
67
|
|
|
66
68
|
if (binary) {
|
|
67
|
-
const data =
|
|
69
|
+
const data = flatGeojsonToBinary(features as FlatFeature[], geometryInfo);
|
|
68
70
|
// Add the original byteLength (as a reasonable approximation of the size of the binary data)
|
|
69
71
|
// TODO decide where to store extra fields like byteLength (header etc) and document
|
|
70
72
|
// @ts-ignore
|
|
@@ -134,13 +136,13 @@ function getDecodedFeature(
|
|
|
134
136
|
function getDecodedFeatureBinary(
|
|
135
137
|
feature: VectorTileFeatureBinary,
|
|
136
138
|
options: MvtOptions
|
|
137
|
-
):
|
|
139
|
+
): FlatFeature {
|
|
138
140
|
const decodedFeature = feature.toBinaryCoordinates(
|
|
139
141
|
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
|
|
140
142
|
);
|
|
141
143
|
|
|
142
144
|
// Add layer name to GeoJSON properties
|
|
143
|
-
if (options.layerProperty) {
|
|
145
|
+
if (options.layerProperty && decodedFeature.properties) {
|
|
144
146
|
decodedFeature.properties[options.layerProperty] = options.layerName;
|
|
145
147
|
}
|
|
146
148
|
|
package/src/lib/types.ts
CHANGED
|
@@ -5,14 +5,6 @@ export type MvtOptions = {
|
|
|
5
5
|
layerName: string;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
-
export type MvtBinaryGeometry = {
|
|
9
|
-
data: number[];
|
|
10
|
-
lines: any[];
|
|
11
|
-
areas?: number[];
|
|
12
|
-
type?: string;
|
|
13
|
-
id?: number;
|
|
14
|
-
};
|
|
15
|
-
|
|
16
8
|
export type MvtMapboxGeometry = {
|
|
17
9
|
type?: string;
|
|
18
10
|
id?: number;
|
|
@@ -20,13 +12,6 @@ export type MvtMapboxGeometry = {
|
|
|
20
12
|
coordinates?: any[];
|
|
21
13
|
};
|
|
22
14
|
|
|
23
|
-
export type MvtBinaryCoordinates = {
|
|
24
|
-
type: string;
|
|
25
|
-
geometry: MvtBinaryGeometry;
|
|
26
|
-
properties: {[x: string]: string | number | boolean | null};
|
|
27
|
-
id?: number;
|
|
28
|
-
};
|
|
29
|
-
|
|
30
15
|
export type MvtMapboxCoordinates = {
|
|
31
16
|
type: string;
|
|
32
17
|
geometry: {
|
|
@@ -36,63 +21,3 @@ export type MvtMapboxCoordinates = {
|
|
|
36
21
|
properties: {[x: string]: string | number | boolean | null};
|
|
37
22
|
id?: number;
|
|
38
23
|
};
|
|
39
|
-
|
|
40
|
-
export type MvtPropArrayConstructor =
|
|
41
|
-
| Float32ArrayConstructor
|
|
42
|
-
| Float64ArrayConstructor
|
|
43
|
-
| ArrayConstructor;
|
|
44
|
-
|
|
45
|
-
export type MvtBinaryOptions = {
|
|
46
|
-
numericPropKeys: string[];
|
|
47
|
-
propArrayTypes: {[key: string]: MvtPropArrayConstructor};
|
|
48
|
-
PositionDataType: Float32ArrayConstructor;
|
|
49
|
-
};
|
|
50
|
-
|
|
51
|
-
export type MvtFirstPassedData = {
|
|
52
|
-
pointPositionsCount: number;
|
|
53
|
-
pointFeaturesCount: number;
|
|
54
|
-
linePositionsCount: number;
|
|
55
|
-
linePathsCount: number;
|
|
56
|
-
lineFeaturesCount: number;
|
|
57
|
-
polygonPositionsCount: number;
|
|
58
|
-
polygonObjectsCount: number;
|
|
59
|
-
polygonRingsCount: number;
|
|
60
|
-
polygonFeaturesCount: number;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type MvtPoints = {
|
|
64
|
-
positions: Float32Array;
|
|
65
|
-
globalFeatureIds: Uint16Array | Uint32Array;
|
|
66
|
-
featureIds: Uint16Array | Uint32Array;
|
|
67
|
-
numericProps: object;
|
|
68
|
-
properties: {}[];
|
|
69
|
-
fields: {
|
|
70
|
-
id?: number;
|
|
71
|
-
}[];
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export type MvtLines = {
|
|
75
|
-
pathIndices: Uint16Array | Uint32Array;
|
|
76
|
-
positions: Float32Array;
|
|
77
|
-
globalFeatureIds: Uint16Array | Uint32Array;
|
|
78
|
-
featureIds: Uint16Array | Uint32Array;
|
|
79
|
-
numericProps: object;
|
|
80
|
-
properties: {}[];
|
|
81
|
-
fields: {
|
|
82
|
-
id?: number;
|
|
83
|
-
}[];
|
|
84
|
-
};
|
|
85
|
-
|
|
86
|
-
export type MvtPolygons = {
|
|
87
|
-
polygonIndices: Uint16Array | Uint32Array;
|
|
88
|
-
primitivePolygonIndices: Uint16Array | Uint32Array;
|
|
89
|
-
positions: Float32Array;
|
|
90
|
-
triangles: number[];
|
|
91
|
-
globalFeatureIds: Uint16Array | Uint32Array;
|
|
92
|
-
featureIds: Uint16Array | Uint32Array;
|
|
93
|
-
numericProps: object;
|
|
94
|
-
properties: {}[];
|
|
95
|
-
fields: {
|
|
96
|
-
id?: number;
|
|
97
|
-
}[];
|
|
98
|
-
};
|