@loaders.gl/mvt 4.0.0-alpha.5 → 4.0.0-alpha.7

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 (175) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dist.min.js +1748 -913
  3. package/dist/es5/bundle.js +6 -0
  4. package/dist/es5/bundle.js.map +1 -0
  5. package/dist/es5/helpers/binary-util-functions.js +82 -0
  6. package/dist/es5/helpers/binary-util-functions.js.map +1 -0
  7. package/dist/es5/helpers/mapbox-util-functions.js +50 -0
  8. package/dist/es5/helpers/mapbox-util-functions.js.map +1 -0
  9. package/dist/es5/index.js +26 -0
  10. package/dist/es5/index.js.map +1 -0
  11. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +168 -0
  12. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  13. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +62 -0
  14. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  15. package/dist/es5/lib/binary-vector-tile/vector-tile.js +28 -0
  16. package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -0
  17. package/dist/es5/lib/geojson-tiler/LICENSE +19 -0
  18. package/dist/es5/lib/geojson-tiler/clip.js +237 -0
  19. package/dist/es5/lib/geojson-tiler/clip.js.map +1 -0
  20. package/dist/es5/lib/geojson-tiler/convert.js +161 -0
  21. package/dist/es5/lib/geojson-tiler/convert.js.map +1 -0
  22. package/dist/es5/lib/geojson-tiler/feature.js +62 -0
  23. package/dist/es5/lib/geojson-tiler/feature.js.map +1 -0
  24. package/dist/es5/lib/geojson-tiler/geojson-tiler.js +186 -0
  25. package/dist/es5/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  26. package/dist/es5/lib/geojson-tiler/simplify.js +52 -0
  27. package/dist/es5/lib/geojson-tiler/simplify.js.map +1 -0
  28. package/dist/es5/lib/geojson-tiler/tile.js +121 -0
  29. package/dist/es5/lib/geojson-tiler/tile.js.map +1 -0
  30. package/dist/es5/lib/geojson-tiler/transform.js +50 -0
  31. package/dist/es5/lib/geojson-tiler/transform.js.map +1 -0
  32. package/dist/es5/lib/geojson-tiler/wrap.js +122 -0
  33. package/dist/es5/lib/geojson-tiler/wrap.js.map +1 -0
  34. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +184 -0
  35. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  36. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +62 -0
  37. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  38. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +28 -0
  39. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  40. package/dist/es5/lib/parse-mvt.js +143 -0
  41. package/dist/es5/lib/parse-mvt.js.map +1 -0
  42. package/dist/es5/lib/types.js +2 -0
  43. package/dist/es5/lib/types.js.map +1 -0
  44. package/dist/es5/mvt-loader.js +58 -0
  45. package/dist/es5/mvt-loader.js.map +1 -0
  46. package/dist/es5/workers/mvt-worker.js +6 -0
  47. package/dist/es5/workers/mvt-worker.js.map +1 -0
  48. package/dist/esm/bundle.js +4 -0
  49. package/dist/esm/bundle.js.map +1 -0
  50. package/dist/esm/helpers/binary-util-functions.js +73 -0
  51. package/dist/esm/helpers/binary-util-functions.js.map +1 -0
  52. package/dist/esm/helpers/mapbox-util-functions.js +41 -0
  53. package/dist/esm/helpers/mapbox-util-functions.js.map +1 -0
  54. package/dist/esm/index.js +3 -0
  55. package/dist/esm/index.js.map +1 -0
  56. package/dist/esm/lib/binary-vector-tile/LICENSE.txt +31 -0
  57. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +136 -0
  58. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  59. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +47 -0
  60. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  61. package/dist/esm/lib/binary-vector-tile/vector-tile.js +19 -0
  62. package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -0
  63. package/dist/esm/lib/geojson-tiler/LICENSE +19 -0
  64. package/dist/esm/lib/geojson-tiler/clip.js +173 -0
  65. package/dist/esm/lib/geojson-tiler/clip.js.map +1 -0
  66. package/dist/esm/lib/geojson-tiler/convert.js +116 -0
  67. package/dist/esm/lib/geojson-tiler/convert.js.map +1 -0
  68. package/dist/esm/lib/geojson-tiler/feature.js +35 -0
  69. package/dist/esm/lib/geojson-tiler/feature.js.map +1 -0
  70. package/dist/esm/lib/geojson-tiler/geojson-tiler.js +171 -0
  71. package/dist/esm/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  72. package/dist/esm/lib/geojson-tiler/simplify.js +46 -0
  73. package/dist/esm/lib/geojson-tiler/simplify.js.map +1 -0
  74. package/dist/esm/lib/geojson-tiler/tile.js +103 -0
  75. package/dist/esm/lib/geojson-tiler/tile.js.map +1 -0
  76. package/dist/esm/lib/geojson-tiler/transform.js +32 -0
  77. package/dist/esm/lib/geojson-tiler/transform.js.map +1 -0
  78. package/dist/esm/lib/geojson-tiler/wrap.js +67 -0
  79. package/dist/esm/lib/geojson-tiler/wrap.js.map +1 -0
  80. package/dist/esm/lib/mapbox-vector-tile/LICENSE.txt +31 -0
  81. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +163 -0
  82. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  83. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +47 -0
  84. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  85. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +19 -0
  86. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  87. package/dist/esm/lib/parse-mvt.js +138 -0
  88. package/dist/esm/lib/parse-mvt.js.map +1 -0
  89. package/dist/esm/lib/types.js +2 -0
  90. package/dist/esm/lib/types.js.map +1 -0
  91. package/dist/esm/mvt-loader.js +29 -0
  92. package/dist/esm/mvt-loader.js.map +1 -0
  93. package/dist/esm/workers/mvt-worker.js +4 -0
  94. package/dist/esm/workers/mvt-worker.js.map +1 -0
  95. package/dist/helpers/binary-util-functions.js +111 -77
  96. package/dist/helpers/mapbox-util-functions.d.ts +2 -2
  97. package/dist/helpers/mapbox-util-functions.js +78 -45
  98. package/dist/index.d.ts +2 -0
  99. package/dist/index.d.ts.map +1 -1
  100. package/dist/index.js +9 -2
  101. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -1
  102. package/dist/lib/binary-vector-tile/vector-tile-feature.js +142 -143
  103. package/dist/lib/binary-vector-tile/vector-tile-layer.js +85 -58
  104. package/dist/lib/binary-vector-tile/vector-tile.js +25 -19
  105. package/dist/lib/geojson-tiler/clip.d.ts +16 -0
  106. package/dist/lib/geojson-tiler/clip.d.ts.map +1 -0
  107. package/dist/lib/geojson-tiler/clip.js +209 -0
  108. package/dist/lib/geojson-tiler/convert.d.ts +10 -0
  109. package/dist/lib/geojson-tiler/convert.d.ts.map +1 -0
  110. package/dist/lib/geojson-tiler/convert.js +134 -0
  111. package/dist/lib/geojson-tiler/feature.d.ts +3 -0
  112. package/dist/lib/geojson-tiler/feature.d.ts.map +1 -0
  113. package/dist/lib/geojson-tiler/feature.js +46 -0
  114. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +44 -0
  115. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -0
  116. package/dist/lib/geojson-tiler/geojson-tiler.js +210 -0
  117. package/dist/lib/geojson-tiler/simplify.d.ts +10 -0
  118. package/dist/lib/geojson-tiler/simplify.d.ts.map +1 -0
  119. package/dist/lib/geojson-tiler/simplify.js +68 -0
  120. package/dist/lib/geojson-tiler/tile.d.ts +38 -0
  121. package/dist/lib/geojson-tiler/tile.d.ts.map +1 -0
  122. package/dist/lib/geojson-tiler/tile.js +125 -0
  123. package/dist/lib/geojson-tiler/transform.d.ts +7 -0
  124. package/dist/lib/geojson-tiler/transform.d.ts.map +1 -0
  125. package/dist/lib/geojson-tiler/transform.js +43 -0
  126. package/dist/lib/geojson-tiler/wrap.d.ts +17 -0
  127. package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -0
  128. package/dist/lib/geojson-tiler/wrap.js +86 -0
  129. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +4 -4
  130. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +161 -193
  131. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +83 -58
  132. package/dist/lib/mapbox-vector-tile/vector-tile.js +25 -19
  133. package/dist/lib/parse-mvt.d.ts +6 -9
  134. package/dist/lib/parse-mvt.d.ts.map +1 -1
  135. package/dist/lib/parse-mvt.js +148 -98
  136. package/dist/lib/types.d.ts +48 -7
  137. package/dist/lib/types.d.ts.map +1 -1
  138. package/dist/lib/types.js +2 -2
  139. package/dist/mvt-loader.d.ts.map +1 -1
  140. package/dist/mvt-loader.js +43 -22
  141. package/dist/mvt-worker.js +903 -829
  142. package/dist/workers/mvt-worker.js +5 -4
  143. package/package.json +7 -7
  144. package/src/helpers/mapbox-util-functions.ts +2 -2
  145. package/src/index.ts +7 -0
  146. package/src/lib/binary-vector-tile/vector-tile-feature.ts +6 -1
  147. package/src/lib/geojson-tiler/LICENSE +19 -0
  148. package/src/lib/geojson-tiler/clip.ts +248 -0
  149. package/src/lib/geojson-tiler/convert.ts +158 -0
  150. package/src/lib/geojson-tiler/feature.ts +45 -0
  151. package/src/lib/geojson-tiler/geojson-tiler.ts +281 -0
  152. package/src/lib/geojson-tiler/simplify.ts +77 -0
  153. package/src/lib/geojson-tiler/tile.ts +185 -0
  154. package/src/lib/geojson-tiler/transform.ts +55 -0
  155. package/src/lib/geojson-tiler/wrap.ts +108 -0
  156. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +4 -4
  157. package/src/lib/parse-mvt.ts +136 -93
  158. package/src/lib/types.ts +53 -7
  159. package/src/mvt-loader.ts +13 -9
  160. package/dist/bundle.js.map +0 -1
  161. package/dist/helpers/binary-util-functions.js.map +0 -1
  162. package/dist/helpers/mapbox-util-functions.js.map +0 -1
  163. package/dist/index.js.map +0 -1
  164. package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +0 -1
  165. package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +0 -1
  166. package/dist/lib/binary-vector-tile/vector-tile.js.map +0 -1
  167. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js.map +0 -1
  168. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js.map +0 -1
  169. package/dist/lib/mapbox-vector-tile/vector-tile.js.map +0 -1
  170. package/dist/lib/parse-mvt.js.map +0 -1
  171. package/dist/lib/types.js.map +0 -1
  172. package/dist/mvt-loader.js.map +0 -1
  173. package/dist/workers/mvt-worker.js.map +0 -1
  174. /package/dist/{lib → es5/lib}/binary-vector-tile/LICENSE.txt +0 -0
  175. /package/dist/{lib → es5/lib}/mapbox-vector-tile/LICENSE.txt +0 -0
