@loaders.gl/mvt 4.2.0-alpha.4 → 4.2.0-alpha.5
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/dist.dev.js +274 -252
- package/dist/dist.min.js +14 -0
- package/dist/helpers/binary-util-functions.d.ts +1 -1
- package/dist/helpers/binary-util-functions.d.ts.map +1 -1
- package/dist/helpers/binary-util-functions.js +99 -61
- package/dist/helpers/mapbox-util-functions.d.ts +2 -2
- package/dist/helpers/mapbox-util-functions.d.ts.map +1 -1
- package/dist/helpers/mapbox-util-functions.js +67 -33
- package/dist/index.cjs +37 -61
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +8 -8
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-feature.js +135 -118
- package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -1
- package/dist/lib/binary-vector-tile/vector-tile-layer.js +77 -38
- package/dist/lib/binary-vector-tile/vector-tile.d.ts +1 -1
- package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -1
- package/dist/lib/binary-vector-tile/vector-tile.js +17 -12
- package/dist/lib/geojson-tiler/clip.d.ts +1 -1
- package/dist/lib/geojson-tiler/clip.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/clip.js +177 -142
- package/dist/lib/geojson-tiler/convert.d.ts +1 -1
- package/dist/lib/geojson-tiler/convert.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/convert.js +116 -100
- package/dist/lib/geojson-tiler/feature.d.ts +1 -1
- package/dist/lib/geojson-tiler/feature.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/feature.js +38 -29
- package/dist/lib/geojson-tiler/geojson-tiler.d.ts +1 -1
- package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/geojson-tiler.js +197 -159
- package/dist/lib/geojson-tiler/simplify.js +60 -40
- package/dist/lib/geojson-tiler/tile.js +108 -88
- package/dist/lib/geojson-tiler/transform.d.ts +1 -1
- package/dist/lib/geojson-tiler/transform.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/transform.js +35 -26
- package/dist/lib/geojson-tiler/wrap.d.ts +1 -1
- package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -1
- package/dist/lib/geojson-tiler/wrap.js +71 -53
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +156 -151
- package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +75 -38
- package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -1
- package/dist/lib/mapbox-vector-tile/vector-tile.js +17 -12
- package/dist/lib/parse-mvt.d.ts +1 -1
- package/dist/lib/parse-mvt.d.ts.map +1 -1
- package/dist/lib/parse-mvt.js +142 -115
- package/dist/lib/parse-tilejson.js +234 -176
- package/dist/lib/types.js +0 -1
- package/dist/mvt-loader.d.ts +1 -1
- package/dist/mvt-loader.d.ts.map +1 -1
- package/dist/mvt-loader.js +41 -21
- package/dist/mvt-source.js +148 -139
- package/dist/tilejson-loader.d.ts +1 -1
- package/dist/tilejson-loader.d.ts.map +1 -1
- package/dist/tilejson-loader.js +31 -30
- package/dist/workers/mvt-worker.js +0 -1
- package/package.json +12 -8
- package/dist/helpers/binary-util-functions.js.map +0 -1
- package/dist/helpers/mapbox-util-functions.js.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/lib/binary-vector-tile/LICENSE.txt +0 -31
- package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +0 -1
- package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +0 -1
- package/dist/lib/binary-vector-tile/vector-tile.js.map +0 -1
- package/dist/lib/geojson-tiler/LICENSE +0 -19
- package/dist/lib/geojson-tiler/clip.js.map +0 -1
- package/dist/lib/geojson-tiler/convert.js.map +0 -1
- package/dist/lib/geojson-tiler/feature.js.map +0 -1
- package/dist/lib/geojson-tiler/geojson-tiler.js.map +0 -1
- package/dist/lib/geojson-tiler/simplify.js.map +0 -1
- package/dist/lib/geojson-tiler/tile.js.map +0 -1
- package/dist/lib/geojson-tiler/transform.js.map +0 -1
- package/dist/lib/geojson-tiler/wrap.js.map +0 -1
- package/dist/lib/mapbox-vector-tile/LICENSE.txt +0 -31
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.js.map +0 -1
- package/dist/lib/mapbox-vector-tile/vector-tile-layer.js.map +0 -1
- package/dist/lib/mapbox-vector-tile/vector-tile.js.map +0 -1
- package/dist/lib/parse-mvt.js.map +0 -1
- package/dist/lib/parse-tilejson.js.map +0 -1
- package/dist/lib/types.js.map +0 -1
- package/dist/mvt-loader.js.map +0 -1
- package/dist/mvt-source.js.map +0 -1
- package/dist/tilejson-loader.js.map +0 -1
- package/dist/workers/mvt-worker.js.map +0 -1
package/dist/lib/parse-mvt.js
CHANGED
|
@@ -2,138 +2,165 @@ import { flatGeojsonToBinary } from '@loaders.gl/gis';
|
|
|
2
2
|
import Protobuf from 'pbf';
|
|
3
3
|
import VectorTile from "./mapbox-vector-tile/vector-tile.js";
|
|
4
4
|
import BinaryVectorTile from "./binary-vector-tile/vector-tile.js";
|
|
5
|
+
/**
|
|
6
|
+
* Parse MVT arrayBuffer and return GeoJSON.
|
|
7
|
+
*
|
|
8
|
+
* @param arrayBuffer A MVT arrayBuffer
|
|
9
|
+
* @param options
|
|
10
|
+
* @returns A GeoJSON geometry object or a binary representation
|
|
11
|
+
*/
|
|
5
12
|
export default function parseMVT(arrayBuffer, options) {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
case 'binary':
|
|
29
|
-
return parseToBinary(arrayBuffer, mvtOptions);
|
|
30
|
-
default:
|
|
31
|
-
throw new Error(shape || 'undefined shape');
|
|
32
|
-
}
|
|
13
|
+
const mvtOptions = normalizeOptions(options);
|
|
14
|
+
const shape = options?.gis?.format || options?.mvt?.shape || options?.shape;
|
|
15
|
+
switch (shape) {
|
|
16
|
+
case 'columnar-table': // binary + some JS arrays
|
|
17
|
+
return { shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions) };
|
|
18
|
+
case 'geojson-table': {
|
|
19
|
+
const table = {
|
|
20
|
+
shape: 'geojson-table',
|
|
21
|
+
type: 'FeatureCollection',
|
|
22
|
+
features: parseToGeojsonFeatures(arrayBuffer, mvtOptions)
|
|
23
|
+
};
|
|
24
|
+
return table;
|
|
25
|
+
}
|
|
26
|
+
case 'geojson':
|
|
27
|
+
return parseToGeojsonFeatures(arrayBuffer, mvtOptions);
|
|
28
|
+
case 'binary-geometry':
|
|
29
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
30
|
+
case 'binary':
|
|
31
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
32
|
+
default:
|
|
33
|
+
throw new Error(shape || 'undefined shape');
|
|
34
|
+
}
|
|
33
35
|
}
|
|
34
36
|
function parseToBinary(arrayBuffer, options) {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
37
|
+
const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
|
|
38
|
+
const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);
|
|
39
|
+
// Add the original byteLength (as a reasonable approximation of the size of the binary data)
|
|
40
|
+
// TODO decide where to store extra fields like byteLength (header etc) and document
|
|
41
|
+
// @ts-ignore
|
|
42
|
+
binaryData.byteLength = arrayBuffer.byteLength;
|
|
43
|
+
return binaryData;
|
|
39
44
|
}
|
|
40
45
|
function parseToFlatGeoJson(arrayBuffer, options) {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
}
|
|
57
|
-
const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
|
|
58
|
-
const selectedLayers = options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
59
|
-
selectedLayers.forEach(layerName => {
|
|
60
|
-
const vectorTileLayer = tile.layers[layerName];
|
|
61
|
-
if (!vectorTileLayer) {
|
|
62
|
-
return;
|
|
63
|
-
}
|
|
64
|
-
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
65
|
-
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
66
|
-
const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
|
|
67
|
-
features.push(decodedFeature);
|
|
46
|
+
const features = [];
|
|
47
|
+
const geometryInfo = {
|
|
48
|
+
coordLength: 2,
|
|
49
|
+
pointPositionsCount: 0,
|
|
50
|
+
pointFeaturesCount: 0,
|
|
51
|
+
linePositionsCount: 0,
|
|
52
|
+
linePathsCount: 0,
|
|
53
|
+
lineFeaturesCount: 0,
|
|
54
|
+
polygonPositionsCount: 0,
|
|
55
|
+
polygonObjectsCount: 0,
|
|
56
|
+
polygonRingsCount: 0,
|
|
57
|
+
polygonFeaturesCount: 0
|
|
58
|
+
};
|
|
59
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
60
|
+
return [features, geometryInfo];
|
|
68
61
|
}
|
|
69
|
-
|
|
70
|
-
|
|
62
|
+
const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
|
|
63
|
+
const selectedLayers = options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
64
|
+
selectedLayers.forEach((layerName) => {
|
|
65
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
66
|
+
if (!vectorTileLayer) {
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
70
|
+
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
71
|
+
const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
|
|
72
|
+
features.push(decodedFeature);
|
|
73
|
+
}
|
|
74
|
+
});
|
|
75
|
+
return [features, geometryInfo];
|
|
71
76
|
}
|
|
72
77
|
function parseToGeojsonFeatures(arrayBuffer, options) {
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
}
|
|
76
|
-
const features = [];
|
|
77
|
-
const tile = new VectorTile(new Protobuf(arrayBuffer));
|
|
78
|
-
const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
79
|
-
selectedLayers.forEach(layerName => {
|
|
80
|
-
const vectorTileLayer = tile.layers[layerName];
|
|
81
|
-
if (!vectorTileLayer) {
|
|
82
|
-
return;
|
|
83
|
-
}
|
|
84
|
-
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
85
|
-
const vectorTileFeature = vectorTileLayer.feature(i);
|
|
86
|
-
const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
|
|
87
|
-
features.push(decodedFeature);
|
|
78
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
79
|
+
return [];
|
|
88
80
|
}
|
|
89
|
-
|
|
90
|
-
|
|
81
|
+
const features = [];
|
|
82
|
+
const tile = new VectorTile(new Protobuf(arrayBuffer));
|
|
83
|
+
const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
84
|
+
selectedLayers.forEach((layerName) => {
|
|
85
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
86
|
+
if (!vectorTileLayer) {
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
90
|
+
const vectorTileFeature = vectorTileLayer.feature(i);
|
|
91
|
+
const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
|
|
92
|
+
features.push(decodedFeature);
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
return features;
|
|
91
96
|
}
|
|
92
97
|
function normalizeOptions(options) {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
tileIndex
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
98
|
+
if (!options?.mvt) {
|
|
99
|
+
throw new Error('mvt options required');
|
|
100
|
+
}
|
|
101
|
+
// Validate
|
|
102
|
+
const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';
|
|
103
|
+
const { tileIndex } = options.mvt;
|
|
104
|
+
const hasTileIndex = tileIndex &&
|
|
105
|
+
Number.isFinite(tileIndex.x) &&
|
|
106
|
+
Number.isFinite(tileIndex.y) &&
|
|
107
|
+
Number.isFinite(tileIndex.z);
|
|
108
|
+
if (wgs84Coordinates && !hasTileIndex) {
|
|
109
|
+
throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
|
|
110
|
+
}
|
|
111
|
+
return options.mvt;
|
|
106
112
|
}
|
|
113
|
+
/**
|
|
114
|
+
* @param feature
|
|
115
|
+
* @param options
|
|
116
|
+
* @returns decoded feature
|
|
117
|
+
*/
|
|
107
118
|
function getDecodedFeature(feature, options, layerName) {
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
119
|
+
const decodedFeature = feature.toGeoJSON(
|
|
120
|
+
// @ts-expect-error What is going on here?
|
|
121
|
+
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates);
|
|
122
|
+
// Add layer name to GeoJSON properties
|
|
123
|
+
if (options.layerProperty) {
|
|
124
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
125
|
+
}
|
|
126
|
+
return decodedFeature;
|
|
113
127
|
}
|
|
128
|
+
/**
|
|
129
|
+
* @param feature
|
|
130
|
+
* @param options
|
|
131
|
+
* @returns decoded binary feature
|
|
132
|
+
*/
|
|
114
133
|
function getDecodedFeatureBinary(feature, options, layerName) {
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
134
|
+
const decodedFeature = feature.toBinaryCoordinates(
|
|
135
|
+
// @ts-expect-error What is going on here?
|
|
136
|
+
options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary);
|
|
137
|
+
// Add layer name to GeoJSON properties
|
|
138
|
+
if (options.layerProperty && decodedFeature.properties) {
|
|
139
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
140
|
+
}
|
|
141
|
+
return decodedFeature;
|
|
120
142
|
}
|
|
143
|
+
/**
|
|
144
|
+
* @param line
|
|
145
|
+
* @param feature
|
|
146
|
+
*/
|
|
121
147
|
function transformToLocalCoordinates(line, feature) {
|
|
122
|
-
|
|
123
|
-
extent
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
const
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
148
|
+
// This function transforms local coordinates in a
|
|
149
|
+
// [0 - bufferSize, this.extent + bufferSize] range to a
|
|
150
|
+
// [0 - (bufferSize / this.extent), 1 + (bufferSize / this.extent)] range.
|
|
151
|
+
// The resulting extent would be 1.
|
|
152
|
+
const { extent } = feature;
|
|
153
|
+
for (let i = 0; i < line.length; i++) {
|
|
154
|
+
const p = line[i];
|
|
155
|
+
p[0] /= extent;
|
|
156
|
+
p[1] /= extent;
|
|
157
|
+
}
|
|
130
158
|
}
|
|
131
159
|
function transformToLocalCoordinatesBinary(data, feature) {
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
160
|
+
// For the binary code path, the feature data is just
|
|
161
|
+
// one big flat array, so we just divide each value
|
|
162
|
+
const { extent } = feature;
|
|
163
|
+
for (let i = 0, il = data.length; i < il; ++i) {
|
|
164
|
+
data[i] /= extent;
|
|
165
|
+
}
|
|
138
166
|
}
|
|
139
|
-
//# sourceMappingURL=parse-mvt.js.map
|