@math.gl/dggs 5.0.0-alpha.10

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 (118) hide show
  1. package/LICENSE +140 -0
  2. package/README.md +7 -0
  3. package/dist/a5-decoder.d.ts +15 -0
  4. package/dist/a5-decoder.d.ts.map +1 -0
  5. package/dist/a5-decoder.js +36 -0
  6. package/dist/a5-decoder.js.map +1 -0
  7. package/dist/a5.cjs +134 -0
  8. package/dist/a5.cjs.map +6 -0
  9. package/dist/a5.d.ts +2 -0
  10. package/dist/a5.d.ts.map +1 -0
  11. package/dist/a5.js +5 -0
  12. package/dist/a5.js.map +1 -0
  13. package/dist/converters/s2-to-obb-points.d.ts +15 -0
  14. package/dist/converters/s2-to-obb-points.d.ts.map +1 -0
  15. package/dist/converters/s2-to-obb-points.js +60 -0
  16. package/dist/converters/s2-to-obb-points.js.map +1 -0
  17. package/dist/converters/s2-to-region.d.ts +6 -0
  18. package/dist/converters/s2-to-region.d.ts.map +1 -0
  19. package/dist/converters/s2-to-region.js +27 -0
  20. package/dist/converters/s2-to-region.js.map +1 -0
  21. package/dist/dggs-decoder.d.ts +46 -0
  22. package/dist/dggs-decoder.d.ts.map +1 -0
  23. package/dist/dggs-decoder.js +52 -0
  24. package/dist/dggs-decoder.js.map +1 -0
  25. package/dist/geohash-decoder.d.ts +17 -0
  26. package/dist/geohash-decoder.d.ts.map +1 -0
  27. package/dist/geohash-decoder.js +88 -0
  28. package/dist/geohash-decoder.js.map +1 -0
  29. package/dist/geohash.cjs +181 -0
  30. package/dist/geohash.cjs.map +6 -0
  31. package/dist/geohash.d.ts +2 -0
  32. package/dist/geohash.d.ts.map +1 -0
  33. package/dist/geohash.js +5 -0
  34. package/dist/geohash.js.map +1 -0
  35. package/dist/h3-decoder.d.ts +15 -0
  36. package/dist/h3-decoder.d.ts.map +1 -0
  37. package/dist/h3-decoder.js +46 -0
  38. package/dist/h3-decoder.js.map +1 -0
  39. package/dist/h3.cjs +144 -0
  40. package/dist/h3.cjs.map +6 -0
  41. package/dist/h3.d.ts +2 -0
  42. package/dist/h3.d.ts.map +1 -0
  43. package/dist/h3.js +5 -0
  44. package/dist/h3.js.map +1 -0
  45. package/dist/index.cjs +863 -0
  46. package/dist/index.cjs.map +6 -0
  47. package/dist/index.d.ts +27 -0
  48. package/dist/index.d.ts.map +1 -0
  49. package/dist/index.js +54 -0
  50. package/dist/index.js.map +1 -0
  51. package/dist/plus-code-decoder.d.ts +16 -0
  52. package/dist/plus-code-decoder.d.ts.map +1 -0
  53. package/dist/plus-code-decoder.js +50 -0
  54. package/dist/plus-code-decoder.js.map +1 -0
  55. package/dist/plus-code.cjs +161 -0
  56. package/dist/plus-code.cjs.map +6 -0
  57. package/dist/plus-code.d.ts +2 -0
  58. package/dist/plus-code.d.ts.map +1 -0
  59. package/dist/plus-code.js +5 -0
  60. package/dist/plus-code.js.map +1 -0
  61. package/dist/quadkey-decoder.d.ts +14 -0
  62. package/dist/quadkey-decoder.d.ts.map +1 -0
  63. package/dist/quadkey-decoder.js +86 -0
  64. package/dist/quadkey-decoder.js.map +1 -0
  65. package/dist/quadkey.cjs +179 -0
  66. package/dist/quadkey.cjs.map +6 -0
  67. package/dist/quadkey.d.ts +2 -0
  68. package/dist/quadkey.d.ts.map +1 -0
  69. package/dist/quadkey.js +5 -0
  70. package/dist/quadkey.js.map +1 -0
  71. package/dist/s2-decoder.d.ts +36 -0
  72. package/dist/s2-decoder.d.ts.map +1 -0
  73. package/dist/s2-decoder.js +144 -0
  74. package/dist/s2-decoder.js.map +1 -0
  75. package/dist/s2-geometry/s2-geometry.d.ts +33 -0
  76. package/dist/s2-geometry/s2-geometry.d.ts.map +1 -0
  77. package/dist/s2-geometry/s2-geometry.js +249 -0
  78. package/dist/s2-geometry/s2-geometry.js.map +1 -0
  79. package/dist/s2-geometry/s2-to-boundary.d.ts +14 -0
  80. package/dist/s2-geometry/s2-to-boundary.d.ts.map +1 -0
  81. package/dist/s2-geometry/s2-to-boundary.js +60 -0
  82. package/dist/s2-geometry/s2-to-boundary.js.map +1 -0
  83. package/dist/s2-geometry/s2-token.d.ts +43 -0
  84. package/dist/s2-geometry/s2-token.d.ts.map +1 -0
  85. package/dist/s2-geometry/s2-token.js +127 -0
  86. package/dist/s2-geometry/s2-token.js.map +1 -0
  87. package/dist/s2.cjs +543 -0
  88. package/dist/s2.cjs.map +6 -0
  89. package/dist/s2.d.ts +5 -0
  90. package/dist/s2.d.ts.map +1 -0
  91. package/dist/s2.js +7 -0
  92. package/dist/s2.js.map +1 -0
  93. package/dist/unwrap-dggs-boundary.d.ts +12 -0
  94. package/dist/unwrap-dggs-boundary.d.ts.map +1 -0
  95. package/dist/unwrap-dggs-boundary.js +49 -0
  96. package/dist/unwrap-dggs-boundary.js.map +1 -0
  97. package/package.json +80 -0
  98. package/src/a5-decoder.ts +47 -0
  99. package/src/a5.ts +5 -0
  100. package/src/converters/s2-to-obb-points.ts +78 -0
  101. package/src/converters/s2-to-region.ts +30 -0
  102. package/src/dggs-decoder.ts +108 -0
  103. package/src/geohash-decoder.ts +104 -0
  104. package/src/geohash.ts +5 -0
  105. package/src/h3-decoder.ts +58 -0
  106. package/src/h3.ts +5 -0
  107. package/src/index.ts +72 -0
  108. package/src/open-location-code.d.ts +19 -0
  109. package/src/plus-code-decoder.ts +61 -0
  110. package/src/plus-code.ts +5 -0
  111. package/src/quadkey-decoder.ts +103 -0
  112. package/src/quadkey.ts +5 -0
  113. package/src/s2-decoder.ts +157 -0
  114. package/src/s2-geometry/s2-geometry.ts +303 -0
  115. package/src/s2-geometry/s2-to-boundary.ts +76 -0
  116. package/src/s2-geometry/s2-token.ts +139 -0
  117. package/src/s2.ts +15 -0
  118. package/src/unwrap-dggs-boundary.ts +54 -0
