@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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@loaders.gl/mvt",
3
3
  "description": "Loader for Mapbox Vector Tiles",
4
- "version": "3.1.7",
4
+ "version": "3.2.0-alpha.2",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public"
@@ -32,14 +32,14 @@
32
32
  "build-worker": "esbuild src/workers/mvt-worker.ts --bundle --outfile=dist/mvt-worker.js --define:__VERSION__=\\\"$npm_package_version\\\""
33
33
  },
34
34
  "dependencies": {
35
- "@loaders.gl/gis": "3.1.7",
36
- "@loaders.gl/loader-utils": "3.1.7",
37
- "@loaders.gl/schema": "3.1.7",
35
+ "@loaders.gl/gis": "3.2.0-alpha.2",
36
+ "@loaders.gl/loader-utils": "3.2.0-alpha.2",
37
+ "@loaders.gl/schema": "3.2.0-alpha.2",
38
38
  "@math.gl/polygon": "^3.5.1",
39
39
  "pbf": "^3.2.1"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@types/pbf": "^3.0.2"
43
43
  },
44
- "gitHead": "e48d29e426e9f23879875763e73efc5c58345be1"
44
+ "gitHead": "52a602739cbfce60fc314f474efc984d199dff78"
45
45
  }
@@ -1,5 +1,5 @@
1
1
  import Protobuf from 'pbf';
2
- import {MvtMapboxGeometry} from '../lib/types';
2
+ import {MVTMapboxGeometry} from '../lib/types';
3
3
  import VectorTileFeature from '../lib/mapbox-vector-tile/vector-tile-feature';
4
4
 
5
5
  /**
@@ -7,7 +7,7 @@ import VectorTileFeature from '../lib/mapbox-vector-tile/vector-tile-feature';
7
7
  * @param rings
8
8
  * @returns polygons
9
9
  */
