@loaders.gl/mvt 4.2.0-alpha.3 → 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.
Files changed (93) hide show
  1. package/dist/dist.dev.js +274 -252
  2. package/dist/dist.min.js +14 -0
  3. package/dist/helpers/binary-util-functions.d.ts +1 -1
  4. package/dist/helpers/binary-util-functions.d.ts.map +1 -1
  5. package/dist/helpers/binary-util-functions.js +99 -61
  6. package/dist/helpers/mapbox-util-functions.d.ts +2 -2
  7. package/dist/helpers/mapbox-util-functions.d.ts.map +1 -1
  8. package/dist/helpers/mapbox-util-functions.js +67 -33
  9. package/dist/index.cjs +37 -61
  10. package/dist/index.cjs.map +7 -0
  11. package/dist/index.d.ts +8 -8
  12. package/dist/index.d.ts.map +1 -1
  13. package/dist/index.js +3 -1
  14. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +1 -1
  15. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -1
  16. package/dist/lib/binary-vector-tile/vector-tile-feature.js +135 -118
  17. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +1 -1
  18. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -1
  19. package/dist/lib/binary-vector-tile/vector-tile-layer.js +77 -38
  20. package/dist/lib/binary-vector-tile/vector-tile.d.ts +1 -1
  21. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -1
  22. package/dist/lib/binary-vector-tile/vector-tile.js +17 -12
  23. package/dist/lib/geojson-tiler/clip.d.ts +1 -1
  24. package/dist/lib/geojson-tiler/clip.d.ts.map +1 -1
  25. package/dist/lib/geojson-tiler/clip.js +177 -142
  26. package/dist/lib/geojson-tiler/convert.d.ts +1 -1
  27. package/dist/lib/geojson-tiler/convert.d.ts.map +1 -1
  28. package/dist/lib/geojson-tiler/convert.js +116 -100
  29. package/dist/lib/geojson-tiler/feature.d.ts +1 -1
  30. package/dist/lib/geojson-tiler/feature.d.ts.map +1 -1
  31. package/dist/lib/geojson-tiler/feature.js +38 -29
  32. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +1 -1
  33. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -1
  34. package/dist/lib/geojson-tiler/geojson-tiler.js +197 -159
  35. package/dist/lib/geojson-tiler/simplify.js +60 -40
  36. package/dist/lib/geojson-tiler/tile.js +108 -88
  37. package/dist/lib/geojson-tiler/transform.d.ts +1 -1
  38. package/dist/lib/geojson-tiler/transform.d.ts.map +1 -1
  39. package/dist/lib/geojson-tiler/transform.js +35 -26
  40. package/dist/lib/geojson-tiler/wrap.d.ts +1 -1
  41. package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -1
  42. package/dist/lib/geojson-tiler/wrap.js +71 -53
  43. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +1 -1
  44. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -1
  45. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +156 -151
  46. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +1 -1
  47. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -1
  48. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +75 -38
  49. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +1 -1
  50. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -1
  51. package/dist/lib/mapbox-vector-tile/vector-tile.js +17 -12
  52. package/dist/lib/parse-mvt.d.ts +1 -1
  53. package/dist/lib/parse-mvt.d.ts.map +1 -1
  54. package/dist/lib/parse-mvt.js +142 -115
  55. package/dist/lib/parse-tilejson.js +234 -176
  56. package/dist/lib/types.js +0 -1
  57. package/dist/mvt-loader.d.ts +1 -1
  58. package/dist/mvt-loader.d.ts.map +1 -1
  59. package/dist/mvt-loader.js +42 -22
  60. package/dist/mvt-source.js +148 -139
  61. package/dist/mvt-worker.js +1 -1
  62. package/dist/tilejson-loader.d.ts +1 -1
  63. package/dist/tilejson-loader.d.ts.map +1 -1
  64. package/dist/tilejson-loader.js +32 -31
  65. package/dist/workers/mvt-worker.js +0 -1
  66. package/package.json +12 -8
  67. package/dist/helpers/binary-util-functions.js.map +0 -1
  68. package/dist/helpers/mapbox-util-functions.js.map +0 -1
  69. package/dist/index.js.map +0 -1
  70. package/dist/lib/binary-vector-tile/LICENSE.txt +0 -31
  71. package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +0 -1
  72. package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +0 -1
  73. package/dist/lib/binary-vector-tile/vector-tile.js.map +0 -1
  74. package/dist/lib/geojson-tiler/LICENSE +0 -19
  75. package/dist/lib/geojson-tiler/clip.js.map +0 -1
  76. package/dist/lib/geojson-tiler/convert.js.map +0 -1
  77. package/dist/lib/geojson-tiler/feature.js.map +0 -1
  78. package/dist/lib/geojson-tiler/geojson-tiler.js.map +0 -1
  79. package/dist/lib/geojson-tiler/simplify.js.map +0 -1
  80. package/dist/lib/geojson-tiler/tile.js.map +0 -1
  81. package/dist/lib/geojson-tiler/transform.js.map +0 -1
  82. package/dist/lib/geojson-tiler/wrap.js.map +0 -1
  83. package/dist/lib/mapbox-vector-tile/LICENSE.txt +0 -31
  84. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js.map +0 -1
  85. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js.map +0 -1
  86. package/dist/lib/mapbox-vector-tile/vector-tile.js.map +0 -1
  87. package/dist/lib/parse-mvt.js.map +0 -1
  88. package/dist/lib/parse-tilejson.js.map +0 -1
  89. package/dist/lib/types.js.map +0 -1
  90. package/dist/mvt-loader.js.map +0 -1
  91. package/dist/mvt-source.js.map +0 -1
  92. package/dist/tilejson-loader.js.map +0 -1
  93. package/dist/workers/mvt-worker.js.map +0 -1
