@mailwoman/spatial 11.1.0 → 11.3.0
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 +2 -1
- package/lib/digipin.ts +282 -0
- package/lib/geometries/point/coordinates.ts +1 -1
- package/lib/geometries/point.ts +1 -1
- package/lib/geometries/ring-blob.ts +1 -1
- package/lib/geometries.ts +1 -1
- package/lib/h3/interior-cells.ts +5 -3
- package/lib/h3/polygon-cells.ts +4 -4
- package/lib/index.ts +2 -1
- package/lib/open-location-code.ts +45 -30
- package/lib/point/reading.ts +323 -0
- package/lib/point/text.ts +70 -0
- package/lib/polyline.ts +8 -6
- package/lib/position.ts +1 -1
- package/lib/projection/transform.ts +1 -1
- package/out/digipin.d.ts +94 -0
- package/out/digipin.d.ts.map +1 -0
- package/out/digipin.js +242 -0
- package/out/digipin.js.map +1 -0
- package/out/geometries/point/coordinates.js +1 -1
- package/out/geometries/point/coordinates.js.map +1 -1
- package/out/geometries/point.js +1 -1
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +1 -1
- package/out/geometries.d.ts +1 -1
- package/out/geometries.js +1 -1
- package/out/h3/interior-cells.d.ts.map +1 -1
- package/out/h3/interior-cells.js +5 -3
- package/out/h3/interior-cells.js.map +1 -1
- package/out/h3/polygon-cells.d.ts +3 -2
- package/out/h3/polygon-cells.d.ts.map +1 -1
- package/out/h3/polygon-cells.js +4 -4
- package/out/h3/polygon-cells.js.map +1 -1
- package/out/index.d.ts +2 -1
- package/out/index.d.ts.map +1 -1
- package/out/index.js +2 -1
- package/out/index.js.map +1 -1
- package/out/open-location-code.d.ts +24 -16
- package/out/open-location-code.d.ts.map +1 -1
- package/out/open-location-code.js +27 -27
- package/out/open-location-code.js.map +1 -1
- package/out/point/reading.d.ts +144 -0
- package/out/point/reading.d.ts.map +1 -0
- package/out/point/reading.js +167 -0
- package/out/point/reading.js.map +1 -0
- package/out/point/text.d.ts +117 -0
- package/out/point/text.d.ts.map +1 -0
- package/out/point/text.js +44 -0
- package/out/point/text.js.map +1 -0
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +8 -6
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +1 -1
- package/out/projection/transform.js +1 -1
- package/out/projection/transform.js.map +1 -1
- package/out/tools/ogr/stream.js +2 -2
- package/out/tools/ogr/stream.js.map +1 -1
- package/out/tools/ogr.js +3 -3
- package/out/tools/ogr.js.map +1 -1
- package/out/tools/tsconfig.tsbuildinfo +1 -1
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +6 -5
- package/tools/ogr/stream.ts +2 -2
- package/tools/ogr.ts +3 -3
- package/lib/point-query.ts +0 -294
- package/out/point-query.d.ts +0 -71
- package/out/point-query.d.ts.map +0 -1
- package/out/point-query.js +0 -194
- package/out/point-query.js.map +0 -1
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
|
|
|
23
23
|
| ---------------------- | ------------------------------------------------------------------------------------ |
|
|
24
24
|
| **Coordinate math** | Haversine distance, direction, bounding-box containment |
|
|
25
25
|
| **H3 indexing** | Hexagonal grid indexing via `@sister.software/h3` — spatial blocking for the matcher |
|
|
26
|
+
| **DIGIPIN** | Encode and decode geographic coordinates using the DIGIPIN system |
|
|
26
27
|
| **WKB/WKT geometry** | Parse/format Well-Known Binary geometry via `wkx` |
|
|
27
28
|
| **Coordinate parsing** | Parse decimal, DMS, and UTM coordinate strings via `geo-coordinates-parser` |
|
|
28
29
|
| **GeoJSON** | GeoJSON Feature/FeatureCollection types and utilities |
|
|
@@ -40,7 +41,7 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
|
|
|
40
41
|
- [`@mailwoman/match`](../match) — uses H3 cells for geo-first record blocking
|
|
41
42
|
- [`@mailwoman/address-id`](../address-id) — uses H3 for stable address primary keys
|
|
42
43
|
- [`@mailwoman/core`](../core) — consumes spatial types
|
|
43
|
-
- [`@mailwoman/codex/country`](../codex/country) — ISO 3166-1 country codes and names
|
|
44
|
+
- [`@mailwoman/codex/country`](../codex/country) — ISO 3166-1 country codes and names
|
|
44
45
|
|
|
45
46
|
## License
|
|
46
47
|
|
package/lib/digipin.ts
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
|
|
4
|
+
import type { Validated } from "@mailwoman/recognizer/types"
|
|
5
|
+
|
|
6
|
+
import type { Coordinates2D } from "#position"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @file DIGIPIN geographic encoding and decoding.
|
|
10
|
+
*
|
|
11
|
+
* Developed by India Post, Department of Posts.
|
|
12
|
+
*
|
|
13
|
+
* DIGIPIN uses a hierarchical 4×4 grid covering:
|
|
14
|
+
*
|
|
15
|
+
* - Latitude: 2.5° to 38.5°
|
|
16
|
+
* - Longitude: 63.5° to 99.5°
|
|
17
|
+
*
|
|
18
|
+
* Each character represents four bits of spatial information:
|
|
19
|
+
* two latitude bits and two longitude bits.
|
|
20
|
+
*
|
|
21
|
+
* A complete 10-character DIGIPIN represents 40 bits,
|
|
22
|
+
* providing 20-bit resolution along each axis.
|
|
23
|
+
*
|
|
24
|
+
* @see {@link https://en.wikipedia.org/wiki/National_Level_Addressing_Grid | National Level Addressing Grid}
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
//#region Constants and Lookup Tables
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* DIGIPIN character alphabet, flattened in row-major order from the official 4×4 character grid.
|
|
31
|
+
*
|
|
32
|
+
* Rows are ordered north to south.
|
|
33
|
+
* Columns are ordered west to east.
|
|
34
|
+
*/
|
|
35
|
+
const ALPHABET = "FC98J327K456LMPT"
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Southern boundary of the DIGIPIN coverage area, in degrees.
|
|
39
|
+
*/
|
|
40
|
+
const MIN_LAT = 2.5
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Northern boundary of the DIGIPIN coverage area, in degrees.
|
|
44
|
+
*/
|
|
45
|
+
const MAX_LAT = 38.5
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Western boundary of the DIGIPIN coverage area, in degrees.
|
|
49
|
+
*/
|
|
50
|
+
const MIN_LON = 63.5
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Eastern boundary of the DIGIPIN coverage area, in degrees.
|
|
54
|
+
*/
|
|
55
|
+
const MAX_LON = 99.5
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Geographic extent of each DIGIPIN axis, in degrees.
|
|
59
|
+
*/
|
|
60
|
+
const SPAN = 36
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Number of characters in a complete DIGIPIN.
|
|
64
|
+
*
|
|
65
|
+
* Each character contributes two bits per coordinate axis.
|
|
66
|
+
*/
|
|
67
|
+
const PRECISION = 10
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Number of subdivisions along each coordinate axis.
|
|
71
|
+
*
|
|
72
|
+
* Ten levels of four-way subdivision produce 4^10 cells along each axis, equivalent to 20 bits.
|
|
73
|
+
*/
|
|
74
|
+
const RESOLUTION = 1 << 20
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Maximum zero-based cell index along either axis.
|
|
78
|
+
*/
|
|
79
|
+
const MAX_INDEX = RESOLUTION - 1
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Conversion factor from geographic degrees to cell indices.
|
|
83
|
+
*/
|
|
84
|
+
const SCALE = RESOLUTION / SPAN
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Geographic width or height of one DIGIPIN cell, in degrees.
|
|
88
|
+
*
|
|
89
|
+
* It is the span of the cell whose center a decoded position is.
|
|
90
|
+
*/
|
|
91
|
+
export const DIGIPIN_CELL_DEGREES = SPAN / RESOLUTION
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* ASCII character-to-grid-index lookup table.
|
|
95
|
+
*
|
|
96
|
+
* Each uppercase DIGIPIN character maps to its zero-based position in the flattened character grid.
|
|
97
|
+
* The decoders read only validated codes, so no other character reaches the table.
|
|
98
|
+
*
|
|
99
|
+
* Initialized once at module evaluation.
|
|
100
|
+
* It doesn't cache encoding or decoding results.
|
|
101
|
+
*/
|
|
102
|
+
const DECODE = buildDecodeTable()
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Build the ASCII character-to-grid-index lookup table from {@link ALPHABET}.
|
|
106
|
+
*/
|
|
107
|
+
function buildDecodeTable(): Uint8Array {
|
|
108
|
+
const table = new Uint8Array(128)
|
|
109
|
+
|
|
110
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
111
|
+
table[ALPHABET.charCodeAt(i)] = i
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
return table
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// #endregion
|
|
118
|
+
|
|
119
|
+
// #region Public Functions
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Validate geographic coordinates against DIGIPIN coverage bounds.
|
|
123
|
+
*
|
|
124
|
+
* Both coordinates must be finite numbers within the supported geographic region, including its boundaries.
|
|
125
|
+
*
|
|
126
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
127
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
128
|
+
*
|
|
129
|
+
* @throws {RangeError} If latitude or longitude is outside DIGIPIN bounds.
|
|
130
|
+
*/
|
|
131
|
+
export function validateDIGIPINCoordinates(latitude: number, longitude: number): void {
|
|
132
|
+
if (!Number.isFinite(latitude) || latitude < MIN_LAT || latitude > MAX_LAT) {
|
|
133
|
+
throw new RangeError("Latitude out of range")
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
if (!Number.isFinite(longitude) || longitude < MIN_LON || longitude > MAX_LON) {
|
|
137
|
+
throw new RangeError("Longitude out of range")
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Encode geographic coordinates into a 10-character DIGIPIN.
|
|
143
|
+
*
|
|
144
|
+
* Coordinates are quantized into 20-bit integer cell indices.
|
|
145
|
+
* Each iteration extracts two bits from each coordinate and combines them into
|
|
146
|
+
* an index into the DIGIPIN alphabet.
|
|
147
|
+
*
|
|
148
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
149
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
150
|
+
* @returns Uppercase 10-character DIGIPIN.
|
|
151
|
+
*
|
|
152
|
+
* @throws {RangeError} If coordinates are outside DIGIPIN bounds.
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
155
|
+
* const pin = getDIGIPIN(13.11179621, 80.20264269)
|
|
156
|
+
*/
|
|
157
|
+
export function getDIGIPIN(latitude: number, longitude: number): Validated.Digipin {
|
|
158
|
+
validateDIGIPINCoordinates(latitude, longitude)
|
|
159
|
+
|
|
160
|
+
// The bounds are checked, so the code is ten characters of the alphabet.
|
|
161
|
+
return getDIGIPINUnsafe(latitude, longitude) as Validated.Digipin
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Encode geographic coordinates without validating their bounds.
|
|
166
|
+
*
|
|
167
|
+
* Intended for performance-sensitive batch processing where coordinates have already been validated.
|
|
168
|
+
*
|
|
169
|
+
* Coordinates are quantized into 20-bit indices and encoded using bitwise operations.
|
|
170
|
+
*
|
|
171
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
172
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
173
|
+
* @returns Uppercase 10-character DIGIPIN.
|
|
174
|
+
*
|
|
175
|
+
* @remarks Inputs must be finite and inside DIGIPIN coverage bounds.
|
|
176
|
+
* Out-of-range inputs produce unspecified results.
|
|
177
|
+
*/
|
|
178
|
+
export function getDIGIPINUnsafe(latitude: number, longitude: number): string {
|
|
179
|
+
/**
|
|
180
|
+
* Quantized latitude cell index.
|
|
181
|
+
*/
|
|
182
|
+
const lat = Math.min(MAX_INDEX, Math.floor((latitude - MIN_LAT) * SCALE))
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Quantized longitude cell index.
|
|
186
|
+
*/
|
|
187
|
+
const lon = Math.min(MAX_INDEX, Math.floor((longitude - MIN_LON) * SCALE))
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Encoded DIGIPIN string.
|
|
191
|
+
*/
|
|
192
|
+
let pin = ""
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Extract two bits per axis, beginning with the highest-order bits.
|
|
196
|
+
*/
|
|
197
|
+
for (let shift = 18; shift >= 0; shift -= 2) {
|
|
198
|
+
const row = 3 - ((lat >>> shift) & 3)
|
|
199
|
+
const col = (lon >>> shift) & 3
|
|
200
|
+
|
|
201
|
+
pin += ALPHABET[(row << 2) | col]
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return pin
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Decode a DIGIPIN into the center coordinates of its cell.
|
|
209
|
+
*
|
|
210
|
+
* Returns a GeoJSON-compatible position in longitude-first order.
|
|
211
|
+
* Coordinates retain floating-point precision without decimal rounding.
|
|
212
|
+
*
|
|
213
|
+
* The parameter is the validated code of the `spatial` family of `@mailwoman/recognizer`:
|
|
214
|
+
* ten upper-case characters without hyphens.
|
|
215
|
+
* The validator accepts lower case and the display form `39J-49L-L8T4`
|
|
216
|
+
* and normalizes both, so this decoder skips every check.
|
|
217
|
+
*
|
|
218
|
+
* A caller that holds a string reads it through the recognizer.
|
|
219
|
+
*
|
|
220
|
+
* @param pin - Validated DIGIPIN.
|
|
221
|
+
* @returns Cell center as [longitude, latitude].
|
|
222
|
+
*
|
|
223
|
+
* @example
|
|
224
|
+
* const [longitude, latitude] = getLatLngFromDIGIPIN(getDIGIPIN(13.11179621, 80.20264269))
|
|
225
|
+
*/
|
|
226
|
+
export function getLatLngFromDIGIPIN(pin: Validated.Digipin): Coordinates2D {
|
|
227
|
+
/**
|
|
228
|
+
* Result position in GeoJSON coordinate order.
|
|
229
|
+
*/
|
|
230
|
+
const position: Coordinates2D = [0, 0]
|
|
231
|
+
|
|
232
|
+
getLatLngFromDIGIPINInto(pin, position)
|
|
233
|
+
|
|
234
|
+
return position
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/**
|
|
238
|
+
* Decode a DIGIPIN directly into an existing coordinate buffer.
|
|
239
|
+
*
|
|
240
|
+
* Avoids allocating intermediate coordinate tuples.
|
|
241
|
+
*
|
|
242
|
+
* The destination receives longitude followed by latitude.
|
|
243
|
+
* Supports both ordinary JavaScript arrays and Float64Array buffers.
|
|
244
|
+
*
|
|
245
|
+
* @param pin - Validated DIGIPIN, ten upper-case characters.
|
|
246
|
+
* @param output - Destination coordinate buffer.
|
|
247
|
+
* @param offset - Starting element offset.
|
|
248
|
+
* Defaults to zero.
|
|
249
|
+
*
|
|
250
|
+
* @example
|
|
251
|
+
* const pins = [getDIGIPIN(13.11179621, 80.20264269), getDIGIPIN(28.6139, 77.209)]
|
|
252
|
+
* const output = new Float64Array(pins.length * 2)
|
|
253
|
+
*
|
|
254
|
+
* for (let i = 0; i < pins.length; i++) {
|
|
255
|
+
* getLatLngFromDIGIPINInto(pins[i]!, output, i * 2)
|
|
256
|
+
* }
|
|
257
|
+
*/
|
|
258
|
+
export function getLatLngFromDIGIPINInto(pin: Validated.Digipin, output: number[] | Float64Array, offset = 0): void {
|
|
259
|
+
/**
|
|
260
|
+
* Accumulated 20-bit latitude cell index.
|
|
261
|
+
*/
|
|
262
|
+
let lat = 0
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Accumulated 20-bit longitude cell index.
|
|
266
|
+
*/
|
|
267
|
+
let lon = 0
|
|
268
|
+
|
|
269
|
+
for (let i = 0; i < PRECISION; i++) {
|
|
270
|
+
/**
|
|
271
|
+
* Flattened DIGIPIN grid position.
|
|
272
|
+
*/
|
|
273
|
+
const index = DECODE[pin.charCodeAt(i)]!
|
|
274
|
+
|
|
275
|
+
lat = (lat << 2) | (3 - (index >>> 2))
|
|
276
|
+
lon = (lon << 2) | (index & 3)
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
output[offset] = MIN_LON + (lon + 0.5) * DIGIPIN_CELL_DEGREES
|
|
280
|
+
output[offset + 1] = MIN_LAT + (lat + 0.5) * DIGIPIN_CELL_DEGREES
|
|
281
|
+
}
|
|
282
|
+
// #endregion
|
|
@@ -10,7 +10,7 @@
|
|
|
10
10
|
|
|
11
11
|
/// <reference types="google.maps" />
|
|
12
12
|
|
|
13
|
-
import { tryParsingJSON } from "@mailwoman/core/json"
|
|
13
|
+
import { tryParsingJSON } from "@mailwoman/core/values/json"
|
|
14
14
|
|
|
15
15
|
import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
|
|
16
16
|
import { type GeoObjectLiteral, GeometryType } from "#objects"
|
package/lib/geometries/point.ts
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
|
|
4
4
|
/// <reference types="google.maps" />
|
|
5
5
|
|
|
6
|
-
import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
|
|
6
|
+
import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/values/json"
|
|
7
7
|
import { latLngToCell } from "@sister.software/h3"
|
|
8
8
|
import { convert as convertCoords } from "geo-coordinates-parser"
|
|
9
9
|
|
|
@@ -294,7 +294,7 @@ export function assertRingsInsideExtent(
|
|
|
294
294
|
}
|
|
295
295
|
|
|
296
296
|
/**
|
|
297
|
-
* One stored polygon's bounding box and geometry, as
|
|
297
|
+
* One stored polygon's bounding box and geometry, as stored in each polygon layer's row.
|
|
298
298
|
*/
|
|
299
299
|
export interface EncodedArea {
|
|
300
300
|
min_lat: number
|
package/lib/geometries.ts
CHANGED
package/lib/h3/interior-cells.ts
CHANGED
|
@@ -2,11 +2,13 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* This module turns an outline into the H3 cell set used by a coverage claim.
|
|
6
|
+
*
|
|
7
|
+
* It admits a cell only when its whole `gridDisk(cell, 1)` and all six boundary
|
|
8
|
+
* vertices lie inside the outline.
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
|
-
import { stringifyJSON } from "@mailwoman/core/json"
|
|
11
|
+
import { stringifyJSON } from "@mailwoman/core/values/json"
|
|
10
12
|
import { cellToBoundary, gridDisk, polygonToCells, type H3Cell } from "@sister.software/h3"
|
|
11
13
|
|
|
12
14
|
import { arealPolygons, geometryContains, type ParsedGeometry, type PolygonRings } from "#geometries/polygon"
|
package/lib/h3/polygon-cells.ts
CHANGED
|
@@ -2,8 +2,9 @@
|
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
*
|
|
5
|
+
* This module turns one authority polygon into H3 cells at build time.
|
|
6
|
+
*
|
|
7
|
+
* Runtime queries then inspect the cell structure instead of reading the geometry.
|
|
7
8
|
*/
|
|
8
9
|
|
|
9
10
|
import {
|
|
@@ -309,8 +310,7 @@ export function featureCellRows(cells: FeatureCells): Array<{
|
|
|
309
310
|
const short = shortCellToInt(cell)
|
|
310
311
|
|
|
311
312
|
// A cell cannot be both for one polygon.
|
|
312
|
-
// This subtraction
|
|
313
|
-
// parent the partial set also names.
|
|
313
|
+
// This subtraction handles a compaction that produced a parent the partial set also names.
|
|
314
314
|
if (wholeShort.has(short)) continue
|
|
315
315
|
|
|
316
316
|
rows.push({ h3Cell: short, resolution: cells.resolution, containment: "partial" })
|
package/lib/index.ts
CHANGED
|
@@ -13,7 +13,8 @@ export * from "#google"
|
|
|
13
13
|
export * from "#h3"
|
|
14
14
|
export * from "#objects"
|
|
15
15
|
export * from "#osgb36"
|
|
16
|
-
export * from "#point
|
|
16
|
+
export * from "#point/reading"
|
|
17
|
+
export * from "#point/text"
|
|
17
18
|
export * from "#polyline"
|
|
18
19
|
export * from "#position"
|
|
19
20
|
export { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
|
|
@@ -1,10 +1,27 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
|
|
4
|
+
/**
|
|
5
|
+
* @file Open Location Code arithmetic.
|
|
6
|
+
*
|
|
7
|
+
* A code is read by the `spatial` family of `@mailwoman/recognizer`, which owns the alphabet, the separator
|
|
8
|
+
* and the digit counts. The decoders here take the validated, branded `Validated.FullPlusCode` and `Validated.ShortPlusCode` the family returns,
|
|
9
|
+
* so a caller cannot pass an unchecked string. A test or a tool that holds a string reads it through the recognizer.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import type { Validated } from "@mailwoman/recognizer/types"
|
|
13
|
+
|
|
4
14
|
import type { DegreeCell } from "#coordinate/cell"
|
|
5
15
|
|
|
6
16
|
const OLC_ALPHABET = "23456789CFGHJMPQRVWX"
|
|
7
17
|
|
|
18
|
+
/**
|
|
19
|
+
* The digits before the separator of a full code.
|
|
20
|
+
*
|
|
21
|
+
* The validator reads eight as a full code and two, four or six as a short one.
|
|
22
|
+
*/
|
|
23
|
+
const FULL_PREFIX_LENGTH = 8
|
|
24
|
+
|
|
8
25
|
const OLC_DIGIT_VALUE = new Map<string, number>([...OLC_ALPHABET].map((c, i) => [c, i]))
|
|
9
26
|
|
|
10
27
|
const PAIR_RESOLUTIONS = [20, 1, 1 / 20, 1 / 400, 1 / 8000] as const
|
|
@@ -19,29 +36,20 @@ const GRID_ROWS = 5
|
|
|
19
36
|
export type DecodedPlusCode = DegreeCell
|
|
20
37
|
|
|
21
38
|
/**
|
|
22
|
-
*
|
|
39
|
+
* Decodes a full code to its cell.
|
|
23
40
|
*
|
|
24
|
-
*
|
|
41
|
+
* The validator guarantees the alphabet and the digit counts, and the brand guarantees
|
|
42
|
+
* the eight digits before the separator, so no input to this function fails.
|
|
43
|
+
* A short code has its own brand and decodes through {@link recoverNearestPlusCode}.
|
|
25
44
|
*/
|
|
26
|
-
export function
|
|
27
|
-
return
|
|
45
|
+
export function decodePlusCode(code: Validated.FullPlusCode): DecodedPlusCode {
|
|
46
|
+
return decodeDigits(code.replace("+", ""))
|
|
28
47
|
}
|
|
29
48
|
|
|
30
49
|
/**
|
|
31
|
-
*
|
|
32
|
-
* such as the `VFQ6+92P` form Google prints on place cards.
|
|
50
|
+
* Decodes the digits of a full code, 10 or 11 of them, without the separator.
|
|
33
51
|
*/
|
|
34
|
-
|
|
35
|
-
return /^[23456789CFGHJMPQRVWX]{2,6}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token) && token.indexOf("+") % 2 === 0
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
/**
|
|
39
|
-
* Decodes a full plus code to its cell, returning null for anything {@link isFullPlusCode} rejects.
|
|
40
|
-
*/
|
|
41
|
-
export function decodePlusCode(code: string): DecodedPlusCode | null {
|
|
42
|
-
if (!isFullPlusCode(code)) return null
|
|
43
|
-
const digits = code.toUpperCase().replace("+", "")
|
|
44
|
-
|
|
52
|
+
function decodeDigits(digits: string): DecodedPlusCode {
|
|
45
53
|
let latLo = -90
|
|
46
54
|
let lonLo = -180
|
|
47
55
|
let latSpan = 400
|
|
@@ -116,36 +124,43 @@ function encodePairDigits(lat: number, lon: number, length: number): string {
|
|
|
116
124
|
export type PlusCodeLength = 10 | 11
|
|
117
125
|
|
|
118
126
|
/**
|
|
119
|
-
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms
|
|
127
|
+
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms an address holds.
|
|
120
128
|
*
|
|
121
129
|
* The eleventh digit refines the last pair's cell on the 4 × 5 grid the spec defines.
|
|
130
|
+
* The digits come from the specification's alphabet and the separator follows the eighth,
|
|
131
|
+
* so the code has the form the validator accepts and the decoders take.
|
|
122
132
|
*/
|
|
123
|
-
export function encodePlusCode(lat: number, lon: number, length: PlusCodeLength = 10):
|
|
133
|
+
export function encodePlusCode(lat: number, lon: number, length: PlusCodeLength = 10): Validated.FullPlusCode {
|
|
124
134
|
const { digits, latRemainder, lonRemainder } = encodePairs(lat, lon, 10)
|
|
125
|
-
const code = `${digits.slice(0,
|
|
135
|
+
const code = `${digits.slice(0, FULL_PREFIX_LENGTH)}+${digits.slice(FULL_PREFIX_LENGTH)}`
|
|
126
136
|
|
|
127
|
-
if (length === 10) return code
|
|
137
|
+
if (length === 10) return code as Validated.FullPlusCode
|
|
128
138
|
|
|
129
139
|
const cell = PAIR_RESOLUTIONS[4]!
|
|
130
140
|
const row = Math.min(Math.floor(latRemainder / (cell / GRID_ROWS)), GRID_ROWS - 1)
|
|
131
141
|
const column = Math.min(Math.floor(lonRemainder / (cell / GRID_COLUMNS)), GRID_COLUMNS - 1)
|
|
132
142
|
|
|
133
|
-
return code + OLC_ALPHABET[row * GRID_COLUMNS + column]!
|
|
143
|
+
return (code + OLC_ALPHABET[row * GRID_COLUMNS + column]!) as Validated.FullPlusCode
|
|
134
144
|
}
|
|
135
145
|
|
|
136
146
|
/**
|
|
137
147
|
* Recovers a short plus code to the matching cell nearest a reference coordinate,
|
|
138
148
|
* following the spec's `recoverNearest`.
|
|
139
|
-
*
|
|
149
|
+
*
|
|
150
|
+
* The brand guarantees two, four or six digits before the separator, so every short code recovers to a cell.
|
|
151
|
+
* The reference must be a finite coordinate.
|
|
152
|
+
*
|
|
153
|
+
* Every caller already holds one.
|
|
154
|
+
* A full code has its own brand and decodes through {@link decodePlusCode}.
|
|
140
155
|
*/
|
|
141
|
-
export function recoverNearestPlusCode(
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
156
|
+
export function recoverNearestPlusCode(
|
|
157
|
+
shortCode: Validated.ShortPlusCode,
|
|
158
|
+
refLat: number,
|
|
159
|
+
refLon: number
|
|
160
|
+
): DecodedPlusCode {
|
|
161
|
+
const missing = FULL_PREFIX_LENGTH - shortCode.indexOf("+")
|
|
145
162
|
const prefix = encodePairDigits(refLat, refLon, missing)
|
|
146
|
-
const candidate =
|
|
147
|
-
|
|
148
|
-
if (!candidate) return null
|
|
163
|
+
const candidate = decodeDigits(prefix + shortCode.replace("+", ""))
|
|
149
164
|
|
|
150
165
|
const LAT_MIN = -90
|
|
151
166
|
const LAT_MAX = 90
|