@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,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
+ }
@@ -1,6 +1,6 @@
1
1
  // This code is forked from https://github.com/mapbox/vector-tile-js under BSD 3-clause license.
2
2
  import Protobuf from 'pbf';
3
- import {MvtMapboxCoordinates, MvtMapboxGeometry} from '../types';
3
+ import {MVTMapboxCoordinates, MVTMapboxGeometry} from '../types';
4
4
  import {readFeature, classifyRings} from '../../helpers/mapbox-util-functions';
5
5
 
6
6
  export default class VectorTileFeature {
@@ -39,7 +39,7 @@ export default class VectorTileFeature {
39
39
  }
40
40
 
41
41
  // eslint-disable-next-line complexity, max-statements
42
- loadGeometry(): MvtMapboxGeometry {
42
+ loadGeometry(): MVTMapboxGeometry {
43
43
  const pbf = this._pbf;
44
44
  pbf.pos = this._geometry;
45
45
 
@@ -163,7 +163,7 @@ export default class VectorTileFeature {
163
163
  type = `Multi${type}`;
164
164
  }
165
165
 
166
- const result: MvtMapboxCoordinates = {
166
+ const result: MVTMapboxCoordinates = {
167
167
  type: 'Feature',
168
168
  geometry: {
169
169
  type,
@@ -181,7 +181,7 @@ export default class VectorTileFeature {
181
181
 
182
182
  toGeoJSON(
183
183
  options: {x: number; y: number; z: number} | ((data: number[], feature: {extent: any}) => void)
184
- ): MvtMapboxCoordinates {
184
+ ): MVTMapboxCoordinates {
185
185
  if (typeof options === 'function') {
186
186
  return this._toGeoJSON(options);
187
187
  }
@@ -1,13 +1,19 @@
1
- // import {VectorTile} from '@mapbox/vector-tile';
1
+ import {flatGeojsonToBinary} from '@loaders.gl/gis';
2
+ import type {
3
+ FlatFeature,
4
+ Feature,
5
+ GeojsonGeometryInfo,
6
+ BinaryFeatures,
7
+ GeoJSONRowTable
8
+ } from '@loaders.gl/schema';
9
+ import Protobuf from 'pbf';
10
+
11
+ import type {MVTMapboxCoordinates, MVTOptions, MVTLoaderOptions} from '../lib/types';
12
+
2
13
  import VectorTile from './mapbox-vector-tile/vector-tile';
3
14
  import BinaryVectorTile from './binary-vector-tile/vector-tile';
4
-
5
- import {featuresToBinary} from './binary-vector-tile/features-to-binary';
6
- import Protobuf from 'pbf';
7
- import {MvtBinaryCoordinates, MvtMapboxCoordinates, MvtOptions} from '../lib/types';
8
15
  import VectorTileFeatureBinary from './binary-vector-tile/vector-tile-feature';
9
16
  import VectorTileFeatureMapBox from './mapbox-vector-tile/vector-tile-feature';
10
- import {LoaderOptions} from '@loaders.gl/loader-utils/';
11
17
 
12
18
  /**
13
19
  * Parse MVT arrayBuffer and return GeoJSON.
@@ -16,93 +22,130 @@ import {LoaderOptions} from '@loaders.gl/loader-utils/';
16
22
  * @param options
17
23
  * @returns A GeoJSON geometry object or a binary representation
18
24
  */
19
- export default function parseMVT(arrayBuffer: ArrayBuffer, options?: LoaderOptions) {
20
- options = normalizeOptions(options);
21
- const features: (MvtBinaryCoordinates | MvtMapboxCoordinates)[] = [];
22
-
23
- if (options) {
24
- const binary = options.gis.format === 'binary';
25
- const firstPassData = {
26
- pointPositionsCount: 0,
27
- pointFeaturesCount: 0,
28
- linePositionsCount: 0,
29
- linePathsCount: 0,
30
- lineFeaturesCount: 0,
31
- polygonPositionsCount: 0,
32
- polygonObjectsCount: 0,
33
- polygonRingsCount: 0,
34
- polygonFeaturesCount: 0
35
- };
36
-
37
- if (arrayBuffer.byteLength > 0) {
38
- const tile = binary
39
- ? new BinaryVectorTile(new Protobuf(arrayBuffer))
40
- : new VectorTile(new Protobuf(arrayBuffer));
41
- const loaderOptions = options.mvt;
42
-
43
- const selectedLayers = Array.isArray(loaderOptions.layers)
44
- ? loaderOptions.layers
45
- : Object.keys(tile.layers);
46
-
47
- selectedLayers.forEach((layerName: string) => {
48
- const vectorTileLayer = tile.layers[layerName];
49
- const featureOptions = {...loaderOptions, layerName};
50
-
51
- if (!vectorTileLayer) {
52
- return;
53
- }
54
-
55
- for (let i = 0; i < vectorTileLayer.length; i++) {
56
- const vectorTileFeature = vectorTileLayer.feature(i, firstPassData);
57
-
58
- const decodedFeature = binary
59
- ? getDecodedFeatureBinary(vectorTileFeature as VectorTileFeatureBinary, featureOptions)
60
- : getDecodedFeature(vectorTileFeature as VectorTileFeatureMapBox, featureOptions);
61
- features.push(decodedFeature);
62
- }
63
- });
25
+ export default function parseMVT(arrayBuffer: ArrayBuffer, options?: MVTLoaderOptions) {
26
+ const mvtOptions = normalizeOptions(options);
27
+
28
+ const shape = options?.gis?.format || options?.mvt?.shape;
29
+ switch (shape) {
30
+ case 'columnar-table': // binary + some JS arrays
31
+ return {shape: 'columnar-table', data: parseToBinary(arrayBuffer, mvtOptions)};
32
+ case 'geojson-row-table': {
33
+ const table: GeoJSONRowTable = {
34
+ shape: 'geojson-row-table',
35
+ data: parseToGeojson(arrayBuffer, mvtOptions)
36
+ };
37
+ return table;
64
38
  }
39
+ case 'geojson':
40
+ return parseToGeojson(arrayBuffer, mvtOptions);
41
+ case 'binary-geometry':
42
+ return parseToBinary(arrayBuffer, mvtOptions);
43
+ case 'binary':
44
+ return parseToBinary(arrayBuffer, mvtOptions);
45
+ default:
46
+ throw new Error(shape);
47
+ }
48
+ }
65
49
 
66
- if (binary) {
67
- const data = featuresToBinary(features as MvtBinaryCoordinates[], firstPassData);
68
- // Add the original byteLength (as a reasonable approximation of the size of the binary data)
69
- // TODO decide where to store extra fields like byteLength (header etc) and document
70
- // @ts-ignore
71
- data.byteLength = arrayBuffer.byteLength;
72
- return data;
73
- }
50
+ function parseToBinary(arrayBuffer: ArrayBuffer, options: MVTOptions): BinaryFeatures {
51
+ const [flatGeoJsonFeatures, geometryInfo] = parseToFlatGeoJson(arrayBuffer, options);
52
+
53
+ const binaryData = flatGeojsonToBinary(flatGeoJsonFeatures, geometryInfo);
54
+ // Add the original byteLength (as a reasonable approximation of the size of the binary data)
55
+ // TODO decide where to store extra fields like byteLength (header etc) and document
56
+ // @ts-ignore
57
+ binaryData.byteLength = arrayBuffer.byteLength;
58
+ return binaryData;
59
+ }
60
+
61
+ function parseToFlatGeoJson(
62
+ arrayBuffer: ArrayBuffer,
63
+ options: MVTOptions
64
+ ): [FlatFeature[], GeojsonGeometryInfo] {
65
+ const features: FlatFeature[] = [];
66
+ const geometryInfo: GeojsonGeometryInfo = {
67
+ coordLength: 2,
68
+ pointPositionsCount: 0,
69
+ pointFeaturesCount: 0,
70
+ linePositionsCount: 0,
71
+ linePathsCount: 0,
72
+ lineFeaturesCount: 0,
73
+ polygonPositionsCount: 0,
74
+ polygonObjectsCount: 0,
75
+ polygonRingsCount: 0,
76
+ polygonFeaturesCount: 0
77
+ };
78
+
79
+ if (arrayBuffer.byteLength <= 0) {
80
+ return [features, geometryInfo];
74
81
  }
75
- return features;
82
+
83
+ const tile = new BinaryVectorTile(new Protobuf(arrayBuffer));
84
+
85
+ const selectedLayers =
86
+ options && Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
87
+
88
+ selectedLayers.forEach((layerName: string) => {
89
+ const vectorTileLayer = tile.layers[layerName];
90
+ if (!vectorTileLayer) {
91
+ return;
92
+ }
93
+
94
+ for (let i = 0; i < vectorTileLayer.length; i++) {
95
+ const vectorTileFeature = vectorTileLayer.feature(i, geometryInfo);
96
+ const decodedFeature = getDecodedFeatureBinary(vectorTileFeature, options, layerName);
97
+ features.push(decodedFeature);
98
+ }
99
+ });
100
+
101
+ return [features, geometryInfo];
76
102
  }
77
103
 
78
- /**
79
- * @param options
80
- * @returns options
81
- */
82
- function normalizeOptions(options: LoaderOptions | undefined) {
83
- if (options) {
84
- options = {
85
- ...options,
86
- mvt: options.mvt || {},
87
- gis: options.gis || {}
88
- };
89
-
90
- // Validate
91
- const wgs84Coordinates = options.coordinates === 'wgs84';
92
- const {tileIndex} = options;
93
- const hasTileIndex =
94
- tileIndex &&
95
- Number.isFinite(tileIndex.x) &&
96
- Number.isFinite(tileIndex.y) &&
97
- Number.isFinite(tileIndex.z);
98
-
99
- if (wgs84Coordinates && !hasTileIndex) {
100
- throw new Error(
101
- 'MVT Loader: WGS84 coordinates need tileIndex property. Check documentation.'
102
- );
104
+ function parseToGeojson(arrayBuffer: ArrayBuffer, options: MVTOptions): Feature[] {
105
+ if (arrayBuffer.byteLength <= 0) {
106
+ return [];
107
+ }
108
+
109
+ const features: MVTMapboxCoordinates[] = [];
110
+ const tile = new VectorTile(new Protobuf(arrayBuffer));
111
+
112
+ const selectedLayers = Array.isArray(options.layers) ? options.layers : Object.keys(tile.layers);
113
+
114
+ selectedLayers.forEach((layerName: string) => {
115
+ const vectorTileLayer = tile.layers[layerName];
116
+ if (!vectorTileLayer) {
117
+ return;
103
118
  }
119
+
120
+ for (let i = 0; i < vectorTileLayer.length; i++) {
121
+ const vectorTileFeature = vectorTileLayer.feature(i);
122
+ const decodedFeature = getDecodedFeature(vectorTileFeature, options, layerName);
123
+ features.push(decodedFeature);
124
+ }
125
+ });
126
+
127
+ return features as Feature[];
128
+ }
129
+
130
+ function normalizeOptions(options?: MVTLoaderOptions): MVTOptions {
131
+ if (!options?.mvt) {
132
+ throw new Error('mvt options required');
104
133
  }
105
- return options;
134
+
135
+ // Validate
136
+ const wgs84Coordinates = options.mvt?.coordinates === 'wgs84';
137
+ const {tileIndex} = options.mvt;
138
+ const hasTileIndex =
139
+ tileIndex &&
140
+ Number.isFinite(tileIndex.x) &&
141
+ Number.isFinite(tileIndex.y) &&
142
+ Number.isFinite(tileIndex.z);
143
+
144
+ if (wgs84Coordinates && !hasTileIndex) {
145
+ throw new Error('MVT Loader: WGS84 coordinates need tileIndex property');
146
+ }
147
+
148
+ return options.mvt;
106
149
  }
107
150
 
108
151
  /**
@@ -112,15 +155,16 @@ function normalizeOptions(options: LoaderOptions | undefined) {
112
155
  */
113
156
  function getDecodedFeature(
114
157
  feature: VectorTileFeatureMapBox,
115
- options: MvtOptions
116
- ): MvtMapboxCoordinates {
158
+ options: MVTOptions,
159
+ layerName: string
160
+ ): MVTMapboxCoordinates {
117
161
  const decodedFeature = feature.toGeoJSON(
118
162
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinates
119
163
  );
120
164
 
121
165
  // Add layer name to GeoJSON properties
122
166
  if (options.layerProperty) {
123
- decodedFeature.properties[options.layerProperty] = options.layerName;
167
+ decodedFeature.properties[options.layerProperty] = layerName;
124
168
  }
125
169
 
126
170
  return decodedFeature;
@@ -133,15 +177,16 @@ function getDecodedFeature(
133
177
  */
134
178
  function getDecodedFeatureBinary(
135
179
  feature: VectorTileFeatureBinary,
136
- options: MvtOptions
137
- ): MvtBinaryCoordinates {
180
+ options: MVTOptions,
181
+ layerName: string
182
+ ): FlatFeature {
138
183
  const decodedFeature = feature.toBinaryCoordinates(
139
184
  options.coordinates === 'wgs84' ? options.tileIndex : transformToLocalCoordinatesBinary
140
185
  );
141
186
 
142
187
  // Add layer name to GeoJSON properties
143
- if (options.layerProperty) {
144
- decodedFeature.properties[options.layerProperty] = options.layerName;
188
+ if (options.layerProperty && decodedFeature.properties) {
189
+ decodedFeature.properties[options.layerProperty] = layerName;
145
190
  }
146
191
 
147
192
  return decodedFeature;