@mailwoman/spatial 11.0.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.
Files changed (214) hide show
  1. package/README.md +2 -1
  2. package/lib/bbox.ts +3 -4
  3. package/lib/coordinate/bounds.ts +3 -4
  4. package/lib/coordinate/cell.ts +42 -0
  5. package/lib/coordinate/formats.ts +61 -5
  6. package/lib/digipin.ts +282 -0
  7. package/lib/distance.ts +5 -5
  8. package/lib/feature.ts +2 -5
  9. package/lib/gb/codepoint.ts +6 -7
  10. package/lib/gb/os-downloads.ts +6 -6
  11. package/lib/geometries/collection.ts +3 -4
  12. package/lib/geometries/line-string.ts +3 -4
  13. package/lib/geometries/point/coordinates.ts +8 -8
  14. package/lib/geometries/point/distance.ts +4 -4
  15. package/lib/geometries/point.ts +10 -17
  16. package/lib/geometries/polygon.ts +3 -6
  17. package/lib/geometries/ring-blob.ts +9 -10
  18. package/lib/geometries/round.ts +73 -0
  19. package/lib/geometries/simplify.ts +126 -0
  20. package/lib/geometries.ts +3 -4
  21. package/lib/google/place-id.ts +3 -4
  22. package/lib/google.ts +2 -5
  23. package/lib/h3/cell.ts +5 -5
  24. package/lib/h3/coverage.ts +8 -9
  25. package/lib/h3/interior-cells.ts +5 -6
  26. package/lib/h3/polygon-cells.ts +9 -10
  27. package/lib/h3.ts +2 -5
  28. package/lib/index.ts +5 -5
  29. package/lib/objects.ts +2 -5
  30. package/lib/open-location-code.ts +85 -39
  31. package/lib/osgb36.ts +2 -5
  32. package/lib/point/reading.ts +323 -0
  33. package/lib/point/text.ts +70 -0
  34. package/lib/polyline.ts +9 -10
  35. package/lib/position.ts +8 -7
  36. package/lib/projection/transform.ts +36 -21
  37. package/lib/projection.ts +2 -5
  38. package/lib/regions/codes.ts +2 -5
  39. package/lib/regions/names.ts +2 -5
  40. package/lib/regions.ts +2 -5
  41. package/lib/well-known-text.ts +3 -4
  42. package/out/bbox.d.ts +0 -7
  43. package/out/bbox.d.ts.map +1 -1
  44. package/out/bbox.js +2 -4
  45. package/out/bbox.js.map +1 -1
  46. package/out/coordinate/bounds.d.ts +0 -4
  47. package/out/coordinate/bounds.d.ts.map +1 -1
  48. package/out/coordinate/bounds.js +2 -4
  49. package/out/coordinate/bounds.js.map +1 -1
  50. package/out/coordinate/cell.d.ts +23 -0
  51. package/out/coordinate/cell.d.ts.map +1 -0
  52. package/out/coordinate/cell.js +24 -0
  53. package/out/coordinate/cell.js.map +1 -0
  54. package/out/coordinate/formats.d.ts +12 -5
  55. package/out/coordinate/formats.d.ts.map +1 -1
  56. package/out/coordinate/formats.js +50 -5
  57. package/out/coordinate/formats.js.map +1 -1
  58. package/out/digipin.d.ts +94 -0
  59. package/out/digipin.d.ts.map +1 -0
  60. package/out/digipin.js +242 -0
  61. package/out/digipin.js.map +1 -0
  62. package/out/distance.d.ts +1 -11
  63. package/out/distance.d.ts.map +1 -1
  64. package/out/distance.js +4 -5
  65. package/out/distance.js.map +1 -1
  66. package/out/feature.d.ts +0 -5
  67. package/out/feature.d.ts.map +1 -1
  68. package/out/feature.js +2 -5
  69. package/out/feature.js.map +1 -1
  70. package/out/gb/codepoint.d.ts +1 -9
  71. package/out/gb/codepoint.d.ts.map +1 -1
  72. package/out/gb/codepoint.js +5 -7
  73. package/out/gb/codepoint.js.map +1 -1
  74. package/out/gb/os-downloads.d.ts +3 -6
  75. package/out/gb/os-downloads.d.ts.map +1 -1
  76. package/out/gb/os-downloads.js +5 -6
  77. package/out/gb/os-downloads.js.map +1 -1
  78. package/out/geometries/collection.d.ts +0 -4
  79. package/out/geometries/collection.d.ts.map +1 -1
  80. package/out/geometries/collection.js +2 -7
  81. package/out/geometries/collection.js.map +1 -1
  82. package/out/geometries/line-string.d.ts +0 -4
  83. package/out/geometries/line-string.d.ts.map +1 -1
  84. package/out/geometries/line-string.js +2 -7
  85. package/out/geometries/line-string.js.map +1 -1
  86. package/out/geometries/point/coordinates.d.ts +2 -12
  87. package/out/geometries/point/coordinates.d.ts.map +1 -1
  88. package/out/geometries/point/coordinates.js +5 -5
  89. package/out/geometries/point/coordinates.js.map +1 -1
  90. package/out/geometries/point/distance.d.ts +1 -4
  91. package/out/geometries/point/distance.d.ts.map +1 -1
  92. package/out/geometries/point/distance.js +3 -4
  93. package/out/geometries/point/distance.js.map +1 -1
  94. package/out/geometries/point.d.ts +6 -17
  95. package/out/geometries/point.d.ts.map +1 -1
  96. package/out/geometries/point.js +6 -8
  97. package/out/geometries/point.js.map +1 -1
  98. package/out/geometries/polygon.d.ts +1 -6
  99. package/out/geometries/polygon.d.ts.map +1 -1
  100. package/out/geometries/polygon.js +3 -6
  101. package/out/geometries/polygon.js.map +1 -1
  102. package/out/geometries/ring-blob.d.ts +3 -7
  103. package/out/geometries/ring-blob.d.ts.map +1 -1
  104. package/out/geometries/ring-blob.js +6 -13
  105. package/out/geometries/ring-blob.js.map +1 -1
  106. package/out/geometries/round.d.ts +25 -0
  107. package/out/geometries/round.d.ts.map +1 -0
  108. package/out/geometries/round.js +47 -0
  109. package/out/geometries/round.js.map +1 -0
  110. package/out/geometries/simplify.d.ts +22 -0
  111. package/out/geometries/simplify.d.ts.map +1 -0
  112. package/out/geometries/simplify.js +93 -0
  113. package/out/geometries/simplify.js.map +1 -0
  114. package/out/geometries.d.ts +0 -4
  115. package/out/geometries.d.ts.map +1 -1
  116. package/out/geometries.js +2 -4
  117. package/out/geometries.js.map +1 -1
  118. package/out/google/place-id.d.ts +0 -4
  119. package/out/google/place-id.d.ts.map +1 -1
  120. package/out/google/place-id.js +2 -7
  121. package/out/google/place-id.js.map +1 -1
  122. package/out/google.d.ts +0 -5
  123. package/out/google.d.ts.map +1 -1
  124. package/out/google.js +2 -5
  125. package/out/google.js.map +1 -1
  126. package/out/h3/cell.d.ts +1 -4
  127. package/out/h3/cell.d.ts.map +1 -1
  128. package/out/h3/cell.js +4 -5
  129. package/out/h3/cell.js.map +1 -1
  130. package/out/h3/coverage.d.ts +5 -9
  131. package/out/h3/coverage.d.ts.map +1 -1
  132. package/out/h3/coverage.js +7 -9
  133. package/out/h3/coverage.js.map +1 -1
  134. package/out/h3/interior-cells.d.ts +0 -8
  135. package/out/h3/interior-cells.d.ts.map +1 -1
  136. package/out/h3/interior-cells.js +4 -6
  137. package/out/h3/interior-cells.js.map +1 -1
  138. package/out/h3/polygon-cells.d.ts +6 -10
  139. package/out/h3/polygon-cells.d.ts.map +1 -1
  140. package/out/h3/polygon-cells.js +6 -8
  141. package/out/h3/polygon-cells.js.map +1 -1
  142. package/out/h3.d.ts +0 -5
  143. package/out/h3.d.ts.map +1 -1
  144. package/out/h3.js +2 -5
  145. package/out/h3.js.map +1 -1
  146. package/out/index.d.ts +3 -5
  147. package/out/index.d.ts.map +1 -1
  148. package/out/index.js +5 -5
  149. package/out/index.js.map +1 -1
  150. package/out/objects.d.ts +0 -5
  151. package/out/objects.d.ts.map +1 -1
  152. package/out/objects.js +2 -5
  153. package/out/objects.js.map +1 -1
  154. package/out/open-location-code.d.ts +30 -20
  155. package/out/open-location-code.d.ts.map +1 -1
  156. package/out/open-location-code.js +47 -32
  157. package/out/open-location-code.js.map +1 -1
  158. package/out/osgb36.d.ts +0 -5
  159. package/out/osgb36.d.ts.map +1 -1
  160. package/out/osgb36.js +2 -5
  161. package/out/osgb36.js.map +1 -1
  162. package/out/point/reading.d.ts +144 -0
  163. package/out/point/reading.d.ts.map +1 -0
  164. package/out/point/reading.js +167 -0
  165. package/out/point/reading.js.map +1 -0
  166. package/out/point/text.d.ts +117 -0
  167. package/out/point/text.d.ts.map +1 -0
  168. package/out/point/text.js +44 -0
  169. package/out/point/text.js.map +1 -0
  170. package/out/polyline.d.ts +1 -14
  171. package/out/polyline.d.ts.map +1 -1
  172. package/out/polyline.js +8 -10
  173. package/out/polyline.js.map +1 -1
  174. package/out/position.d.ts +3 -7
  175. package/out/position.d.ts.map +1 -1
  176. package/out/position.js +4 -8
  177. package/out/position.js.map +1 -1
  178. package/out/projection/transform.d.ts +33 -21
  179. package/out/projection/transform.d.ts.map +1 -1
  180. package/out/projection/transform.js +4 -2
  181. package/out/projection/transform.js.map +1 -1
  182. package/out/projection.d.ts +0 -5
  183. package/out/projection.d.ts.map +1 -1
  184. package/out/projection.js +2 -5
  185. package/out/projection.js.map +1 -1
  186. package/out/regions/codes.d.ts +0 -5
  187. package/out/regions/codes.d.ts.map +1 -1
  188. package/out/regions/codes.js +2 -5
  189. package/out/regions/codes.js.map +1 -1
  190. package/out/regions/names.d.ts +0 -5
  191. package/out/regions/names.d.ts.map +1 -1
  192. package/out/regions/names.js +2 -5
  193. package/out/regions/names.js.map +1 -1
  194. package/out/regions.d.ts +0 -5
  195. package/out/regions.d.ts.map +1 -1
  196. package/out/regions.js +2 -5
  197. package/out/regions.js.map +1 -1
  198. package/out/tools/ogr/stream.d.ts +0 -6
  199. package/out/tools/ogr/stream.d.ts.map +1 -1
  200. package/out/tools/ogr/stream.js +2 -3
  201. package/out/tools/ogr/stream.js.map +1 -1
  202. package/out/tools/ogr.d.ts +0 -14
  203. package/out/tools/ogr.d.ts.map +1 -1
  204. package/out/tools/ogr.js +10 -12
  205. package/out/tools/ogr.js.map +1 -1
  206. package/out/tools/tsconfig.tsbuildinfo +1 -1
  207. package/out/tsconfig.test.tsbuildinfo +1 -1
  208. package/out/well-known-text.d.ts +0 -7
  209. package/out/well-known-text.d.ts.map +1 -1
  210. package/out/well-known-text.js +2 -4
  211. package/out/well-known-text.js.map +1 -1
  212. package/package.json +7 -10
  213. package/tools/ogr/stream.ts +3 -3
  214. package/tools/ogr.ts +11 -12
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 |
@@ -33,7 +34,7 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
33
34
  - [`wkx`](https://github.com/cschwarz/wkx) — Well-Known Binary geometry parser
34
35
  - [`geo-coordinates-parser`](https://github.com/geops/geo-coordinates-parser) — coordinate string parsing
35
36
 
36
- Optional: `@googlemaps/google-maps-services-js` for Google Maps API integration.
37
+ - [`@types/google.maps`](https://www.npmjs.com/package/@types/google.maps) — type declarations for the Google Maps `LatLngLiteral` shape that `GeoPoint` accepts and returns
37
38
 
38
39
  ## Related
39
40
 
package/lib/bbox.ts CHANGED
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * GeoJSON Bounding Boxes
7
6
  */
8
7
 
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * Dependency-free WGS-84 coordinate bounds shared by parsers and geometry objects.
7
6
  */
8
7
 
@@ -0,0 +1,42 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /**
5
+ * @file A rectangular cell on the globe, as a plus code, a geohash or an H3 cell's box describes one.
6
+ */
7
+
8
+ import type { LatLonBounds } from "#bbox"
9
+ import { METRES_PER_DEGREE } from "#distance"
10
+
11
+ /**
12
+ * A cell by its center and its span in degrees, from which callers derive its box or an uncertainty radius.
13
+ */
14
+ export interface DegreeCell {
15
+ lat: number
16
+ lon: number
17
+ latSpanDeg: number
18
+ lonSpanDeg: number
19
+ }
20
+
21
+ /**
22
+ * The cell's bounding box.
23
+ */
24
+ export function cellBounds(cell: DegreeCell): LatLonBounds {
25
+ return {
26
+ minLat: cell.lat - cell.latSpanDeg / 2,
27
+ maxLat: cell.lat + cell.latSpanDeg / 2,
28
+ minLon: cell.lon - cell.lonSpanDeg / 2,
29
+ maxLon: cell.lon + cell.lonSpanDeg / 2,
30
+ }
31
+ }
32
+
33
+ /**
34
+ * The cell's half-diagonal in meters, never below one, so a point anywhere in
35
+ * the cell is within it of the center.
36
+ */
37
+ export function cellRadiusMeters(cell: DegreeCell): number {
38
+ const latHalf = (cell.latSpanDeg / 2) * METRES_PER_DEGREE
39
+ const lonHalf = (cell.lonSpanDeg / 2) * METRES_PER_DEGREE * Math.cos((cell.lat * Math.PI) / 180)
40
+
41
+ return Math.max(1, Math.round(Math.hypot(latHalf, lonHalf)))
42
+ }
@@ -1,12 +1,10 @@
1
- /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  import type { AnnotationSet, Annotator } from "@mailwoman/annotations"
8
5
 
9
6
  import { MERCATOR_LATITUDE_MAX, MERCATOR_LATITUDE_MIN } from "#coordinate/bounds"
7
+ import type { DegreeCell } from "#coordinate/cell"
10
8
 
11
9
  const GEOHASH_BITS_PER_CHAR = 5
12
10
 
@@ -127,6 +125,64 @@ export function toGeohash(lat: number, lon: number, precision = 9): string {
127
125
  return hash
128
126
  }
129
127
 
128
+ /**
129
+ * The cell a geohash names: its center and its span in degrees.
130
+ */
131
+ export type DecodedGeohash = DegreeCell
132
+
133
+ /**
134
+ * Decode a geohash to its cell, or `null` for a string outside the geohash alphabet.
135
+ *
136
+ * The alphabet excludes `a`, `i`, `l` and `o`, so the test is a character-class test, and the
137
+ * reading walks the same alternating longitude and latitude bits {@link toGeohash} writes.
138
+ */
139
+ export function fromGeohash(hash: string): DecodedGeohash | null {
140
+ if (!hash) return null
141
+
142
+ let latMin = -90
143
+ let latMax = 90
144
+ let lonMin = -180
145
+ let lonMax = 180
146
+ let evenBit = true
147
+
148
+ for (const char of hash.toLowerCase()) {
149
+ const value = GEOHASH_BASE32.indexOf(char)
150
+
151
+ if (value === -1) return null
152
+
153
+ for (let shift = GEOHASH_BITS_PER_CHAR - 1; shift >= 0; shift--) {
154
+ const bit = (value >> shift) & 1
155
+
156
+ if (evenBit) {
157
+ const mid = (lonMin + lonMax) / 2
158
+
159
+ if (bit) {
160
+ lonMin = mid
161
+ } else {
162
+ lonMax = mid
163
+ }
164
+ } else {
165
+ const mid = (latMin + latMax) / 2
166
+
167
+ if (bit) {
168
+ latMin = mid
169
+ } else {
170
+ latMax = mid
171
+ }
172
+ }
173
+
174
+ evenBit = !evenBit
175
+ }
176
+ }
177
+
178
+ return {
179
+ lat: (latMin + latMax) / 2,
180
+ lon: (lonMin + lonMax) / 2,
181
+ latSpanDeg: latMax - latMin,
182
+ lonSpanDeg: lonMax - lonMin,
183
+ }
184
+ }
185
+
130
186
  const A_CODE = "A".charCodeAt(0)
131
187
 
132
188
  /**
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
package/lib/distance.ts CHANGED
@@ -1,11 +1,11 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
5
  * @file Great-circle and segment distance math over plain numbers.
6
6
  *
7
7
  * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, which embeds its WebAssembly
8
- * binary, and a caller that only measures a distance must not load that. The object-form distances
8
+ * binary, and a caller that only measures a distance mustn't load that. The object-form distances
9
9
  * that take a `GeoPointInput` live in `#geometries/point/distance`.
10
10
  */
11
11
 
@@ -111,7 +111,7 @@ export const M2_PER_KM2 = 1_000_000
111
111
  * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
112
112
  * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
113
113
  *
114
- * That is the difference between "two channels rendered the same edge slightly
114
+ * That's the difference between "two channels rendered the same edge slightly
115
115
  * differently" and "the conversion is wrong".
116
116
  *
117
117
  * The longitude scaling matters at the same scale: comparing raw degrees treats
package/lib/feature.ts CHANGED
@@ -1,8 +1,5 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
6
3
 
7
4
  /* eslint-disable @typescript-eslint/no-explicit-any */
8
5
  import type { GeometryLiteral } from "#geometries"
@@ -1,10 +1,9 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
- * Streams unit-postcode rows from extracted Code-Point Open CSVs and converts their OSGB36
7
- * coordinates to WGS84.
5
+ * Streams unit-postcode rows from extracted Code-Point Open CSVs and converts
6
+ * their OSGB36 coordinates to WGS84.
8
7
  */
9
8
 
10
9
  import { normalizePostcodeDisplay } from "@mailwoman/postcode/display-form"
@@ -28,7 +27,7 @@ const CODEPOINT_COLUMNS = 10
28
27
  /**
29
28
  * The ONS country codes in Code-Point Open, mapped to ISO 3166-2 subdivisions.
30
29
  *
31
- * Northern Ireland has no code because the product does not cover it.
30
+ * Northern Ireland has no code because the product doesn't cover it.
32
31
  */
33
32
  export const CODEPOINT_COUNTRY_CODES = {
34
33
  E92000001: "ENG",
@@ -1,12 +1,12 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * The Ordnance Survey Downloads API's record of one downloadable archive.
7
6
  *
8
- * The API serves OS OpenData products without authentication. `GET /products/<id>/downloads`
9
- * lists one record per archive, and every OS product fetcher in this repository reads that list.
7
+ * The API serves OS OpenData products without authentication.
8
+ * `GET /products/<id>/downloads` lists one record per archive, and every OS
9
+ * product fetcher in this repository reads that list.
10
10
  */
11
11
 
12
12
  /**
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * GeoJSON Geometry Collection
7
6
  */
8
7
 
@@ -1,8 +1,7 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- *
6
5
  * GeoJSON Line String
7
6
  */
8
7
 
@@ -1,14 +1,15 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
5
  * @file The shapes a point can arrive in, and the reading of their coordinates.
6
6
  *
7
7
  * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a caller that only needs
8
- * the coordinates of a `GeoPointInput` must not load its embedded WebAssembly binary.
8
+ * the coordinates of a `GeoPointInput` mustn't load its embedded WebAssembly binary.
9
9
  */
10
10
 
11
- import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
11
+ /// <reference types="google.maps" />
12
+
12
13
  import { tryParsingJSON } from "@mailwoman/core/json"
13
14
 
14
15
  import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
@@ -97,14 +98,13 @@ export type GeoPointInput =
97
98
  | Point3DCoordinates
98
99
  | GeolocationCoordinatesLike
99
100
  | InternalPointCoordinates
100
- | LatLngLiteral
101
- | LatLng
101
+ | google.maps.LatLngLiteral
102
102
 
103
103
  /**
104
104
  * Reads the `[longitude, latitude(, altitude)]` coordinates out of a point of any accepted shape.
105
105
  *
106
106
  * It only dispatches on shape: no range check applies, and `null` answers an input
107
- * that is none of them. {@link readPointCoordinates} adds the validation.
107
+ * that's none of them. {@link readPointCoordinates} adds the validation.
108
108
  */
109
109
  export function pointInputCoordinates(input: unknown): Point2DCoordinates | Point3DCoordinates | null {
110
110
  if (isCoordPairLiteral(input)) return input
@@ -1,10 +1,10 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
1
4
  /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
5
  * @file Great-circle distances between `GeoPointInput` values or plain coordinates.
6
6
  *
7
- * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a distance must not pay for that.
7
+ * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a distance mustn't pay for that.
8
8
  * Points are read through `#geometries/point/coordinates` instead.
9
9
  */
10
10
 
@@ -1,11 +1,8 @@
1
- /**
2
- * @copyright Sister Software.
3
- * @license AGPL-3.0
4
- * @author Teffen Ellis, et al.
5
- */
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+
4
+ /// <reference types="google.maps" />
6
5
 
7
- import type google from "@googlemaps/google-maps-services-js"
8
- import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
9
6
  import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
10
7
  import { latLngToCell } from "@sister.software/h3"
11
8
  import { convert as convertCoords } from "geo-coordinates-parser"
@@ -75,7 +72,7 @@ export class GeoPoint implements PointLiteral {
75
72
 
76
73
  /**
77
74
  * Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the interface,
78
- * never inferred from the magnitudes — and rejects a coordinate that is not on the globe.
75
+ * never inferred from the magnitudes — and rejects a coordinate that's not on the globe.
79
76
  *
80
77
  * See {@link GeoPoint.from} for why both halves of that sentence are required.
81
78
  *
@@ -169,18 +166,14 @@ export class GeoPoint implements PointLiteral {
169
166
 
170
167
  /**
171
168
  * Creates a new GeoPoint instance from a Google Maps API
172
- * {@linkcode google.LatLngLiteral | LatLngLiteral} object.
169
+ * {@linkcode google.maps.LatLngLiteral | LatLngLiteral} object.
173
170
  */
174
- constructor(latLngLiteral: google.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
171
+ constructor(latLngLiteral: google.maps.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
175
172
 
176
173
  /**
177
174
  * Creates a new GeoPoint instance from internal coordinates.
178
175
  */
179
176
  constructor(interpolatedCoords: InternalPointCoordinates, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
180
- /**
181
- * Creates a new GeoPoint instance from a Google Maps API {@linkcode google.LatLng | LatLng} object.
182
- */
183
- constructor(latLng: LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
184
177
  /**
185
178
  * Create a new GeoPoint instance from another {@linkcode PointLiteral}.
186
179
  */
@@ -206,7 +199,7 @@ export class GeoPoint implements PointLiteral {
206
199
 
207
200
  /**
208
201
  * Attempts to create a new GeoPoint instance from the given input, returning `null`
209
- * rather than throwing when the input is not a coordinate this class will stand behind.
202
+ * rather than throwing when the input isn't a coordinate this class will stand behind.
210
203
  *
211
204
  * Two rules, both of which this constructor got wrong until 2026-08-05
212
205
  * (the defect was recorded in `e9bfd139` and routed around rather than fixed):
@@ -299,9 +292,9 @@ export class GeoPoint implements PointLiteral {
299
292
  }
300
293
 
301
294
  /**
302
- * Converts the GeoPoint to a Google Maps API {@linkcode google.LatLngLiteral | LatLngLiteral} object.
295
+ * Converts the GeoPoint to a Google Maps API {@linkcode google.maps.LatLngLiteral | LatLngLiteral} object.
303
296
  */
304
- public toGoogleLatLngLiteral(): google.LatLngLiteral {
297
+ public toGoogleLatLngLiteral(): google.maps.LatLngLiteral {
305
298
  return {
306
299
  lat: this.#latitude,
307
300
  lng: this.#longitude,