@loaders.gl/mvt 4.2.1 → 4.3.0-alpha.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 (107) hide show
  1. package/dist/dist.dev.js +1311 -767
  2. package/dist/dist.min.js +1 -1
  3. package/dist/geojson-tile-source.d.ts +79 -0
  4. package/dist/geojson-tile-source.d.ts.map +1 -0
  5. package/dist/{lib/geojson-tiler/geojson-tiler.js → geojson-tile-source.js} +137 -27
  6. package/dist/index.cjs +568 -558
  7. package/dist/index.cjs.map +4 -4
  8. package/dist/index.d.ts +2 -2
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +1 -1
  11. package/dist/lib/geojsonvt/clip.d.ts.map +1 -0
  12. package/dist/lib/geojsonvt/convert.d.ts +10 -0
  13. package/dist/lib/geojsonvt/convert.d.ts.map +1 -0
  14. package/dist/lib/geojsonvt/feature.d.ts.map +1 -0
  15. package/dist/lib/geojsonvt/simplify.d.ts.map +1 -0
  16. package/dist/lib/geojsonvt/tile.d.ts.map +1 -0
  17. package/dist/lib/geojsonvt/transform.d.ts.map +1 -0
  18. package/dist/lib/geojsonvt/wrap.d.ts.map +1 -0
  19. package/dist/lib/parse-mvt.d.ts +3 -2
  20. package/dist/lib/parse-mvt.d.ts.map +1 -1
  21. package/dist/lib/parse-mvt.js +13 -15
  22. package/dist/lib/types.d.ts +1 -39
  23. package/dist/lib/types.d.ts.map +1 -1
  24. package/dist/lib/utils/geometry-utils.d.ts +46 -0
  25. package/dist/lib/utils/geometry-utils.d.ts.map +1 -0
  26. package/dist/{helpers/binary-util-functions.js → lib/utils/geometry-utils.js} +79 -46
  27. package/dist/lib/{binary-vector-tile → vector-tile}/vector-tile-feature.d.ts +27 -15
  28. package/dist/lib/vector-tile/vector-tile-feature.d.ts.map +1 -0
  29. package/dist/lib/vector-tile/vector-tile-feature.js +345 -0
  30. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.d.ts +10 -1
  31. package/dist/lib/vector-tile/vector-tile-layer.d.ts.map +1 -0
  32. package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.js +16 -1
  33. package/dist/lib/vector-tile/vector-tile.d.ts.map +1 -0
  34. package/dist/mvt-loader.d.ts +20 -9
  35. package/dist/mvt-loader.d.ts.map +1 -1
  36. package/dist/mvt-loader.js +2 -1
  37. package/dist/mvt-source.js +4 -4
  38. package/dist/mvt-worker.js +1172 -735
  39. package/package.json +6 -6
  40. package/src/{lib/geojson-tiler/geojson-tiler.ts → geojson-tile-source.ts} +172 -33
  41. package/src/index.ts +3 -3
  42. package/src/lib/{geojson-tiler → geojsonvt}/convert.ts +2 -2
  43. package/src/lib/{geojson-tiler → geojsonvt}/tile.ts +2 -2
  44. package/src/lib/parse-mvt.ts +24 -33
  45. package/src/lib/types.ts +1 -39
  46. package/src/{helpers/binary-util-functions.ts → lib/utils/geometry-utils.ts} +92 -48
  47. package/src/lib/vector-tile/vector-tile-feature.ts +407 -0
  48. package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile-layer.ts +28 -2
  49. package/src/mvt-loader.ts +14 -6
  50. package/src/mvt-source.ts +4 -4
  51. package/dist/helpers/binary-util-functions.d.ts +0 -35
  52. package/dist/helpers/binary-util-functions.d.ts.map +0 -1
  53. package/dist/helpers/mapbox-util-functions.d.ts +0 -29
  54. package/dist/helpers/mapbox-util-functions.d.ts.map +0 -1
  55. package/dist/helpers/mapbox-util-functions.js +0 -78
  56. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +0 -1
  57. package/dist/lib/binary-vector-tile/vector-tile-feature.js +0 -163
  58. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +0 -23
  59. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +0 -1
  60. package/dist/lib/binary-vector-tile/vector-tile-layer.js +0 -96
  61. package/dist/lib/binary-vector-tile/vector-tile.d.ts +0 -9
  62. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +0 -1
  63. package/dist/lib/binary-vector-tile/vector-tile.js +0 -27
  64. package/dist/lib/geojson-tiler/clip.d.ts.map +0 -1
  65. package/dist/lib/geojson-tiler/convert.d.ts +0 -10
  66. package/dist/lib/geojson-tiler/convert.d.ts.map +0 -1
  67. package/dist/lib/geojson-tiler/feature.d.ts.map +0 -1
  68. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +0 -44
  69. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +0 -1
  70. package/dist/lib/geojson-tiler/simplify.d.ts.map +0 -1
  71. package/dist/lib/geojson-tiler/tile.d.ts.map +0 -1
  72. package/dist/lib/geojson-tiler/transform.d.ts.map +0 -1
  73. package/dist/lib/geojson-tiler/wrap.d.ts.map +0 -1
  74. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +0 -27
  75. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +0 -1
  76. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +0 -178
  77. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +0 -1
  78. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +0 -1
  79. package/src/helpers/mapbox-util-functions.ts +0 -82
  80. package/src/lib/binary-vector-tile/vector-tile-feature.ts +0 -203
  81. package/src/lib/binary-vector-tile/vector-tile-layer.ts +0 -113
  82. package/src/lib/binary-vector-tile/vector-tile.ts +0 -36
  83. package/src/lib/mapbox-vector-tile/LICENSE.txt +0 -31
  84. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +0 -207
  85. /package/dist/lib/{geojson-tiler → geojsonvt}/clip.d.ts +0 -0
  86. /package/dist/lib/{geojson-tiler → geojsonvt}/clip.js +0 -0
  87. /package/dist/lib/{geojson-tiler → geojsonvt}/convert.js +0 -0
  88. /package/dist/lib/{geojson-tiler → geojsonvt}/feature.d.ts +0 -0
  89. /package/dist/lib/{geojson-tiler → geojsonvt}/feature.js +0 -0
  90. /package/dist/lib/{geojson-tiler → geojsonvt}/simplify.d.ts +0 -0
  91. /package/dist/lib/{geojson-tiler → geojsonvt}/simplify.js +0 -0
  92. /package/dist/lib/{geojson-tiler → geojsonvt}/tile.d.ts +0 -0
  93. /package/dist/lib/{geojson-tiler → geojsonvt}/tile.js +0 -0
  94. /package/dist/lib/{geojson-tiler → geojsonvt}/transform.d.ts +0 -0
  95. /package/dist/lib/{geojson-tiler → geojsonvt}/transform.js +0 -0
  96. /package/dist/lib/{geojson-tiler → geojsonvt}/wrap.d.ts +0 -0
  97. /package/dist/lib/{geojson-tiler → geojsonvt}/wrap.js +0 -0
  98. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.d.ts +0 -0
  99. /package/dist/lib/{mapbox-vector-tile → vector-tile}/vector-tile.js +0 -0
  100. /package/src/lib/{geojson-tiler → geojsonvt}/LICENSE +0 -0
  101. /package/src/lib/{geojson-tiler → geojsonvt}/clip.ts +0 -0
  102. /package/src/lib/{geojson-tiler → geojsonvt}/feature.ts +0 -0
  103. /package/src/lib/{geojson-tiler → geojsonvt}/simplify.ts +0 -0
  104. /package/src/lib/{geojson-tiler → geojsonvt}/transform.ts +0 -0
  105. /package/src/lib/{geojson-tiler → geojsonvt}/wrap.ts +0 -0
  106. /package/src/lib/{binary-vector-tile → vector-tile}/LICENSE.txt +0 -0
  107. /package/src/lib/{mapbox-vector-tile → vector-tile}/vector-tile.ts +0 -0
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": "4.2.1",
4
+ "version": "4.3.0-alpha.1",
5
5
  "license": "MIT",