@@ -1,35 +1,44 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
1
5
  export function createFeature(id, type, geom, tags) {
2
- const feature = {
3
- id: id == null ? null : id,
4
- type,
5
- geometry: geom,
6
- tags,
7
- minX: Infinity,
8
- minY: Infinity,
9
- maxX: -Infinity,
10
- maxY: -Infinity
11
- };
12
- if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
13
- calcLineBBox(feature, geom);
14
- } else if (type === 'Polygon') {
15
- calcLineBBox(feature, geom[0]);
16
- } else if (type === 'MultiLineString') {
17
- for (const line of geom) {
18
- calcLineBBox(feature, line);
6
+ const feature = {
7
+ // eslint-disable-next-line
8
+ id: id == null ? null : id,
9
+ type,
10
+ geometry: geom,
11
+ tags,
12
+ minX: Infinity,
13
+ minY: Infinity,
14
+ maxX: -Infinity,
15
+ maxY: -Infinity
16
+ };
17
+ if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
18
+ calcLineBBox(feature, geom);
19
19
  }
20
- } else if (type === 'MultiPolygon') {
21
- for (const polygon of geom) {
22
- calcLineBBox(feature, polygon[0]);
20
+ else if (type === 'Polygon') {
21
+ // the outer ring (ie [0]) contains all inner rings
22
+ calcLineBBox(feature, geom[0]);
23
23
  }
24
- }
25
- return feature;
24
+ else if (type === 'MultiLineString') {
25
+ for (const line of geom) {
26
+ calcLineBBox(feature, line);
27
+ }
28
+ }
29
+ else if (type === 'MultiPolygon') {
30
+ for (const polygon of geom) {
31
+ // the outer ring (ie [0]) contains all inner rings
32
+ calcLineBBox(feature, polygon[0]);
33
+ }
34
+ }
35
+ return feature;
26
36
  }
27
37
  function calcLineBBox(feature, geom) {
28
- for (let i = 0; i < geom.length; i += 3) {
29
- feature.minX = Math.min(feature.minX, geom[i]);
30
- feature.minY = Math.min(feature.minY, geom[i + 1]);
31
- feature.maxX = Math.max(feature.maxX, geom[i]);
32
- feature.maxY = Math.max(feature.maxY, geom[i + 1]);
33
- }
38
+ for (let i = 0; i < geom.length; i += 3) {
39
+ feature.minX = Math.min(feature.minX, geom[i]);
40
+ feature.minY = Math.min(feature.minY, geom[i + 1]);
41
+ feature.maxX = Math.max(feature.maxX, geom[i]);
42
+ feature.maxY = Math.max(feature.maxY, geom[i + 1]);
43
+ }
34
44
  }
35
- //# sourceMappingURL=feature.js.map
@@ -1,4 +1,4 @@
1
- import type { GeoJSONTile, GeoJSONTileFeature } from './tile';
1
+ import type { GeoJSONTile, GeoJSONTileFeature } from "./tile.js";
2
2
  /** Options to configure tiling */
