@mailwoman/spatial 11.1.0 → 11.2.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 +1 -0
- package/lib/digipin.ts +282 -0
- 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/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/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/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +6 -5
- 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/out/digipin.js
ADDED
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
|
+
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
|
+
/**
|
|
4
|
+
* @file DIGIPIN geographic encoding and decoding.
|
|
5
|
+
*
|
|
6
|
+
* Developed by India Post, Department of Posts.
|
|
7
|
+
*
|
|
8
|
+
* DIGIPIN uses a hierarchical 4×4 grid covering:
|
|
9
|
+
*
|
|
10
|
+
* - Latitude: 2.5° to 38.5°
|
|
11
|
+
* - Longitude: 63.5° to 99.5°
|
|
12
|
+
*
|
|
13
|
+
* Each character represents four bits of spatial information:
|
|
14
|
+
* two latitude bits and two longitude bits.
|
|
15
|
+
*
|
|
16
|
+
* A complete 10-character DIGIPIN represents 40 bits,
|
|
17
|
+
* providing 20-bit resolution along each axis.
|
|
18
|
+
*
|
|
19
|
+
* @see {@link https://en.wikipedia.org/wiki/National_Level_Addressing_Grid | National Level Addressing Grid}
|
|
20
|
+
*/
|
|
21
|
+
//#region Constants and Lookup Tables
|
|
22
|
+
/**
|
|
23
|
+
* DIGIPIN character alphabet, flattened in row-major order from the official 4×4 character grid.
|
|
24
|
+
*
|
|
25
|
+
* Rows are ordered north to south.
|
|
26
|
+
* Columns are ordered west to east.
|
|
27
|
+
*/
|
|
28
|
+
const ALPHABET = "FC98J327K456LMPT";
|
|
29
|
+
/**
|
|
30
|
+
* Southern boundary of the DIGIPIN coverage area, in degrees.
|
|
31
|
+
*/
|
|
32
|
+
const MIN_LAT = 2.5;
|
|
33
|
+
/**
|
|
34
|
+
* Northern boundary of the DIGIPIN coverage area, in degrees.
|
|
35
|
+
*/
|
|
36
|
+
const MAX_LAT = 38.5;
|
|
37
|
+
/**
|
|
38
|
+
* Western boundary of the DIGIPIN coverage area, in degrees.
|
|
39
|
+
*/
|
|
40
|
+
const MIN_LON = 63.5;
|
|
41
|
+
/**
|
|
42
|
+
* Eastern boundary of the DIGIPIN coverage area, in degrees.
|
|
43
|
+
*/
|
|
44
|
+
const MAX_LON = 99.5;
|
|
45
|
+
/**
|
|
46
|
+
* Geographic extent of each DIGIPIN axis, in degrees.
|
|
47
|
+
*/
|
|
48
|
+
const SPAN = 36;
|
|
49
|
+
/**
|
|
50
|
+
* Number of characters in a complete DIGIPIN.
|
|
51
|
+
*
|
|
52
|
+
* Each character contributes two bits per coordinate axis.
|
|
53
|
+
*/
|
|
54
|
+
const PRECISION = 10;
|
|
55
|
+
/**
|
|
56
|
+
* Number of subdivisions along each coordinate axis.
|
|
57
|
+
*
|
|
58
|
+
* Ten levels of four-way subdivision produce 4^10 cells along each axis, equivalent to 20 bits.
|
|
59
|
+
*/
|
|
60
|
+
const RESOLUTION = 1 << 20;
|
|
61
|
+
/**
|
|
62
|
+
* Maximum zero-based cell index along either axis.
|
|
63
|
+
*/
|
|
64
|
+
const MAX_INDEX = RESOLUTION - 1;
|
|
65
|
+
/**
|
|
66
|
+
* Conversion factor from geographic degrees to cell indices.
|
|
67
|
+
*/
|
|
68
|
+
const SCALE = RESOLUTION / SPAN;
|
|
69
|
+
/**
|
|
70
|
+
* Geographic width or height of one DIGIPIN cell, in degrees.
|
|
71
|
+
*
|
|
72
|
+
* It is the span of the cell whose center a decoded position is.
|
|
73
|
+
*/
|
|
74
|
+
export const DIGIPIN_CELL_DEGREES = SPAN / RESOLUTION;
|
|
75
|
+
/**
|
|
76
|
+
* ASCII character-to-grid-index lookup table.
|
|
77
|
+
*
|
|
78
|
+
* Each uppercase DIGIPIN character maps to its zero-based position in the flattened character grid.
|
|
79
|
+
* The decoders read only validated codes, so no other character reaches the table.
|
|
80
|
+
*
|
|
81
|
+
* Initialized once at module evaluation.
|
|
82
|
+
* It doesn't cache encoding or decoding results.
|
|
83
|
+
*/
|
|
84
|
+
const DECODE = buildDecodeTable();
|
|
85
|
+
/**
|
|
86
|
+
* Build the ASCII character-to-grid-index lookup table from {@link ALPHABET}.
|
|
87
|
+
*/
|
|
88
|
+
function buildDecodeTable() {
|
|
89
|
+
const table = new Uint8Array(128);
|
|
90
|
+
for (let i = 0; i < ALPHABET.length; i++) {
|
|
91
|
+
table[ALPHABET.charCodeAt(i)] = i;
|
|
92
|
+
}
|
|
93
|
+
return table;
|
|
94
|
+
}
|
|
95
|
+
// #endregion
|
|
96
|
+
// #region Public Functions
|
|
97
|
+
/**
|
|
98
|
+
* Validate geographic coordinates against DIGIPIN coverage bounds.
|
|
99
|
+
*
|
|
100
|
+
* Both coordinates must be finite numbers within the supported geographic region, including its boundaries.
|
|
101
|
+
*
|
|
102
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
103
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
104
|
+
*
|
|
105
|
+
* @throws {RangeError} If latitude or longitude is outside DIGIPIN bounds.
|
|
106
|
+
*/
|
|
107
|
+
export function validateDIGIPINCoordinates(latitude, longitude) {
|
|
108
|
+
if (!Number.isFinite(latitude) || latitude < MIN_LAT || latitude > MAX_LAT) {
|
|
109
|
+
throw new RangeError("Latitude out of range");
|
|
110
|
+
}
|
|
111
|
+
if (!Number.isFinite(longitude) || longitude < MIN_LON || longitude > MAX_LON) {
|
|
112
|
+
throw new RangeError("Longitude out of range");
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Encode geographic coordinates into a 10-character DIGIPIN.
|
|
117
|
+
*
|
|
118
|
+
* Coordinates are quantized into 20-bit integer cell indices.
|
|
119
|
+
* Each iteration extracts two bits from each coordinate and combines them into
|
|
120
|
+
* an index into the DIGIPIN alphabet.
|
|
121
|
+
*
|
|
122
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
123
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
124
|
+
* @returns Uppercase 10-character DIGIPIN.
|
|
125
|
+
*
|
|
126
|
+
* @throws {RangeError} If coordinates are outside DIGIPIN bounds.
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* const pin = getDIGIPIN(13.11179621, 80.20264269)
|
|
130
|
+
*/
|
|
131
|
+
export function getDIGIPIN(latitude, longitude) {
|
|
132
|
+
validateDIGIPINCoordinates(latitude, longitude);
|
|
133
|
+
// The bounds are checked, so the code is ten characters of the alphabet.
|
|
134
|
+
return getDIGIPINUnsafe(latitude, longitude);
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* Encode geographic coordinates without validating their bounds.
|
|
138
|
+
*
|
|
139
|
+
* Intended for performance-sensitive batch processing where coordinates have already been validated.
|
|
140
|
+
*
|
|
141
|
+
* Coordinates are quantized into 20-bit indices and encoded using bitwise operations.
|
|
142
|
+
*
|
|
143
|
+
* @param latitude - Latitude in WGS 84 degrees.
|
|
144
|
+
* @param longitude - Longitude in WGS 84 degrees.
|
|
145
|
+
* @returns Uppercase 10-character DIGIPIN.
|
|
146
|
+
*
|
|
147
|
+
* @remarks Inputs must be finite and inside DIGIPIN coverage bounds.
|
|
148
|
+
* Out-of-range inputs produce unspecified results.
|
|
149
|
+
*/
|
|
150
|
+
export function getDIGIPINUnsafe(latitude, longitude) {
|
|
151
|
+
/**
|
|
152
|
+
* Quantized latitude cell index.
|
|
153
|
+
*/
|
|
154
|
+
const lat = Math.min(MAX_INDEX, Math.floor((latitude - MIN_LAT) * SCALE));
|
|
155
|
+
/**
|
|
156
|
+
* Quantized longitude cell index.
|
|
157
|
+
*/
|
|
158
|
+
const lon = Math.min(MAX_INDEX, Math.floor((longitude - MIN_LON) * SCALE));
|
|
159
|
+
/**
|
|
160
|
+
* Encoded DIGIPIN string.
|
|
161
|
+
*/
|
|
162
|
+
let pin = "";
|
|
163
|
+
/**
|
|
164
|
+
* Extract two bits per axis, beginning with the highest-order bits.
|
|
165
|
+
*/
|
|
166
|
+
for (let shift = 18; shift >= 0; shift -= 2) {
|
|
167
|
+
const row = 3 - ((lat >>> shift) & 3);
|
|
168
|
+
const col = (lon >>> shift) & 3;
|
|
169
|
+
pin += ALPHABET[(row << 2) | col];
|
|
170
|
+
}
|
|
171
|
+
return pin;
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Decode a DIGIPIN into the center coordinates of its cell.
|
|
175
|
+
*
|
|
176
|
+
* Returns a GeoJSON-compatible position in longitude-first order.
|
|
177
|
+
* Coordinates retain floating-point precision without decimal rounding.
|
|
178
|
+
*
|
|
179
|
+
* The parameter is the validated code of the `spatial` family of `@mailwoman/recognizer`:
|
|
180
|
+
* ten upper-case characters without hyphens.
|
|
181
|
+
* The validator accepts lower case and the display form `39J-49L-L8T4`
|
|
182
|
+
* and normalizes both, so this decoder skips every check.
|
|
183
|
+
*
|
|
184
|
+
* A caller that holds a string reads it through the recognizer.
|
|
185
|
+
*
|
|
186
|
+
* @param pin - Validated DIGIPIN.
|
|
187
|
+
* @returns Cell center as [longitude, latitude].
|
|
188
|
+
*
|
|
189
|
+
* @example
|
|
190
|
+
* const [longitude, latitude] = getLatLngFromDIGIPIN(getDIGIPIN(13.11179621, 80.20264269))
|
|
191
|
+
*/
|
|
192
|
+
export function getLatLngFromDIGIPIN(pin) {
|
|
193
|
+
/**
|
|
194
|
+
* Result position in GeoJSON coordinate order.
|
|
195
|
+
*/
|
|
196
|
+
const position = [0, 0];
|
|
197
|
+
getLatLngFromDIGIPINInto(pin, position);
|
|
198
|
+
return position;
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Decode a DIGIPIN directly into an existing coordinate buffer.
|
|
202
|
+
*
|
|
203
|
+
* Avoids allocating intermediate coordinate tuples.
|
|
204
|
+
*
|
|
205
|
+
* The destination receives longitude followed by latitude.
|
|
206
|
+
* Supports both ordinary JavaScript arrays and Float64Array buffers.
|
|
207
|
+
*
|
|
208
|
+
* @param pin - Validated DIGIPIN, ten upper-case characters.
|
|
209
|
+
* @param output - Destination coordinate buffer.
|
|
210
|
+
* @param offset - Starting element offset.
|
|
211
|
+
* Defaults to zero.
|
|
212
|
+
*
|
|
213
|
+
* @example
|
|
214
|
+
* const pins = [getDIGIPIN(13.11179621, 80.20264269), getDIGIPIN(28.6139, 77.209)]
|
|
215
|
+
* const output = new Float64Array(pins.length * 2)
|
|
216
|
+
*
|
|
217
|
+
* for (let i = 0; i < pins.length; i++) {
|
|
218
|
+
* getLatLngFromDIGIPINInto(pins[i]!, output, i * 2)
|
|
219
|
+
* }
|
|
220
|
+
*/
|
|
221
|
+
export function getLatLngFromDIGIPINInto(pin, output, offset = 0) {
|
|
222
|
+
/**
|
|
223
|
+
* Accumulated 20-bit latitude cell index.
|
|
224
|
+
*/
|
|
225
|
+
let lat = 0;
|
|
226
|
+
/**
|
|
227
|
+
* Accumulated 20-bit longitude cell index.
|
|
228
|
+
*/
|
|
229
|
+
let lon = 0;
|
|
230
|
+
for (let i = 0; i < PRECISION; i++) {
|
|
231
|
+
/**
|
|
232
|
+
* Flattened DIGIPIN grid position.
|
|
233
|
+
*/
|
|
234
|
+
const index = DECODE[pin.charCodeAt(i)];
|
|
235
|
+
lat = (lat << 2) | (3 - (index >>> 2));
|
|
236
|
+
lon = (lon << 2) | (index & 3);
|
|
237
|
+
}
|
|
238
|
+
output[offset] = MIN_LON + (lon + 0.5) * DIGIPIN_CELL_DEGREES;
|
|
239
|
+
output[offset + 1] = MIN_LAT + (lat + 0.5) * DIGIPIN_CELL_DEGREES;
|
|
240
|
+
}
|
|
241
|
+
// #endregion
|
|
242
|
+
//# sourceMappingURL=digipin.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"digipin.js","sourceRoot":"","sources":["../lib/digipin.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAMlE;;;;;;;;;;;;;;;;;GAiBG;AAEH,qCAAqC;AAErC;;;;;GAKG;AACH,MAAM,QAAQ,GAAG,kBAAkB,CAAA;AAEnC;;GAEG;AACH,MAAM,OAAO,GAAG,GAAG,CAAA;AAEnB;;GAEG;AACH,MAAM,OAAO,GAAG,IAAI,CAAA;AAEpB;;GAEG;AACH,MAAM,OAAO,GAAG,IAAI,CAAA;AAEpB;;GAEG;AACH,MAAM,OAAO,GAAG,IAAI,CAAA;AAEpB;;GAEG;AACH,MAAM,IAAI,GAAG,EAAE,CAAA;AAEf;;;;GAIG;AACH,MAAM,SAAS,GAAG,EAAE,CAAA;AAEpB;;;;GAIG;AACH,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,CAAA;AAE1B;;GAEG;AACH,MAAM,SAAS,GAAG,UAAU,GAAG,CAAC,CAAA;AAEhC;;GAEG;AACH,MAAM,KAAK,GAAG,UAAU,GAAG,IAAI,CAAA;AAE/B;;;;GAIG;AACH,MAAM,CAAC,MA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AAC,GAAG,SAAS,EAAE,CAAC,EAAE,EAAE,CAAC;QACpC;;WAEG;QACH,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC,CAAC,CAAE,CAAA;QAExC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,KAAK,KAAK,CAAC,CAAC,CAAC,CAAA;QACtC,GAAG,GAAG,CAAC,GAAG,IAAI,CAAC,CAAC,GAAG,CAAC,KAAK,GAAG,CAAC,CAAC,CAAA;IAC/B,CAAC;IAED,MAAM,CAAC,MAAM,CAAC,GAAG,OAAO,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,oBAAoB,CAAA;IAC7D,MAAM,CAAC,MAAM,GAAG,CAAC,CAAC,GAAG,OAAO,GAAG,CAAC,GAAG,GAAG,GAAG,CAAC,GAAG,oBAAoB,CAAA;AAClE,CAAC;AACD,aAAa"}
|
package/out/index.d.ts
CHANGED
|
@@ -10,7 +10,8 @@ export * from "#google";
|
|
|
10
10
|
export * from "#h3";
|
|
11
11
|
export * from "#objects";
|
|
12
12
|
export * from "#osgb36";
|
|
13
|
-
export * from "#point
|
|
13
|
+
export * from "#point/reading";
|
|
14
|
+
export * from "#point/text";
|
|
14
15
|
export * from "#polyline";
|
|
15
16
|
export * from "#position";
|
|
16
17
|
export { isValidLatitude, isValidLongitude } from "#coordinate/bounds";
|
package/out/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAGA,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,KAAK,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAGA,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,KAAK,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACtE,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA"}
|
package/out/index.js
CHANGED
|
@@ -12,7 +12,8 @@ export * from "#google";
|
|
|
12
12
|
export * from "#h3";
|
|
13
13
|
export * from "#objects";
|
|
14
14
|
export * from "#osgb36";
|
|
15
|
-
export * from "#point
|
|
15
|
+
export * from "#point/reading";
|
|
16
|
+
export * from "#point/text";
|
|
16
17
|
export * from "#polyline";
|
|
17
18
|
export * from "#position";
|
|
18
19
|
export { isValidLatitude, isValidLongitude } from "#coordinate/bounds";
|
package/out/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAElE,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,KAAK,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAElE,cAAc,OAAO,CAAA;AACrB,cAAc,qBAAqB,CAAA;AACnC,cAAc,oBAAoB,CAAA;AAClC,cAAc,kBAAkB,CAAA;AAChC,cAAc,WAAW,CAAA;AACzB,cAAc,UAAU,CAAA;AACxB,cAAc,qBAAqB,CAAA;AACnC,cAAc,aAAa,CAAA;AAC3B,cAAc,SAAS,CAAA;AACvB,cAAc,KAAK,CAAA;AACnB,cAAc,UAAU,CAAA;AACxB,cAAc,SAAS,CAAA;AACvB,cAAc,gBAAgB,CAAA;AAC9B,cAAc,aAAa,CAAA;AAC3B,cAAc,WAAW,CAAA;AACzB,cAAc,WAAW,CAAA;AACzB,OAAO,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACtE,cAAc,aAAa,CAAA;AAC3B,cAAc,UAAU,CAAA"}
|
|
@@ -1,3 +1,11 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file Open Location Code arithmetic.
|
|
3
|
+
*
|
|
4
|
+
* A code is read by the `spatial` family of `@mailwoman/recognizer`, which owns the alphabet, the separator
|
|
5
|
+
* and the digit counts. The decoders here take the validated, branded `Validated.FullPlusCode` and `Validated.ShortPlusCode` the family returns,
|
|
6
|
+
* so a caller cannot pass an unchecked string. A test or a tool that holds a string reads it through the recognizer.
|
|
7
|
+
*/
|
|
8
|
+
import type { Validated } from "@mailwoman/recognizer/types";
|
|
1
9
|
import type { DegreeCell } from "#coordinate/cell";
|
|
2
10
|
/**
|
|
3
11
|
* A decoded plus-code cell by its center and its span in degrees, from
|
|
@@ -5,35 +13,35 @@ import type { DegreeCell } from "#coordinate/cell";
|
|
|
5
13
|
*/
|
|
6
14
|
export type DecodedPlusCode = DegreeCell;
|
|
7
15
|
/**
|
|
8
|
-
*
|
|
16
|
+
* Decodes a full code to its cell.
|
|
9
17
|
*
|
|
10
|
-
*
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Returns true for a short plus code with 2, 4 or 6 digits before the `+`,
|
|
15
|
-
* such as the `VFQ6+92P` form Google prints on place cards.
|
|
18
|
+
* The validator guarantees the alphabet and the digit counts, and the brand guarantees
|
|
19
|
+
* the eight digits before the separator, so no input to this function fails.
|
|
20
|
+
* A short code has its own brand and decodes through {@link recoverNearestPlusCode}.
|
|
16
21
|
*/
|
|
17
|
-
export declare function
|
|
18
|
-
/**
|
|
19
|
-
* Decodes a full plus code to its cell, returning null for anything {@link isFullPlusCode} rejects.
|
|
20
|
-
*/
|
|
21
|
-
export declare function decodePlusCode(code: string): DecodedPlusCode | null;
|
|
22
|
+
export declare function decodePlusCode(code: Validated.FullPlusCode): DecodedPlusCode;
|
|
22
23
|
/**
|
|
23
24
|
* The code lengths this encoder writes: the ten-digit code Google prints,
|
|
24
25
|
* or the eleven-digit code with one grid refinement digit.
|
|
25
26
|
*/
|
|
26
27
|
export type PlusCodeLength = 10 | 11;
|
|
27
28
|
/**
|
|
28
|
-
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms
|
|
29
|
+
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms an address holds.
|
|
29
30
|
*
|
|
30
31
|
* The eleventh digit refines the last pair's cell on the 4 × 5 grid the spec defines.
|
|
32
|
+
* The digits come from the specification's alphabet and the separator follows the eighth,
|
|
33
|
+
* so the code has the form the validator accepts and the decoders take.
|
|
31
34
|
*/
|
|
32
|
-
export declare function encodePlusCode(lat: number, lon: number, length?: PlusCodeLength):
|
|
35
|
+
export declare function encodePlusCode(lat: number, lon: number, length?: PlusCodeLength): Validated.FullPlusCode;
|
|
33
36
|
/**
|
|
34
37
|
* Recovers a short plus code to the matching cell nearest a reference coordinate,
|
|
35
38
|
* following the spec's `recoverNearest`.
|
|
36
|
-
*
|
|
39
|
+
*
|
|
40
|
+
* The brand guarantees two, four or six digits before the separator, so every short code recovers to a cell.
|
|
41
|
+
* The reference must be a finite coordinate.
|
|
42
|
+
*
|
|
43
|
+
* Every caller already holds one.
|
|
44
|
+
* A full code has its own brand and decodes through {@link decodePlusCode}.
|
|
37
45
|
*/
|
|
38
|
-
export declare function recoverNearestPlusCode(shortCode:
|
|
46
|
+
export declare function recoverNearestPlusCode(shortCode: Validated.ShortPlusCode, refLat: number, refLon: number): DecodedPlusCode;
|
|
39
47
|
//# sourceMappingURL=open-location-code.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"open-location-code.d.ts","sourceRoot":"","sources":["../lib/open-location-code.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,
|
|
1
|
+
{"version":3,"file":"open-location-code.d.ts","sourceRoot":"","sources":["../lib/open-location-code.ts"],"names":[],"mappings":"AAGA;;;;;;GAMG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,6BAA6B,CAAA;AAE5D,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAA;AAkBlD;;;GAGG;AACH,MAAM,MAAM,eAAe,GAAG,UAAU,CAAA;AAExC;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,IAAI,EAAE,SAAS,CAAC,YAAY,GAAG,eAAe,CAE5E;AAyED;;;GAGG;AACH,MAAM,MAAM,cAAc,GAAG,EAAE,GAAG,EAAE,CAAA;AAEpC;;;;;;GAMG;AACH,wBAAgB,cAAc,CAAC,GAAG,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,MAAM,GAAE,cAAmB,GAAG,SAAS,CAAC,YAAY,CAW5G;AAED;;;;;;;;;GASG;AACH,wBAAgB,sBAAsB,CACrC,SAAS,EAAE,SAAS,CAAC,aAAa,EAClC,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACZ,eAAe,CAuBjB"}
|
|
@@ -1,32 +1,30 @@
|
|
|
1
1
|
// SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
|
|
2
2
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
|
|
3
3
|
const OLC_ALPHABET = "23456789CFGHJMPQRVWX";
|
|
4
|
+
/**
|
|
5
|
+
* The digits before the separator of a full code.
|
|
6
|
+
*
|
|
7
|
+
* The validator reads eight as a full code and two, four or six as a short one.
|
|
8
|
+
*/
|
|
9
|
+
const FULL_PREFIX_LENGTH = 8;
|
|
4
10
|
const OLC_DIGIT_VALUE = new Map([...OLC_ALPHABET].map((c, i) => [c, i]));
|
|
5
11
|
const PAIR_RESOLUTIONS = [20, 1, 1 / 20, 1 / 400, 1 / 8000];
|
|
6
12
|
const GRID_COLUMNS = 4;
|
|
7
13
|
const GRID_ROWS = 5;
|
|
8
14
|
/**
|
|
9
|
-
*
|
|
15
|
+
* Decodes a full code to its cell.
|
|
10
16
|
*
|
|
11
|
-
*
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
return /^[23456789CFGHJMPQRVWX]{8}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token);
|
|
15
|
-
}
|
|
16
|
-
/**
|
|
17
|
-
* Returns true for a short plus code with 2, 4 or 6 digits before the `+`,
|
|
18
|
-
* such as the `VFQ6+92P` form Google prints on place cards.
|
|
17
|
+
* The validator guarantees the alphabet and the digit counts, and the brand guarantees
|
|
18
|
+
* the eight digits before the separator, so no input to this function fails.
|
|
19
|
+
* A short code has its own brand and decodes through {@link recoverNearestPlusCode}.
|
|
19
20
|
*/
|
|
20
|
-
export function
|
|
21
|
-
return
|
|
21
|
+
export function decodePlusCode(code) {
|
|
22
|
+
return decodeDigits(code.replace("+", ""));
|
|
22
23
|
}
|
|
23
24
|
/**
|
|
24
|
-
* Decodes a full
|
|
25
|
+
* Decodes the digits of a full code, 10 or 11 of them, without the separator.
|
|
25
26
|
*/
|
|
26
|
-
|
|
27
|
-
if (!isFullPlusCode(code))
|
|
28
|
-
return null;
|
|
29
|
-
const digits = code.toUpperCase().replace("+", "");
|
|
27
|
+
function decodeDigits(digits) {
|
|
30
28
|
let latLo = -90;
|
|
31
29
|
let lonLo = -180;
|
|
32
30
|
let latSpan = 400;
|
|
@@ -72,34 +70,36 @@ function encodePairDigits(lat, lon, length) {
|
|
|
72
70
|
return encodePairs(lat, lon, length).digits;
|
|
73
71
|
}
|
|
74
72
|
/**
|
|
75
|
-
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms
|
|
73
|
+
* Encodes a coordinate as a full plus code of 10 or 11 digits, the forms an address holds.
|
|
76
74
|
*
|
|
77
75
|
* The eleventh digit refines the last pair's cell on the 4 × 5 grid the spec defines.
|
|
76
|
+
* The digits come from the specification's alphabet and the separator follows the eighth,
|
|
77
|
+
* so the code has the form the validator accepts and the decoders take.
|
|
78
78
|
*/
|
|
79
79
|
export function encodePlusCode(lat, lon, length = 10) {
|
|
80
80
|
const { digits, latRemainder, lonRemainder } = encodePairs(lat, lon, 10);
|
|
81
|
-
const code = `${digits.slice(0,
|
|
81
|
+
const code = `${digits.slice(0, FULL_PREFIX_LENGTH)}+${digits.slice(FULL_PREFIX_LENGTH)}`;
|
|
82
82
|
if (length === 10)
|
|
83
83
|
return code;
|
|
84
84
|
const cell = PAIR_RESOLUTIONS[4];
|
|
85
85
|
const row = Math.min(Math.floor(latRemainder / (cell / GRID_ROWS)), GRID_ROWS - 1);
|
|
86
86
|
const column = Math.min(Math.floor(lonRemainder / (cell / GRID_COLUMNS)), GRID_COLUMNS - 1);
|
|
87
|
-
return code + OLC_ALPHABET[row * GRID_COLUMNS + column];
|
|
87
|
+
return (code + OLC_ALPHABET[row * GRID_COLUMNS + column]);
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Recovers a short plus code to the matching cell nearest a reference coordinate,
|
|
91
91
|
* following the spec's `recoverNearest`.
|
|
92
|
-
*
|
|
92
|
+
*
|
|
93
|
+
* The brand guarantees two, four or six digits before the separator, so every short code recovers to a cell.
|
|
94
|
+
* The reference must be a finite coordinate.
|
|
95
|
+
*
|
|
96
|
+
* Every caller already holds one.
|
|
97
|
+
* A full code has its own brand and decodes through {@link decodePlusCode}.
|
|
93
98
|
*/
|
|
94
99
|
export function recoverNearestPlusCode(shortCode, refLat, refLon) {
|
|
95
|
-
|
|
96
|
-
return null;
|
|
97
|
-
const upper = shortCode.toUpperCase();
|
|
98
|
-
const missing = 8 - upper.indexOf("+");
|
|
100
|
+
const missing = FULL_PREFIX_LENGTH - shortCode.indexOf("+");
|
|
99
101
|
const prefix = encodePairDigits(refLat, refLon, missing);
|
|
100
|
-
const candidate =
|
|
101
|
-
if (!candidate)
|
|
102
|
-
return null;
|
|
102
|
+
const candidate = decodeDigits(prefix + shortCode.replace("+", ""));
|
|
103
103
|
const LAT_MIN = -90;
|
|
104
104
|
const LAT_MAX = 90;
|
|
105
105
|
const prefixResolution = PAIR_RESOLUTIONS[missing / 2 - 1];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"open-location-code.js","sourceRoot":"","sources":["../lib/open-location-code.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;
|
|
1
|
+
{"version":3,"file":"open-location-code.js","sourceRoot":"","sources":["../lib/open-location-code.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAclE,MAAM,YAAY,GAAG,sBAAsB,CAAA;AAE3C;;;;GAIG;AACH,MAAM,kBAAkB,GAAG,CAAC,CAAA;AAE5B,MAAM,eAAe,GAAG,IAAI,GAAG,CAAiB,CAAC,GAAG,YAAY,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,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AC,GAAG,IAAI,gBAAgB,CAAA;IAC/B,CAAC;IAED,OAAO,MAAM,CAAA;AACd,CAAC"}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @file What a typed query resolves to when it is a point rather than an address, read from recognitions.
|
|
3
|
+
*
|
|
4
|
+
* A search field receives coordinates in a dozen spellings, a plus code, an H3 cell, a geohash or a
|
|
5
|
+
* Who's On First id beside ordinary addresses. {@link recognizePointQuery} runs one recognizer over the `coordinates`,
|
|
6
|
+
* `spatial` and `identifiers` families of `@mailwoman/recognizer` and answers from the recognition that spans the text.
|
|
7
|
+
* It decodes no cell: decoding a code to a coordinate belongs to the module that owns the format,
|
|
8
|
+
* and the H3 reader loads a WebAssembly binary that a text test shouldn't pay for.
|
|
9
|
+
*
|
|
10
|
+
* # What makes a recognition a reading
|
|
11
|
+
*
|
|
12
|
+
* A recognition is trusted when the engine's validator accepted it and its span is the whole text.
|
|
13
|
+
* An embedded span or a rejected recognition never becomes a reading: `Apt 4, 12 Main St` holds the valid pair `4, 12`.
|
|
14
|
+
*
|
|
15
|
+
* The grammars admit no white space around a value, so **the caller trims** the text before it is read.
|
|
16
|
+
* The earth app trims where it takes the typed input.
|
|
17
|
+
*
|
|
18
|
+
* # One handler for every kind
|
|
19
|
+
*
|
|
20
|
+
* `handlers` has an entry for each kind of the three families, and the object is checked against
|
|
21
|
+
* `HandlerMap<typeof families>`. A kind added to any family fails the TypeScript build until it has a handler.
|
|
22
|
+
* A handler returns a point reading, or a {@link NotAPointQuery} with the recognized kind and the reason it isn't a point.
|
|
23
|
+
*/
|
|
24
|
+
import { type KindsOf } from "@mailwoman/recognizer";
|
|
25
|
+
import type { AxisOrder, Validated } from "@mailwoman/recognizer/types";
|
|
26
|
+
/**
|
|
27
|
+
* The families a point query is read from, composed once.
|
|
28
|
+
*/
|
|
29
|
+
declare const families: readonly [import("@mailwoman/recognizer").FamilyDeclaration<import("@mailwoman/recognizer/coordinates.generated").CoordinatesKind, import("@mailwoman/recognizer/coordinates.generated").CoordinatesPayloads, Record<never, never>, import("@mailwoman/recognizer/coordinates.generated").CoordinatesRejections>, import("@mailwoman/recognizer").FamilyDeclaration<import("@mailwoman/recognizer/spatial.generated").SpatialKind, import("@mailwoman/recognizer/spatial.generated").SpatialPayloads, Record<never, never>, import("@mailwoman/recognizer/spatial.generated").SpatialRejections>, import("@mailwoman/recognizer").FamilyDeclaration<import("@mailwoman/recognizer/identifiers.generated").IdentifiersKind, import("@mailwoman/recognizer/identifiers.generated").IdentifiersPayloads, Record<never, never>, import("@mailwoman/recognizer/identifiers.generated").IdentifiersRejections>];
|
|
30
|
+
/**
|
|
31
|
+
* Every kind of recognition a point query reads, one per shape of the three families.
|
|
32
|
+
*/
|
|
33
|
+
export type PointQueryKind = KindsOf<typeof families>;
|
|
34
|
+
/**
|
|
35
|
+
* A point reading is the point a text resolves to.
|
|
36
|
+
*
|
|
37
|
+
* `recognized` is the generated kind of the recognition the reading came from.
|
|
38
|
+
*/
|
|
39
|
+
export type PointReading = {
|
|
40
|
+
readonly recognized: PointQueryKind;
|
|
41
|
+
} & ({
|
|
42
|
+
/**
|
|
43
|
+
* A coordinate pair in decimal, degrees-minutes-seconds, labeled, `geo:`
|
|
44
|
+
* or Google Maps form, latitude and longitude as the validator branded them.
|
|
45
|
+
*
|
|
46
|
+
* `lon_lat` is reported when the first number is out of latitude range,
|
|
47
|
+
* so it can only be a longitude.
|
|
48
|
+
*/
|
|
49
|
+
readonly kind: "coordinates";
|
|
50
|
+
readonly latitude: Validated.Latitude;
|
|
51
|
+
readonly longitude: Validated.Longitude;
|
|
52
|
+
readonly order: AxisOrder;
|
|
53
|
+
} | {
|
|
54
|
+
/**
|
|
55
|
+
* A GeoJSON `Point`.
|
|
56
|
+
* Its position is already latitude first in the payload.
|
|
57
|
+
*/
|
|
58
|
+
readonly kind: "geojson_point";
|
|
59
|
+
readonly latitude: Validated.Latitude;
|
|
60
|
+
readonly longitude: Validated.Longitude;
|
|
61
|
+
} | {
|
|
62
|
+
/**
|
|
63
|
+
* A full Open Location Code, upper-cased.
|
|
64
|
+
* It decodes to a cell with no further input.
|
|
65
|
+
*/
|
|
66
|
+
readonly kind: "plus_code";
|
|
67
|
+
readonly full: true;
|
|
68
|
+
readonly code: Validated.FullPlusCode;
|
|
69
|
+
} | {
|
|
70
|
+
/**
|
|
71
|
+
* A short Open Location Code, upper-cased.
|
|
72
|
+
* It decodes only beside a reference position.
|
|
73
|
+
*/
|
|
74
|
+
readonly kind: "plus_code";
|
|
75
|
+
readonly full: false;
|
|
76
|
+
readonly code: Validated.ShortPlusCode;
|
|
77
|
+
} | {
|
|
78
|
+
/**
|
|
79
|
+
* A DIGIPIN, ten upper-case characters without hyphens.
|
|
80
|
+
*/
|
|
81
|
+
readonly kind: "digipin";
|
|
82
|
+
readonly code: Validated.Digipin;
|
|
83
|
+
} | {
|
|
84
|
+
/**
|
|
85
|
+
* An H3 cell index in its hexadecimal form, lower-cased.
|
|
86
|
+
*
|
|
87
|
+
* The validator checks the shape, and the H3 library decides validity when it decodes the cell.
|
|
88
|
+
*/
|
|
89
|
+
readonly kind: "h3";
|
|
90
|
+
readonly cell: string;
|
|
91
|
+
} | {
|
|
92
|
+
/**
|
|
93
|
+
* A geohash behind its `geohash:` or `gh:` prefix, lower-cased.
|
|
94
|
+
*/
|
|
95
|
+
readonly kind: "geohash";
|
|
96
|
+
readonly hash: string;
|
|
97
|
+
} | {
|
|
98
|
+
/**
|
|
99
|
+
* A Who's On First id, from `wof:85633793`, `wof 85633793` or a Spelunker link.
|
|
100
|
+
*/
|
|
101
|
+
readonly kind: "wof";
|
|
102
|
+
readonly id: Validated.Integer;
|
|
103
|
+
});
|
|
104
|
+
/**
|
|
105
|
+
* Lists why a recognized text isn't a point query.
|
|
106
|
+
*
|
|
107
|
+
* - `names_no_position`: the text is an identifier or a document with no single position,
|
|
108
|
+
* such as a Census GEOID, a GNIS feature id or a GeoJSON `LineString`.
|
|
109
|
+
* - `ambiguous_without_prefix`: the text is a valid code whose spelling is also a phone number
|
|
110
|
+
* or a count, so no prefix or separator makes it a position.
|
|
111
|
+
* - `digit_group`: the text is a well-formed code made only of digits in a grouped form.
|
|
112
|
+
* A phone number is written the same way.
|
|
113
|
+
*/
|
|
114
|
+
export type NotAPointReason = "names_no_position" | "ambiguous_without_prefix" | "digit_group";
|
|
115
|
+
/**
|
|
116
|
+
* The text is a recognized shape and not a point query: a caller sends it to the address classifier.
|
|
117
|
+
*/
|
|
118
|
+
export interface NotAPointQuery {
|
|
119
|
+
readonly kind: "not_a_point";
|
|
120
|
+
readonly recognized: PointQueryKind;
|
|
121
|
+
readonly reason: NotAPointReason;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* A text that's a whole recognition resolves to a point or to a shape that isn't a point.
|
|
125
|
+
*/
|
|
126
|
+
export type PointQueryReading = PointReading | NotAPointQuery;
|
|
127
|
+
/**
|
|
128
|
+
* Reads a trimmed text as a point, or as a recognized shape that isn't a point query.
|
|
129
|
+
*
|
|
130
|
+
* Returns `null` when no recognition of the three families is validated and covers the whole text.
|
|
131
|
+
* Every address is such a text.
|
|
132
|
+
*
|
|
133
|
+
* A coordinate out of range in both axis orders is a rejected recognition,
|
|
134
|
+
* so it reads as `null`, never clamped.
|
|
135
|
+
*
|
|
136
|
+
* @param text The text the caller already trimmed, because the grammars admit no outer white space.
|
|
137
|
+
*/
|
|
138
|
+
export declare function recognizePointQuery(text: string): PointQueryReading | null;
|
|
139
|
+
/**
|
|
140
|
+
* Reads a trimmed text as a point, or `null` when it is an address or a recognized shape with no position.
|
|
141
|
+
*/
|
|
142
|
+
export declare function pointFromText(text: string): PointReading | null;
|
|
143
|
+
export {};
|
|
144
|
+
//# sourceMappingURL=reading.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"reading.d.ts","sourceRoot":"","sources":["../../lib/point/reading.ts"],"names":[],"mappings":"AAGA;;;;;;;;;;;;;;;;;;;;;;GAsBG;AAEH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