6
6
  "type": "module",
7
7
  "publishConfig": {
@@ -41,10 +41,10 @@
41
41
  "build-worker": "esbuild src/workers/mvt-worker.ts --bundle --outfile=dist/mvt-worker.js --define:__VERSION__=\\\"$npm_package_version\\\""
42
42
  },
43
43
  "dependencies": {
44
- "@loaders.gl/gis": "4.2.1",
45
- "@loaders.gl/images": "4.2.1",
46
- "@loaders.gl/loader-utils": "4.2.1",
47
- "@loaders.gl/schema": "4.2.1",
44
+ "@loaders.gl/gis": "4.3.0-alpha.1",
45
+ "@loaders.gl/images": "4.3.0-alpha.1",
46
+ "@loaders.gl/loader-utils": "4.3.0-alpha.1",
47
+ "@loaders.gl/schema": "4.3.0-alpha.1",
48
48
  "@math.gl/polygon": "^4.0.0",
49
49
  "pbf": "^3.2.1"
50
50
  },
@@ -54,5 +54,5 @@
54
54
  "peerDependencies": {
55
55
  "@loaders.gl/core": "^4.0.0"
56
56
  },
57
- "gitHead": "b2ea799ff9f233ab8af0a6c3696e28ddf6e3c5a3"
57
+ "gitHead": "568446ea69eb590f0c42d965459b636216cda74e"
58
58
  }