3
3
  export type GeoJSONTilerOptions = {
4
4
  maxZoom?: number /** max zoom to preserve detail on */;
@@ -1 +1 @@
1
- {"version":3,"file":"geojson-tiler.d.ts","sourceRoot":"","sources":["../../../src/lib/geojson-tiler/geojson-tiler.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,WAAW,EAAE,kBAAkB,EAAC,MAAM,QAAQ,CAAC;AAQ5D,kCAAkC;AAClC,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC,qCAAqC,CAAC;IACvD,YAAY,CAAC,EAAE,MAAM,CAAC,iCAAiC,CAAC;IACxD,cAAc,CAAC,EAAE,MAAM,CAAC,sDAAsD,CAAC;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAC,sDAAsD,CAAC;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC,kBAAkB,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC,+BAA+B,CAAC;IAChD,WAAW,CAAC,EAAE,OAAO,CAAC,wCAAwC,CAAC;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAC,8DAA8D,CAAC;IAClF,UAAU,CAAC,EAAE,OAAO,CAAC,qEAAqE,CAAC;IAC3F,KAAK,CAAC,EAAE,MAAM,CAAC,gCAAgC,CAAC;CACjD,CAAC;AAgBF,qBAAa,YAAY;IACvB,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAGvC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAM;IACxC,UAAU,EAAE;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAC,EAAE,CAAM;IAErD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IACnC,KAAK,EAAE,MAAM,CAAK;gBAEN,IAAI,KAAA,EAAE,OAAO,CAAC,EAAE,mBAAmB;IA6C/C;;;;;;OAMG;IAEH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;IAkD5D;;;;;;;OAOG;IAEH,SAAS,CACP,QAAQ,EAAE,kBAAkB,EAAE,EAC9B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,EAAE,CAAC,EAAE,MAAM,EACX,EAAE,CAAC,EAAE,MAAM,EACX,EAAE,CAAC,EAAE,MAAM,GACV,IAAI;CAyGR"}
1
+ {"version":3,"file":"geojson-tiler.d.ts","sourceRoot":"","sources":["../../../src/lib/geojson-tiler/geojson-tiler.ts"],"names":[],"mappings":"AAOA,OAAO,KAAK,EAAC,WAAW,EAAE,kBAAkB,EAAC,kBAAe;AAQ5D,kCAAkC;AAClC,MAAM,MAAM,mBAAmB,GAAG;IAChC,OAAO,CAAC,EAAE,MAAM,CAAC,qCAAqC,CAAC;IACvD,YAAY,CAAC,EAAE,MAAM,CAAC,iCAAiC,CAAC;IACxD,cAAc,CAAC,EAAE,MAAM,CAAC,sDAAsD,CAAC;IAC/E,SAAS,CAAC,EAAE,MAAM,CAAC,sDAAsD,CAAC;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC,kBAAkB,CAAC;IACnC,MAAM,CAAC,EAAE,MAAM,CAAC,+BAA+B,CAAC;IAChD,WAAW,CAAC,EAAE,OAAO,CAAC,wCAAwC,CAAC;IAC/D,SAAS,CAAC,EAAE,MAAM,CAAC,8DAA8D,CAAC;IAClF,UAAU,CAAC,EAAE,OAAO,CAAC,qEAAqE,CAAC;IAC3F,KAAK,CAAC,EAAE,MAAM,CAAC,gCAAgC,CAAC;CACjD,CAAC;AAgBF,qBAAa,YAAY;IACvB,OAAO,EAAE,QAAQ,CAAC,mBAAmB,CAAC,CAAC;IAGvC,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,WAAW,CAAC,CAAM;IACxC,UAAU,EAAE;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAC;QAAC,CAAC,EAAE,MAAM,CAAA;KAAC,EAAE,CAAM;IAErD,KAAK,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAM;IACnC,KAAK,EAAE,MAAM,CAAK;gBAEN,IAAI,KAAA,EAAE,OAAO,CAAC,EAAE,mBAAmB;IA6C/C;;;;;;OAMG;IAEH,OAAO,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,WAAW,GAAG,IAAI;IAkD5D;;;;;;;OAOG;IAEH,SAAS,CACP,QAAQ,EAAE,kBAAkB,EAAE,EAC9B,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,CAAC,EAAE,MAAM,EACT,EAAE,CAAC,EAAE,MAAM,EACX,EAAE,CAAC,EAAE,MAAM,EACX,EAAE,CAAC,EAAE,MAAM,GACV,IAAI;CAyGR"}
@@ -1,170 +1,208 @@
1
- import { convert } from "./convert.js";
2
- import { clip } from "./clip.js";
3
- import { wrap } from "./wrap.js";
4
- import { transformTile } from "./transform.js";
5
- import { createTile } from "./tile.js";
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
5
+ import { convert } from "./convert.js"; // GeoJSON conversion and preprocessing
6
+ import { clip } from "./clip.js"; // stripe clipping algorithm
7
+ import { wrap } from "./wrap.js"; // date line processing
8
+ import { transformTile } from "./transform.js"; // coordinate transformation
9
+ import { createTile } from "./tile.js"; // final simplified tile generation
6
10
  const DEFAULT_OPTIONS = {
7
- maxZoom: 14,
8
- indexMaxZoom: 5,
9
- indexMaxPoints: 100000,
10
- tolerance: 3,
11
- extent: 4096,
12
- buffer: 64,
13
- lineMetrics: false,
14
- promoteId: undefined,
15
- generateId: false,
16
- debug: 0
11
+ maxZoom: 14, // max zoom to preserve detail on
12
+ indexMaxZoom: 5, // max zoom in the tile index
13
+ indexMaxPoints: 100000, // max number of points per tile in the tile index
14
+ tolerance: 3, // simplification tolerance (higher means simpler)
15
+ extent: 4096, // tile extent
16
+ buffer: 64, // tile buffer on each side
17
+ lineMetrics: false, // whether to calculate line metrics
18
+ // @ts-expect-error Ensures all these required params have defaults
19
+ promoteId: undefined, // name of a feature property to be promoted to feature.id
20
+ generateId: false, // whether to generate feature ids. Cannot be used with promoteId
21
+ debug: 0 // logging level (0, 1 or 2)
17
22
  };
18
23
  export class GeoJSONTiler {
19
- constructor(data, options) {
20
- this.options = void 0;
21
- this.tiles = {};
22
- this.tileCoords = [];
23
- this.stats = {};
24
- this.total = 0;
25
- this.options = {
26
- ...DEFAULT_OPTIONS,
27
- ...options
28
- };
29
- options = this.options;
30
- const debug = options.debug;
31
- if (debug) console.time('preprocess data');
32
- if (this.options.maxZoom < 0 || this.options.maxZoom > 24) {
33
- throw new Error('maxZoom should be in the 0-24 range');
34
- }
35
- if (options.promoteId && this.options.generateId) {
36
- throw new Error('promoteId and generateId cannot be used together.');
37
- }
38
- let features = convert(data, options);
39
- if (debug) {
40
- console.timeEnd('preprocess data');
41
- console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
42
- console.time('generate tiles');
43
- }
44
- features = wrap(features, this.options);
45
- if (features.length) {
46
- this.splitTile(features, 0, 0, 0);
47
- }
48
- if (debug) {
49
- if (features.length) {
50
- console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
51
- }
52
- console.timeEnd('generate tiles');
53
- console.log('tiles generated:', this.total, JSON.stringify(this.stats));
54
- }
55
- }
56
- getTile(z, x, y) {
57
- const {
58
- extent,
59
- debug
60
- } = this.options;
61
- if (z < 0 || z > 24) {
62
- return null;
63
- }
64
- const z2 = 1 << z;
65
- x = x + z2 & z2 - 1;
66
- const id = toID(z, x, y);
67
- if (this.tiles[id]) {
68
- return transformTile(this.tiles[id], extent);
69
- }
70
- if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
71
- let z0 = z;
72
- let x0 = x;
73
- let y0 = y;
74
- let parent;
75
- while (!parent && z0 > 0) {
76
- z0--;
77
- x0 = x0 >> 1;
78
- y0 = y0 >> 1;
79
- parent = this.tiles[toID(z0, x0, y0)];
80
- }
81
- if (!parent || !parent.source) {
82
- return null;
83
- }
84
- if (debug > 1) {
85
- console.log('found parent tile z%d-%d-%d', z0, x0, y0);
86
- console.time('drilling down');
87
- }
88
- this.splitTile(parent.source, z0, x0, y0, z, x, y);
89
- if (debug > 1) {
90
- console.timeEnd('drilling down');
24
+ constructor(data, options) {
25
+ // tiles and tileCoords are part of the public API
26
+ this.tiles = {};
27
+ this.tileCoords = [];
28
+ this.stats = {};
29
+ this.total = 0;
30
+ this.options = { ...DEFAULT_OPTIONS, ...options };
31
+ options = this.options;
32
+ const debug = options.debug;
33
+ if (debug)
34
+ console.time('preprocess data');
35
+ if (this.options.maxZoom < 0 || this.options.maxZoom > 24) {
36
+ throw new Error('maxZoom should be in the 0-24 range');
37
+ }
38
+ if (options.promoteId && this.options.generateId) {
39
+ throw new Error('promoteId and generateId cannot be used together.');
40
+ }
41
+ // projects and adds simplification info
42
+ let features = convert(data, options);
43
+ if (debug) {
44
+ console.timeEnd('preprocess data');
45
+ console.log('index: maxZoom: %d, maxPoints: %d', options.indexMaxZoom, options.indexMaxPoints);
46
+ console.time('generate tiles');
47
+ }
48
+ // wraps features (ie extreme west and extreme east)
49
+ features = wrap(features, this.options);
50
+ // start slicing from the top tile down
51
+ if (features.length) {
52
+ this.splitTile(features, 0, 0, 0);
53
+ }
54
+ if (debug) {
55
+ if (features.length) {
56
+ console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
57
+ }
58
+ console.timeEnd('generate tiles');
59
+ console.log('tiles generated:', this.total, JSON.stringify(this.stats));
60
+ }
91
61
  }
92
- return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
93
- }
94
- splitTile(features, z, x, y, cz, cx, cy) {
95
- const stack = [features, z, x, y];
96
- const options = this.options;
97
- const debug = options.debug;
98
- while (stack.length) {
99
- y = stack.pop();
100
- x = stack.pop();
101
- z = stack.pop();
102
- features = stack.pop();
103
- const z2 = 1 << z;
104
- const id = toID(z, x, y);
105
- let tile = this.tiles[id];
106
- if (!tile) {
62
+ /**
63
+ * Get a tile at the specified index
64
+ * @param z
65
+ * @param x
66
+ * @param y
67
+ * @returns
68
+ */
69
+ // eslint-disable-next-line complexity, max-statements
70
+ getTile(z, x, y) {
71
+ // z = +z;
72
+ // x = +x;
73
+ // y = +y;
74
+ const { extent, debug } = this.options;
75
+ if (z < 0 || z > 24) {
76
+ return null;
77
+ }
78
+ const z2 = 1 << z;
79
+ x = (x + z2) & (z2 - 1); // wrap tile x coordinate
80
+ const id = toID(z, x, y);
81
+ if (this.tiles[id]) {
82
+ return transformTile(this.tiles[id], extent);
83
+ }
84
+ if (debug > 1)
85
+ console.log('drilling down to z%d-%d-%d', z, x, y);
86
+ let z0 = z;
87
+ let x0 = x;
88
+ let y0 = y;
89
+ let parent;
90
+ while (!parent && z0 > 0) {
91
+ z0--;
92
+ x0 = x0 >> 1;
93
+ y0 = y0 >> 1;
94
+ parent = this.tiles[toID(z0, x0, y0)];
95
+ }
96
+ if (!parent || !parent.source) {
97
+ return null;
98
+ }
99
+ // if we found a parent tile containing the original geometry, we can drill down from it
107
100
  if (debug > 1) {
108
- console.time('creation');
101
+ console.log('found parent tile z%d-%d-%d', z0, x0, y0);
102
+ console.time('drilling down');
109
103
  }
110
- tile = this.tiles[id] = createTile(features, z, x, y, options);
111
- this.tileCoords.push({
112
- z,
113
- x,
114
- y
115
- });
116
- if (debug) {
117
- if (debug > 1) {
118
- console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
119
- console.timeEnd('creation');
120
- }
121
- const key = `z${z}`;
122
- this.stats[key] = (this.stats[key] || 0) + 1;
123
- this.total++;
104
+ this.splitTile(parent.source, z0, x0, y0, z, x, y);
105
+ if (debug > 1) {
106
+ console.timeEnd('drilling down');
107
+ }
108
+ return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
109
+ }
110
+ /**
111
+ * splits features from a parent tile to sub-tiles.
112
+ * @param z, x, and y are the coordinates of the parent tile
113
+ * @param cz, cx, and cy are the coordinates of the target tile
114
+ *
115
+ * If no target tile is specified, splitting stops when we reach the maximum
116
+ * zoom or the number of points is low as specified in the options.
117
+ */
118
+ // eslint-disable-next-line max-params, max-statements, complexity
119
+ splitTile(features, z, x, y, cz, cx, cy) {
120
+ const stack = [features, z, x, y];
121
+ const options = this.options;
122
+ const debug = options.debug;
123
+ // avoid recursion by using a processing queue
124
+ while (stack.length) {
125
+ y = stack.pop();
126
+ x = stack.pop();
127
+ z = stack.pop();
128
+ features = stack.pop();
129
+ const z2 = 1 << z;
130
+ const id = toID(z, x, y);
131
+ let tile = this.tiles[id];
132
+ if (!tile) {
133
+ if (debug > 1) {
134
+ console.time('creation');
135
+ }
136
+ tile = this.tiles[id] = createTile(features, z, x, y, options);
137
+ this.tileCoords.push({ z, x, y });
138
+ if (debug) {
139
+ if (debug > 1) {
140
+ console.log('tile z%d-%d-%d (features: %d, points: %d, simplified: %d)', z, x, y, tile.numFeatures, tile.numPoints, tile.numSimplified);
141
+ console.timeEnd('creation');
142
+ }
143
+ const key = `z${z}`;
144
+ this.stats[key] = (this.stats[key] || 0) + 1;
145
+ this.total++;
146
+ }
147
+ }
148
+ // save reference to original geometry in tile so that we can drill down later if we stop now
149
+ tile.source = features;
150
+ // if it's the first-pass tiling
151
+ if (cz === undefined) {
152
+ // stop tiling if we reached max zoom, or if the tile is too simple
153
+ if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints)
154
+ continue;
155
+ // if a drilldown to a specific tile
156
+ }
157
+ else if (z === options.maxZoom || z === cz) {
158
+ // stop tiling if we reached base zoom or our target tile zoom
159
+ continue;
160
+ }
161
+ else if (cz !== undefined) {
162
+ // stop tiling if it's not an ancestor of the target tile
163
+ const zoomSteps = cz - z;
164
+ // @ts-expect-error TODO fix the types of cx cy
165
+ if (x !== cx >> zoomSteps || y !== cy >> zoomSteps)
166
+ continue;
167
+ }
168
+ // if we slice further down, no need to keep source geometry
169
+ tile.source = null;
170
+ if (features.length === 0)
171
+ continue;
172
+ if (debug > 1)
173
+ console.time('clipping');
174
+ // values we'll use for clipping
175
+ const k1 = (0.5 * options.buffer) / options.extent;
176
+ const k2 = 0.5 - k1;
177
+ const k3 = 0.5 + k1;
178
+ const k4 = 1 + k1;
179
+ let tl = null;
180
+ let bl = null;
181
+ let tr = null;
182
+ let br = null;
183
+ let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
184
+ let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
185
+ // @ts-expect-error - unclear why this is needed?
186
+ features = null;
187
+ if (left) {
188
+ tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
189
+ bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
190
+ left = null;
191
+ }
192
+ if (right) {
193
+ tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
194
+ br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
195
+ right = null;
196
+ }
197
+ if (debug > 1)
198
+ console.timeEnd('clipping');
199
+ stack.push(tl || [], z + 1, x * 2, y * 2);
200
+ stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
201
+ stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
202
+ stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
124
203
  }
125
- }
126
- tile.source = features;
127
- if (cz === undefined) {
128
- if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
129
- } else if (z === options.maxZoom || z === cz) {
130
- continue;
131
- } else if (cz !== undefined) {
132
- const zoomSteps = cz - z;
133
- if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
134
- }
135
- tile.source = null;
136
- if (features.length === 0) continue;
137
- if (debug > 1) console.time('clipping');
138
- const k1 = 0.5 * options.buffer / options.extent;
139
- const k2 = 0.5 - k1;
140
- const k3 = 0.5 + k1;
141
- const k4 = 1 + k1;
142
- let tl = null;
143
- let bl = null;
144
- let tr = null;
145
- let br = null;
146
- let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
147
- let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
148
- features = null;
149
- if (left) {
150
- tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
151
- bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
152
- left = null;
153
- }
154
- if (right) {
155
- tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
156
- br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
157
- right = null;
158
- }
159
- if (debug > 1) console.timeEnd('clipping');
160
- stack.push(tl || [], z + 1, x * 2, y * 2);
161
- stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
162
- stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
163
- stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
164
204
  }
165
- }
166
205
  }
167
206
  function toID(z, x, y) {
168
- return ((1 << z) * y + x) * 32 + z;
207
+ return ((1 << z) * y + x) * 32 + z;
169
208
  }
170
- //# sourceMappingURL=geojson-tiler.js.map
@@ -1,46 +1,66 @@
1
+ // loaders.gl
2
+ // SPDX-License-Identifier: MIT
3
+ // Copyright (c) vis.gl contributors
4
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
5
+ /**
6
+ * Calculate simplification data using optimized Douglas-Peucker algorithm
7
+ *
8
+ * @param coords contiguous list of coordinates
9
+ * @param first first coord to simplify
10
+ * @param last last coord to simplify
11
+ * @param sqTolerance tolerance (square distance)
12
+ */
1
13
  export function simplify(coords, first, last, sqTolerance) {
2
- let maxSqDist = sqTolerance;
3
- const mid = last - first >> 1;
4
- let minPosToMid = last - first;
5
- let index;
6
- const ax = coords[first];
7
- const ay = coords[first + 1];
8
- const bx = coords[last];
9
- const by = coords[last + 1];
10
- for (let i = first + 3; i < last; i += 3) {
11
- const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
12
- if (d > maxSqDist) {
13
- index = i;
14
- maxSqDist = d;
15
- } else if (d === maxSqDist) {
16
- const posToMid = Math.abs(i - mid);
17
- if (posToMid < minPosToMid) {
18
- index = i;
19
- minPosToMid = posToMid;
20
- }
14
+ let maxSqDist = sqTolerance;
15
+ const mid = (last - first) >> 1;
16
+ let minPosToMid = last - first;
17
+ let index;
18
+ const ax = coords[first];
19
+ const ay = coords[first + 1];
20
+ const bx = coords[last];
21
+ const by = coords[last + 1];
22
+ for (let i = first + 3; i < last; i += 3) {
23
+ const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
24
+ if (d > maxSqDist) {
25
+ index = i;
26
+ maxSqDist = d;
27
+ }
28
+ else if (d === maxSqDist) {
29
+ // a workaround to ensure we choose a pivot close to the middle of the list,
30
+ // reducing recursion depth, for certain degenerate inputs
31
+ // https://github.com/mapbox/geojson-vt/issues/104
32
+ const posToMid = Math.abs(i - mid);
33
+ if (posToMid < minPosToMid) {
34
+ index = i;
35
+ minPosToMid = posToMid;
36
+ }
37
+ }
38
+ }
39
+ if (maxSqDist > sqTolerance) {
40
+ if (index - first > 3)
41
+ simplify(coords, first, index, sqTolerance);
42
+ coords[index + 2] = maxSqDist;
43
+ if (last - index > 3)
44
+ simplify(coords, index, last, sqTolerance);
21
45
  }
22
- }
23
- if (maxSqDist > sqTolerance) {
24
- if (index - first > 3) simplify(coords, first, index, sqTolerance);
25
- coords[index + 2] = maxSqDist;
26
- if (last - index > 3) simplify(coords, index, last, sqTolerance);
27
- }
28
46
  }
47
+ /** square distance from a point to a segment */
48
+ // eslint-disable-next-line max-params
29
49
  function getSqSegDist(px, py, x, y, bx, by) {
30
- let dx = bx - x;
31
- let dy = by - y;
32
- if (dx !== 0 || dy !== 0) {
33
- const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
34
- if (t > 1) {
35
- x = bx;
36
- y = by;
37
- } else if (t > 0) {
38
- x += dx * t;
39
- y += dy * t;
50
+ let dx = bx - x;
51
+ let dy = by - y;
52
+ if (dx !== 0 || dy !== 0) {
53
+ const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
54
+ if (t > 1) {
55
+ x = bx;
56
+ y = by;
57
+ }
58
+ else if (t > 0) {
59
+ x += dx * t;
60
+ y += dy * t;
61
+ }
40
62
  }
41
- }
42
- dx = px - x;
43
- dy = py - y;
44
- return dx * dx + dy * dy;
63
+ dx = px - x;
64
+ dy = py - y;
65
+ return dx * dx + dy * dy;
45
66
  }
46
- //# sourceMappingURL=simplify.js.map