@loaders.gl/mvt 3.1.7 → 3.2.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.
- package/dist/dist.min.js +978 -941
- package/dist/es5/helpers/mapbox-util-functions.js.map +1 -1
- package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/es5/lib/parse-mvt.js +117 -66
- package/dist/es5/lib/parse-mvt.js.map +1 -1
- package/dist/es5/mvt-loader.js +11 -9
- package/dist/es5/mvt-loader.js.map +1 -1
- package/dist/esm/helpers/mapbox-util-functions.js.map +1 -1
- package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -1
- package/dist/esm/lib/parse-mvt.js +113 -60
- package/dist/esm/lib/parse-mvt.js.map +1 -1
- package/dist/esm/mvt-loader.js +11 -9
- package/dist/esm/mvt-loader.js.map +1 -1
- package/dist/helpers/mapbox-util-functions.d.ts +2 -2
- package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +4 -4
- package/dist/lib/parse-mvt.d.ts +6 -9
- package/dist/lib/parse-mvt.d.ts.map +1 -1
- package/dist/lib/parse-mvt.js +97 -73
- package/dist/lib/types.d.ts +48 -7
- package/dist/lib/types.d.ts.map +1 -1
- package/dist/mvt-loader.d.ts.map +1 -1
- package/dist/mvt-loader.js +10 -8
- package/dist/mvt-worker.js +1006 -936
- package/package.json +5 -5
- package/src/helpers/mapbox-util-functions.ts +2 -2
- package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +4 -4
- package/src/lib/parse-mvt.ts +136 -93
- package/src/lib/types.ts +53 -7
- package/src/mvt-loader.ts +13 -9
|
@@ -1,93 +1,146 @@
|
|
|
1
|
-
import VectorTile from './mapbox-vector-tile/vector-tile';
|
|
2
|
-
import BinaryVectorTile from './binary-vector-tile/vector-tile';
|
|
3
1
|
import { flatGeojsonToBinary } from '@loaders.gl/gis';
|
|
4
2
|
import Protobuf from 'pbf';
|
|
3
|
+
import VectorTile from './mapbox-vector-tile/vector-tile';
|
|
4
|
+
import BinaryVectorTile from './binary-vector-tile/vector-tile';
|
|
5
5
|
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
|
-
if (arrayBuffer.byteLength > 0) {
|
|
25
|
-
const tile = binary ? new BinaryVectorTile(new Protobuf(arrayBuffer)) : new VectorTile(new Protobuf(arrayBuffer));
|
|
26
|
-
const loaderOptions = options.mvt;
|
|
27
|
-
const selectedLayers = Array.isArray(loaderOptions.layers) ? loaderOptions.layers : Object.keys(tile.layers);
|
|
28
|
-
selectedLayers.forEach(layerName => {
|
|
29
|
-
const vectorTileLayer = tile.layers[layerName];
|
|
30
|
-
const featureOptions = { ...loaderOptions,
|
|
31
|
-
layerName
|
|
6
|
+
var _options$gis, _options$mvt;
|
|
7
|
+
|
|
8
|
+
const mvtOptions = normalizeOptions(options);
|
|
9
|
+
const shape = (options === null || options === void 0 ? void 0 : (_options$gis = options.gis) === null || _options$gis === void 0 ? void 0 : _options$gis.format) || (options === null || options === void 0 ? void 0 : (_options$mvt = options.mvt) === null || _options$mvt === void 0 ? void 0 : _options$mvt.shape);
|
|
10
|
+
|
|
11
|
+
switch (shape) {
|
|
12
|
+
case 'columnar-table':
|
|
13
|
+
return {
|
|
14
|
+
shape: 'columnar-table',
|
|
15
|
+
data: parseToBinary(arrayBuffer, mvtOptions)
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
case 'geojson-row-table':
|
|
19
|
+
{
|
|
20
|
+
const table = {
|
|
21
|
+
shape: 'geojson-row-table',
|
|
22
|
+
data: parseToGeojson(arrayBuffer, mvtOptions)
|
|
32
23
|
};
|
|
24
|
+
return table;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
case 'geojson':
|
|
28
|
+
return parseToGeojson(arrayBuffer, mvtOptions);
|
|
29
|
+
|
|
30
|
+
case 'binary-geometry':
|
|
31
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
32
|
+
|
|
33
|
+
case 'binary':
|
|
34
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
35
|
+
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(shape);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function parseToBinary(arrayBuffer, options) {
|
|
42
|
+
const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
|
|
43
|
+
const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);
|
|
44
|
+
binaryData.byteLength = arrayBuffer.byteLength;
|
|
45
|
+
return binaryData;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function parseToFlatGeoJson(arrayBuffer, options) {
|
|
49
|
+
const features = [];
|
|
50
|
+
const geometryInfo = {
|
|
51
|
+
coordLength: 2,
|
|
52
|
+
pointPositionsCount: 0,
|
|
53
|
+
pointFeaturesCount: 0,
|
|
54
|
+
linePositionsCount: 0,
|
|
55
|
+
linePathsCount: 0,
|
|
56
|
+
lineFeaturesCount: 0,
|
|
57
|
+
polygonPositionsCount: 0,
|
|
58
|
+
polygonObjectsCount: 0,
|
|
59
|
+
polygonRingsCount: 0,
|
|
60
|
+
polygonFeaturesCount: 0
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
64
|
+
return [features, geometryInfo];
|
|
65
|
+
}
|
|
33
66
|
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
67
|
+
const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
|
|
68
|
+
const selectedLayers = options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
69
|
+
selectedLayers.forEach(layerName => {
|
|
70
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
37
71
|
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
const decodedFeature = binary ? getDecodedFeatureBinary(vectorTileFeature, featureOptions) : getDecodedFeature(vectorTileFeature, featureOptions);
|
|
41
|
-
features.push(decodedFeature);
|
|
42
|
-
}
|
|
43
|
-
});
|
|
72
|
+
if (!vectorTileLayer) {
|
|
73
|
+
return;
|
|
44
74
|
}
|
|
45
75
|
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
|
|
76
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
77
|
+
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
78
|
+
const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
|
|
79
|
+
features.push(decodedFeature);
|
|
50
80
|
}
|
|
81
|
+
});
|
|
82
|
+
return [features, geometryInfo];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function parseToGeojson(arrayBuffer, options) {
|
|
86
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
87
|
+
return [];
|
|
51
88
|
}
|
|
52
89
|
|
|
90
|
+
const features = [];
|
|
91
|
+
const tile = new VectorTile(new Protobuf(arrayBuffer));
|
|
92
|
+
const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
93
|
+
selectedLayers.forEach(layerName => {
|
|
94
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
95
|
+
|
|
96
|
+
if (!vectorTileLayer) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
101
|
+
const vectorTileFeature = vectorTileLayer.feature(i);
|
|
102
|
+
const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
|
|
103
|
+
features.push(decodedFeature);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
53
106
|
return features;
|
|
54
107
|
}
|
|
55
108
|
|
|
56
109
|
function normalizeOptions(options) {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
110
|
+
var _options$mvt2;
|
|
111
|
+
|
|
112
|
+
if (!(options !== null && options !== void 0 && options.mvt)) {
|
|
113
|
+
throw new Error('mvt options required');
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
const wgs84Coordinates = ((_options$mvt2 = options.mvt) === null || _options$mvt2 === void 0 ? void 0 : _options$mvt2.coordinates) === 'wgs84';
|
|
117
|
+
const {
|
|
118
|
+
tileIndex
|
|
119
|
+
} = options.mvt;
|
|
120
|
+
const hasTileIndex = tileIndex && Number.isFinite(tileIndex.x) && Number.isFinite(tileIndex.y) && Number.isFinite(tileIndex.z);
|
|
121
|
+
|
|
122
|
+
if (wgs84Coordinates && !hasTileIndex) {
|
|
123
|
+
throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
|
|
71
124
|
}
|
|
72
125
|
|
|
73
|
-
return options;
|
|
126
|
+
return options.mvt;
|
|
74
127
|
}
|
|
75
128
|
|
|
76
|
-
function getDecodedFeature(feature, options) {
|
|
129
|
+
function getDecodedFeature(feature, options, layerName) {
|
|
77
130
|
const decodedFeature = feature.toGeoJSON(options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates);
|
|
78
131
|
|
|
79
132
|
if (options.layerProperty) {
|
|
80
|
-
decodedFeature.properties[options.layerProperty] =
|
|
133
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
81
134
|
}
|
|
82
135
|
|
|
83
136
|
return decodedFeature;
|
|
84
137
|
}
|
|
85
138
|
|
|
86
|
-
function getDecodedFeatureBinary(feature, options) {
|
|
139
|
+
function getDecodedFeatureBinary(feature, options, layerName) {
|
|
87
140
|
const decodedFeature = feature.toBinaryCoordinates(options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary);
|
|
88
141
|
|
|
89
142
|
if (options.layerProperty && decodedFeature.properties) {
|
|
90
|
-
decodedFeature.properties[options.layerProperty] =
|
|
143
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
91
144
|
}
|
|
92
145
|
|
|
93
146
|
return decodedFeature;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/parse-mvt.ts"],"names":["VectorTile","BinaryVectorTile","flatGeojsonToBinary","Protobuf","parseMVT","arrayBuffer","options","normalizeOptions","features","binary","gis","format","geometryInfo","coordLength","pointPositionsCount","pointFeaturesCount","linePositionsCount","linePathsCount","lineFeaturesCount","polygonPositionsCount","polygonObjectsCount","polygonRingsCount","polygonFeaturesCount","byteLength","tile","loaderOptions","mvt","selectedLayers","Array","isArray","layers","Object","keys","forEach","layerName","vectorTileLayer","featureOptions","i","length","vectorTileFeature","feature","decodedFeature","getDecodedFeatureBinary","getDecodedFeature","push","data","wgs84Coordinates","coordinates","tileIndex","hasTileIndex","Number","isFinite","x","y","z","Error","toGeoJSON","transformToLocalCoordinates","layerProperty","properties","toBinaryCoordinates","transformToLocalCoordinatesBinary","line","extent","p","il"],"mappings":"AACA,OAAOA,UAAP,MAAuB,kCAAvB;AACA,OAAOC,gBAAP,MAA6B,kCAA7B;AAEA,SAAQC,mBAAR,QAAkC,iBAAlC;AACA,OAAOC,QAAP,MAAqB,KAArB;AAcA,eAAe,SAASC,QAAT,CAAkBC,WAAlB,EAA4CC,OAA5C,EAAqE;AAClFA,EAAAA,OAAO,GAAGC,gBAAgB,CAACD,OAAD,CAA1B;AACA,QAAME,QAAgD,GAAG,EAAzD;;AAEA,MAAIF,OAAJ,EAAa;AACX,UAAMG,MAAM,GAAGH,OAAO,CAACI,GAAR,CAAYC,MAAZ,KAAuB,QAAtC;AACA,UAAMC,YAAY,GAAG;AACnBC,MAAAA,WAAW,EA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A+C;AAC7CQ,IAAAA,IAAI,CAACR,CAAD,CAAJ,IAAW0B,MAAX;AACD;AACF","sourcesContent":["// import {VectorTile} from '@mapbox/vector-tile';\nimport VectorTile from './mapbox-vector-tile/vector-tile';\nimport BinaryVectorTile from './binary-vector-tile/vector-tile';\n\nimport {flatGeojsonToBinary} from '@loaders.gl/gis';\nimport Protobuf from 'pbf';\nimport type {FlatFeature} from '@loaders.gl/schema';\nimport type {MvtMapboxCoordinates, MvtOptions} from '../lib/types';\nimport VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';\nimport VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';\nimport {LoaderOptions} from '@loaders.gl/loader-utils';\n\n/**\n * Parse MVT arrayBuffer and return GeoJSON.\n *\n * @param arrayBuffer A MVT arrayBuffer\n * @param options\n * @returns A GeoJSON geometry object or a binary representation\n */\nexport default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {\n options = normalizeOptions(options);\n const features: (FlatFeature | MvtMapboxCoordinates)[] = [];\n\n if (options) {\n const binary = options.gis.format === 'binary';\n const geometryInfo = {\n coordLength: 2,\n pointPositionsCount: 0,\n pointFeaturesCount: 0,\n linePositionsCount: 0,\n linePathsCount: 0,\n lineFeaturesCount: 0,\n polygonPositionsCount: 0,\n polygonObjectsCount: 0,\n polygonRingsCount: 0,\n polygonFeaturesCount: 0\n };\n\n if (arrayBuffer.byteLength > 0) {\n const tile = binary\n ? new BinaryVectorTile(new Protobuf(arrayBuffer))\n : new VectorTile(new Protobuf(arrayBuffer));\n const loaderOptions = options.mvt;\n\n const selectedLayers = Array.isArray(loaderOptions.layers)\n ? loaderOptions.layers\n : Object.keys(tile.layers);\n\n selectedLayers.forEach((layerName: string) => {\n const vectorTileLayer = tile.layers[layerName];\n const featureOptions = {...loaderOptions, layerName};\n\n if (!vectorTileLayer) {\n return;\n }\n\n for (let i = 0; i < vectorTileLayer.length; i++) {\n const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);\n\n const decodedFeature = binary\n ? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)\n : getDecodedFeature(vectorTileFeature as VectorTileFeatureMapBox, featureOptions);\n features.push(decodedFeature);\n }\n });\n }\n\n if (binary) {\n const data = flatGeojsonToBinary(features as FlatFeature[], geometryInfo);\n // Add the original byteLength (as a reasonable approximation of the size of the binary data)\n // TODO decide where to store extra fields like byteLength (header etc) and document\n // @ts-ignore\n data.byteLength = arrayBuffer.byteLength;\n return data;\n }\n }\n return features;\n}\n\n/**\n * @param options\n * @returns options\n */\nfunction normalizeOptions(options: LoaderOptions | undefined) {\n if (options) {\n options = {\n ...options,\n mvt: options.mvt || {},\n gis: options.gis || {}\n };\n\n // Validate\n const wgs84Coordinates = options.coordinates === 'wgs84';\n const {tileIndex} = options;\n const hasTileIndex =\n tileIndex &&\n Number.isFinite(tileIndex.x) &&\n Number.isFinite(tileIndex.y) &&\n Number.isFinite(tileIndex.z);\n\n if (wgs84Coordinates && !hasTileIndex) {\n throw new Error(\n 'MVT Loader: WGS84 coordinates need tileIndex property. Check documentation.'\n );\n }\n }\n return options;\n}\n\n/**\n * @param feature\n * @param options\n * @returns decoded feature\n */\nfunction getDecodedFeature(\n feature: VectorTileFeatureMapBox,\n options: MvtOptions\n): MvtMapboxCoordinates {\n const decodedFeature = feature.toGeoJSON(\n options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates\n );\n\n // Add layer name to GeoJSON properties\n if (options.layerProperty) {\n decodedFeature.properties[options.layerProperty] = options.layerName;\n }\n\n return decodedFeature;\n}\n\n/**\n * @param feature\n * @param options\n * @returns decoded binary feature\n */\nfunction getDecodedFeatureBinary(\n feature: VectorTileFeatureBinary,\n options: MvtOptions\n): FlatFeature {\n const decodedFeature = feature.toBinaryCoordinates(\n options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary\n );\n\n // Add layer name to GeoJSON properties\n if (options.layerProperty && decodedFeature.properties) {\n decodedFeature.properties[options.layerProperty] = options.layerName;\n }\n\n return decodedFeature;\n}\n\n/**\n * @param line\n * @param feature\n */\nfunction transformToLocalCoordinates(line: number[], feature: {extent: any}): void {\n // This function transforms local coordinates in a\n // [0 - bufferSize, this.extent + bufferSize] range to a\n // [0 - (bufferSize / this.extent), 1 + (bufferSize / this.extent)] range.\n // The resulting extent would be 1.\n const {extent} = feature;\n for (let i = 0; i < line.length; i++) {\n const p = line[i];\n p[0] /= extent;\n p[1] /= extent;\n }\n}\n\nfunction transformToLocalCoordinatesBinary(data: number[], feature: {extent: any}) {\n // For the binary code path, the feature data is just\n // one big flat array, so we just divide each value\n const {extent} = feature;\n for (let i = 0, il = data.length; i < il; ++i) {\n data[i] /= extent;\n }\n}\n"],"file":"parse-mvt.js"}
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/parse-mvt.ts"],"names":["flatGeojsonToBinary","Protobuf","VectorTile","BinaryVectorTile","parseMVT","arrayBuffer","options","mvtOptions","normalizeOptions","shape","gis","format","mvt","data","parseToBinary","table","parseToGeojson","Error","flatGeoJsonFeatures","geometryInfo","parseToFlatGeoJson","binaryData","byteLength","features","coordLength","pointPositionsCount","pointFeaturesCount","linePositionsCount","linePathsCount","lineFeaturesCount","polygonPositionsCount","polygonObjectsCount","polygonRingsCount","polygonFeaturesCount","tile","selectedLayers","Array","isArray","layers","Object","keys","forEach","layerName","vectorTileLayer","i","length","vectorTileFeature","feature","decodedFeature","getDecodedFeatureBinary","push","getDecodedFeature","wgs84Coordinates","coordinates","tileIndex","hasTileIndex","Number","isFinite","x","y","z","toGeoJSON","transformToLocalCoordinates","layerProperty","properties","toBinaryCoordinates","transformToLocalCoordinatesBinary","line","extent","p","il"],"mappings":"AAAA,SAAQA,mBAAR,QAAkC,iBAAlC;AAQA,OAAOC,QAAP,MAAqB,KAArB;AAIA,OAAOC,UAAP,MAAuB,kCAAvB;AACA,OAAOC,gBAAP,MAA6B,kCAA7B;AAWA,eAAe,SAASC,QAAT,CAAkBC,WAA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AAmDrB,SAAnD;AACD;;AAED,SAAOM,cAAP;AACD;;AAMD,SAASc,2BAAT,CAAqCK,IAArC,EAAqDpB,OAArD,EAAmF;AAKjF,QAAM;AAACqB,IAAAA;AAAD,MAAWrB,OAAjB;;AACA,OAAK,IAAIH,CAAC,GAAG,CAAb,EAAgBA,CAAC,GAAGuB,IAAI,CAACtB,MAAzB,EAAiCD,CAAC,EAAlC,EAAsC;AACpC,UAAMyB,CAAC,GAAGF,IAAI,CAACvB,CAAD,CAAd;AACAyB,IAAAA,CAAC,CAAC,CAAD,CAAD,IAAQD,MAAR;AACAC,IAAAA,CAAC,CAAC,CAAD,CAAD,IAAQD,MAAR;AACD;AACF;;AAED,SAASF,iCAAT,CAA2CrD,IAA3C,EAA2DkC,OAA3D,EAAmF;AAGjF,QAAM;AAACqB,IAAAA;AAAD,MAAWrB,OAAjB;;AACA,OAAK,IAAIH,CAAC,GAAG,CAAR,EAAW0B,EAAE,GAAGzD,IAAI,CAACgC,MAA1B,EAAkCD,CAAC,GAAG0B,EAAtC,EAA0C,EAAE1B,CAA5C,EAA+C;AAC7C/B,IAAAA,IAAI,CAAC+B,CAAD,CAAJ,IAAWwB,MAAX;AACD;AACF","sourcesContent":["import {flatGeojsonToBinary} from '@loaders.gl/gis';\nimport type {\n FlatFeature,\n Feature,\n GeojsonGeometryInfo,\n BinaryFeatures,\n GeoJSONRowTable\n} from '@loaders.gl/schema';\nimport Protobuf from 'pbf';\n\nimport type {MVTMapboxCoordinates, MVTOptions, MVTLoaderOptions} from '../lib/types';\n\nimport VectorTile from './mapbox-vector-tile/vector-tile';\nimport BinaryVectorTile from './binary-vector-tile/vector-tile';\nimport VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';\nimport VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';\n\n/**\n * Parse MVT arrayBuffer and return GeoJSON.\n *\n * @param arrayBuffer A MVT arrayBuffer\n * @param options\n * @returns A GeoJSON geometry object or a binary representation\n */\nexport default function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions) {\n const mvtOptions = normalizeOptions(options);\n\n const shape = options?.gis?.format || options?.mvt?.shape;\n switch (shape) {\n case 'columnar-table': // binary + some JS arrays\n return {shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions)};\n case 'geojson-row-table': {\n const table: GeoJSONRowTable = {\n shape: 'geojson-row-table',\n data: parseToGeojson(arrayBuffer, mvtOptions)\n };\n return table;\n }\n case 'geojson':\n return parseToGeojson(arrayBuffer, mvtOptions);\n case 'binary-geometry':\n return parseToBinary(arrayBuffer, mvtOptions);\n case 'binary':\n return parseToBinary(arrayBuffer, mvtOptions);\n default:\n throw new Error(shape);\n }\n}\n\nfunction parseToBinary(arrayBuffer: ArrayBuffer, options: MVTOptions): BinaryFeatures {\n const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);\n\n const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);\n // Add the original byteLength (as a reasonable approximation of the size of the binary data)\n // TODO decide where to store extra fields like byteLength (header etc) and document\n // @ts-ignore\n binaryData.byteLength = arrayBuffer.byteLength;\n return binaryData;\n}\n\nfunction parseToFlatGeoJson(\n arrayBuffer: ArrayBuffer,\n options: MVTOptions\n): [FlatFeature[], GeojsonGeometryInfo] {\n const features: FlatFeature[] = [];\n const geometryInfo: GeojsonGeometryInfo = {\n coordLength: 2,\n pointPositionsCount: 0,\n pointFeaturesCount: 0,\n linePositionsCount: 0,\n linePathsCount: 0,\n lineFeaturesCount: 0,\n polygonPositionsCount: 0,\n polygonObjectsCount: 0,\n polygonRingsCount: 0,\n polygonFeaturesCount: 0\n };\n\n if (arrayBuffer.byteLength <= 0) {\n return [features, geometryInfo];\n }\n\n const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));\n\n const selectedLayers =\n options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);\n\n selectedLayers.forEach((layerName: string) => {\n const vectorTileLayer = tile.layers[layerName];\n if (!vectorTileLayer) {\n return;\n }\n\n for (let i = 0; i < vectorTileLayer.length; i++) {\n const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);\n const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);\n features.push(decodedFeature);\n }\n });\n\n return [features, geometryInfo];\n}\n\nfunction parseToGeojson(arrayBuffer: ArrayBuffer, options: MVTOptions): Feature[] {\n if (arrayBuffer.byteLength <= 0) {\n return [];\n }\n\n const features: MVTMapboxCoordinates[] = [];\n const tile = new VectorTile(new Protobuf(arrayBuffer));\n\n const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);\n\n selectedLayers.forEach((layerName: string) => {\n const vectorTileLayer = tile.layers[layerName];\n if (!vectorTileLayer) {\n return;\n }\n\n for (let i = 0; i < vectorTileLayer.length; i++) {\n const vectorTileFeature = vectorTileLayer.feature(i);\n const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);\n features.push(decodedFeature);\n }\n });\n\n return features as Feature[];\n}\n\nfunction normalizeOptions(options?: MVTLoaderOptions): MVTOptions {\n if (!options?.mvt) {\n throw new Error('mvt options required');\n }\n\n // Validate\n const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';\n const {tileIndex} = options.mvt;\n const hasTileIndex =\n tileIndex &&\n Number.isFinite(tileIndex.x) &&\n Number.isFinite(tileIndex.y) &&\n Number.isFinite(tileIndex.z);\n\n if (wgs84Coordinates && !hasTileIndex) {\n throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');\n }\n\n return options.mvt;\n}\n\n/**\n * @param feature\n * @param options\n * @returns decoded feature\n */\nfunction getDecodedFeature(\n feature: VectorTileFeatureMapBox,\n options: MVTOptions,\n layerName: string\n): MVTMapboxCoordinates {\n const decodedFeature = feature.toGeoJSON(\n options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates\n );\n\n // Add layer name to GeoJSON properties\n if (options.layerProperty) {\n decodedFeature.properties[options.layerProperty] = layerName;\n }\n\n return decodedFeature;\n}\n\n/**\n * @param feature\n * @param options\n * @returns decoded binary feature\n */\nfunction getDecodedFeatureBinary(\n feature: VectorTileFeatureBinary,\n options: MVTOptions,\n layerName: string\n): FlatFeature {\n const decodedFeature = feature.toBinaryCoordinates(\n options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary\n );\n\n // Add layer name to GeoJSON properties\n if (options.layerProperty && decodedFeature.properties) {\n decodedFeature.properties[options.layerProperty] = layerName;\n }\n\n return decodedFeature;\n}\n\n/**\n * @param line\n * @param feature\n */\nfunction transformToLocalCoordinates(line: number[], feature: {extent: any}): void {\n // This function transforms local coordinates in a\n // [0 - bufferSize, this.extent + bufferSize] range to a\n // [0 - (bufferSize / this.extent), 1 + (bufferSize / this.extent)] range.\n // The resulting extent would be 1.\n const {extent} = feature;\n for (let i = 0; i < line.length; i++) {\n const p = line[i];\n p[0] /= extent;\n p[1] /= extent;\n }\n}\n\nfunction transformToLocalCoordinatesBinary(data: number[], feature: {extent: any}) {\n // For the binary code path, the feature data is just\n // one big flat array, so we just divide each value\n const {extent} = feature;\n for (let i = 0, il = data.length; i < il; ++i) {\n data[i] /= extent;\n }\n}\n"],"file":"parse-mvt.js"}
|
package/dist/esm/mvt-loader.js
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
import parseMVT from './lib/parse-mvt';
|
|
2
|
-
const VERSION = typeof "3.
|
|
2
|
+
const VERSION = typeof "3.2.0-alpha.2" !== 'undefined' ? "3.2.0-alpha.2" : 'latest';
|
|
3
|
+
const DEFAULT_MVT_LOADER_OPTIONS = {
|
|
4
|
+
mvt: {
|
|
5
|
+
shape: 'geojson',
|
|
6
|
+
coordinates: 'local',
|
|
7
|
+
layerProperty: 'layerName',
|
|
8
|
+
layers: undefined,
|
|
9
|
+
tileIndex: null
|
|
10
|
+
}
|
|
11
|
+
};
|
|
3
12
|
export const MVTWorkerLoader = {
|
|
4
13
|
name: 'Mapbox Vector Tile',
|
|
5
14
|
id: 'mvt',
|
|
@@ -9,14 +18,7 @@ export const MVTWorkerLoader = {
|
|
|
9
18
|
mimeTypes: ['application/vnd.mapbox-vector-tile', 'application/x-protobuf'],
|
|
10
19
|
worker: true,
|
|
11
20
|
category: 'geometry',
|
|
12
|
-
options:
|
|
13
|
-
mvt: {
|
|
14
|
-
coordinates: 'local',
|
|
15
|
-
layerProperty: 'layerName',
|
|
16
|
-
layers: null,
|
|
17
|
-
tileIndex: null
|
|
18
|
-
}
|
|
19
|
-
}
|
|
21
|
+
options: DEFAULT_MVT_LOADER_OPTIONS
|
|
20
22
|
};
|
|
21
23
|
export const MVTLoader = { ...MVTWorkerLoader,
|
|
22
24
|
parse: async (arrayBuffer, options) => parseMVT(arrayBuffer, options),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/mvt-loader.ts"],"names":["parseMVT","VERSION","
|
|
1
|
+
{"version":3,"sources":["../../src/mvt-loader.ts"],"names":["parseMVT","VERSION","DEFAULT_MVT_LOADER_OPTIONS","mvt","shape","coordinates","layerProperty","layers","undefined","tileIndex","MVTWorkerLoader","name","id","module","version","extensions","mimeTypes","worker","category","options","MVTLoader","parse","arrayBuffer","parseSync","binary"],"mappings":"AAEA,OAAOA,QAAP,MAAqB,iBAArB;AAIA,MAAMC,OAAO,GAAG,2BAAuB,WAAvB,qBAAmD,QAAnE;AAEA,MAAMC,0BAA4C,GAAG;AACnDC,EAAAA,GAAG,EAAE;AACHC,IAAAA,KAAK,EAAE,SADJ;AAEHC,IAAAA,WAAW,EAAE,OAFV;AAGHC,IAAAA,aAAa,EAAE,WAHZ;AAIHC,IAAAA,MAAM,EAAEC,SAJL;AAKHC,IAAAA,SAAS,EAAE;AALR;AAD8C,CAArD;AAaA,OAAO,MAAMC,eAAuB,GAAG;AACrCC,EAAAA,IAAI,EAAE,oBAD+B;AAErCC,EAAAA,EAAE,EAAE,KAFiC;AAGrCC,EAAAA,MAAM,EAAE,KAH6B;AAIrCC,EAAAA,OAAO,EAAEb,OAJ4B;AAMrCc,EAAAA,UAAU,EAAE,CAAC,KAAD,EAAQ,KAAR,CANyB;AAOrCC,EAAAA,SAAS,EAAE,CACT,oCADS,EAET,wBAFS,CAP0B;AAYrCC,EAAAA,MAAM,EAAE,IAZ6B;AAarCC,EAAAA,QAAQ,EAAE,UAb2B;AAcrCC,EAAAA,OAAO,EAAEjB;AAd4B,CAAhC;AAoBP,OAAO,MAAMkB,SAA2B,GAAG,EACzC,GAAGV,eADsC;AAEzCW,EAAAA,KAAK,EAAE,OAAOC,WAAP,EAAoBH,OAApB,KAAmDnB,QAAQ,CAACsB,WAAD,EAAcH,OAAd,CAFzB;AAGzCI,EAAAA,SAAS,EAAEvB,QAH8B;AAIzCwB,EAAAA,MAAM,EAAE;AAJiC,CAApC","sourcesContent":["import type {Loader, LoaderWithParser} from '@loaders.gl/loader-utils';\nimport type {MVTLoaderOptions} from './lib/types';\nimport parseMVT from './lib/parse-mvt';\n\n// __VERSION__ is injected by babel-plugin-version-inline\n// @ts-ignore TS2304: Cannot find name '__VERSION__'.\nconst VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';\n\nconst DEFAULT_MVT_LOADER_OPTIONS: MVTLoaderOptions = {\n mvt: {\n shape: 'geojson',\n coordinates: 'local',\n layerProperty: 'layerName',\n layers: undefined,\n tileIndex: null\n }\n};\n\n/**\n * Worker loader for the Mapbox Vector Tile format\n */\nexport const MVTWorkerLoader: Loader = {\n name: 'Mapbox Vector Tile',\n id: 'mvt',\n module: 'mvt',\n version: VERSION,\n // Note: ArcGIS uses '.pbf' extension and 'application/octet-stream'\n extensions: ['mvt', 'pbf'],\n mimeTypes: [\n 'application/vnd.mapbox-vector-tile',\n 'application/x-protobuf'\n // 'application/octet-stream'\n ],\n worker: true,\n category: 'geometry',\n options: DEFAULT_MVT_LOADER_OPTIONS\n};\n\n/**\n * Loader for the Mapbox Vector Tile format\n */\nexport const MVTLoader: LoaderWithParser = {\n ...MVTWorkerLoader,\n parse: async (arrayBuffer, options?: MVTLoaderOptions) => parseMVT(arrayBuffer, options),\n parseSync: parseMVT,\n binary: true\n};\n"],"file":"mvt-loader.js"}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import Protobuf from 'pbf';
|
|
2
|
-
import {
|
|
2
|
+
import { MVTMapboxGeometry } from '../lib/types';
|
|
3
3
|
import VectorTileFeature from '../lib/mapbox-vector-tile/vector-tile-feature';
|
|
4
4
|
/**
|
|
5
5
|
* Classifies an array of rings into polygons with outer rings and holes
|
|
6
6
|
* @param rings
|
|
7
7
|
* @returns polygons
|
|
8
8
|
*/
|
|
9
|
-
export declare function classifyRings(rings:
|
|
9
|
+
export declare function classifyRings(rings: MVTMapboxGeometry): MVTMapboxGeometry[] | number[][][];
|
|
10
10
|
/**
|
|
11
11
|
*
|
|
12
12
|
* @param ring
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import Protobuf from 'pbf';
|
|
2
|
-
import {
|
|
2
|
+
import { MVTMapboxCoordinates, MVTMapboxGeometry } from '../types';
|
|
3
3
|
export default class VectorTileFeature {
|
|
4
4
|
properties: {
|
|
5
5
|
[x: string]: string | number | boolean | null;
|
|
@@ -13,15 +13,15 @@ export default class VectorTileFeature {
|
|
|
13
13
|
_values: (string | number | boolean | null)[];
|
|
14
14
|
static get types(): string[];
|
|
15
15
|
constructor(pbf: Protobuf, end: number, extent: any, keys: string[], values: (string | number | boolean | null)[]);
|
|
16
|
-
loadGeometry():
|
|
16
|
+
loadGeometry(): MVTMapboxGeometry;
|
|
17
17
|
bbox(): number[];
|
|
18
|
-
_toGeoJSON(transform: any):
|
|
18
|
+
_toGeoJSON(transform: any): MVTMapboxCoordinates;
|
|
19
19
|
toGeoJSON(options: {
|
|
20
20
|
x: number;
|
|
21
21
|
y: number;
|
|
22
22
|
z: number;
|
|
23
23
|
} | ((data: number[], feature: {
|
|
24
24
|
extent: any;
|
|
25
|
-
}) => void)):
|
|
25
|
+
}) => void)): MVTMapboxCoordinates;
|
|
26
26
|
}
|
|
27
27
|
//# sourceMappingURL=vector-tile-feature.d.ts.map
|
package/dist/lib/parse-mvt.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import {
|
|
1
|
+
import type { Feature, BinaryFeatures, GeoJSONRowTable } from '@loaders.gl/schema';
|
|
2
|
+
import type { MVTLoaderOptions } from '../lib/types';
|
|
3
3
|
/**
|
|
4
4
|
* Parse MVT arrayBuffer and return GeoJSON.
|
|
5
5
|
*
|
|
@@ -7,11 +7,8 @@ import { LoaderOptions } from '@loaders.gl/loader-utils';
|
|
|
7
7
|
* @param options
|
|
8
8
|
* @returns A GeoJSON geometry object or a binary representation
|
|
9
9
|
*/
|
|
10
|
-
export default function parseMVT(arrayBuffer: ArrayBuffer, options?:
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
properties: import("geojson").GeoJsonProperties;
|
|
15
|
-
bbox?: import("geojson").BBox | undefined;
|
|
16
|
-
})[] | import("@loaders.gl/schema").BinaryFeatures;
|
|
10
|
+
export default function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions): BinaryFeatures | GeoJSONRowTable | Feature<import("geojson").Geometry, import("geojson").GeoJsonProperties>[] | {
|
|
11
|
+
shape: string;
|
|
12
|
+
data: BinaryFeatures;
|
|
13
|
+
};
|
|
17
14
|
//# sourceMappingURL=parse-mvt.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-mvt.d.ts","sourceRoot":"","sources":["../../src/lib/parse-mvt.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"parse-mvt.d.ts","sourceRoot":"","sources":["../../src/lib/parse-mvt.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAEV,OAAO,EAEP,cAAc,EACd,eAAe,EAChB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,KAAK,EAAmC,gBAAgB,EAAC,MAAM,cAAc,CAAC;AAOrF;;;;;;GAMG;AACH,MAAM,CAAC,OAAO,UAAU,QAAQ,CAAC,WAAW,EAAE,WAAW,EAAE,OAAO,CAAC,EAAE,gBAAgB;;;EAuBpF"}
|
package/dist/lib/parse-mvt.js
CHANGED
|
@@ -3,11 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
// import {VectorTile} from '@mapbox/vector-tile';
|
|
7
|
-
const vector_tile_1 = __importDefault(require("./mapbox-vector-tile/vector-tile"));
|
|
8
|
-
const vector_tile_2 = __importDefault(require("./binary-vector-tile/vector-tile"));
|
|
9
6
|
const gis_1 = require("@loaders.gl/gis");
|
|
10
7
|
const pbf_1 = __importDefault(require("pbf"));
|
|
8
|
+
const vector_tile_1 = __importDefault(require("./mapbox-vector-tile/vector-tile"));
|
|
9
|
+
const vector_tile_2 = __importDefault(require("./binary-vector-tile/vector-tile"));
|
|
11
10
|
/**
|
|
12
11
|
* Parse MVT arrayBuffer and return GeoJSON.
|
|
13
12
|
*
|
|
@@ -16,91 +15,116 @@ const pbf_1 = __importDefault(require("pbf"));
|
|
|
16
15
|
* @returns A GeoJSON geometry object or a binary representation
|
|
17
16
|
*/
|
|
18
17
|
function parseMVT(arrayBuffer, options) {
|
|
19
|
-
|
|
18
|
+
const mvtOptions = normalizeOptions(options);
|
|
19
|
+
const shape = options?.gis?.format || options?.mvt?.shape;
|
|
20
|
+
switch (shape) {
|
|
21
|
+
case 'columnar-table': // binary + some JS arrays
|
|
22
|
+
return { shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions) };
|
|
23
|
+
case 'geojson-row-table': {
|
|
24
|
+
const table = {
|
|
25
|
+
shape: 'geojson-row-table',
|
|
26
|
+
data: parseToGeojson(arrayBuffer, mvtOptions)
|
|
27
|
+
};
|
|
28
|
+
return table;
|
|
29
|
+
}
|
|
30
|
+
case 'geojson':
|
|
31
|
+
return parseToGeojson(arrayBuffer, mvtOptions);
|
|
32
|
+
case 'binary-geometry':
|
|
33
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
34
|
+
case 'binary':
|
|
35
|
+
return parseToBinary(arrayBuffer, mvtOptions);
|
|
36
|
+
default:
|
|
37
|
+
throw new Error(shape);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
exports.default = parseMVT;
|
|
41
|
+
function parseToBinary(arrayBuffer, options) {
|
|
42
|
+
const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
|
|
43
|
+
const binaryData = (0, gis_1.flatGeojsonToBinary)(flatGeoJsonFeatures, geometryInfo);
|
|
44
|
+
// Add the original byteLength (as a reasonable approximation of the size of the binary data)
|
|
45
|
+
// TODO decide where to store extra fields like byteLength (header etc) and document
|
|
46
|
+
// @ts-ignore
|
|
47
|
+
binaryData.byteLength = arrayBuffer.byteLength;
|
|
48
|
+
return binaryData;
|
|
49
|
+
}
|
|
50
|
+
function parseToFlatGeoJson(arrayBuffer, options) {
|
|
20
51
|
const features = [];
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
: Object.keys(tile.layers);
|
|
43
|
-
selectedLayers.forEach((layerName) => {
|
|
44
|
-
const vectorTileLayer = tile.layers[layerName];
|
|
45
|
-
const featureOptions = { ...loaderOptions, layerName };
|
|
46
|
-
if (!vectorTileLayer) {
|
|
47
|
-
return;
|
|
48
|
-
}
|
|
49
|
-
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
50
|
-
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
51
|
-
const decodedFeature = binary
|
|
52
|
-
? getDecodedFeatureBinary(vectorTileFeature, featureOptions)
|
|
53
|
-
: getDecodedFeature(vectorTileFeature, featureOptions);
|
|
54
|
-
features.push(decodedFeature);
|
|
55
|
-
}
|
|
56
|
-
});
|
|
52
|
+
const geometryInfo = {
|
|
53
|
+
coordLength: 2,
|
|
54
|
+
pointPositionsCount: 0,
|
|
55
|
+
pointFeaturesCount: 0,
|
|
56
|
+
linePositionsCount: 0,
|
|
57
|
+
linePathsCount: 0,
|
|
58
|
+
lineFeaturesCount: 0,
|
|
59
|
+
polygonPositionsCount: 0,
|
|
60
|
+
polygonObjectsCount: 0,
|
|
61
|
+
polygonRingsCount: 0,
|
|
62
|
+
polygonFeaturesCount: 0
|
|
63
|
+
};
|
|
64
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
65
|
+
return [features, geometryInfo];
|
|
66
|
+
}
|
|
67
|
+
const tile = new vector_tile_2.default(new pbf_1.default(arrayBuffer));
|
|
68
|
+
const selectedLayers = options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
69
|
+
selectedLayers.forEach((layerName) => {
|
|
70
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
71
|
+
if (!vectorTileLayer) {
|
|
72
|
+
return;
|
|
57
73
|
}
|
|
58
|
-
|
|
59
|
-
const
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
// @ts-ignore
|
|
63
|
-
data.byteLength = arrayBuffer.byteLength;
|
|
64
|
-
return data;
|
|
74
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
75
|
+
const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
|
|
76
|
+
const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
|
|
77
|
+
features.push(decodedFeature);
|
|
65
78
|
}
|
|
79
|
+
});
|
|
80
|
+
return [features, geometryInfo];
|
|
81
|
+
}
|
|
82
|
+
function parseToGeojson(arrayBuffer, options) {
|
|
83
|
+
if (arrayBuffer.byteLength <= 0) {
|
|
84
|
+
return [];
|
|
66
85
|
}
|
|
86
|
+
const features = [];
|
|
87
|
+
const tile = new vector_tile_1.default(new pbf_1.default(arrayBuffer));
|
|
88
|
+
const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
|
|
89
|
+
selectedLayers.forEach((layerName) => {
|
|
90
|
+
const vectorTileLayer = tile.layers[layerName];
|
|
91
|
+
if (!vectorTileLayer) {
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
for (let i = 0; i < vectorTileLayer.length; i++) {
|
|
95
|
+
const vectorTileFeature = vectorTileLayer.feature(i);
|
|
96
|
+
const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
|
|
97
|
+
features.push(decodedFeature);
|
|
98
|
+
}
|
|
99
|
+
});
|
|
67
100
|
return features;
|
|
68
101
|
}
|
|
69
|
-
exports.default = parseMVT;
|
|
70
|
-
/**
|
|
71
|
-
* @param options
|
|
72
|
-
* @returns options
|
|
73
|
-
*/
|
|
74
102
|
function normalizeOptions(options) {
|
|
75
|
-
if (options) {
|
|
76
|
-
options
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
Number.isFinite(tileIndex.z);
|
|
88
|
-
if (wgs84Coordinates && !hasTileIndex) {
|
|
89
|
-
throw new Error('MVT Loader: WGS84 coordinates need tileIndex property. Check documentation.');
|
|
90
|
-
}
|
|
103
|
+
if (!options?.mvt) {
|
|
104
|
+
throw new Error('mvt options required');
|
|
105
|
+
}
|
|
106
|
+
// Validate
|
|
107
|
+
const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';
|
|
108
|
+
const { tileIndex } = options.mvt;
|
|
109
|
+
const hasTileIndex = tileIndex &&
|
|
110
|
+
Number.isFinite(tileIndex.x) &&
|
|
111
|
+
Number.isFinite(tileIndex.y) &&
|
|
112
|
+
Number.isFinite(tileIndex.z);
|
|
113
|
+
if (wgs84Coordinates && !hasTileIndex) {
|
|
114
|
+
throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
|
|
91
115
|
}
|
|
92
|
-
return options;
|
|
116
|
+
return options.mvt;
|
|
93
117
|
}
|
|
94
118
|
/**
|
|
95
119
|
* @param feature
|
|
96
120
|
* @param options
|
|
97
121
|
* @returns decoded feature
|
|
98
122
|
*/
|
|
99
|
-
function getDecodedFeature(feature, options) {
|
|
123
|
+
function getDecodedFeature(feature, options, layerName) {
|
|
100
124
|
const decodedFeature = feature.toGeoJSON(options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates);
|
|
101
125
|
// Add layer name to GeoJSON properties
|
|
102
126
|
if (options.layerProperty) {
|
|
103
|
-
decodedFeature.properties[options.layerProperty] =
|
|
127
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
104
128
|
}
|
|
105
129
|
return decodedFeature;
|
|
106
130
|
}
|
|
@@ -109,11 +133,11 @@ function getDecodedFeature(feature, options) {
|
|
|
109
133
|
* @param options
|
|
110
134
|
* @returns decoded binary feature
|
|
111
135
|
*/
|
|
112
|
-
function getDecodedFeatureBinary(feature, options) {
|
|
136
|
+
function getDecodedFeatureBinary(feature, options, layerName) {
|
|
113
137
|
const decodedFeature = feature.toBinaryCoordinates(options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary);
|
|
114
138
|
// Add layer name to GeoJSON properties
|
|
115
139
|
if (options.layerProperty && decodedFeature.properties) {
|
|
116
|
-
decodedFeature.properties[options.layerProperty] =
|
|
140
|
+
decodedFeature.properties[options.layerProperty] = layerName;
|
|
117
141
|
}
|
|
118
142
|
return decodedFeature;
|
|
119
143
|
}
|