package/dist/index.cjs ADDED
@@ -0,0 +1,863 @@
1
+ var __create = Object.create;
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __getProtoOf = Object.getPrototypeOf;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __export = (target, all) => {
8
+ for (var name in all)
9
+ __defProp(target, name, { get: all[name], enumerable: true });
10
+ };
11
+ var __copyProps = (to, from, except, desc) => {
12
+ if (from && typeof from === "object" || typeof from === "function") {
13
+ for (let key of __getOwnPropNames(from))
14
+ if (!__hasOwnProp.call(to, key) && key !== except)
15
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
16
+ }
17
+ return to;
18
+ };
19
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
20
+ // If the importer is in node compatibility mode or this is not an ESM
21
+ // file that has been converted to a CommonJS file using a Babel-
22
+ // compatible transform (i.e. "__esModule" has not been set), then set
23
+ // "default" to the CommonJS "module.exports" for node compatibility.
24
+ isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
25
+ mod
26
+ ));
27
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
28
+
29
+ // dist/index.js
30
+ var index_exports = {};
31
+ __export(index_exports, {
32
+ A5Decoder: () => A5Decoder,
33
+ DGGS_DECODERS: () => DGGS_DECODERS,
34
+ GeohashDecoder: () => GeohashDecoder,
35
+ H3Decoder: () => H3Decoder,
36
+ PlusCodeDecoder: () => PlusCodeDecoder,
37
+ QuadkeyDecoder: () => QuadkeyDecoder,
38
+ S2Decoder: () => S2Decoder,
39
+ findDGGSCellColumn: () => findDGGSCellColumn,
40
+ getS2BoundaryFlat: () => getS2BoundaryFlat,
41
+ getS2Bounds: () => getS2Bounds,
42
+ getS2ChildIndex: () => getS2ChildIndex,
43
+ getS2DescendantIndex: () => getS2DescendantIndex,
44
+ getS2IndexFromCell: () => getS2IndexFromCell,
45
+ getS2IndexFromToken: () => getS2IndexFromToken,
46
+ getS2Level: () => getS2Level,
47
+ getS2LngLat: () => getS2LngLat,
48
+ getS2TokenFromIndex: () => getS2TokenFromIndex,
49
+ isS2IndexValid: () => isS2IndexValid,
50
+ isS2TokenValid: () => isS2TokenValid,
51
+ quadkeyToWorldBounds: () => quadkeyToWorldBounds,
52
+ unwrapDGGSBoundary: () => unwrapDGGSBoundary
53
+ });
54
+ module.exports = __toCommonJS(index_exports);
55
+
56
+ // dist/a5-decoder.js
57
+ var import_a5_js = require("a5-js");
58
+
59
+ // dist/unwrap-dggs-boundary.js
60
+ function unwrapDGGSBoundary(boundary, referenceLongitude) {
61
+ const result = boundary.map(([longitude, latitude]) => [longitude, latitude]);
62
+ if (result.length === 0) {
63
+ return result;
64
+ }
65
+ let west = Infinity;
66
+ let east = -Infinity;
67
+ for (const [longitude] of boundary) {
68
+ west = Math.min(west, longitude);
69
+ east = Math.max(east, longitude);
70
+ }
71
+ if (east - west >= 360) {
72
+ return result;
73
+ }
74
+ let previous = referenceLongitude ?? result[0][0];
75
+ for (const point of result) {
76
+ const delta = point[0] - previous;
77
+ if (delta > 180) {
78
+ point[0] -= 360 * Math.ceil((delta - 180) / 360);
79
+ } else if (delta < -180) {
80
+ point[0] += 360 * Math.ceil((-delta - 180) / 360);
81
+ }
82
+ previous = point[0];
83
+ }
84
+ const first = boundary[0];
85
+ const last = boundary[boundary.length - 1];
86
+ if (first[0] === last[0] && first[1] === last[1]) {
87
+ if (Math.abs(result[result.length - 1][0] - result[0][0]) > 180) {
88
+ return boundary.map(([longitude, latitude]) => [longitude, latitude]);
89
+ }
90
+ result[result.length - 1] = [...result[0]];
91
+ }
92
+ return result;
93
+ }
94
+
95
+ // dist/dggs-decoder.js
96
+ function getDGGSCellToken(cell, gridName) {
97
+ if (typeof cell !== "string") {
98
+ throw new Error(`${gridName} cell identifiers must be strings`);
99
+ }
100
+ return cell;
101
+ }
102
+ function getDGGSCellBounds(boundary) {
103
+ let west = Infinity;
104
+ let south = Infinity;
105
+ let east = -Infinity;
106
+ let north = -Infinity;
107
+ for (const [longitude, latitude] of boundary) {
108
+ west = Math.min(west, longitude);
109
+ south = Math.min(south, latitude);
110
+ east = Math.max(east, longitude);
111
+ north = Math.max(north, latitude);
112
+ }
113
+ return [
114
+ [west, south],
115
+ [east, north]
116
+ ];
117
+ }
118
+ function withDGGSBoundaryOptions(decoder) {
119
+ const cellToBoundary3 = (cell, options) => {
120
+ const boundary = decoder.cellToBoundary(cell);
121
+ return (options == null ? void 0 : options.unwrap) ? unwrapDGGSBoundary(boundary, options.referenceLongitude) : boundary;
122
+ };
123
+ return {
124
+ ...decoder,
125
+ cellToBoundary: cellToBoundary3,
126
+ cellToBoundaryFlat: (cell, options) => (options == null ? void 0 : options.unwrap) ? cellToBoundary3(cell, options).flat() : decoder.cellToBoundaryFlat(cell),
127
+ cellToBounds: (cell, options) => {
128
+ const bounds = decoder.cellToBounds(cell);
129
+ if (!(options == null ? void 0 : options.unwrap) || bounds[1][0] - bounds[0][0] >= 360) {
130
+ return bounds;
131
+ }
132
+ const unwrapped = getDGGSCellBounds(cellToBoundary3(cell, options));
133
+ unwrapped[0][1] = bounds[0][1];
134
+ unwrapped[1][1] = bounds[1][1];
135
+ return unwrapped;
136
+ }
137
+ };
138
+ }
139
+
140
+ // dist/a5-decoder.js
141
+ var A5Decoder = withDGGSBoundaryOptions({
142
+ name: "a5",
143
+ hasNumericRepresentation: true,
144
+ cellColumnNames: [
145
+ "a5",
146
+ "a5Token",
147
+ "a5_token",
148
+ "a5Cell",
149
+ "a5_cell",
150
+ "a5CellId",
151
+ "a5_cell_id",
152
+ "pentagon"
153
+ ],
154
+ tokenToCell: (token) => (0, import_a5_js.hexToU64)(removeHexPrefix(token)),
155
+ cellToToken: (cell) => typeof cell === "string" ? removeHexPrefix(cell) : (0, import_a5_js.u64ToHex)(cell),
156
+ cellToLngLat: (cell) => (0, import_a5_js.cellToLonLat)(getA5CellIndex(cell)),
157
+ cellToBoundary: (cell) => getA5Boundary(cell),
158
+ cellToBoundaryFlat: (cell) => getA5Boundary(cell).flat(),
159
+ cellToBounds: (cell) => getDGGSCellBounds(getA5Boundary(cell))
160
+ });
161
+ function getA5CellIndex(cell) {
162
+ return typeof cell === "bigint" ? cell : (0, import_a5_js.hexToU64)(removeHexPrefix(cell));
163
+ }
164
+ function getA5Boundary(cell) {
165
+ return (0, import_a5_js.cellToBoundary)(getA5CellIndex(cell)).map(([longitude, latitude]) => [longitude, latitude]);
166
+ }
167
+ function removeHexPrefix(token) {
168
+ return token.startsWith("0x") || token.startsWith("0X") ? token.slice(2) : token;
169
+ }
170
+
171
+ // dist/geohash-decoder.js
172
+ var GeohashDecoder = withDGGSBoundaryOptions({
173
+ name: "geohash",
174
+ hasNumericRepresentation: false,
175
+ cellColumnNames: ["geohash", "geohashId", "geohash_id"],
176
+ cellToLngLat: (cell) => getGeohashLngLat(getDGGSCellToken(cell, "GeoHash")),
177
+ cellToBoundary: (cell) => getGeohashBoundary(getDGGSCellToken(cell, "GeoHash")),
178
+ cellToBoundaryFlat: (cell) => getGeohashBoundaryFlat(getDGGSCellToken(cell, "GeoHash")),
179
+ cellToBounds: (cell) => getGeohashBounds(getDGGSCellToken(cell, "GeoHash"))
180
+ });
181
+ var BASE32_CODES = "0123456789bcdefghjkmnpqrstuvwxyz";
182
+ var BASE32_CODES_DICT = {};
183
+ for (let i = 0; i < BASE32_CODES.length; i++) {
184
+ BASE32_CODES_DICT[BASE32_CODES.charAt(i)] = i;
185
+ }
186
+ var MIN_LAT = -90;
187
+ var MAX_LAT = 90;
188
+ var MIN_LON = -180;
189
+ var MAX_LON = 180;
190
+ function getGeohashLngLat(geohash) {
191
+ const [[w, s], [e, n]] = getGeohashBounds(geohash);
192
+ return [(e + w) / 2, (n + s) / 2];
193
+ }
194
+ function getGeohashBoundary(geohash) {
195
+ const [[w, s], [e, n]] = getGeohashBounds(geohash);
196
+ return [
197
+ [e, n],
198
+ [e, s],
199
+ [w, s],
200
+ [w, n],
201
+ [e, n]
202
+ ];
203
+ }
204
+ function getGeohashBoundaryFlat(geohash) {
205
+ const [[w, s], [e, n]] = getGeohashBounds(geohash);
206
+ return [e, n, e, s, w, s, w, n, e, n];
207
+ }
208
+ function getGeohashBounds(geohash) {
209
+ let isLon = true;
210
+ let maxLat = MAX_LAT;
211
+ let minLat = MIN_LAT;
212
+ let maxLon = MAX_LON;
213
+ let minLon = MIN_LON;
214
+ let mid;
215
+ let hashValue = 0;
216
+ for (let i = 0, l = geohash.length; i < l; i++) {
217
+ const code = geohash[i].toLowerCase();
218
+ hashValue = BASE32_CODES_DICT[code];
219
+ for (let bits = 4; bits >= 0; bits--) {
220
+ const bit = hashValue >> bits & 1;
221
+ if (isLon) {
222
+ mid = (maxLon + minLon) / 2;
223
+ if (bit === 1) {
224
+ minLon = mid;
225
+ } else {
226
+ maxLon = mid;
227
+ }
228
+ } else {
229
+ mid = (maxLat + minLat) / 2;
230
+ if (bit === 1) {
231
+ minLat = mid;
232
+ } else {
233
+ maxLat = mid;
234
+ }
235
+ }
236
+ isLon = !isLon;
237
+ }
238
+ }
239
+ return [
240
+ [minLon, minLat],
241
+ [maxLon, maxLat]
242
+ ];
243
+ }
244
+
245
+ // dist/h3-decoder.js
246
+ var import_h3_js = require("h3-js");
247
+ var H3Decoder = withDGGSBoundaryOptions({
248
+ name: "h3",
249
+ hasNumericRepresentation: true,
250
+ cellColumnNames: [
251
+ "h3",
252
+ "h3Index",
253
+ "h3_index",
254
+ "h3Cell",
255
+ "h3_cell",
256
+ "h3CellId",
257
+ "h3_cell_id",
258
+ "hexagon"
259
+ ],
260
+ tokenToCell: (token) => getBigIntFromHexToken(token),
261
+ cellToToken: (cell) => getH3Token(cell),
262
+ cellToLngLat: (cell) => getH3LngLat(cell),
263
+ cellToBoundary: (cell) => getH3Boundary(cell),
264
+ cellToBoundaryFlat: (cell) => getH3Boundary(cell).flat(),
265
+ cellToBounds: (cell) => getDGGSCellBounds(getH3Boundary(cell))
266
+ });
267
+ function getH3Token(cell) {
268
+ return typeof cell === "bigint" ? cell.toString(16) : removeHexPrefix2(cell);
269
+ }
270
+ function getH3LngLat(cell) {
271
+ const [latitude, longitude] = (0, import_h3_js.cellToLatLng)(getH3Token(cell));
272
+ return [longitude, latitude];
273
+ }
274
+ function getH3Boundary(cell) {
275
+ return (0, import_h3_js.cellToBoundary)(getH3Token(cell), true).map(([longitude, latitude]) => [
276
+ longitude,
277
+ latitude
278
+ ]);
279
+ }
280
+ function getBigIntFromHexToken(token) {
281
+ return BigInt(`0x${removeHexPrefix2(token)}`);
282
+ }
283
+ function removeHexPrefix2(token) {
284
+ return token.startsWith("0x") || token.startsWith("0X") ? token.slice(2) : token;
285
+ }
286
+
287
+ // dist/plus-code-decoder.js
288
+ var import_open_location_code = __toESM(require("open-location-code"), 1);
289
+ var openLocationCode = new import_open_location_code.default.OpenLocationCode();
290
+ var PlusCodeDecoder = withDGGSBoundaryOptions({
291
+ name: "plus-code",
292
+ hasNumericRepresentation: false,
293
+ cellColumnNames: ["plusCode", "plus_code", "openLocationCode", "open_location_code", "olc"],
294
+ cellToLngLat: (cell) => getPlusCodeLngLat(cell),
295
+ cellToBoundary: (cell) => getPlusCodeBoundary(decodePlusCode(cell)),
296
+ cellToBoundaryFlat: (cell) => {
297
+ const area = decodePlusCode(cell);
298
+ return getPlusCodeBoundary(area).flat();
299
+ },
300
+ cellToBounds: (cell) => {
301
+ const area = decodePlusCode(cell);
302
+ return [
303
+ [area.longitudeLo, area.latitudeLo],
304
+ [area.longitudeHi, area.latitudeHi]
305
+ ];
306
+ }
307
+ });
308
+ function getPlusCodeLngLat(cell) {
309
+ const area = decodePlusCode(cell);
310
+ return [area.longitudeCenter, area.latitudeCenter];
311
+ }
312
+ function decodePlusCode(cell) {
313
+ const code = getDGGSCellToken(cell, "Plus Code");
314
+ if (!openLocationCode.isFull(code)) {
315
+ throw new Error(`Plus Code decoder requires a full code: ${code}`);
316
+ }
317
+ return openLocationCode.decode(code);
318
+ }
319
+ function getPlusCodeBoundary(area) {
320
+ const { longitudeLo: west, latitudeLo: south, longitudeHi: east, latitudeHi: north } = area;
321
+ return [
322
+ [east, north],
323
+ [east, south],
324
+ [west, south],
325
+ [west, north],
326
+ [east, north]
327
+ ];
328
+ }
329
+
330
+ // dist/quadkey-decoder.js
331
+ var TILE_SIZE = 512;
332
+ var QuadkeyDecoder = withDGGSBoundaryOptions({
333
+ name: "quadkey",
334
+ hasNumericRepresentation: false,
335
+ cellColumnNames: ["quadkey", "quadkeyId", "quadkey_id"],
336
+ cellToLngLat: (cell) => getQuadkeyLngLat(getDGGSCellToken(cell, "Quadkey")),
337
+ cellToBoundary: (cell) => getQuadkeyBoundary(getDGGSCellToken(cell, "Quadkey")),
338
+ cellToBoundaryFlat: (cell) => getQuadkeyBoundaryFlat(getDGGSCellToken(cell, "Quadkey")),
339
+ cellToBounds: (cell) => getQuadkeyBounds(getDGGSCellToken(cell, "Quadkey"))
340
+ });
341
+ function getQuadkeyLngLat(quadkey) {
342
+ const [topLeft, bottomRight] = quadkeyToWorldBounds(quadkey);
343
+ const [w, n] = worldToLngLat(topLeft);
344
+ const [e, s] = worldToLngLat(bottomRight);
345
+ return [(e + w) / 2, (s + n) / 2];
346
+ }
347
+ function getQuadkeyBoundary(quadkey) {
348
+ const [topLeft, bottomRight] = quadkeyToWorldBounds(quadkey);
349
+ const [w, n] = worldToLngLat(topLeft);
350
+ const [e, s] = worldToLngLat(bottomRight);
351
+ return [
352
+ [e, n],
353
+ [e, s],
354
+ [w, s],
355
+ [w, n],
356
+ [e, n]
357
+ ];
358
+ }
359
+ function getQuadkeyBoundaryFlat(quadkey) {
360
+ const [topLeft, bottomRight] = quadkeyToWorldBounds(quadkey);
361
+ const [w, n] = worldToLngLat(topLeft);
362
+ const [e, s] = worldToLngLat(bottomRight);
363
+ return [e, n, e, s, w, s, w, n, e, n];
364
+ }
365
+ function getQuadkeyBounds(quadkey) {
366
+ const [topLeft, bottomRight] = quadkeyToWorldBounds(quadkey);
367
+ const [w, n] = worldToLngLat(topLeft);
368
+ const [e, s] = worldToLngLat(bottomRight);
369
+ return [
370
+ [w, s],
371
+ [e, n]
372
+ ];
373
+ }
374
+ function quadkeyToWorldBounds(quadkey) {
375
+ let x = 0;
376
+ let y = 0;
377
+ let mask = 1 << quadkey.length;
378
+ const scale = mask / TILE_SIZE;
379
+ for (let i = 0; i < quadkey.length; i++) {
380
+ mask >>= 1;
381
+ const q = parseInt(quadkey[i], 10);
382
+ if (q % 2)
383
+ x |= mask;
384
+ if (q > 1)
385
+ y |= mask;
386
+ }
387
+ return [
388
+ [x / scale, TILE_SIZE - y / scale],
389
+ [(x + 0.99) / scale, TILE_SIZE - (y + 0.99) / scale]
390
+ ];
391
+ }
392
+ var PI = Math.PI;
393
+ var PI_4 = PI / 4;
394
+ var RADIANS_TO_DEGREES = 180 / PI;
395
+ function worldToLngLat(xy) {
396
+ const [x, y] = xy;
397
+ const lambda2 = x / TILE_SIZE * (2 * PI) - PI;
398
+ const phi2 = 2 * (Math.atan(Math.exp(y / TILE_SIZE * (2 * PI) - PI)) - PI_4);
399
+ return [lambda2 * RADIANS_TO_DEGREES, phi2 * RADIANS_TO_DEGREES];
400
+ }
401
+
402
+ // dist/s2-geometry/s2-token.js
403
+ var MAXIMUM_TOKEN_LENGTH = 16;
404
+ var MAXIMUM_S2_INDEX = (1n << 64n) - 1n;
405
+ var VALID_LOWEST_ON_BITS = 0x1555555555555555n;
406
+ var MAXIMUM_LEVEL = 30;
407
+ function getS2IndexFromToken(token) {
408
+ if (token === "X") {
409
+ return 0n;
410
+ }
411
+ if (!isS2TokenValid(token)) {
412
+ throw new Error(`Invalid S2 token: ${token}`);
413
+ }
414
+ const paddedToken = token.padEnd(MAXIMUM_TOKEN_LENGTH, "0");
415
+ return BigInt(`0x${paddedToken}`);
416
+ }
417
+ function getS2TokenFromIndex(cellId) {
418
+ if (cellId === 0n) {
419
+ return "X";
420
+ }
421
+ if (!isS2IndexValid(cellId)) {
422
+ throw new Error(`Invalid S2 index: ${cellId}`);
423
+ }
424
+ let numZeroDigits = countTrailingZeros(cellId);
425
+ const remainder = numZeroDigits % 4;
426
+ numZeroDigits = (numZeroDigits - remainder) / 4;
427
+ const trailingZeroHexChars = numZeroDigits;
428
+ numZeroDigits *= 4;
429
+ const x = cellId >> BigInt(numZeroDigits);
430
+ const hexString = x.toString(16).replace(/0+$/, "");
431
+ const zeroString = Array(17 - trailingZeroHexChars - hexString.length).join("0");
432
+ return zeroString + hexString;
433
+ }
434
+ function isS2IndexValid(s2Index) {
435
+ if (typeof s2Index !== "bigint" || s2Index <= 0n || s2Index > MAXIMUM_S2_INDEX) {
436
+ return false;
437
+ }
438
+ const face = s2Index >> 61n;
439
+ const lowestOnBit = s2Index & -s2Index;
440
+ return face < 6n && (lowestOnBit & VALID_LOWEST_ON_BITS) !== 0n;
441
+ }
442
+ function isS2TokenValid(token) {
443
+ if (typeof token !== "string" || !/^[0-9a-fA-F]{1,16}$/.test(token) || token.endsWith("0")) {
444
+ return false;
445
+ }
446
+ const paddedToken = token.padEnd(MAXIMUM_TOKEN_LENGTH, "0");
447
+ return isS2IndexValid(BigInt(`0x${paddedToken}`));
448
+ }
449
+ function getS2Level(s2Index) {
450
+ if (!isS2IndexValid(s2Index)) {
451
+ throw new Error(`Invalid S2 index: ${s2Index}`);
452
+ }
453
+ return (60 - countTrailingZeros(s2Index)) / 2;
454
+ }
455
+ function getS2ChildIndex(s2Index, index) {
456
+ if (!isS2IndexValid(s2Index)) {
457
+ throw new Error(`Invalid S2 index: ${s2Index}`);
458
+ }
459
+ if (!Number.isInteger(index) || index < 0 || index > 3) {
460
+ throw new Error(`Invalid S2 child index: ${index}`);
461
+ }
462
+ if (getS2Level(s2Index) === MAXIMUM_LEVEL) {
463
+ throw new Error("Cannot subdivide an S2 leaf cell");
464
+ }
465
+ const newLsb = lsb(s2Index) >> 2n;
466
+ const childCellId = s2Index + BigInt(2 * index + 1 - 4) * newLsb;
467
+ return childCellId;
468
+ }
469
+ function lsb(cellId) {
470
+ return cellId & -cellId;
471
+ }
472
+ function countTrailingZeros(n) {
473
+ let count = 0;
474
+ while (n % 2n === 0n) {
475
+ n /= 2n;
476
+ count++;
477
+ }
478
+ return count;
479
+ }
480
+
481
+ // dist/s2-geometry/s2-geometry.js
482
+ function getS2Cell(s2Index) {
483
+ if (!isS2IndexValid(s2Index)) {
484
+ throw new Error(`Invalid S2 index: ${s2Index}`);
485
+ }
486
+ const key = toHilbertQuadkey(s2Index);
487
+ const s2cell = fromHilbertQuadKey(key);
488
+ return s2cell;
489
+ }
490
+ var FACE_BITS = 3;
491
+ var MAX_LEVEL = 30;
492
+ var POS_BITS = 2 * MAX_LEVEL + 1;
493
+ var RADIAN_TO_DEGREE = 180 / Math.PI;
494
+ var HILBERT_MAP = {
495
+ a: [
496
+ [0, "d"],
497
+ [1, "a"],
498
+ [3, "b"],
499
+ [2, "a"]
500
+ ],
501
+ b: [
502
+ [2, "b"],
503
+ [1, "b"],
504
+ [3, "a"],
505
+ [0, "c"]
506
+ ],
507
+ c: [
508
+ [2, "c"],
509
+ [3, "d"],
510
+ [1, "c"],
511
+ [0, "b"]
512
+ ],
513
+ d: [
514
+ [0, "a"],
515
+ [3, "c"],
516
+ [1, "d"],
517
+ [2, "d"]
518
+ ]
519
+ };
520
+ function fromHilbertQuadKey(hilbertQuadkey) {
521
+ if (!/^[0-5]\/[0-3]{0,30}$/.test(hilbertQuadkey)) {
522
+ throw new Error(`Invalid S2 Hilbert quadkey: ${hilbertQuadkey}`);
523
+ }
524
+ const parts = hilbertQuadkey.split("/");
525
+ const face = parseInt(parts[0], 10);
526
+ const position = parts[1] || "";
527
+ const maxLevel = position.length;
528
+ let level = 0;
529
+ const point = [0, 0];
530
+ for (let i = maxLevel - 1; i >= 0; i--) {
531
+ level = maxLevel - i;
532
+ const bit = position[i];
533
+ let rx = 0;
534
+ let ry = 0;
535
+ if (bit === "1") {
536
+ ry = 1;
537
+ } else if (bit === "2") {
538
+ rx = 1;
539
+ ry = 1;
540
+ } else if (bit === "3") {
541
+ rx = 1;
542
+ }
543
+ const val = Math.pow(2, level - 1);
544
+ rotateAndFlipQuadrant(val, point, rx, ry);
545
+ point[0] += val * rx;
546
+ point[1] += val * ry;
547
+ }
548
+ if (face % 2 === 1) {
549
+ const t = point[0];
550
+ point[0] = point[1];
551
+ point[1] = t;
552
+ }
553
+ return { face, ij: point, level };
554
+ }
555
+ function toHilbertQuadkey(id) {
556
+ if (!isS2IndexValid(id)) {
557
+ throw new Error(`Invalid S2 index: ${id}`);
558
+ }
559
+ let bin = id.toString(2);
560
+ while (bin.length < FACE_BITS + POS_BITS) {
561
+ bin = "0" + bin;
562
+ }
563
+ const lsbIndex = bin.lastIndexOf("1");
564
+ const faceB = bin.substring(0, 3);
565
+ const posB = bin.substring(3, lsbIndex);
566
+ const levelN = posB.length / 2;
567
+ const faceS = BigInt(`0b${faceB}`).toString(10);
568
+ let posS = "";
569
+ if (levelN !== 0) {
570
+ posS = BigInt(`0b${posB}`).toString(4);
571
+ while (posS.length < levelN) {
572
+ posS = "0" + posS;
573
+ }
574
+ }
575
+ return `${faceS}/${posS}`;
576
+ }
577
+ function getS2IndexFromCell(s2Cell) {
578
+ const { face, ij, level } = s2Cell;
579
+ if (!Number.isInteger(face) || face < 0 || face > 5) {
580
+ throw new Error(`Invalid S2 face: ${face}`);
581
+ }
582
+ if (!Number.isInteger(level) || level < 0 || level > MAX_LEVEL) {
583
+ throw new Error(`Invalid S2 level: ${level}`);
584
+ }
585
+ const maximumCoordinate = 2 ** level;
586
+ if (!Number.isInteger(ij[0]) || !Number.isInteger(ij[1]) || ij[0] < 0 || ij[1] < 0 || ij[0] >= maximumCoordinate || ij[1] >= maximumCoordinate) {
587
+ throw new Error(`Invalid S2 IJ coordinates: ${ij[0]}, ${ij[1]}`);
588
+ }
589
+ const positions = getHilbertQuadList(ij[0], ij[1], level, face);
590
+ let s2Index = BigInt(face) << 61n;
591
+ for (let positionIndex = 0; positionIndex < positions.length; positionIndex++) {
592
+ const bitOffset = 59 - positionIndex * 2;
593
+ s2Index |= BigInt(positions[positionIndex]) << BigInt(bitOffset);
594
+ }
595
+ s2Index |= 1n << BigInt(60 - level * 2);
596
+ return s2Index;
597
+ }
598
+ function getS2DescendantIndex(rootIndex, relativeLevel, x, y) {
599
+ const rootCell = getS2Cell(rootIndex);
600
+ if (!Number.isInteger(relativeLevel) || relativeLevel < 0 || rootCell.level + relativeLevel > MAX_LEVEL) {
601
+ throw new Error(`Invalid S2 relative level: ${relativeLevel}`);
602
+ }
603
+ const divisionCount = 2 ** relativeLevel;
604
+ if (!Number.isInteger(x) || !Number.isInteger(y) || x < 0 || y < 0 || x >= divisionCount || y >= divisionCount) {
605
+ throw new Error(`Invalid S2 descendant coordinates: ${x}, ${y}`);
606
+ }
607
+ return getS2IndexFromCell({
608
+ face: rootCell.face,
609
+ ij: [rootCell.ij[0] * divisionCount + x, rootCell.ij[1] * divisionCount + y],
610
+ level: rootCell.level + relativeLevel
611
+ });
612
+ }
613
+ function IJToST(ij, order, offsets) {
614
+ const maxSize = 1 << order;
615
+ return [(ij[0] + offsets[0]) / maxSize, (ij[1] + offsets[1]) / maxSize];
616
+ }
617
+ function singleSTtoUV(st) {
618
+ if (st >= 0.5) {
619
+ return 1 / 3 * (4 * st * st - 1);
620
+ }
621
+ return 1 / 3 * (1 - 4 * (1 - st) * (1 - st));
622
+ }
623
+ function STToUV(st) {
624
+ return [singleSTtoUV(st[0]), singleSTtoUV(st[1])];
625
+ }
626
+ function FaceUVToXYZ(face, [u, v]) {
627
+ switch (face) {
628
+ case 0:
629
+ return [1, u, v];
630
+ case 1:
631
+ return [-u, 1, v];
632
+ case 2:
633
+ return [-u, -v, 1];
634
+ case 3:
635
+ return [-1, -v, -u];
636
+ case 4:
637
+ return [v, -1, -u];
638
+ case 5:
639
+ return [v, u, -1];
640
+ default:
641
+ throw new Error("Invalid face");
642
+ }
643
+ }
644
+ function XYZToLngLat([x, y, z]) {
645
+ const lat = Math.atan2(z, Math.sqrt(x * x + y * y));
646
+ const lng = Math.atan2(y, x);
647
+ return [lng * RADIAN_TO_DEGREE, lat * RADIAN_TO_DEGREE];
648
+ }
649
+ function rotateAndFlipQuadrant(n, point, rx, ry) {
650
+ if (ry === 0) {
651
+ if (rx === 1) {
652
+ point[0] = n - 1 - point[0];
653
+ point[1] = n - 1 - point[1];
654
+ }
655
+ const x = point[0];
656
+ point[0] = point[1];
657
+ point[1] = x;
658
+ }
659
+ }
660
+ function getHilbertQuadList(x, y, level, face) {
661
+ let orientation = face % 2 === 1 ? "d" : "a";
662
+ const positions = [];
663
+ for (let bit = level - 1; bit >= 0; bit--) {
664
+ const mask = 1 << bit;
665
+ const quadrantX = (x & mask) !== 0 ? 1 : 0;
666
+ const quadrantY = (y & mask) !== 0 ? 1 : 0;
667
+ const [position, nextOrientation] = HILBERT_MAP[orientation][quadrantX * 2 + quadrantY];
668
+ positions.push(position);
669
+ orientation = nextOrientation;
670
+ }
671
+ return positions;
672
+ }
673
+
674
+ // dist/s2-geometry/s2-to-boundary.js
675
+ var MAX_RESOLUTION = 100;
676
+ function getS2GeoBounds(s2Index) {
677
+ const s2Cell = getS2Cell(s2Index);
678
+ return getS2GeoBoundsFromCell(s2Cell);
679
+ }
680
+ function getS2GeoBoundsFromCell({ face, ij, level }) {
681
+ const offsets = [
682
+ [0, 0],
683
+ [0, 1],
684
+ [1, 1],
685
+ [1, 0],
686
+ [0, 0]
687
+ ];
688
+ const resolution = Math.max(1, Math.ceil(MAX_RESOLUTION * Math.pow(2, -level)));
689
+ const result = new Float64Array(4 * resolution * 2 + 2);
690
+ let ptIndex = 0;
691
+ let prevLng = 0;
692
+ for (let i = 0; i < 4; i++) {
693
+ const offset = offsets[i].slice(0);
694
+ const nextOffset = offsets[i + 1];
695
+ const stepI = (nextOffset[0] - offset[0]) / resolution;
696
+ const stepJ = (nextOffset[1] - offset[1]) / resolution;
697
+ for (let j = 0; j < resolution; j++) {
698
+ offset[0] += stepI;
699
+ offset[1] += stepJ;
700
+ const st = IJToST(ij, level, offset);
701
+ const uv = STToUV(st);
702
+ const xyz = FaceUVToXYZ(face, uv);
703
+ const lngLat = XYZToLngLat(xyz);
704
+ if (Math.abs(lngLat[1]) > 89.999) {
705
+ lngLat[0] = prevLng;
706
+ }
707
+ const deltaLng = lngLat[0] - prevLng;
708
+ lngLat[0] += deltaLng > 180 ? -360 : deltaLng < -180 ? 360 : 0;
709
+ result[ptIndex++] = lngLat[0];
710
+ result[ptIndex++] = lngLat[1];
711
+ prevLng = lngLat[0];
712
+ }
713
+ }
714
+ result[ptIndex++] = result[0];
715
+ result[ptIndex++] = result[1];
716
+ return result;
717
+ }
718
+
719
+ // dist/s2-decoder.js
720
+ var S2Decoder = withDGGSBoundaryOptions({
721
+ name: "s2",
722
+ hasNumericRepresentation: true,
723
+ cellColumnNames: ["s2", "s2Token", "s2_token", "s2Cell", "s2_cell", "s2CellId", "s2_cell_id"],
724
+ tokenToCell: (s2Token) => getS2IndexFromToken(s2Token),
725
+ cellToToken: (cell) => getS2TokenFromIndex(getS2CellIndex(cell)),
726
+ cellToLngLat: (cell) => getS2LngLat(cell),
727
+ cellToBoundary: (cell) => getS2Boundary(cell),
728
+ cellToBoundaryFlat: (cell) => getS2BoundaryFlat(cell),
729
+ cellToBounds: (cell) => getS2Bounds(cell)
730
+ });
731
+ function getS2LngLat(cell) {
732
+ const s2Index = getS2CellIndex(cell);
733
+ const s2Cell = getS2Cell(s2Index);
734
+ const st = IJToST(s2Cell.ij, s2Cell.level, [0.5, 0.5]);
735
+ const uv = STToUV(st);
736
+ const xyz = FaceUVToXYZ(s2Cell.face, uv);
737
+ const lngLat = XYZToLngLat(xyz);
738
+ return lngLat;
739
+ }
740
+ function getS2BoundaryFlat(cell) {
741
+ const s2Index = getS2CellIndex(cell);
742
+ const float64Array = getS2GeoBounds(s2Index);
743
+ return Array.from(float64Array);
744
+ }
745
+ function getS2Boundary(cell) {
746
+ const flatBoundary = getS2BoundaryFlat(cell);
747
+ const boundary = [];
748
+ for (let i = 0; i < flatBoundary.length; i += 2) {
749
+ boundary.push([flatBoundary[i], flatBoundary[i + 1]]);
750
+ }
751
+ return boundary;
752
+ }
753
+ function getS2Bounds(cell) {
754
+ const s2Cell = getS2Cell(getS2CellIndex(cell));
755
+ if (s2Cell.level === 0) {
756
+ return getS2RootBounds(s2Cell.face);
757
+ }
758
+ const offsets = [
759
+ [0, 0],
760
+ [0, 1],
761
+ [1, 1],
762
+ [1, 0]
763
+ ];
764
+ let west = Infinity;
765
+ let south = Infinity;
766
+ let east = -Infinity;
767
+ let north = -Infinity;
768
+ let previousLongitude;
769
+ for (const offset of offsets) {
770
+ const st = IJToST(s2Cell.ij, s2Cell.level, offset);
771
+ const uv = STToUV(st);
772
+ const [x, y, z] = FaceUVToXYZ(s2Cell.face, uv);
773
+ let [longitude, latitude] = XYZToLngLat([x, y, z]);
774
+ if (x === 0 && y === 0 && previousLongitude !== void 0) {
775
+ longitude = previousLongitude;
776
+ }
777
+ if (previousLongitude !== void 0) {
778
+ const longitudeDelta = longitude - previousLongitude;
779
+ longitude += longitudeDelta > 180 ? -360 : longitudeDelta < -180 ? 360 : 0;
780
+ }
781
+ previousLongitude = longitude;
782
+ west = Math.min(west, longitude);
783
+ south = Math.min(south, latitude);
784
+ east = Math.max(east, longitude);
785
+ north = Math.max(north, latitude);
786
+ }
787
+ const angularEpsilon = 2 * Number.EPSILON * 180 / Math.PI;
788
+ return [
789
+ [west - angularEpsilon, Math.max(-90, south - angularEpsilon)],
790
+ [east + angularEpsilon, Math.min(90, north + angularEpsilon)]
791
+ ];
792
+ }
793
+ function getS2CellIndex(cell) {
794
+ return typeof cell === "bigint" ? cell : getS2IndexFromToken(cell);
795
+ }
796
+ function getS2RootBounds(face) {
797
+ const polarLatitude = Math.asin(Math.sqrt(1 / 3)) * 180 / Math.PI;
798
+ switch (face) {
799
+ case 0:
800
+ return [
801
+ [-45, -45],
802
+ [45, 45]
803
+ ];
804
+ case 1:
805
+ return [
806
+ [45, -45],
807
+ [135, 45]
808
+ ];
809
+ case 2:
810
+ return [
811
+ [-180, polarLatitude],
812
+ [180, 90]
813
+ ];
814
+ case 3:
815
+ return [
816
+ [135, -45],
817
+ [225, 45]
818
+ ];
819
+ case 4:
820
+ return [
821
+ [-135, -45],
822
+ [-45, 45]
823
+ ];
824
+ case 5:
825
+ return [
826
+ [-180, -90],
827
+ [180, -polarLatitude]
828
+ ];
829
+ default:
830
+ throw new Error(`Invalid S2 face: ${face}`);
831
+ }
832
+ }
833
+
834
+ // dist/index.js
835
+ var DGGS_DECODERS = [
836
+ A5Decoder,
837
+ GeohashDecoder,
838
+ H3Decoder,
839
+ PlusCodeDecoder,
840
+ QuadkeyDecoder,
841
+ S2Decoder
842
+ ];
843
+ function findDGGSCellColumn(columnNames, decoders = DGGS_DECODERS) {
844
+ const actualNames = /* @__PURE__ */ new Map();
845
+ for (const columnName of columnNames) {
846
+ actualNames.set(columnName.toLowerCase(), columnName);
847
+ }
848
+ let match = null;
849
+ for (const decoder of decoders) {
850
+ for (const candidate of decoder.cellColumnNames) {
851
+ const columnName = actualNames.get(candidate.toLowerCase());
852
+ if (!columnName) {
853
+ continue;
854
+ }
855
+ if (match && (match.decoder !== decoder || match.columnName !== columnName)) {
856
+ return null;
857
+ }
858
+ match = { columnName, decoder };
859
+ }
860
+ }
861
+ return match;
862
+ }
863
+ //# sourceMappingURL=index.cjs.map