@loaders.gl/mvt 3.3.0-alpha.6 → 3.3.0-alpha.8

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 (83) hide show
  1. package/dist/dist.min.js +766 -0
  2. package/dist/es5/index.js +7 -0
  3. package/dist/es5/index.js.map +1 -1
  4. package/dist/es5/lib/geojson-tiler/LICENSE +19 -0
  5. package/dist/es5/lib/geojson-tiler/clip.js +247 -0
  6. package/dist/es5/lib/geojson-tiler/clip.js.map +1 -0
  7. package/dist/es5/lib/geojson-tiler/convert.js +163 -0
  8. package/dist/es5/lib/geojson-tiler/convert.js.map +1 -0
  9. package/dist/es5/lib/geojson-tiler/feature.js +63 -0
  10. package/dist/es5/lib/geojson-tiler/feature.js.map +1 -0
  11. package/dist/es5/lib/geojson-tiler/geojson-tiler.js +203 -0
  12. package/dist/es5/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  13. package/dist/es5/lib/geojson-tiler/simplify.js +54 -0
  14. package/dist/es5/lib/geojson-tiler/simplify.js.map +1 -0
  15. package/dist/es5/lib/geojson-tiler/tile.js +127 -0
  16. package/dist/es5/lib/geojson-tiler/tile.js.map +1 -0
  17. package/dist/es5/lib/geojson-tiler/transform.js +52 -0
  18. package/dist/es5/lib/geojson-tiler/transform.js.map +1 -0
  19. package/dist/es5/lib/geojson-tiler/wrap.js +127 -0
  20. package/dist/es5/lib/geojson-tiler/wrap.js.map +1 -0
  21. package/dist/es5/mvt-loader.js +1 -1
  22. package/dist/esm/index.js +4 -0
  23. package/dist/esm/index.js.map +1 -1
  24. package/dist/esm/lib/geojson-tiler/LICENSE +19 -0
  25. package/dist/esm/lib/geojson-tiler/clip.js +184 -0
  26. package/dist/esm/lib/geojson-tiler/clip.js.map +1 -0
  27. package/dist/esm/lib/geojson-tiler/convert.js +120 -0
  28. package/dist/esm/lib/geojson-tiler/convert.js.map +1 -0
  29. package/dist/esm/lib/geojson-tiler/feature.js +37 -0
  30. package/dist/esm/lib/geojson-tiler/feature.js.map +1 -0
  31. package/dist/esm/lib/geojson-tiler/geojson-tiler.js +189 -0
  32. package/dist/esm/lib/geojson-tiler/geojson-tiler.js.map +1 -0
  33. package/dist/esm/lib/geojson-tiler/simplify.js +49 -0
  34. package/dist/esm/lib/geojson-tiler/simplify.js.map +1 -0
  35. package/dist/esm/lib/geojson-tiler/tile.js +108 -0
  36. package/dist/esm/lib/geojson-tiler/tile.js.map +1 -0
  37. package/dist/esm/lib/geojson-tiler/transform.js +35 -0
  38. package/dist/esm/lib/geojson-tiler/transform.js.map +1 -0
  39. package/dist/esm/lib/geojson-tiler/wrap.js +75 -0
  40. package/dist/esm/lib/geojson-tiler/wrap.js.map +1 -0
  41. package/dist/esm/mvt-loader.js +1 -1
  42. package/dist/index.d.ts +2 -0
  43. package/dist/index.d.ts.map +1 -1
  44. package/dist/index.js +4 -1
  45. package/dist/lib/geojson-tiler/clip.d.ts +16 -0
  46. package/dist/lib/geojson-tiler/clip.d.ts.map +1 -0
  47. package/dist/lib/geojson-tiler/clip.js +209 -0
  48. package/dist/lib/geojson-tiler/convert.d.ts +10 -0
  49. package/dist/lib/geojson-tiler/convert.d.ts.map +1 -0
  50. package/dist/lib/geojson-tiler/convert.js +134 -0
  51. package/dist/lib/geojson-tiler/feature.d.ts +3 -0
  52. package/dist/lib/geojson-tiler/feature.d.ts.map +1 -0
  53. package/dist/lib/geojson-tiler/feature.js +46 -0
  54. package/dist/lib/geojson-tiler/geojson-tiler.d.ts +44 -0
  55. package/dist/lib/geojson-tiler/geojson-tiler.d.ts.map +1 -0
  56. package/dist/lib/geojson-tiler/geojson-tiler.js +210 -0
  57. package/dist/lib/geojson-tiler/simplify.d.ts +10 -0
  58. package/dist/lib/geojson-tiler/simplify.d.ts.map +1 -0
  59. package/dist/lib/geojson-tiler/simplify.js +68 -0
  60. package/dist/lib/geojson-tiler/tile.d.ts +38 -0
  61. package/dist/lib/geojson-tiler/tile.d.ts.map +1 -0
  62. package/dist/lib/geojson-tiler/tile.js +125 -0
  63. package/dist/lib/geojson-tiler/transform.d.ts +7 -0
  64. package/dist/lib/geojson-tiler/transform.d.ts.map +1 -0
  65. package/dist/lib/geojson-tiler/transform.js +43 -0
  66. package/dist/lib/geojson-tiler/wrap.d.ts +17 -0
  67. package/dist/lib/geojson-tiler/wrap.d.ts.map +1 -0
  68. package/dist/lib/geojson-tiler/wrap.js +86 -0
  69. package/dist/lib/mapbox-vector-tile/vector-tile-feature.js +3 -3
  70. package/dist/lib/types.d.ts +6 -6
  71. package/dist/lib/types.d.ts.map +1 -1
  72. package/dist/mvt-worker.js +1 -1
  73. package/package.json +5 -5
  74. package/src/index.ts +7 -0
  75. package/src/lib/geojson-tiler/LICENSE +19 -0
  76. package/src/lib/geojson-tiler/clip.ts +248 -0
  77. package/src/lib/geojson-tiler/convert.ts +158 -0
  78. package/src/lib/geojson-tiler/feature.ts +45 -0
  79. package/src/lib/geojson-tiler/geojson-tiler.ts +281 -0
  80. package/src/lib/geojson-tiler/simplify.ts +77 -0
  81. package/src/lib/geojson-tiler/tile.ts +185 -0
  82. package/src/lib/geojson-tiler/transform.ts +55 -0
  83. package/src/lib/geojson-tiler/wrap.ts +108 -0
@@ -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
+ }