@@ -0,0 +1,281 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ /* eslint-disable no-console, no-continue */
5
+
6
+ import type {GeoJSONTile, GeoJSONTileFeature} from './tile';
7
+
8
+ import {convert} from './convert'; // GeoJSON conversion and preprocessing
9
+ import {clip} from './clip'; // stripe clipping algorithm
10
+ import {wrap} from './wrap'; // date line processing
11
+ import {transformTile} from './transform'; // coordinate transformation
12
+ import {createTile} from './tile'; // final simplified tile generation
13
+
14
+ /** Options to configure tiling */
15
+ export type GeoJSONTilerOptions = {
16
+ maxZoom?: number /** max zoom to preserve detail on */;
17
+ indexMaxZoom?: number /** max zoom in the tile index */;
18
+ indexMaxPoints?: number /** max number of points per tile in the tile index */;
19
+ tolerance?: number /** simplification tolerance (higher means simpler) */;
20
+ extent?: number /** tile extent */;
21
+ buffer?: number /** tile buffer on each side */;
22
+ lineMetrics?: boolean /** whether to calculate line metrics */;
23
+ promoteId?: string /** name of a feature property to be promoted to feature.id */;
24
+ generateId?: boolean /** whether to generate feature ids. Cannot be used with promoteId */;
25
+ debug?: number /** logging level (0, 1 or 2) */;
26
+ };
27
+
28
+ const DEFAULT_OPTIONS: Required<GeoJSONTilerOptions> = {
29
+ maxZoom: 14, // max zoom to preserve detail on
30
+ indexMaxZoom: 5, // max zoom in the tile index
31
+ indexMaxPoints: 100000, // max number of points per tile in the tile index
32
+ tolerance: 3, // simplification tolerance (higher means simpler)
33
+ extent: 4096, // tile extent
34
+ buffer: 64, // tile buffer on each side
35
+ lineMetrics: false, // whether to calculate line metrics
36
+ // @ts-expect-error Ensures all these required params have defaults
37
+ promoteId: undefined, // name of a feature property to be promoted to feature.id
38
+ generateId: false, // whether to generate feature ids. Cannot be used with promoteId
39
+ debug: 0 // logging level (0, 1 or 2)
40
+ };
41
+
42
+ export class GeoJSONTiler {
43
+ options: Required<GeoJSONTilerOptions>;
44
+
45
+ // tiles and tileCoords are part of the public API
46
+ tiles: Record<string, GeoJSONTile> = {};
47
+ tileCoords: {x: number; y: number; z: number}[] = [];
48
+
49
+ stats: Record<string, number> = {};
50
+ total: number = 0;
51
+
52
+ constructor(data, options?: GeoJSONTilerOptions) {
53
+ this.options = {...DEFAULT_OPTIONS, ...options};
54
+ options = this.options;
55
+
56
+ const debug = options.debug;
57
+
58
+ if (debug) console.time('preprocess data');
59
+
60
+ if (this.options.maxZoom < 0 || this.options.maxZoom > 24) {
61
+ throw new Error('maxZoom should be in the 0-24 range');
62
+ }
63
+ if (options.promoteId && this.options.generateId) {
64
+ throw new Error('promoteId and generateId cannot be used together.');
65
+ }
66
+
67
+ // projects and adds simplification info
68
+ let features = convert(data, options);
69
+
70
+ if (debug) {
71
+ console.timeEnd('preprocess data');
72
+ console.log(
73
+ 'index: maxZoom: %d, maxPoints: %d',
74
+ options.indexMaxZoom,
75
+ options.indexMaxPoints
76
+ );
77
+ console.time('generate tiles');
78
+ }
79
+
80
+ // wraps features (ie extreme west and extreme east)
81
+ features = wrap(features, this.options);
82
+
83
+ // start slicing from the top tile down
84
+ if (features.length) {
85
+ this.splitTile(features, 0, 0, 0);
86
+ }
87
+
88
+ if (debug) {
89
+ if (features.length) {
90
+ console.log('features: %d, points: %d', this.tiles[0].numFeatures, this.tiles[0].numPoints);
91
+ }
92
+ console.timeEnd('generate tiles');
93
+ console.log('tiles generated:', this.total, JSON.stringify(this.stats));
94
+ }
95
+ }
96
+
97
+ /**
98
+ * Get a tile at the specified index
99
+ * @param z
100
+ * @param x
101
+ * @param y
102
+ * @returns
103
+ */
104
+ // eslint-disable-next-line complexity, max-statements
105
+ getTile(z: number, x: number, y: number): GeoJSONTile | null {
106
+ // z = +z;
107
+ // x = +x;
108
+ // y = +y;
109
+
110
+ const {extent, debug} = this.options;
111
+
112
+ if (z < 0 || z > 24) {
113
+ return null;
114
+ }
115
+
116
+ const z2 = 1 << z;
117
+ x = (x + z2) & (z2 - 1); // wrap tile x coordinate
118
+
119
+ const id = toID(z, x, y);
120
+ if (this.tiles[id]) {
121
+ return transformTile(this.tiles[id], extent);
122
+ }
123
+
124
+ if (debug > 1) console.log('drilling down to z%d-%d-%d', z, x, y);
125
+
126
+ let z0 = z;
127
+ let x0 = x;
128
+ let y0 = y;
129
+ let parent;
130
+
131
+ while (!parent && z0 > 0) {
132
+ z0--;
133
+ x0 = x0 >> 1;
134
+ y0 = y0 >> 1;
135
+ parent = this.tiles[toID(z0, x0, y0)];
136
+ }
137
+
138
+ if (!parent || !parent.source) {
139
+ return null;
140
+ }
141
+
142
+ // if we found a parent tile containing the original geometry, we can drill down from it
143
+ if (debug > 1) {
144
+ console.log('found parent tile z%d-%d-%d', z0, x0, y0);
145
+ console.time('drilling down');
146
+ }
147
+ this.splitTile(parent.source, z0, x0, y0, z, x, y);
148
+ if (debug > 1) {
149
+ console.timeEnd('drilling down');
150
+ }
151
+
152
+ return this.tiles[id] ? transformTile(this.tiles[id], extent) : null;
153
+ }
154
+
155
+ /**
156
+ * splits features from a parent tile to sub-tiles.
157
+ * @param z, x, and y are the coordinates of the parent tile
158
+ * @param cz, cx, and cy are the coordinates of the target tile
159
+ *
160
+ * If no target tile is specified, splitting stops when we reach the maximum
161
+ * zoom or the number of points is low as specified in the options.
162
+ */
163
+ // eslint-disable-next-line max-params, max-statements, complexity
164
+ splitTile(
165
+ features: GeoJSONTileFeature[],
166
+ z: number,
167
+ x: number,
168
+ y: number,
169
+ cz?: number,
170
+ cx?: number,
171
+ cy?: number
172
+ ): void {
173
+ const stack: any[] = [features, z, x, y];
174
+ const options = this.options;
175
+ const debug = options.debug;
176
+
177
+ // avoid recursion by using a processing queue
178
+ while (stack.length) {
179
+ y = stack.pop();
180
+ x = stack.pop();
181
+ z = stack.pop();
182
+ features = stack.pop();
183
+
184
+ const z2 = 1 << z;
185
+ const id = toID(z, x, y);
186
+ let tile = this.tiles[id];
187
+
188
+ if (!tile) {
189
+ if (debug > 1) {
190
+ console.time('creation');
191
+ }
192
+
193
+ tile = this.tiles[id] = createTile(features, z, x, y, options);
194
+ this.tileCoords.push({z, x, y});
195
+
196
+ if (debug) {
197
+ if (debug > 1) {
198
+ console.log(
199
+ 'tile z%d-%d-%d (features: %d, points: %d, simplified: %d)',
200
+ z,
201
+ x,
202
+ y,
203
+ tile.numFeatures,
204
+ tile.numPoints,
205
+ tile.numSimplified
206
+ );
207
+ console.timeEnd('creation');
208
+ }
209
+ const key = `z${z}`;
210
+ this.stats[key] = (this.stats[key] || 0) + 1;
211
+ this.total++;
212
+ }
213
+ }
214
+
215
+ // save reference to original geometry in tile so that we can drill down later if we stop now
216
+ tile.source = features;
217
+
218
+ // if it's the first-pass tiling
219
+ if (cz === undefined) {
220
+ // stop tiling if we reached max zoom, or if the tile is too simple
221
+ if (z === options.indexMaxZoom || tile.numPoints <= options.indexMaxPoints) continue;
222
+ // if a drilldown to a specific tile
223
+ } else if (z === options.maxZoom || z === cz) {
224
+ // stop tiling if we reached base zoom or our target tile zoom
225
+ continue;
226
+ } else if (cz !== undefined) {
227
+ // stop tiling if it's not an ancestor of the target tile
228
+ const zoomSteps = cz - z;
229
+ // @ts-expect-error TODO fix the types of cx cy
230
+ if (x !== cx >> zoomSteps || y !== cy >> zoomSteps) continue;
231
+ }
232
+
233
+ // if we slice further down, no need to keep source geometry
234
+ tile.source = null;
235
+
236
+ if (features.length === 0) continue;
237
+
238
+ if (debug > 1) console.time('clipping');
239
+
240
+ // values we'll use for clipping
241
+ const k1 = (0.5 * options.buffer) / options.extent;
242
+ const k2 = 0.5 - k1;
243
+ const k3 = 0.5 + k1;
244
+ const k4 = 1 + k1;
245
+
246
+ let tl: GeoJSONTileFeature[] | null = null;
247
+ let bl: GeoJSONTileFeature[] | null = null;
248
+ let tr: GeoJSONTileFeature[] | null = null;
249
+ let br: GeoJSONTileFeature[] | null = null;
250
+
251
+ let left = clip(features, z2, x - k1, x + k3, 0, tile.minX, tile.maxX, options);
252
+ let right = clip(features, z2, x + k2, x + k4, 0, tile.minX, tile.maxX, options);
253
+
254
+ // @ts-expect-error - unclear why this is needed?
255
+ features = null;
256
+
257
+ if (left) {
258
+ tl = clip(left, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
259
+ bl = clip(left, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
260
+ left = null;
261
+ }
262
+
263
+ if (right) {
264
+ tr = clip(right, z2, y - k1, y + k3, 1, tile.minY, tile.maxY, options);
265
+ br = clip(right, z2, y + k2, y + k4, 1, tile.minY, tile.maxY, options);
266
+ right = null;
267
+ }
268
+
269
+ if (debug > 1) console.timeEnd('clipping');
270
+
271
+ stack.push(tl || [], z + 1, x * 2, y * 2);
272
+ stack.push(bl || [], z + 1, x * 2, y * 2 + 1);
273
+ stack.push(tr || [], z + 1, x * 2 + 1, y * 2);
274
+ stack.push(br || [], z + 1, x * 2 + 1, y * 2 + 1);
275
+ }
276
+ }
277
+ }
278
+
279
+ function toID(z, x, y) {
280
+ return ((1 << z) * y + x) * 32 + z;
281
+ }
@@ -0,0 +1,77 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ /**
5
+ * Calculate simplification data using optimized Douglas-Peucker algorithm
6
+ *
7
+ * @param coords contiguous list of coordinates
8
+ * @param first first coord to simplify
9
+ * @param last last coord to simplify
10
+ * @param sqTolerance tolerance (square distance)
11
+ */
12
+ export function simplify(coords: number[], first: number, last: number, sqTolerance: number): void {
13
+ let maxSqDist = sqTolerance;
14
+ const mid = (last - first) >> 1;
15
+ let minPosToMid = last - first;
16
+ let index;
17
+
18
+ const ax = coords[first];
19
+ const ay = coords[first + 1];
20
+ const bx = coords[last];
21
+ const by = coords[last + 1];
22
+
23
+ for (let i = first + 3; i < last; i += 3) {
24
+ const d = getSqSegDist(coords[i], coords[i + 1], ax, ay, bx, by);
25
+
26
+ if (d > maxSqDist) {
27
+ index = i;
28
+ maxSqDist = d;
29
+ } else if (d === maxSqDist) {
30
+ // a workaround to ensure we choose a pivot close to the middle of the list,
31
+ // reducing recursion depth, for certain degenerate inputs
32
+ // https://github.com/mapbox/geojson-vt/issues/104
33
+ const posToMid = Math.abs(i - mid);
34
+ if (posToMid < minPosToMid) {
35
+ index = i;
36
+ minPosToMid = posToMid;
37
+ }
38
+ }
39
+ }
40
+
41
+ if (maxSqDist > sqTolerance) {
42
+ if (index - first > 3) simplify(coords, first, index, sqTolerance);
43
+ coords[index + 2] = maxSqDist;
44
+ if (last - index > 3) simplify(coords, index, last, sqTolerance);
45
+ }
46
+ }
47
+
48
+ /** square distance from a point to a segment */
49
+ // eslint-disable-next-line max-params
50
+ function getSqSegDist(
51
+ px: number,
52
+ py: number,
53
+ x: number,
54
+ y: number,
55
+ bx: number,
56
+ by: number
57
+ ): number {
58
+ let dx = bx - x;
59
+ let dy = by - y;
60
+
61
+ if (dx !== 0 || dy !== 0) {
62
+ const t = ((px - x) * dx + (py - y) * dy) / (dx * dx + dy * dy);
63
+
64
+ if (t > 1) {
65
+ x = bx;
66
+ y = by;
67
+ } else if (t > 0) {
68
+ x += dx * t;
69
+ y += dy * t;
70
+ }
71
+ }
72
+
73
+ dx = px - x;
74
+ dy = py - y;
75
+
76
+ return dx * dx + dy * dy;
77
+ }
@@ -0,0 +1,185 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ // import type {Feature} from '@loaders.gl/schema';
5
+
6
+ export type GeoJSONTileFeature = {
7
+ type: any;
8
+ geometry: any;
9
+
10
+ // book keeping
11
+ id?: string;
12
+ tags?: string[];
13
+
14
+ // spatial extents
15
+ minX: number;
16
+ maxX: number;
17
+ minY: number;
18
+ maxY: number;
19
+ };
20
+
21
+ export type GeoJSONTile = {
22
+ features: GeoJSONTileFeature[]; // Feature[]; Doesn't seem JSON compatible??
23
+ type?: number;
24
+ tags?: Record<string, string>;
25
+
26
+ // tile coordinates
27
+ x: number;
28
+ y: number;
29
+ z: number;
30
+
31
+ // spatial extents
32
+ minX: number;
33
+ maxX: number;
34
+ minY: number;
35
+ maxY: number;
36
+
37
+ transformed: boolean;
38
+ numPoints: number;
39
+ numSimplified: number;
40
+ numFeatures: number;
41
+ source: any | null;
42
+ };
43
+
44
+ export type CreateTileOptions = {
45
+ maxZoom?: number;
46
+ tolerance: number;
47
+ extent: number;
48
+ lineMetrics: boolean;
49
+ };
50
+
51
+ /**
52
+ * Create a tile from features and tile index
53
+ */
54
+ export function createTile(features: any[], z, tx, ty, options: CreateTileOptions): GeoJSONTile {
55
+ const tolerance = z === options.maxZoom ? 0 : options.tolerance / ((1 << z) * options.extent);
56
+ const tile: GeoJSONTile = {
57
+ features: [],
58
+ numPoints: 0,
59
+ numSimplified: 0,
60
+ numFeatures: features.length,
61
+ source: null,
62
+ x: tx,
63
+ y: ty,
64
+ z,
65
+ transformed: false,
66
+ minX: 2,
67
+ minY: 1,
68
+ maxX: -1,
69
+ maxY: 0
70
+ };
71
+ for (const feature of features) {
72
+ addFeature(tile, feature, tolerance, options);
73
+ }
74
+ return tile;
75
+ }
76
+
77
+ // eslint-disable-next-line complexity, max-statements
78
+ function addFeature(tile: GeoJSONTile, feature, tolerance: number, options: CreateTileOptions) {
79
+ const geom = feature.geometry;
80
+ const type = feature.type;
81
+ const simplified: number[] = [];
82
+
83
+ tile.minX = Math.min(tile.minX, feature.minX);
84
+ tile.minY = Math.min(tile.minY, feature.minY);
85
+ tile.maxX = Math.max(tile.maxX, feature.maxX);
86
+ tile.maxY = Math.max(tile.maxY, feature.maxY);
87
+
88
+ if (type === 'Point' || type === 'MultiPoint') {
89
+ for (let i = 0; i < geom.length; i += 3) {
90
+ simplified.push(geom[i], geom[i + 1]);
91
+ tile.numPoints++;
92
+ tile.numSimplified++;
93
+ }
94
+ } else if (type === 'LineString') {
95
+ addLine(simplified, geom, tile, tolerance, false, false);
96
+ } else if (type === 'MultiLineString' || type === 'Polygon') {
97
+ for (let i = 0; i < geom.length; i++) {
98
+ addLine(simplified, geom[i], tile, tolerance, type === 'Polygon', i === 0);
99
+ }
100
+ } else if (type === 'MultiPolygon') {
101
+ for (let k = 0; k < geom.length; k++) {
102
+ const polygon = geom[k];
103
+ for (let i = 0; i < polygon.length; i++) {
104
+ addLine(simplified, polygon[i], tile, tolerance, true, i === 0);
105
+ }
106
+ }
107
+ }
108
+
109
+ if (simplified.length) {
110
+ let tags = feature.tags || null;
111
+
112
+ if (type === 'LineString' && options.lineMetrics) {
113
+ tags = {};
114
+ for (const key in feature.tags) tags[key] = feature.tags[key];
115
+ // eslint-disable-next-line camelcase
116
+ tags.mapbox_clip_start = geom.start / geom.size;
117
+ // eslint-disable-next-line camelcase
118
+ tags.mapbox_clip_end = geom.end / geom.size;
119
+ }
120
+
121
+ // @ts-expect-error TODO - create sub type?
122
+ const tileFeature: GeoJSONTileFeature = {
123
+ geometry: simplified,
124
+ type:
125
+ type === 'Polygon' || type === 'MultiPolygon'
126
+ ? 3
127
+ : type === 'LineString' || type === 'MultiLineString'
128
+ ? 2
129
+ : 1,
130
+ tags
131
+ };
132
+ if (feature.id !== null) {
133
+ tileFeature.id = feature.id;
134
+ }
135
+ tile.features.push(tileFeature);
136
+ }
137
+ }
138
+
139
+ // eslint-disable-next-line max-params, max-statements
140
+ function addLine(
141
+ result,
142
+ geom,
143
+ tile: GeoJSONTile,
144
+ tolerance: number,
145
+ isPolygon: boolean,
146
+ isOuter: boolean
147
+ ): void {
148
+ const sqTolerance = tolerance * tolerance;
149
+
150
+ if (tolerance > 0 && geom.size < (isPolygon ? sqTolerance : tolerance)) {
151
+ tile.numPoints += geom.length / 3;
152
+ return;
153
+ }
154
+
155
+ const ring: number[] = [];
156
+
157
+ for (let i = 0; i < geom.length; i += 3) {
158
+ if (tolerance === 0 || geom[i + 2] > sqTolerance) {
159
+ tile.numSimplified++;
160
+ ring.push(geom[i], geom[i + 1]);
161
+ }
162
+ tile.numPoints++;
163
+ }
164
+
165
+ if (isPolygon) rewind(ring, isOuter);
166
+
167
+ result.push(ring);
168
+ }
169
+
170
+ function rewind(ring: number[], clockwise?: boolean): void {
171
+ let area = 0;
172
+ for (let i = 0, j = ring.length - 2; i < ring.length; j = i, i += 2) {
173
+ area += (ring[i] - ring[j]) * (ring[i + 1] + ring[j + 1]);
174
+ }
175
+ if (area > 0 === clockwise) {
176
+ for (let i = 0, len = ring.length; i < len / 2; i += 2) {
177
+ const x = ring[i];
178
+ const y = ring[i + 1];
179
+ ring[i] = ring[len - 2 - i];
180
+ ring[i + 1] = ring[len - 1 - i];
181
+ ring[len - 2 - i] = x;
182
+ ring[len - 1 - i] = y;
183
+ }
184
+ }
185
+ }
@@ -0,0 +1,55 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ import type {GeoJSONTile} from './tile';
5
+
6
+ /**
7
+ * Transforms the coordinates of each feature in the given tile from
8
+ * mercator-projected space into (extent x extent) tile space.
9
+ */
10
+ export function transformTile(tile: GeoJSONTile, extent: number): GeoJSONTile {
11
+ if (tile.transformed) {
12
+ return tile;
13
+ }
14
+
15
+ const z2 = 1 << tile.z;
16
+ const tx = tile.x;
17
+ const ty = tile.y;
18
+
19
+ for (const feature of tile.features) {
20
+ const geom = feature.geometry;
21
+ const type = feature.type;
22
+
23
+ feature.geometry = [];
24
+
25
+ if (type === 1) {
26
+ for (let j = 0; j < geom.length; j += 2) {
27
+ feature.geometry.push(transformPoint(geom[j], geom[j + 1], extent, z2, tx, ty));
28
+ }
29
+ } else {
30
+ for (let j = 0; j < geom.length; j++) {
31
+ const ring: number[][] = [];
32
+ for (let k = 0; k < geom[j].length; k += 2) {
33
+ ring.push(transformPoint(geom[j][k], geom[j][k + 1], extent, z2, tx, ty));
34
+ }
35
+ feature.geometry.push(ring);
36
+ }
37
+ }
38
+ }
39
+
40
+ tile.transformed = true;
41
+
42
+ return tile;
43
+ }
44
+
45
+ // eslint-disable-next-line max-params
46
+ function transformPoint(
47
+ x: number,
48
+ y: number,
49
+ extent: number,
50
+ z2: number,
51
+ tx: number,
52
+ ty: number
53
+ ): number[] {
54
+ return [Math.round(extent * (x * z2 - tx)), Math.round(extent * (y * z2 - ty))];
55
+ }
@@ -0,0 +1,108 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ import type {GeoJSONTileFeature} from './tile';
5
+ import {clip} from './clip';
6
+ import {createFeature} from './feature';
7
+
8
+ /**
9
+ * Options for wrap()
10
+ */
11
+ export type WrapOptions = {
12
+ buffer: number /** number of pixels of buffer for the tile */;
13
+ extent: number /** extent of each tile */;
14
+ lineMetrics: boolean;
15
+ };
16
+
17
+ /**
18
+ * Wrap across antemeridian, by clipping into two tiles, shifting the overflowing x coordinates
19
+ * @param features list of features to be wrapped
20
+ * @param options buffer and extent
21
+ * @returns
22
+ */
23
+ export function wrap(features: GeoJSONTileFeature[], options: WrapOptions) {
24
+ const buffer = options.buffer / options.extent;
25
+ let merged: GeoJSONTileFeature[] = features;
26
+ const left = clip(features, 1, -1 - buffer, buffer, 0, -1, 2, options); // left world copy
27
+ const right = clip(features, 1, 1 - buffer, 2 + buffer, 0, -1, 2, options); // right world copy
28
+
29
+ if (left || right) {
30
+ merged = clip(features, 1, -buffer, 1 + buffer, 0, -1, 2, options) || []; // center world copy
31
+
32
+ if (left) {
33
+ merged = shiftFeatureCoords(left, 1).concat(merged); // merge left into center
34
+ }
35
+ if (right) {
36
+ merged = merged.concat(shiftFeatureCoords(right, -1)); // merge right into center
37
+ }
38
+ }
39
+
40
+ return merged;
41
+ }
42
+
43
+ /**
44
+ * Shift the x coordinates of a list of features
45
+ * @param features list of features to shift x coordinates for
46
+ * @param offset
47
+ * @returns
48
+ */
49
+ function shiftFeatureCoords(features: GeoJSONTileFeature[], offset: number): GeoJSONTileFeature[] {
50
+ const newFeatures: GeoJSONTileFeature[] = [];
51
+
52
+ for (let i = 0; i < features.length; i++) {
53
+ const feature = features[i];
54
+ const type = feature.type;
55
+
56
+ let newGeometry;
57
+
58
+ if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
59
+ newGeometry = shiftCoords(feature.geometry, offset);
60
+ } else if (type === 'MultiLineString' || type === 'Polygon') {
61
+ newGeometry = [];
62
+ for (const line of feature.geometry) {
63
+ newGeometry.push(shiftCoords(line, offset));
64
+ }
65
+ } else if (type === 'MultiPolygon') {
66
+ newGeometry = [];
67
+ for (const polygon of feature.geometry) {
68
+ const newPolygon: Points = [];
69
+ for (const line of polygon) {
70
+ // @ts-expect-error TODO
71
+ newPolygon.push(shiftCoords(line, offset));
72
+ }
73
+ newGeometry.push(newPolygon);
74
+ }
75
+ }
76
+
77
+ newFeatures.push(createFeature(feature.id, type, newGeometry, feature.tags));
78
+ }
79
+
80
+ return newFeatures;
81
+ }
82
+
83
+ class Points extends Array<number> {
84
+ size?: number;
85
+ start?: number;
86
+ end?: number;
87
+ }
88
+
89
+ /**
90
+ * Shift the x coordinate of every point
91
+ * @param points
92
+ * @param offset
93
+ * @returns
94
+ */
95
+ function shiftCoords(points: Points, offset: number): Points {
96
+ const newPoints: Points = [];
97
+ newPoints.size = points.size;
98
+
99
+ if (points.start !== undefined) {
100
+ newPoints.start = points.start;
101
+ newPoints.end = points.end;
102
+ }
103
+
104
+ for (let i = 0; i < points.length; i += 3) {
105
+ newPoints.push(points[i] + offset, points[i + 1], points[i + 2]);
106
+ }
107
+ return newPoints;
108
+ }