@@ -1,20 +1,26 @@
1
1
  // loaders.gl
2
- // SPDX-License-Identifier: MIT
2
+ // SPDX-License-Identifier: MIT AND ISC
3
3
  // Copyright (c) vis.gl contributors
4
- // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
4
+ // Based on https://github.com/mapbox/geojson-vt under compatible ISC license
5
5
 
6
6
  /* eslint-disable no-console, no-continue */
7
7
 
8
- import type {GeoJSONTile, GeoJSONTileFeature} from './tile';
8
+ import {
9
+ VectorTileSource,
10
+ VectorTileSourceProps,
11
+ TileLoadParameters
12
+ } from '@loaders.gl/loader-utils';
13
+ import {Feature, GeoJSONTable} from '@loaders.gl/schema';
9
14
 
10
- import {convert} from './convert'; // GeoJSON conversion and preprocessing
11
- import {clip} from './clip'; // stripe clipping algorithm
12
- import {wrap} from './wrap'; // date line processing
13
- import {transformTile} from './transform'; // coordinate transformation
14
- import {createTile} from './tile'; // final simplified tile generation
15
+ import type {GeoJSONTile, GeoJSONTileFeature} from './lib/geojsonvt/tile';
16
+ import {convert} from './lib/geojsonvt/convert'; // GeoJSON conversion and preprocessing
17
+ import {clip} from './lib/geojsonvt/clip'; // stripe clipping algorithm
18
+ import {wrap} from './lib/geojsonvt/wrap'; // date line processing
19
+ import {transformTile} from './lib/geojsonvt/transform'; // coordinate transformation
20
+ import {createTile} from './lib/geojsonvt/tile'; // final simplified tile generation
15
21
 
16
22
  /** Options to configure tiling */
