@math.gl/dggs 5.0.0-alpha.4 → 5.0.0-alpha.6
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.
- package/README.md +1 -1
- package/dist/index.cjs +210 -20
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +4 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/index.js.map +1 -1
- package/dist/s2-decoder.d.ts +10 -0
- package/dist/s2-decoder.d.ts.map +1 -1
- package/dist/s2-decoder.js +86 -19
- package/dist/s2-decoder.js.map +1 -1
- package/dist/s2-geometry/s2-geometry.d.ts +20 -0
- package/dist/s2-geometry/s2-geometry.d.ts.map +1 -1
- package/dist/s2-geometry/s2-geometry.js +123 -0
- package/dist/s2-geometry/s2-geometry.js.map +1 -1
- package/dist/s2-geometry/s2-token.d.ts +30 -0
- package/dist/s2-geometry/s2-token.d.ts.map +1 -1
- package/dist/s2-geometry/s2-token.js +70 -4
- package/dist/s2-geometry/s2-token.js.map +1 -1
- package/dist/s2.cjs +211 -21
- package/dist/s2.cjs.map +1 -1
- package/dist/s2.d.ts +4 -2
- package/dist/s2.d.ts.map +1 -1
- package/dist/s2.js +3 -2
- package/dist/s2.js.map +1 -1
- package/package.json +3 -3
- package/src/index.ts +11 -2
- package/src/s2-decoder.ts +87 -17
- package/src/s2-geometry/s2-geometry.ts +137 -0
- package/src/s2-geometry/s2-token.ts +73 -4
- package/src/s2.ts +11 -2
|
@@ -11,6 +11,8 @@ Permission to use, copy, modify, and/or distribute this software for any purpose
|
|
|
11
11
|
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
|
+
import {isS2IndexValid} from './s2-token';
|
|
15
|
+
|
|
14
16
|
export type S2Cell = {
|
|
15
17
|
face: number;
|
|
16
18
|
ij: [number, number];
|
|
@@ -18,6 +20,9 @@ export type S2Cell = {
|
|
|
18
20
|
};
|
|
19
21
|
|
|
20
22
|
export function getS2Cell(s2Index: bigint): S2Cell {
|
|
23
|
+
if (!isS2IndexValid(s2Index)) {
|
|
24
|
+
throw new Error(`Invalid S2 index: ${s2Index}`);
|
|
25
|
+
}
|
|
21
26
|
const key = toHilbertQuadkey(s2Index);
|
|
22
27
|
const s2cell = fromHilbertQuadKey(key);
|
|
23
28
|
return s2cell;
|
|
@@ -31,11 +36,46 @@ const MAX_LEVEL = 30;
|
|
|
31
36
|
const POS_BITS = 2 * MAX_LEVEL + 1; // 61 (60 bits of data, 1 bit lsb marker)
|
|
32
37
|
const RADIAN_TO_DEGREE = 180 / Math.PI;
|
|
33
38
|
|
|
39
|
+
type HilbertOrientation = 'a' | 'b' | 'c' | 'd';
|
|
40
|
+
|
|
41
|
+
const HILBERT_MAP: Record<
|
|
42
|
+
HilbertOrientation,
|
|
43
|
+
readonly (readonly [position: number, orientation: HilbertOrientation])[]
|
|
44
|
+
> = {
|
|
45
|
+
a: [
|
|
46
|
+
[0, 'd'],
|
|
47
|
+
[1, 'a'],
|
|
48
|
+
[3, 'b'],
|
|
49
|
+
[2, 'a']
|
|
50
|
+
],
|
|
51
|
+
b: [
|
|
52
|
+
[2, 'b'],
|
|
53
|
+
[1, 'b'],
|
|
54
|
+
[3, 'a'],
|
|
55
|
+
[0, 'c']
|
|
56
|
+
],
|
|
57
|
+
c: [
|
|
58
|
+
[2, 'c'],
|
|
59
|
+
[3, 'd'],
|
|
60
|
+
[1, 'c'],
|
|
61
|
+
[0, 'b']
|
|
62
|
+
],
|
|
63
|
+
d: [
|
|
64
|
+
[0, 'a'],
|
|
65
|
+
[3, 'c'],
|
|
66
|
+
[1, 'd'],
|
|
67
|
+
[2, 'd']
|
|
68
|
+
]
|
|
69
|
+
};
|
|
70
|
+
|
|
34
71
|
/*
|
|
35
72
|
Original function taken from deck.gl doesn't support the case of (face <= 5)
|
|
36
73
|
It's fixed here.
|
|
37
74
|
*/
|
|
38
75
|
export function fromHilbertQuadKey(hilbertQuadkey: string): S2Cell {
|
|
76
|
+
if (!/^[0-5]\/[0-3]{0,30}$/.test(hilbertQuadkey)) {
|
|
77
|
+
throw new Error(`Invalid S2 Hilbert quadkey: ${hilbertQuadkey}`);
|
|
78
|
+
}
|
|
39
79
|
const parts = hilbertQuadkey.split('/');
|
|
40
80
|
const face = parseInt(parts[0], 10);
|
|
41
81
|
const position = parts[1] || '';
|
|
@@ -75,6 +115,9 @@ export function fromHilbertQuadKey(hilbertQuadkey: string): S2Cell {
|
|
|
75
115
|
}
|
|
76
116
|
|
|
77
117
|
export function toHilbertQuadkey(id: bigint): string {
|
|
118
|
+
if (!isS2IndexValid(id)) {
|
|
119
|
+
throw new Error(`Invalid S2 index: ${id}`);
|
|
120
|
+
}
|
|
78
121
|
let bin = id.toString(2);
|
|
79
122
|
|
|
80
123
|
while (bin.length < FACE_BITS + POS_BITS) {
|
|
@@ -105,6 +148,86 @@ export function toHilbertQuadkey(id: bigint): string {
|
|
|
105
148
|
return `${faceS}/${posS}`;
|
|
106
149
|
}
|
|
107
150
|
|
|
151
|
+
/**
|
|
152
|
+
* Converts face-local integer coordinates into an S2 cell index.
|
|
153
|
+
*
|
|
154
|
+
* @param s2Cell - Face, `(i, j)` coordinates, and level of an S2 cell.
|
|
155
|
+
* @returns The corresponding 64-bit S2 cell index.
|
|
156
|
+
*/
|
|
157
|
+
export function getS2IndexFromCell(s2Cell: S2Cell): bigint {
|
|
158
|
+
const {face, ij, level} = s2Cell;
|
|
159
|
+
if (!Number.isInteger(face) || face < 0 || face > 5) {
|
|
160
|
+
throw new Error(`Invalid S2 face: ${face}`);
|
|
161
|
+
}
|
|
162
|
+
if (!Number.isInteger(level) || level < 0 || level > MAX_LEVEL) {
|
|
163
|
+
throw new Error(`Invalid S2 level: ${level}`);
|
|
164
|
+
}
|
|
165
|
+
const maximumCoordinate = 2 ** level;
|
|
166
|
+
if (
|
|
167
|
+
!Number.isInteger(ij[0]) ||
|
|
168
|
+
!Number.isInteger(ij[1]) ||
|
|
169
|
+
ij[0] < 0 ||
|
|
170
|
+
ij[1] < 0 ||
|
|
171
|
+
ij[0] >= maximumCoordinate ||
|
|
172
|
+
ij[1] >= maximumCoordinate
|
|
173
|
+
) {
|
|
174
|
+
throw new Error(`Invalid S2 IJ coordinates: ${ij[0]}, ${ij[1]}`);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
const positions = getHilbertQuadList(ij[0], ij[1], level, face);
|
|
178
|
+
let s2Index = BigInt(face) << 61n;
|
|
179
|
+
for (let positionIndex = 0; positionIndex < positions.length; positionIndex++) {
|
|
180
|
+
const bitOffset = 59 - positionIndex * 2;
|
|
181
|
+
s2Index |= BigInt(positions[positionIndex]) << BigInt(bitOffset);
|
|
182
|
+
}
|
|
183
|
+
s2Index |= 1n << BigInt(60 - level * 2);
|
|
184
|
+
return s2Index;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Returns a spatially addressed descendant of an S2 cell.
|
|
189
|
+
*
|
|
190
|
+
* Unlike {@link getS2ChildIndex}, `x` and `y` address the descendant along increasing face-local
|
|
191
|
+
* `i` and `j` axes. This accounts for Hilbert orientation changes across faces and levels.
|
|
192
|
+
*
|
|
193
|
+
* @param rootIndex - Valid S2 cell index at the root of the relative grid.
|
|
194
|
+
* @param relativeLevel - Number of subdivision levels below the root.
|
|
195
|
+
* @param x - Descendant coordinate along the root cell's increasing `i` axis.
|
|
196
|
+
* @param y - Descendant coordinate along the root cell's increasing `j` axis.
|
|
197
|
+
* @returns The descendant S2 cell index.
|
|
198
|
+
*/
|
|
199
|
+
export function getS2DescendantIndex(
|
|
200
|
+
rootIndex: bigint,
|
|
201
|
+
relativeLevel: number,
|
|
202
|
+
x: number,
|
|
203
|
+
y: number
|
|
204
|
+
): bigint {
|
|
205
|
+
const rootCell = getS2Cell(rootIndex);
|
|
206
|
+
if (
|
|
207
|
+
!Number.isInteger(relativeLevel) ||
|
|
208
|
+
relativeLevel < 0 ||
|
|
209
|
+
rootCell.level + relativeLevel > MAX_LEVEL
|
|
210
|
+
) {
|
|
211
|
+
throw new Error(`Invalid S2 relative level: ${relativeLevel}`);
|
|
212
|
+
}
|
|
213
|
+
const divisionCount = 2 ** relativeLevel;
|
|
214
|
+
if (
|
|
215
|
+
!Number.isInteger(x) ||
|
|
216
|
+
!Number.isInteger(y) ||
|
|
217
|
+
x < 0 ||
|
|
218
|
+
y < 0 ||
|
|
219
|
+
x >= divisionCount ||
|
|
220
|
+
y >= divisionCount
|
|
221
|
+
) {
|
|
222
|
+
throw new Error(`Invalid S2 descendant coordinates: ${x}, ${y}`);
|
|
223
|
+
}
|
|
224
|
+
return getS2IndexFromCell({
|
|
225
|
+
face: rootCell.face,
|
|
226
|
+
ij: [rootCell.ij[0] * divisionCount + x, rootCell.ij[1] * divisionCount + y],
|
|
227
|
+
level: rootCell.level + relativeLevel
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
|
|
108
231
|
export function IJToST(
|
|
109
232
|
ij: [number, number],
|
|
110
233
|
order: number,
|
|
@@ -164,3 +287,17 @@ function rotateAndFlipQuadrant(n: number, point: [number, number], rx: number, r
|
|
|
164
287
|
point[1] = x;
|
|
165
288
|
}
|
|
166
289
|
}
|
|
290
|
+
|
|
291
|
+
function getHilbertQuadList(x: number, y: number, level: number, face: number): number[] {
|
|
292
|
+
let orientation: HilbertOrientation = face % 2 === 1 ? 'd' : 'a';
|
|
293
|
+
const positions: number[] = [];
|
|
294
|
+
for (let bit = level - 1; bit >= 0; bit--) {
|
|
295
|
+
const mask = 1 << bit;
|
|
296
|
+
const quadrantX = (x & mask) !== 0 ? 1 : 0;
|
|
297
|
+
const quadrantY = (y & mask) !== 0 ? 1 : 0;
|
|
298
|
+
const [position, nextOrientation] = HILBERT_MAP[orientation][quadrantX * 2 + quadrantY];
|
|
299
|
+
positions.push(position);
|
|
300
|
+
orientation = nextOrientation;
|
|
301
|
+
}
|
|
302
|
+
return positions;
|
|
303
|
+
}
|
|
@@ -5,7 +5,10 @@
|
|
|
5
5
|
// s2-geometry is a pure JavaScript port of Google/Niantic's S2 Geometry library
|
|
6
6
|
// which is perfect since it works in the browser.
|
|
7
7
|
|
|
8
|
-
|
|
8
|
+
const MAXIMUM_TOKEN_LENGTH = 16;
|
|
9
|
+
const MAXIMUM_S2_INDEX = (1n << 64n) - 1n;
|
|
10
|
+
const VALID_LOWEST_ON_BITS = 0x1555555555555555n;
|
|
11
|
+
const MAXIMUM_LEVEL = 30;
|
|
9
12
|
|
|
10
13
|
// INDEX CALCULATIONS
|
|
11
14
|
|
|
@@ -16,10 +19,13 @@
|
|
|
16
19
|
*/
|
|
17
20
|
export function getS2IndexFromToken(token: string): bigint {
|
|
18
21
|
if (token === 'X') {
|
|
19
|
-
|
|
22
|
+
return 0n;
|
|
23
|
+
}
|
|
24
|
+
if (!isS2TokenValid(token)) {
|
|
25
|
+
throw new Error(`Invalid S2 token: ${token}`);
|
|
20
26
|
}
|
|
21
27
|
// pad token with zeros to make the length 16
|
|
22
|
-
const paddedToken = token.padEnd(
|
|
28
|
+
const paddedToken = token.padEnd(MAXIMUM_TOKEN_LENGTH, '0');
|
|
23
29
|
return BigInt(`0x${paddedToken}`);
|
|
24
30
|
}
|
|
25
31
|
|
|
@@ -31,6 +37,9 @@ export function getS2TokenFromIndex(cellId: bigint): string {
|
|
|
31
37
|
if (cellId === 0n) {
|
|
32
38
|
return 'X';
|
|
33
39
|
}
|
|
40
|
+
if (!isS2IndexValid(cellId)) {
|
|
41
|
+
throw new Error(`Invalid S2 index: ${cellId}`);
|
|
42
|
+
}
|
|
34
43
|
let numZeroDigits = countTrailingZeros(cellId);
|
|
35
44
|
|
|
36
45
|
const remainder = numZeroDigits % 4;
|
|
@@ -44,7 +53,67 @@ export function getS2TokenFromIndex(cellId: bigint): string {
|
|
|
44
53
|
return zeroString + hexString;
|
|
45
54
|
}
|
|
46
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Returns whether a value is a valid non-empty 64-bit S2 cell index.
|
|
58
|
+
*
|
|
59
|
+
* @param s2Index - Candidate S2 cell index.
|
|
60
|
+
* @returns `true` when the face and level marker encode a valid S2 cell.
|
|
61
|
+
*/
|
|
62
|
+
export function isS2IndexValid(s2Index: unknown): s2Index is bigint {
|
|
63
|
+
if (typeof s2Index !== 'bigint' || s2Index <= 0n || s2Index > MAXIMUM_S2_INDEX) {
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
const face = s2Index >> 61n;
|
|
67
|
+
const lowestOnBit = s2Index & -s2Index;
|
|
68
|
+
return face < 6n && (lowestOnBit & VALID_LOWEST_ON_BITS) !== 0n;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Returns whether a string is a canonical token for a non-empty S2 cell.
|
|
73
|
+
*
|
|
74
|
+
* The empty-cell sentinel `X` is supported by the conversion functions but is not a valid cell.
|
|
75
|
+
*
|
|
76
|
+
* @param token - Candidate hexadecimal S2 token.
|
|
77
|
+
* @returns `true` when the token canonically encodes a valid S2 cell.
|
|
78
|
+
*/
|
|
79
|
+
export function isS2TokenValid(token: unknown): token is string {
|
|
80
|
+
if (typeof token !== 'string' || !/^[0-9a-fA-F]{1,16}$/.test(token) || token.endsWith('0')) {
|
|
81
|
+
return false;
|
|
82
|
+
}
|
|
83
|
+
const paddedToken = token.padEnd(MAXIMUM_TOKEN_LENGTH, '0');
|
|
84
|
+
return isS2IndexValid(BigInt(`0x${paddedToken}`));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Returns the subdivision level encoded by an S2 cell index.
|
|
89
|
+
*
|
|
90
|
+
* @param s2Index - Valid non-empty S2 cell index.
|
|
91
|
+
* @returns The S2 level in the range `[0, 30]`.
|
|
92
|
+
*/
|
|
93
|
+
export function getS2Level(s2Index: bigint): number {
|
|
94
|
+
if (!isS2IndexValid(s2Index)) {
|
|
95
|
+
throw new Error(`Invalid S2 index: ${s2Index}`);
|
|
96
|
+
}
|
|
97
|
+
return (60 - countTrailingZeros(s2Index)) / 2;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Returns one of the four Hilbert-ordered children of an S2 cell.
|
|
102
|
+
*
|
|
103
|
+
* @param s2Index - Valid S2 cell index below level 30.
|
|
104
|
+
* @param index - Hilbert child position in the range `[0, 3]`.
|
|
105
|
+
* @returns The child S2 cell index.
|
|
106
|
+
*/
|
|
47
107
|
export function getS2ChildIndex(s2Index: bigint, index: number): bigint {
|
|
108
|
+
if (!isS2IndexValid(s2Index)) {
|
|
109
|
+
throw new Error(`Invalid S2 index: ${s2Index}`);
|
|
110
|
+
}
|
|
111
|
+
if (!Number.isInteger(index) || index < 0 || index > 3) {
|
|
112
|
+
throw new Error(`Invalid S2 child index: ${index}`);
|
|
113
|
+
}
|
|
114
|
+
if (getS2Level(s2Index) === MAXIMUM_LEVEL) {
|
|
115
|
+
throw new Error('Cannot subdivide an S2 leaf cell');
|
|
116
|
+
}
|
|
48
117
|
// Shift sentinel bit 2 positions to the right.
|
|
49
118
|
const newLsb = lsb(s2Index) >> 2n;
|
|
50
119
|
// Insert child index before the sentinel bit.
|
|
@@ -57,7 +126,7 @@ export function getS2ChildIndex(s2Index: bigint, index: number): bigint {
|
|
|
57
126
|
* @private
|
|
58
127
|
*/
|
|
59
128
|
function lsb(cellId: bigint): bigint {
|
|
60
|
-
return cellId &
|
|
129
|
+
return cellId & -cellId; // eslint-disable-line
|
|
61
130
|
}
|
|
62
131
|
|
|
63
132
|
function countTrailingZeros(n: bigint): number {
|
package/src/s2.ts
CHANGED
|
@@ -2,5 +2,14 @@
|
|
|
2
2
|
// SPDX-License-Identifier: MIT
|
|
3
3
|
// Copyright (c) vis.gl contributors
|
|
4
4
|
|
|
5
|
-
export {S2Decoder} from './s2-decoder';
|
|
6
|
-
export {
|
|
5
|
+
export {S2Decoder, getS2Bounds, getS2LngLat, getS2BoundaryFlat} from './s2-decoder';
|
|
6
|
+
export {
|
|
7
|
+
getS2ChildIndex,
|
|
8
|
+
getS2IndexFromToken,
|
|
9
|
+
getS2Level,
|
|
10
|
+
getS2TokenFromIndex,
|
|
11
|
+
isS2IndexValid,
|
|
12
|
+
isS2TokenValid
|
|
13
|
+
} from './s2-geometry/s2-token';
|
|
14
|
+
export {getS2DescendantIndex, getS2IndexFromCell} from './s2-geometry/s2-geometry';
|
|
15
|
+
export type {S2Cell} from './s2-geometry/s2-geometry';
|