@loaders.gl/mvt 4.0.0-alpha.1 → 4.0.0-alpha.11

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 (198) hide show
  1. package/dist/bundle.d.ts +2 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +2 -2
  4. package/dist/dist.min.js +3124 -0
  5. package/dist/es5/bundle.js +6 -0
  6. package/dist/es5/bundle.js.map +1 -0
  7. package/dist/es5/helpers/binary-util-functions.js +82 -0
  8. package/dist/es5/helpers/binary-util-functions.js.map +1 -0
  9. package/dist/es5/helpers/mapbox-util-functions.js +50 -0
  10. package/dist/es5/helpers/mapbox-util-functions.js.map +1 -0
  11. package/dist/es5/index.js +26 -0
  12. package/dist/es5/index.js.map +1 -0
  13. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js +168 -0
  14. package/dist/es5/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  15. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js +62 -0
  16. package/dist/es5/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  17. package/dist/es5/lib/binary-vector-tile/vector-tile.js +28 -0
  18. package/dist/es5/lib/binary-vector-tile/vector-tile.js.map +1 -0
  19. package/dist/es5/lib/geojson-tiler/LICENSE +19 -0
  20. package/dist/es5/lib/geojson-tiler/clip.js +237 -0
  21. package/dist/es5/lib/geojson-tiler/clip.js.map +1 -0
  22. package/dist/es5/lib/geojson-tiler/convert.js +161 -0
  23. package/dist/es5/lib/geojson-tiler/convert.js.map +1 -0
  24. package/dist/es5/lib/geojson-tiler/feature.js +62 -0
  25. package/dist/es5/lib/geojson-tiler/feature.js.map +1 -0
  26. package/dist/es5/lib/geojson-tiler/geojson-tiler.js +186 -0
  27. package/dist/es5/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  28. package/dist/es5/lib/geojson-tiler/simplify.js +52 -0
  29. package/dist/es5/lib/geojson-tiler/simplify.js.map +1 -0
  30. package/dist/es5/lib/geojson-tiler/tile.js +121 -0
  31. package/dist/es5/lib/geojson-tiler/tile.js.map +1 -0
  32. package/dist/es5/lib/geojson-tiler/transform.js +50 -0
  33. package/dist/es5/lib/geojson-tiler/transform.js.map +1 -0
  34. package/dist/es5/lib/geojson-tiler/wrap.js +122 -0
  35. package/dist/es5/lib/geojson-tiler/wrap.js.map +1 -0
  36. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js +184 -0
  37. package/dist/es5/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  38. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js +62 -0
  39. package/dist/es5/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  40. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js +28 -0
  41. package/dist/es5/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  42. package/dist/es5/lib/parse-mvt.js +143 -0
  43. package/dist/es5/lib/parse-mvt.js.map +1 -0
  44. package/dist/es5/lib/types.js +2 -0
  45. package/dist/es5/lib/types.js.map +1 -0
  46. package/dist/es5/mvt-loader.js +58 -0
  47. package/dist/es5/mvt-loader.js.map +1 -0
  48. package/dist/es5/workers/mvt-worker.js +6 -0
  49. package/dist/es5/workers/mvt-worker.js.map +1 -0
  50. package/dist/esm/bundle.js +4 -0
  51. package/dist/esm/bundle.js.map +1 -0
  52. package/dist/esm/helpers/binary-util-functions.js +73 -0
  53. package/dist/esm/helpers/binary-util-functions.js.map +1 -0
  54. package/dist/esm/helpers/mapbox-util-functions.js +41 -0
  55. package/dist/esm/helpers/mapbox-util-functions.js.map +1 -0
  56. package/dist/esm/index.js +3 -0
  57. package/dist/esm/index.js.map +1 -0
  58. package/dist/esm/lib/binary-vector-tile/LICENSE.txt +31 -0
  59. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js +136 -0
  60. package/dist/esm/lib/binary-vector-tile/vector-tile-feature.js.map +1 -0
  61. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js +47 -0
  62. package/dist/esm/lib/binary-vector-tile/vector-tile-layer.js.map +1 -0
  63. package/dist/esm/lib/binary-vector-tile/vector-tile.js +19 -0
  64. package/dist/esm/lib/binary-vector-tile/vector-tile.js.map +1 -0
  65. package/dist/esm/lib/geojson-tiler/LICENSE +19 -0
  66. package/dist/esm/lib/geojson-tiler/clip.js +173 -0
  67. package/dist/esm/lib/geojson-tiler/clip.js.map +1 -0
  68. package/dist/esm/lib/geojson-tiler/convert.js +116 -0
  69. package/dist/esm/lib/geojson-tiler/convert.js.map +1 -0
  70. package/dist/esm/lib/geojson-tiler/feature.js +35 -0
  71. package/dist/esm/lib/geojson-tiler/feature.js.map +1 -0
  72. package/dist/esm/lib/geojson-tiler/geojson-tiler.js +171 -0
  73. package/dist/esm/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  74. package/dist/esm/lib/geojson-tiler/simplify.js +46 -0
  75. package/dist/esm/lib/geojson-tiler/simplify.js.map +1 -0
  76. package/dist/esm/lib/geojson-tiler/tile.js +103 -0
  77. package/dist/esm/lib/geojson-tiler/tile.js.map +1 -0
  78. package/dist/esm/lib/geojson-tiler/transform.js +32 -0
  79. package/dist/esm/lib/geojson-tiler/transform.js.map +1 -0
  80. package/dist/esm/lib/geojson-tiler/wrap.js +67 -0
  81. package/dist/esm/lib/geojson-tiler/wrap.js.map +1 -0
  82. package/dist/esm/lib/mapbox-vector-tile/LICENSE.txt +31 -0
  83. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js +163 -0
  84. package/dist/esm/lib/mapbox-vector-tile/vector-tile-feature.js.map +1 -0
  85. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js +47 -0
  86. package/dist/esm/lib/mapbox-vector-tile/vector-tile-layer.js.map +1 -0
  87. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js +19 -0
  88. package/dist/esm/lib/mapbox-vector-tile/vector-tile.js.map +1 -0
  89. package/dist/esm/lib/parse-mvt.js +138 -0
  90. package/dist/esm/lib/parse-mvt.js.map +1 -0
  91. package/dist/esm/lib/types.js +2 -0
  92. package/dist/esm/lib/types.js.map +1 -0
  93. package/dist/esm/mvt-loader.js +29 -0
  94. package/dist/esm/mvt-loader.js.map +1 -0
  95. package/dist/esm/workers/mvt-worker.js +4 -0
  96. package/dist/esm/workers/mvt-worker.js.map +1 -0
  97. package/dist/helpers/binary-util-functions.d.ts +35 -0
  98. package/dist/helpers/binary-util-functions.d.ts.map +1 -0
  99. package/dist/helpers/binary-util-functions.js +111 -74
  100. package/dist/helpers/mapbox-util-functions.d.ts +29 -0
  101. package/dist/helpers/mapbox-util-functions.d.ts.map +1 -0
  102. package/dist/helpers/mapbox-util-functions.js +78 -45
  103. package/dist/index.d.ts +4 -0
  104. package/dist/index.d.ts.map +1 -0
  105. package/dist/index.js +9 -2
  106. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts +41 -0
  107. package/dist/lib/binary-vector-tile/vector-tile-feature.d.ts.map +1 -0
  108. package/dist/lib/binary-vector-tile/vector-tile-feature.js +142 -142
  109. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts +23 -0
  110. package/dist/lib/binary-vector-tile/vector-tile-layer.d.ts.map +1 -0
  111. package/dist/lib/binary-vector-tile/vector-tile-layer.js +85 -58
  112. package/dist/lib/binary-vector-tile/vector-tile.d.ts +9 -0
  113. package/dist/lib/binary-vector-tile/vector-tile.d.ts.map +1 -0
  114. package/dist/lib/binary-vector-tile/vector-tile.js +25 -19
  115. package/dist/lib/geojson-tiler/clip.d.ts +16 -0
  116. package/dist/lib/geojson-tiler/clip.d.ts.map +1 -0
  117. package/dist/lib/geojson-tiler/clip.js +209 -0
  118. package/dist/lib/geojson-tiler/convert.d.ts +10 -0
  119. package/dist/lib/geojson-tiler/convert.d.ts.map +1 -0
  120. package/dist/lib/geojson-tiler/convert.js +134 -0
  121. package/dist/lib/geojson-tiler/feature.d.ts +3 -0
  122. package/dist/lib/geojson-tiler/feature.d.ts.map +1 -0
  123. package/dist/lib/geojson-tiler/feature.js +46 -0
  124. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +44 -0
  125. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -0
  126. package/dist/lib/geojson-tiler/geojson-tiler.js +210 -0
  127. package/dist/lib/geojson-tiler/simplify.d.ts +10 -0
  128. package/dist/lib/geojson-tiler/simplify.d.ts.map +1 -0
  129. package/dist/lib/geojson-tiler/simplify.js +68 -0
  130. package/dist/lib/geojson-tiler/tile.d.ts +38 -0
  131. package/dist/lib/geojson-tiler/tile.d.ts.map +1 -0
  132. package/dist/lib/geojson-tiler/tile.js +125 -0
  133. package/dist/lib/geojson-tiler/transform.d.ts +7 -0
  134. package/dist/lib/geojson-tiler/transform.d.ts.map +1 -0
  135. package/dist/lib/geojson-tiler/transform.js +43 -0
  136. package/dist/lib/geojson-tiler/wrap.d.ts +17 -0
  137. package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -0
  138. package/dist/lib/geojson-tiler/wrap.js +86 -0
  139. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts +27 -0
  140. package/dist/lib/mapbox-vector-tile/vector-tile-feature.d.ts.map +1 -0
  141. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +161 -193
  142. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts +20 -0
  143. package/dist/lib/mapbox-vector-tile/vector-tile-layer.d.ts.map +1 -0
  144. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js +83 -58
  145. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts +9 -0
  146. package/dist/lib/mapbox-vector-tile/vector-tile.d.ts.map +1 -0
  147. package/dist/lib/mapbox-vector-tile/vector-tile.js +25 -19
  148. package/dist/lib/parse-mvt.d.ts +14 -0
  149. package/dist/lib/parse-mvt.d.ts.map +1 -0
  150. package/dist/lib/parse-mvt.js +149 -98
  151. package/dist/lib/types.d.ts +69 -0
  152. package/dist/lib/types.d.ts.map +1 -0
  153. package/dist/lib/types.js +2 -2
  154. package/dist/mvt-loader.d.ts +10 -0
  155. package/dist/mvt-loader.d.ts.map +1 -0
  156. package/dist/mvt-loader.js +43 -22
  157. package/dist/mvt-worker.js +845 -766
  158. package/dist/workers/mvt-worker.d.ts +2 -0
  159. package/dist/workers/mvt-worker.d.ts.map +1 -0
  160. package/dist/workers/mvt-worker.js +5 -4
  161. package/package.json +10 -9
  162. package/src/helpers/binary-util-functions.ts +9 -7
  163. package/src/helpers/mapbox-util-functions.ts +2 -2
  164. package/src/index.ts +7 -0
  165. package/src/lib/binary-vector-tile/vector-tile-feature.ts +42 -45
  166. package/src/lib/binary-vector-tile/vector-tile-layer.ts +4 -4
  167. package/src/lib/geojson-tiler/LICENSE +19 -0
  168. package/src/lib/geojson-tiler/clip.ts +248 -0
  169. package/src/lib/geojson-tiler/convert.ts +158 -0
  170. package/src/lib/geojson-tiler/feature.ts +45 -0
  171. package/src/lib/geojson-tiler/geojson-tiler.ts +281 -0
  172. package/src/lib/geojson-tiler/simplify.ts +77 -0
  173. package/src/lib/geojson-tiler/tile.ts +185 -0
  174. package/src/lib/geojson-tiler/transform.ts +55 -0
  175. package/src/lib/geojson-tiler/wrap.ts +108 -0
  176. package/src/lib/mapbox-vector-tile/vector-tile-feature.ts +4 -4
  177. package/src/lib/parse-mvt.ts +138 -93
  178. package/src/lib/types.ts +49 -72
  179. package/src/mvt-loader.ts +13 -9
  180. package/dist/bundle.js.map +0 -1
  181. package/dist/helpers/binary-util-functions.js.map +0 -1
  182. package/dist/helpers/mapbox-util-functions.js.map +0 -1
  183. package/dist/index.js.map +0 -1
  184. package/dist/lib/binary-vector-tile/features-to-binary.js +0 -327
  185. package/dist/lib/binary-vector-tile/features-to-binary.js.map +0 -1
  186. package/dist/lib/binary-vector-tile/vector-tile-feature.js.map +0 -1
  187. package/dist/lib/binary-vector-tile/vector-tile-layer.js.map +0 -1
  188. package/dist/lib/binary-vector-tile/vector-tile.js.map +0 -1
  189. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js.map +0 -1
  190. package/dist/lib/mapbox-vector-tile/vector-tile-layer.js.map +0 -1
  191. package/dist/lib/mapbox-vector-tile/vector-tile.js.map +0 -1
  192. package/dist/lib/parse-mvt.js.map +0 -1
  193. package/dist/lib/types.js.map +0 -1
  194. package/dist/mvt-loader.js.map +0 -1
  195. package/dist/workers/mvt-worker.js.map +0 -1
  196. package/src/lib/binary-vector-tile/features-to-binary.ts +0 -518
  197. /package/dist/{lib → es5/lib}/binary-vector-tile/LICENSE.txt +0 -0
  198. /package/dist/{lib → es5/lib}/mapbox-vector-tile/LICENSE.txt +0 -0