17
- export type GeoJSONTilerOptions = {
23
+ export type GeoJSONTileSourceOptions = VectorTileSourceProps & {
18
24
  maxZoom?: number /** max zoom to preserve detail on */;
19
25
  indexMaxZoom?: number /** max zoom in the tile index */;
20
26
  indexMaxPoints?: number /** max number of points per tile in the tile index */;
@@ -27,22 +33,24 @@ export type GeoJSONTilerOptions = {
27
33
  debug?: number /** logging level (0, 1 or 2) */;
28
34
  };
29
35
 
30
- const DEFAULT_OPTIONS: Required<GeoJSONTilerOptions> = {
31
- maxZoom: 14, // max zoom to preserve detail on
32
- indexMaxZoom: 5, // max zoom in the tile index
33
- indexMaxPoints: 100000, // max number of points per tile in the tile index
34
- tolerance: 3, // simplification tolerance (higher means simpler)
35
- extent: 4096, // tile extent
36
- buffer: 64, // tile buffer on each side
37
- lineMetrics: false, // whether to calculate line metrics
38
- // @ts-expect-error Ensures all these required params have defaults
39
- promoteId: undefined, // name of a feature property to be promoted to feature.id
40
- generateId: false, // whether to generate feature ids. Cannot be used with promoteId
41
- debug: 0 // logging level (0, 1 or 2)
42
- };
43
-
44
- export class GeoJSONTiler {
45
- options: Required<GeoJSONTilerOptions>;
36
+ export class GeoJSONTileSource implements VectorTileSource<any> {
37
+ static defaultOptions: Required<GeoJSONTileSourceOptions> = {
38
+ maxZoom: 14, // max zoom to preserve detail on
39
+ indexMaxZoom: 5, // max zoom in the tile index
40
+ indexMaxPoints: 100000, // max number of points per tile in the tile index
41
+ tolerance: 3, // simplification tolerance (higher means simpler)
42
+ extent: 4096, // tile extent
43
+ buffer: 64, // tile buffer on each side
44
+ lineMetrics: false, // whether to calculate line metrics
45
+ // @ts-expect-error
46
+ promoteId: undefined, // name of a feature property to be promoted to feature.id
47
+ generateId: false, // whether to generate feature ids. Cannot be used with promoteId
48
+ debug: 0 // logging level (0, 1 or 2)
49
+ };
50
+
51
+ mimeType = 'application/vnd.mapbox-vector-tile';
52
+
53
+ options: Required<GeoJSONTileSourceOptions>;
46
54
 
47
55
  // tiles and tileCoords are part of the public API
48
56
  tiles: Record<string, GeoJSONTile> = {};
@@ -51,10 +59,22 @@ export class GeoJSONTiler {
51
59
  stats: Record<string, number> = {};
52
60
  total: number = 0;
53
61
 
54
- constructor(data, options?: GeoJSONTilerOptions) {
55
- this.options = {...DEFAULT_OPTIONS, ...options};
56
- options = this.options;
62
+ /** Sync methods can be called: the input data promise has been resolved and initial top-level tiling is done */
63
+ ready: Promise<void>;
64
+
65
+ constructor(data: GeoJSONTable | Promise<GeoJSONTable>, options?: GeoJSONTileSourceOptions) {
66
+ this.options = {...GeoJSONTileSource.defaultOptions, ...options};
67
+ this.getTileData = this.getTileData.bind(this);
68
+ this.ready = this.initializeTilesAsync(data);
69
+ }
70
+
71
+ async initializeTilesAsync(dataPromise: GeoJSONTable | Promise<GeoJSONTable>): Promise<void> {
72
+ const data = await dataPromise;
73
+ this.initializeTilesSync(data);
74
+ }
57
75
 
76
+ initializeTilesSync(data: GeoJSONTable): void {
77
+ const options = this.options;
58
78
  const debug = options.debug;
59
79
 
60
80
  if (debug) console.time('preprocess data');
@@ -96,15 +116,54 @@ export class GeoJSONTiler {
96
116
  }
97
117
  }
98
118
 
119
+ async getMetadata(): Promise<unknown> {
120
+ return {};
121
+ }
122
+
99
123
  /**
100
124
  * Get a tile at the specified index
101
- * @param z
102
- * @param x
103
- * @param y
125
+ * @param tileIndex z, x, y of tile
104
126
  * @returns
105
127
  */
128
+ async getVectorTile(tileIndex: {z: number; x: number; y: number}): Promise<GeoJSONTable | null> {
129
+ await this.ready;
130
+ const table = this.getTileSync(tileIndex);
131
+ return table;
132
+ }
133
+
134
+ async getTile(tileIndex: {z: number; x: number; y: number}): Promise<GeoJSONTable | null> {
135
+ await this.ready;
136
+ return this.getTileSync(tileIndex);
137
+ }
138
+
139
+ async getTileData(tileParams: TileLoadParameters): Promise<unknown | null> {
140
+ const {x, y, z} = tileParams.index;
141
+ return await this.getVectorTile({x, y, z});
142
+ }
143
+
144
+ // Implementation
145
+
146
+ /**
147
+ * Synchronously request a tile
148
+ * @note Application must await `source.ready` before calling sync methods.
149
+ */
150
+ getTileSync(tileIndex: {z: number; x: number; y: number}): GeoJSONTable | null {
151
+ const rawTile = this.getRawTile(tileIndex);
152
+ if (!rawTile) {
153
+ return null;
154
+ }
155
+
156
+ return convertToGeoJSONTable(rawTile, this.options.extent);
157
+ }
158
+
159
+ /**
160
+ * Return geojsonvt-style "half formed" vector tile
161
+ * @note Application must await `source.ready` before calling sync methods.
162
+ */
106
163
  // eslint-disable-next-line complexity, max-statements
107
- getTile(z: number, x: number, y: number): GeoJSONTile | null {
164
+ getRawTile(tileIndex: {z: number; x: number; y: number}): GeoJSONTile | null {
165
+ const {z, y} = tileIndex;
166
+ let {x} = tileIndex;
108
167
  // z = +z;
109
168
  // x = +x;
110
169
  // y = +y;
@@ -278,6 +337,86 @@ export class GeoJSONTiler {
278
337
  }
279
338
  }
280
339
 
281
- function toID(z, x, y) {
340
+ function toID(z, x, y): number {
282
341
  return ((1 << z) * y + x) * 32 + z;
283
342
  }
343
+
344
+ function convertToGeoJSONTable(vtTile: GeoJSONTile, extent: number): GeoJSONTable {
345
+ const features: Feature[] = [];
346
+ for (const rawFeature of vtTile.features) {
347
+ if (!rawFeature || !rawFeature.geometry) {
348
+ continue;
349
+ }
350
+
351
+ let type:
352
+ | 'Point'
353
+ | 'MultiPoint'
354
+ | 'LineString'
355
+ | 'MultiLineString'
356
+ | 'Polygon'
357
+ | 'MultiPolygon';
358
+
359
+ let coordinates: any;
360
+
361
+ // raw geometry
362
+ switch (rawFeature.type) {
363
+ case 1:
364
+ if (rawFeature.geometry.length === 1) {
365
+ type = 'Point';
366
+ coordinates = rawFeature.geometry[0];
367
+ } else {
368
+ type = 'MultiPoint';
369
+ coordinates = rawFeature.geometry;
370
+ }
371
+ break;
372
+ case 2:
373
+ if (rawFeature.geometry.length === 1) {
374
+ type = 'LineString';
375
+ coordinates = rawFeature.geometry[0];
376
+ } else {
377
+ type = 'MultiLineString';
378
+ coordinates = rawFeature.geometry;
379
+ }
380
+ break;
381
+ case 3:
382
+ if (rawFeature.geometry.length > 1) {
383
+ type = 'MultiPolygon';
384
+ coordinates = [rawFeature.geometry];
385
+ } else {
386
+ type = 'Polygon';
387
+ coordinates = rawFeature.geometry;
388
+ }
389
+ break;
390
+ default:
391
+ continue;
392
+ }
393
+
394
+ coordinates = toLngLat(coordinates, extent);
395
+
396
+ const feature: Feature = {
397
+ type: 'Feature',
398
+ geometry: {
399
+ type,
400
+ coordinates
401
+ },
402
+ properties: rawFeature.tags || {}
403
+ };
404
+
405
+ features.push(feature);
406
+ }
407
+
408
+ const table: GeoJSONTable = {
409
+ shape: 'geojson-table',
410
+ type: 'FeatureCollection',
411
+ features
412
+ };
413
+
414
+ return table;
415
+ }
416
+
417
+ function toLngLat(coords: any, extent: number): any {
418
+ if (Array.isArray(coords[0])) {
419
+ return coords.map((c) => toLngLat(c, extent));
420
+ }
421
+ return [coords[0] / extent, coords[1] / extent];
422
+ }
package/src/index.ts CHANGED
@@ -11,7 +11,7 @@ export {TileJSONLoader} from './tilejson-loader';
11
11
 
12
12
  export {MVTSource} from './mvt-source';
13
13
 
14
- // GeoJSONTiler
14
+ // GeoJSONTileSource
15
15
 
16
- export type {GeoJSONTilerOptions} from './lib/geojson-tiler/geojson-tiler';
17
- export {GeoJSONTiler} from './lib/geojson-tiler/geojson-tiler';
16
+ export type {GeoJSONTileSourceOptions} from './geojson-tile-source';
17
+ export {GeoJSONTileSource} from './geojson-tile-source';
@@ -6,7 +6,7 @@
6
6
  /* eslint-disable */
7
7
  // @ts-nocheck
8
8
 
9
- import type {Feature} from '@loaders.gl/schema';
9
+ import type {Feature, FeatureCollection} from '@loaders.gl/schema';
10
10
  import type {GeoJSONTileFeature} from './tile';
11
11
 
12
12
  import {simplify} from './simplify';
@@ -14,7 +14,7 @@ import {createFeature} from './feature';
14
14
 
15
15
  // converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
16
16
 
17
- export function convert(data: Feature, options): GeoJSONTileFeature[] {
17
+ export function convert(data: Feature | FeatureCollection, options): GeoJSONTileFeature[] {
18
18
  const features = [];
19
19
  if (data.type === 'FeatureCollection') {
20
20
  for (let i = 0; i < data.features.length; i++) {
@@ -127,8 +127,8 @@ function addFeature(tile: GeoJSONTile, feature, tolerance: number, options: Crea
127
127
  type === 'Polygon' || type === 'MultiPolygon'
128
128
  ? 3
129
129
  : type === 'LineString' || type === 'MultiLineString'
130
- ? 2
131
- : 1,
130
+ ? 2
131
+ : 1,
132
132
  tags
133
133
  };
134
134
  if (feature.id !== null) {
@@ -2,23 +2,17 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright vis.gl contributors
4
4
 
5
+ import type {FlatFeature, Feature, GeojsonGeometryInfo} from '@loaders.gl/schema';
6
+ import type {GeoJSONTable, BinaryFeatureCollection} from '@loaders.gl/schema';
5
7
  import {flatGeojsonToBinary} from '@loaders.gl/gis';
6
- import type {
7
- FlatFeature,
8
- Feature,
9
- GeojsonGeometryInfo,
10
- BinaryFeatureCollection,
11
- GeoJSONTable
12
- } from '@loaders.gl/schema';
8
+ import {log} from '@loaders.gl/loader-utils';
13
9
  import Protobuf from 'pbf';
14
10
 
15
- import type {MVTMapboxCoordinates, MVTOptions} from '../lib/types';
16
- import type {MVTLoaderOptions} from '../mvt-loader';
11
+ import {VectorTile} from './vector-tile/vector-tile';
12
+ import {VectorTileFeature} from './vector-tile/vector-tile-feature';
17
13
 
18
- import {VectorTile} from './mapbox-vector-tile/vector-tile';
19
- import {BinaryVectorTile} from './binary-vector-tile/vector-tile';
20
- import {BinaryVectorTileFeature} from './binary-vector-tile/vector-tile-feature';
21
- import {VectorTileFeature as VectorTileFeatureMapBox} from './mapbox-vector-tile/vector-tile-feature';
14
+ import type {MVTLoaderOptions} from '../mvt-loader';
15
+ type MVTOptions = Required<MVTLoaderOptions>['mvt'];
22
16
 
23
17
  /**
24
18
  * Parse MVT arrayBuffer and return GeoJSON.
@@ -28,7 +22,7 @@ import {VectorTileFeature as VectorTileFeatureMapBox} from './mapbox-vector-tile
28
22
  * @returns A GeoJSON geometry object or a binary representation
29
23
  */
30
24
  export function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions) {
31
- const mvtOptions = normalizeOptions(options);
25
+ const mvtOptions = checkOptions(options);
32
26
 
33
27
  const shape: string | undefined =
34
28
  options?.gis?.format || options?.mvt?.shape || (options?.shape as string);
@@ -87,7 +81,7 @@ function parseToFlatGeoJson(
87
81
  return [features, geometryInfo];
88
82
  }
89
83
 
90
- const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
84
+ const tile = new VectorTile(new Protobuf(arrayBuffer));
91
85
 
92
86
  const selectedLayers =
93
87
  options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
@@ -99,7 +93,7 @@ function parseToFlatGeoJson(
99
93
  }
100
94
 
101
95
  for (let i = 0; i < vectorTileLayer.length; i++) {
102
- const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
96
+ const vectorTileFeature = vectorTileLayer.getBinaryFeature(i, geometryInfo);
103
97
  const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
104
98
  features.push(decodedFeature);
105
99
  }
@@ -113,7 +107,7 @@ function parseToGeojsonFeatures(arrayBuffer: ArrayBuffer, options: MVTOptions):
113
107
  return [];
114
108
  }
115
109
 
116
- const features: MVTMapboxCoordinates[] = [];
110
+ const features: Feature[] = [];
117
111
  const tile = new VectorTile(new Protobuf(arrayBuffer));
118
112
 
119
113
  const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
@@ -125,33 +119,29 @@ function parseToGeojsonFeatures(arrayBuffer: ArrayBuffer, options: MVTOptions):
125
119
  }
126
120
 
127
121
  for (let i = 0; i < vectorTileLayer.length; i++) {
128
- const vectorTileFeature = vectorTileLayer.feature(i);
122
+ const vectorTileFeature = vectorTileLayer.getGeoJSONFeature(i);
129
123
  const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
130
124
  features.push(decodedFeature);
131
125
  }
132
126
  });
133
127
 
134
- return features as Feature[];
128
+ return features;
135
129
  }
136
130
 
137
- function normalizeOptions(options?: MVTLoaderOptions): MVTOptions {
131
+ /** Check that options are good */
132
+ function checkOptions(options?: MVTLoaderOptions): MVTOptions {
138
133
  if (!options?.mvt) {
139
134
  throw new Error('mvt options required');
140
135
  }
141
136
 
142
- // Validate
143
- const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';
144
- const {tileIndex} = options.mvt;
145
- const hasTileIndex =
146
- tileIndex &&
147
- Number.isFinite(tileIndex.x) &&
148
- Number.isFinite(tileIndex.y) &&
149
- Number.isFinite(tileIndex.z);
150
-
151
- if (wgs84Coordinates && !hasTileIndex) {
137
+ if (options.mvt?.coordinates === 'wgs84' && !options.mvt.tileIndex) {
152
138
  throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
153
139
  }
154
140
 
141
+ if (options.gis) {
142
+ log.warn('MVTLoader: "options.gis" is deprecated, use "options.mvt.shape" instead')();
143
+ }
144
+
155
145
  return options.mvt;
156
146
  }
157
147
 
@@ -161,10 +151,10 @@ function normalizeOptions(options?: MVTLoaderOptions): MVTOptions {
161
151
  * @returns decoded feature
162
152
  */
163
153
  function getDecodedFeature(
164
- feature: VectorTileFeatureMapBox,
154
+ feature: VectorTileFeature,
165
155
  options: MVTOptions,
166
156
  layerName: string
167
- ): MVTMapboxCoordinates {
157
+ ): Feature {
168
158
  const decodedFeature = feature.toGeoJSON(
169
159
  // @ts-expect-error What is going on here?
170
160
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates
@@ -172,6 +162,7 @@ function getDecodedFeature(
172
162
 
173
163
  // Add layer name to GeoJSON properties
174
164
  if (options.layerProperty) {
165
+ decodedFeature.properties ||= {};
175
166
  decodedFeature.properties[options.layerProperty] = layerName;
176
167
  }
177
168
 
@@ -184,7 +175,7 @@ function getDecodedFeature(
184
175
  * @returns decoded binary feature
185
176
  */
186
177
  function getDecodedFeatureBinary(
187
- feature: BinaryVectorTileFeature,
178
+ feature: VectorTileFeature,
188
179
  options: MVTOptions,
189
180
  layerName: string
190
181
  ): FlatFeature {
package/src/lib/types.ts CHANGED
@@ -2,45 +2,7 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright vis.gl contributors
4
4
 
5
- /** For local coordinates, the tileIndex is not required */
6
- type MVTLocalCoordinatesOptions = {
7
- /**
8
- * When set to `local`, the parser will return a flat array of GeoJSON objects with local coordinates decoded from tile origin.
9
- */
10
- coordinates: 'local';
11
- tileIndex: null;
12
- };
13
-
14
- /** In WGS84 coordinates, the tileIndex is required */
15
- type MVTWgs84CoordinatesOptions = {
16
- /**
17
- * 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.
18
- */
19
- coordinates?: 'wgs84';
20
-
21
- /**
22
- * Mandatory with `wgs84` coordinates option. An object containing tile index values (`x`, `y`,
23
- * `z`) to reproject features' coordinates into WGS84.
24
- */
25
- tileIndex?: {x: number; y: number; z: number};
26
- };
27
-
28
- export type MVTOptions = (MVTLocalCoordinatesOptions | MVTWgs84CoordinatesOptions) & {
29
- shape?: 'geojson-table' | 'columnar-table' | 'geojson' | 'binary' | 'binary-geometry';
30
- /**
31
- * When non-`null`, the layer name of each feature is added to
32
- * `feature.properties[layerProperty]`. (A `feature.properties` object is created if the feature
33
- * has no existing properties). If set to `null`, a layer name property will not be added.
34
- */
35
- layerProperty?: string | number;
36
-
37
- /**
38
- * Optional list of layer names. If not `null`, only features belonging to the named layers will
39
- * be included in the output. If `null`, features from all layers are returned.
40
- */
41
- layers?: string[];
42
- };
43
-
5
+ /** TODO where is this used? */
44
6
  export type MVTMapboxGeometry = {
45
7
  type?: string;
46
8
  id?: number;
@@ -2,10 +2,99 @@
2
2
  // SPDX-License-Identifier: MIT
3
3
  // Copyright vis.gl contributors
4
4
 
5
- import Protobuf from 'pbf';
6
5
  import {getPolygonSignedArea} from '@math.gl/polygon';
7
6
  import {FlatIndexedGeometry, FlatPolygon} from '@loaders.gl/schema';
8
- import {BinaryVectorTileFeature} from '../lib/binary-vector-tile/vector-tile-feature';
7
+
8
+ /**
9
+ *
10
+ * @param ring
11
+ * @returns sum
12
+ */
13
+ export function signedArea(ring: number[][]) {
14
+ let sum = 0;
15
+ for (let i = 0, j = ring.length - 1, p1: number[], p2: number[]; i < ring.length; j = i++) {
16
+ p1 = ring[i];
17
+ p2 = ring[j];
18
+ sum += (p2[0] - p1[0]) * (p1[1] + p2[1]);
19
+ }
20
+ return sum;
21
+ }
22
+
23
+ /**
24
+ * Projects local tile coordinates to lngLat in place.
25
+ * @param points
26
+ * @param tileIndex
27
+ */
28
+ export function projectTileCoordinatesToLngLat(
29
+ points: number[][],
30
+ tileIndex: {x: number; y: number; z: number},
31
+ extent: number
32
+ ) {
33
+ const {x, y, z} = tileIndex;
34
+ const size = extent * Math.pow(2, z);
35
+ const x0 = extent * x;
36
+ const y0 = extent * y;
37
+
38
+ for (const p of points) {
39
+ p[0] = ((p[0] + x0) * 360) / size - 180;
40
+ const y2 = 180 - ((p[1] + y0) * 360) / size;
41
+ p[1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
42
+ }
43
+ }
44
+
45
+ /**
46
+ *
47
+ * @param data
48
+ * @param x0
49
+ * @param y0
50
+ * @param size
51
+ */
52
+ export function projectToLngLatFlat(
53
+ data: number[],
54
+ tileIndex: {x: number; y: number; z: number},
55
+ extent: number
56
+ ): void {
57
+ const {x, y, z} = tileIndex;
58
+ const size = extent * Math.pow(2, z);
59
+ const x0 = extent * x;
60
+ const y0 = extent * y;
61
+
62
+ for (let j = 0, jl = data.length; j < jl; j += 2) {
63
+ data[j] = ((data[j] + x0) * 360) / size - 180;
64
+ const y2 = 180 - ((data[j + 1] + y0) * 360) / size;
65
+ data[j + 1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Classifies an array of rings into polygons with outer rings and holes
71
+ * @param rings
72
+ * @returns polygons
73
+ */
74
+ export function classifyRings(rings: number[][][]): number[][][][] {
75
+ const len = rings.length;
76
+
77
+ if (len <= 1) return [rings];
78
+
79
+ const polygons: number[][][][] = [];
80
+ let polygon: number[][][] | undefined;
81
+ let ccw: boolean | undefined;
82
+
83
+ for (let i = 0; i < len; i++) {
84
+ const area = signedArea(rings[i]);
85
+ if (area === 0) continue; // eslint-disable-line no-continue
86
+
87
+ if (ccw === undefined) ccw = area < 0;
88
+
89
+ if (ccw === area < 0) {
90
+ if (polygon) polygons.push(polygon);
91
+ polygon = [rings[i]];
92
+ } else if (polygon) polygon.push(rings[i]);
93
+ }
94
+ if (polygon) polygons.push(polygon);
95
+
96
+ return polygons;
97
+ }
9
98
 
10
99
  /**
11
100
  * Classifies an array of rings into polygons with outer rings and holes
@@ -17,7 +106,7 @@ import {BinaryVectorTileFeature} from '../lib/binary-vector-tile/vector-tile-fea
17
106
  * @returns object
18
107
  */
19
108
  // eslint-disable-next-line max-statements
20
- export function classifyRings(geom: FlatIndexedGeometry): FlatPolygon {
109
+ export function classifyRingsFlat(geom: FlatIndexedGeometry): FlatPolygon {
21
110
  const len = geom.indices.length;
22
111
  const type = 'Polygon';
23
112
 
@@ -78,48 +167,3 @@ export function classifyRings(geom: FlatIndexedGeometry): FlatPolygon {
78
167
 
79
168
  return {type, areas, indices: polygons, data: geom.data};
80
169
  }
81
-
82
- /**
83
- *
84
- * @param data
85
- * @param x0
86
- * @param y0
87
- * @param size
88
- */
89
- export function project(data: number[], x0: number, y0: number, size: number): void {
90
- for (let j = 0, jl = data.length; j < jl; j += 2) {
91
- data[j] = ((data[j] + x0) * 360) / size - 180;
92
- const y2 = 180 - ((data[j + 1] + y0) * 360) / size;
93
- data[j + 1] = (360 / Math.PI) * Math.atan(Math.exp((y2 * Math.PI) / 180)) - 90;
94
- }
95
- }
96
-
97
- /**
98
- * All code below is unchanged from the original Mapbox implemenation
99
- *
100
- * @param tag
101
- * @param feature
102
- * @param pbf
103
- */
104
- export function readFeature(tag: number, feature?: BinaryVectorTileFeature, pbf?: Protobuf): void {
105
- if (feature && pbf) {
106
- if (tag === 1) feature.id = pbf.readVarint();
107
- else if (tag === 2) readTag(pbf, feature);
108
- else if (tag === 3) feature.type = pbf.readVarint();
109
- else if (tag === 4) feature._geometry = pbf.pos;
110
- }
111
- }
112
-
113
- /**
114
- * @param pbf
115
- * @param feature
116
- */
117
- export function readTag(pbf: Protobuf, feature: BinaryVectorTileFeature): void {
118
- const end = pbf.readVarint() + pbf.pos;
119
-
120
- while (pbf.pos < end) {
121
- const key = feature._keys[pbf.readVarint()];
122
- const value = feature._values[pbf.readVarint()];
123
- feature.properties[key] = value;
124
- }
125
- }