10
- export function classifyRings(rings: MvtMapboxGeometry) {
10
+ export function classifyRings(rings: MVTMapboxGeometry) {
11
11
  const len = rings.length;
12
12
 
13
13
  if (len <= 1) return [rings];
@@ -1,6 +1,6 @@
1
1
  // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
2
2
  import Protobuf from 'pbf';
3
- import {MvtMapboxCoordinates, MvtMapboxGeometry} from '../types';
3
+ import {MVTMapboxCoordinates, MVTMapboxGeometry} from '../types';
4
4
  import {readFeature, classifyRings} from '../../helpers/mapbox-util-functions';
5
5
 
6
6
  export default class VectorTileFeature {
@@ -39,7 +39,7 @@ export default class VectorTileFeature {
39
39
  }
40
40
 
41
41
  // eslint-disable-next-line complexity, max-statements
42
- loadGeometry(): MvtMapboxGeometry {
42
+ loadGeometry(): MVTMapboxGeometry {
43
43
  const pbf = this._pbf;
44
44
  pbf.pos = this._geometry;
45
45
 
@@ -163,7 +163,7 @@ export default class VectorTileFeature {
163
163
  type = `Multi${type}`;
164
164
  }
165
165
 
166
- const result: MvtMapboxCoordinates = {
166
+ const result: MVTMapboxCoordinates = {
167
167
  type: 'Feature',
168
168
  geometry: {
169
169
  type,
@@ -181,7 +181,7 @@ export default class VectorTileFeature {
181
181
 
182
182
  toGeoJSON(
183
183
  options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
184
- ): MvtMapboxCoordinates {
184
+ ): MVTMapboxCoordinates {
185
185
  if (typeof options === 'function') {
186
186
  return this._toGeoJSON(options);
187
187
  }
@@ -1,14 +1,19 @@
1
- // import {VectorTile} from '@mapbox/vector-tile';
2
- import VectorTile from './mapbox-vector-tile/vector-tile';
3
- import BinaryVectorTile from './binary-vector-tile/vector-tile';
4
-
5
1
  import {flatGeojsonToBinary} from '@loaders.gl/gis';
2
+ import type {
3
+ FlatFeature,
4
+ Feature,
5
+ GeojsonGeometryInfo,
6
+ BinaryFeatures,
7
+ GeoJSONRowTable
8
+ } from '@loaders.gl/schema';
6
9
  import Protobuf from 'pbf';
7
- import type {FlatFeature} from '@loaders.gl/schema';
8
- import type {MvtMapboxCoordinates, MvtOptions} from '../lib/types';
10
+
11
+ import type {MVTMapboxCoordinates, MVTOptions, MVTLoaderOptions} from '../lib/types';
12
+
13
+ import VectorTile from './mapbox-vector-tile/vector-tile';
14
+ import BinaryVectorTile from './binary-vector-tile/vector-tile';
9
15
  import VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';
10
16
  import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
11
- import {LoaderOptions} from '@loaders.gl/loader-utils';
12
17
 
13
18
  /**
14
19
  * Parse MVT arrayBuffer and return GeoJSON.
@@ -17,94 +22,130 @@ import {LoaderOptions} from '@loaders.gl/loader-utils';
17
22
  * @param options
18
23
  * @returns A GeoJSON geometry object or a binary representation
19
24
  */
20
- export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {
21
- options = normalizeOptions(options);
22
- const features: (FlatFeature | MvtMapboxCoordinates)[] = [];
23
-
24
- if (options) {
25
- const binary = options.gis.format === 'binary';
26
- const geometryInfo = {
27
- coordLength: 2,
28
- pointPositionsCount: 0,
29
- pointFeaturesCount: 0,
30
- linePositionsCount: 0,
31
- linePathsCount: 0,
32
- lineFeaturesCount: 0,
33
- polygonPositionsCount: 0,
34
- polygonObjectsCount: 0,
35
- polygonRingsCount: 0,
36
- polygonFeaturesCount: 0
37
- };
38
-
39
- if (arrayBuffer.byteLength > 0) {
40
- const tile = binary
41
- ? new BinaryVectorTile(new Protobuf(arrayBuffer))
42
- : new VectorTile(new Protobuf(arrayBuffer));
43
- const loaderOptions = options.mvt;
44
-
45
- const selectedLayers = Array.isArray(loaderOptions.layers)
46
- ? loaderOptions.layers
47
- : Object.keys(tile.layers);
48
-
49
- selectedLayers.forEach((layerName: string) => {
50
- const vectorTileLayer = tile.layers[layerName];
51
- const featureOptions = {...loaderOptions, layerName};
52
-
53
- if (!vectorTileLayer) {
54
- return;
55
- }
56
-
57
- for (let i = 0; i < vectorTileLayer.length; i++) {
58
- const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
59
-
60
- const decodedFeature = binary
61
- ? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)
62
- : getDecodedFeature(vectorTileFeature as VectorTileFeatureMapBox, featureOptions);
63
- features.push(decodedFeature);
64
- }
65
- });
25
+ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions) {
26
+ const mvtOptions = normalizeOptions(options);
27
+
28
+ const shape = options?.gis?.format || options?.mvt?.shape;
29
+ switch (shape) {
30
+ case 'columnar-table': // binary + some JS arrays
31
+ return {shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions)};
32
+ case 'geojson-row-table': {
33
+ const table: GeoJSONRowTable = {
34
+ shape: 'geojson-row-table',
35
+ data: parseToGeojson(arrayBuffer, mvtOptions)
36
+ };
37
+ return table;
66
38
  }
39
+ case 'geojson':
40
+ return parseToGeojson(arrayBuffer, mvtOptions);
41
+ case 'binary-geometry':
42
+ return parseToBinary(arrayBuffer, mvtOptions);
43
+ case 'binary':
44
+ return parseToBinary(arrayBuffer, mvtOptions);
45
+ default:
46
+ throw new Error(shape);
47
+ }
48
+ }
67
49
 
68
- if (binary) {
69
- const data = flatGeojsonToBinary(features as FlatFeature[], geometryInfo);
70
- // Add the original byteLength (as a reasonable approximation of the size of the binary data)
71
- // TODO decide where to store extra fields like byteLength (header etc) and document
72
- // @ts-ignore
73
- data.byteLength = arrayBuffer.byteLength;
74
- return data;
75
- }
50
+ function parseToBinary(arrayBuffer: ArrayBuffer, options: MVTOptions): BinaryFeatures {
51
+ const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
52
+
53
+ const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);
54
+ // Add the original byteLength (as a reasonable approximation of the size of the binary data)
55
+ // TODO decide where to store extra fields like byteLength (header etc) and document
56
+ // @ts-ignore
57
+ binaryData.byteLength = arrayBuffer.byteLength;
58
+ return binaryData;
59
+ }
60
+
61
+ function parseToFlatGeoJson(
62
+ arrayBuffer: ArrayBuffer,
63
+ options: MVTOptions
64
+ ): [FlatFeature[], GeojsonGeometryInfo] {
65
+ const features: FlatFeature[] = [];
66
+ const geometryInfo: GeojsonGeometryInfo = {
67
+ coordLength: 2,
68
+ pointPositionsCount: 0,
69
+ pointFeaturesCount: 0,
70
+ linePositionsCount: 0,
71
+ linePathsCount: 0,
72
+ lineFeaturesCount: 0,
73
+ polygonPositionsCount: 0,
74
+ polygonObjectsCount: 0,
75
+ polygonRingsCount: 0,
76
+ polygonFeaturesCount: 0
77
+ };
78
+
79
+ if (arrayBuffer.byteLength <= 0) {
80
+ return [features, geometryInfo];
76
81
  }
77
- return features;
82
+
83
+ const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
84
+
85
+ const selectedLayers =
86
+ options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
87
+
88
+ selectedLayers.forEach((layerName: string) => {
89
+ const vectorTileLayer = tile.layers[layerName];
90
+ if (!vectorTileLayer) {
91
+ return;
92
+ }
93
+
94
+ for (let i = 0; i < vectorTileLayer.length; i++) {
95
+ const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
96
+ const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
97
+ features.push(decodedFeature);
98
+ }
99
+ });
100
+
101
+ return [features, geometryInfo];
78
102
  }
79
103
 
80
- /**
81
- * @param options
82
- * @returns options
83
- */
84
- function normalizeOptions(options: LoaderOptions | undefined) {
85
- if (options) {
86
- options = {
87
- ...options,
88
- mvt: options.mvt || {},
89
- gis: options.gis || {}
90
- };
91
-
92
- // Validate
93
- const wgs84Coordinates = options.coordinates === 'wgs84';
94
- const {tileIndex} = options;
95
- const hasTileIndex =
96
- tileIndex &&
97
- Number.isFinite(tileIndex.x) &&
98
- Number.isFinite(tileIndex.y) &&
99
- Number.isFinite(tileIndex.z);
100
-
101
- if (wgs84Coordinates && !hasTileIndex) {
102
- throw new Error(
103
- 'MVT Loader: WGS84 coordinates need tileIndex property. Check documentation.'
104
- );
104
+ function parseToGeojson(arrayBuffer: ArrayBuffer, options: MVTOptions): Feature[] {
105
+ if (arrayBuffer.byteLength <= 0) {
106
+ return [];
107
+ }
108
+
109
+ const features: MVTMapboxCoordinates[] = [];
110
+ const tile = new VectorTile(new Protobuf(arrayBuffer));
111
+
112
+ const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
113
+
114
+ selectedLayers.forEach((layerName: string) => {
115
+ const vectorTileLayer = tile.layers[layerName];
116
+ if (!vectorTileLayer) {
117
+ return;
105
118
  }
119
+
120
+ for (let i = 0; i < vectorTileLayer.length; i++) {
121
+ const vectorTileFeature = vectorTileLayer.feature(i);
122
+ const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
123
+ features.push(decodedFeature);
124
+ }
125
+ });
126
+
127
+ return features as Feature[];
128
+ }
129
+
130
+ function normalizeOptions(options?: MVTLoaderOptions): MVTOptions {
131
+ if (!options?.mvt) {
132
+ throw new Error('mvt options required');
133
+ }
134
+
135
+ // Validate
136
+ const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';
137
+ const {tileIndex} = options.mvt;
138
+ const hasTileIndex =
139
+ tileIndex &&
140
+ Number.isFinite(tileIndex.x) &&
141
+ Number.isFinite(tileIndex.y) &&
142
+ Number.isFinite(tileIndex.z);
143
+
144
+ if (wgs84Coordinates && !hasTileIndex) {
145
+ throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
106
146
  }
107
- return options;
147
+
148
+ return options.mvt;
108
149
  }
109
150
 
110
151
  /**
@@ -114,15 +155,16 @@ function normalizeOptions(options: LoaderOptions | undefined) {
114
155
  */
115
156
  function getDecodedFeature(
116
157
  feature: VectorTileFeatureMapBox,
117
- options: MvtOptions
118
- ): MvtMapboxCoordinates {
158
+ options: MVTOptions,
159
+ layerName: string
160
+ ): MVTMapboxCoordinates {
119
161
  const decodedFeature = feature.toGeoJSON(
120
162
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates
121
163
  );
122
164
 
123
165
  // Add layer name to GeoJSON properties
124
166
  if (options.layerProperty) {
125
- decodedFeature.properties[options.layerProperty] = options.layerName;
167
+ decodedFeature.properties[options.layerProperty] = layerName;
126
168
  }
127
169
 
128
170
  return decodedFeature;
@@ -135,7 +177,8 @@ function getDecodedFeature(
135
177
  */
136
178
  function getDecodedFeatureBinary(
137
179
  feature: VectorTileFeatureBinary,
138
- options: MvtOptions
180
+ options: MVTOptions,
181
+ layerName: string
139
182
  ): FlatFeature {
140
183
  const decodedFeature = feature.toBinaryCoordinates(
141
184
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
@@ -143,7 +186,7 @@ function getDecodedFeatureBinary(
143
186
 
144
187
  // Add layer name to GeoJSON properties
145
188
  if (options.layerProperty && decodedFeature.properties) {
146
- decodedFeature.properties[options.layerProperty] = options.layerName;
189
+ decodedFeature.properties[options.layerProperty] = layerName;
147
190
  }
148
191
 
149
192
  return decodedFeature;
package/src/lib/types.ts CHANGED
@@ -1,23 +1,69 @@
1
- export type MvtOptions = {
2
- coordinates: string | number[];
1
+ import type {LoaderOptions} from '@loaders.gl/loader-utils';
2
+
3
+ /** For local coordinates, the tileIndex is not required */
4
+ type MVTLocalCoordinatesOptions = {
5
+ /**
6
+ * When set to `local`, the parser will return a flat array of GeoJSON objects with local coordinates decoded from tile origin.
7
+ */
8
+ coordinates: 'local';
9
+ tileIndex: null;
10
+ };
11
+
12
+ /** In WGS84 coordinates, the tileIndex is required */
13
+ type MVTWgs84CoordinatesOptions = {
14
+ /**
15
+ * When set to `wgs84`, the parser will return a flat array of GeoJSON objects with coordinates in longitude, latitude decoded from the provided tile index.
16
+ */
17
+ coordinates: 'wgs84';
18
+
19
+ /**
20
+ * Mandatory with `wgs84` coordinates option. An object containing tile index values (`x`, `y`,
21
+ * `z`) to reproject features' coordinates into WGS84.
22
+ */
3
23
  tileIndex: {x: number; y: number; z: number};
4
- layerProperty: string | number;
5
- layerName: string;
6
24
  };
7
25
 
8
- export type MvtMapboxGeometry = {
26
+ export type MVTOptions = (MVTLocalCoordinatesOptions | MVTWgs84CoordinatesOptions) & {
27
+ /**
28
+ * When non-`null`, the layer name of each feature is added to
29
+ * `feature.properties[layerProperty]`. (A `feature.properties` object is created if the feature
30
+ * has no existing properties). If set to `null`, a layer name property will not be added.
31
+ */
32
+ layerProperty?: string | number;
33
+
34
+ /**
35
+ * Optional list of layer names. If not `null`, only features belonging to the named layers will
36
+ * be included in the output. If `null`, features from all layers are returned.
37
+ */
38
+ layers?: string[];
39
+ shape?: 'geojson-row-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
40
+ };
41
+
42
+ export type MVTMapboxGeometry = {
9
43
  type?: string;
10
44
  id?: number;
11
45
  length: number;
12
46
  coordinates?: any[];
13
47
  };
14
48
 
15
- export type MvtMapboxCoordinates = {
49
+ export type MVTMapboxCoordinates = {
16
50
  type: string;
17
51
  geometry: {
18
52
  type: string;
19
- coordinates: MvtMapboxGeometry;
53
+ coordinates: MVTMapboxGeometry;
20
54
  };
21
55
  properties: {[x: string]: string | number | boolean | null};
22
56
  id?: number;
23
57
  };
58
+
59
+ export type MVTLoaderOptions = LoaderOptions & {
60
+ mvt?: MVTOptions;
61
+ gis?: {
62
+ /**
63
+ * When set to `true`, the parser will output the data in binary format. This is equivalent to loading the data as GeoJSON and then applying [geojsonToBinary](https://loaders.gl/modules/gis/docs/api-reference/geojson-to-binary).
64
+ */
65
+ binary?: boolean;
66
+ /** @deprecated. Use options.mvt.shape */
67
+ format?: 'geojson-row-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
68
+ };
69
+ };
package/src/mvt-loader.ts CHANGED
@@ -1,10 +1,21 @@
1
1
  import type {Loader, LoaderWithParser} from '@loaders.gl/loader-utils';
2
+ import type {MVTLoaderOptions} from './lib/types';
2
3
  import parseMVT from './lib/parse-mvt';
3
4
 
4
5
  // __VERSION__ is injected by babel-plugin-version-inline
5
6
  // @ts-ignore TS2304: Cannot find name '__VERSION__'.
6
7
  const VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
7
8
 
9
+ const DEFAULT_MVT_LOADER_OPTIONS: MVTLoaderOptions = {
10
+ mvt: {
11
+ shape: 'geojson',
12
+ coordinates: 'local',
13
+ layerProperty: 'layerName',
14
+ layers: undefined,
15
+ tileIndex: null
16
+ }
17
+ };
18
+
8
19
  /**
9
20
  * Worker loader for the Mapbox Vector Tile format
10
21
  */
@@ -22,14 +33,7 @@ export const MVTWorkerLoader: Loader = {
22
33
  ],
23
34
  worker: true,
24
35
  category: 'geometry',
25
- options: {
26
- mvt: {
27
- coordinates: 'local',
28
- layerProperty: 'layerName',
29
- layers: null,
30
- tileIndex: null
31
- }
32
- }
36
+ options: DEFAULT_MVT_LOADER_OPTIONS
33
37
  };
34
38
 
35
39
  /**
@@ -37,7 +41,7 @@ export const MVTWorkerLoader: Loader = {
37
41
  */
38
42
  export const MVTLoader: LoaderWithParser = {
39
43
  ...MVTWorkerLoader,
40
- parse: async (arrayBuffer, options) => parseMVT(arrayBuffer, options),
44
+ parse: async (arrayBuffer, options?: MVTLoaderOptions) => parseMVT(arrayBuffer, options),
41
45
  parseSync: parseMVT,
42
46
  binary: true
43
47
  };