@@ -0,0 +1,158 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ /* eslint-disable */
5
+ // @ts-nocheck
6
+
7
+ import type {Feature} from '@loaders.gl/schema';
8
+ import type {GeoJSONTileFeature} from './tile';
9
+
10
+ import {simplify} from './simplify';
11
+ import {createFeature} from './feature';
12
+
13
+ // converts GeoJSON feature into an intermediate projected JSON vector format with simplification data
14
+
15
+ export function convert(data: Feature, options): GeoJSONTileFeature[] {
16
+ const features = [];
17
+ if (data.type === 'FeatureCollection') {
18
+ for (let i = 0; i < data.features.length; i++) {
19
+ convertFeature(features, data.features[i], options, i);
20
+ }
21
+ } else if (data.type === 'Feature') {
22
+ convertFeature(features, data, options);
23
+ } else {
24
+ // single geometry or a geometry collection
25
+ convertFeature(features, {geometry: data}, options);
26
+ }
27
+
28
+ return features;
29
+ }
30
+
31
+ export type ConvertFeatureOptions = {
32
+ maxZoom?: number;
33
+ tolerance: number;
34
+ extent: number;
35
+ lineMetrics: boolean;
36
+ };
37
+
38
+ function convertFeature(
39
+ features: GeoJSONTileFeature[],
40
+ geojson: Feature,
41
+ options: ConvertFeatureOptions,
42
+ index: number
43
+ ): void {
44
+ if (!geojson.geometry) {
45
+ return;
46
+ }
47
+
48
+ const coords = geojson.geometry.coordinates;
49
+ const type = geojson.geometry.type;
50
+ const tolerance = Math.pow(options.tolerance / ((1 << options.maxZoom) * options.extent), 2);
51
+ let geometry = [];
52
+ let id = geojson.id;
53
+ if (options.promoteId) {
54
+ id = geojson.properties[options.promoteId];
55
+ } else if (options.generateId) {
56
+ id = index || 0;
57
+ }
58
+ if (type === 'Point') {
59
+ convertPoint(coords, geometry);
60
+ } else if (type === 'MultiPoint') {
61
+ for (const p of coords) {
62
+ convertPoint(p, geometry);
63
+ }
64
+ } else if (type === 'LineString') {
65
+ convertLine(coords, geometry, tolerance, false);
66
+ } else if (type === 'MultiLineString') {
67
+ if (options.lineMetrics) {
68
+ // explode into linestrings to be able to track metrics
69
+ for (const line of coords) {
70
+ geometry = [];
71
+ convertLine(line, geometry, tolerance, false);
72
+ features.push(createFeature(id, 'LineString', geometry, geojson.properties));
73
+ }
74
+ return;
75
+ } else {
76
+ convertLines(coords, geometry, tolerance, false);
77
+ }
78
+ } else if (type === 'Polygon') {
79
+ convertLines(coords, geometry, tolerance, true);
80
+ } else if (type === 'MultiPolygon') {
81
+ for (const polygon of coords) {
82
+ const newPolygon = [];
83
+ convertLines(polygon, newPolygon, tolerance, true);
84
+ geometry.push(newPolygon);
85
+ }
86
+ } else if (type === 'GeometryCollection') {
87
+ for (const singleGeometry of geojson.geometry.geometries) {
88
+ convertFeature(
89
+ features,
90
+ {
91
+ id,
92
+ geometry: singleGeometry,
93
+ properties: geojson.properties
94
+ },
95
+ options,
96
+ index
97
+ );
98
+ }
99
+ return;
100
+ } else {
101
+ throw new Error('Input data is not a valid GeoJSON object.');
102
+ }
103
+
104
+ features.push(createFeature(id, type, geometry, geojson.properties));
105
+ }
106
+
107
+ function convertPoint(coords, out): void {
108
+ out.push(projectX(coords[0]), projectY(coords[1]), 0);
109
+ }
110
+
111
+ function convertLine(ring: number[], out, tolerance: number, isPolygon: boolean): void {
112
+ let x0, y0;
113
+ let size = 0;
114
+
115
+ for (let j = 0; j < ring.length; j++) {
116
+ const x = projectX(ring[j][0]);
117
+ const y = projectY(ring[j][1]);
118
+
119
+ out.push(x, y, 0);
120
+
121
+ if (j > 0) {
122
+ if (isPolygon) {
123
+ size += (x0 * y - x * y0) / 2; // area
124
+ } else {
125
+ size += Math.sqrt(Math.pow(x - x0, 2) + Math.pow(y - y0, 2)); // length
126
+ }
127
+ }
128
+ x0 = x;
129
+ y0 = y;
130
+ }
131
+
132
+ const last = out.length - 3;
133
+ out[2] = 1;
134
+ simplify(out, 0, last, tolerance);
135
+ out[last + 2] = 1;
136
+
137
+ out.size = Math.abs(size);
138
+ out.start = 0;
139
+ out.end = out.size;
140
+ }
141
+
142
+ function convertLines(rings: number[][], out, tolerance: number, isPolygon: boolean): void {
143
+ for (let i = 0; i < rings.length; i++) {
144
+ const geom = [];
145
+ convertLine(rings[i], geom, tolerance, isPolygon);
146
+ out.push(geom);
147
+ }
148
+ }
149
+
150
+ function projectX(x: number): number {
151
+ return x / 360 + 0.5;
152
+ }
153
+
154
+ function projectY(y: number): number {
155
+ const sin = Math.sin((y * Math.PI) / 180);
156
+ const y2 = 0.5 - (0.25 * Math.log((1 + sin) / (1 - sin))) / Math.PI;
157
+ return y2 < 0 ? 0 : y2 > 1 ? 1 : y2;
158
+ }
@@ -0,0 +1,45 @@
1
+ // loaders.gl, MIT license
2
+ // Forked from https://github.com/mapbox/geojson-vt under compatible ISC license
3
+
4
+ import {GeoJSONTileFeature} from './tile';
5
+
6
+ export function createFeature(id, type, geom, tags): GeoJSONTileFeature {
7
+ const feature: GeoJSONTileFeature = {
8
+ // eslint-disable-next-line
9
+ id: id == null ? null : id,
10
+ type,
11
+ geometry: geom,
12
+ tags,
13
+ minX: Infinity,
14
+ minY: Infinity,
15
+ maxX: -Infinity,
16
+ maxY: -Infinity
17
+ };
18
+
19
+ if (type === 'Point' || type === 'MultiPoint' || type === 'LineString') {
20
+ calcLineBBox(feature, geom);
21
+ } else if (type === 'Polygon') {
22
+ // the outer ring (ie [0]) contains all inner rings
23
+ calcLineBBox(feature, geom[0]);
24
+ } else if (type === 'MultiLineString') {
25
+ for (const line of geom) {
26
+ calcLineBBox(feature, line);
27
+ }
28
+ } else if (type === 'MultiPolygon') {
29
+ for (const polygon of geom) {
30
+ // the outer ring (ie [0]) contains all inner rings
31
+ calcLineBBox(feature, polygon[0]);
32
+ }
33
+ }
34
+
35
+ return feature;
36
+ }
37
+
38
+ function calcLineBBox(feature, geom) {
39
+ for (let i = 0; i < geom.length; i += 3) {
40
+ feature.minX = Math.min(feature.minX, geom[i]);
41
+ feature.minY = Math.min(feature.minY, geom[i + 1]);
42
+ feature.maxX = Math.max(feature.maxX, geom[i]);
43
+ feature.maxY = Math.max(feature.maxY, geom[i + 1]);
44
+ }
45
+ }
@@ -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
+ }