@mailwoman/spatial 9.4.0 → 10.1.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 (156) hide show
  1. package/README.md +2 -2
  2. package/lib/bbox.ts +41 -34
  3. package/lib/{coordinate-bounds.ts → coordinate/bounds.ts} +2 -2
  4. package/lib/{coordinate-formats.ts → coordinate/formats.ts} +11 -28
  5. package/lib/distance.ts +53 -27
  6. package/lib/feature.ts +2 -2
  7. package/lib/geometries/point.ts +41 -33
  8. package/lib/geometries/polygon.ts +24 -128
  9. package/lib/geometries/ring-blob.ts +33 -130
  10. package/lib/google/place-id.ts +4 -2
  11. package/lib/h3/cell.ts +21 -76
  12. package/lib/h3/coverage.ts +11 -7
  13. package/lib/h3/interior-cells.ts +13 -34
  14. package/lib/h3/polygon-cells.ts +59 -165
  15. package/lib/{h3/index.ts → h3.ts} +1 -1
  16. package/lib/index.ts +6 -6
  17. package/lib/objects.ts +6 -6
  18. package/lib/open-location-code.ts +11 -42
  19. package/lib/osgb36.ts +28 -187
  20. package/lib/polyline.ts +5 -3
  21. package/lib/position.ts +20 -4
  22. package/lib/projection/transform.ts +114 -0
  23. package/lib/projection.ts +1 -1
  24. package/lib/well-known-text.ts +8 -8
  25. package/out/bbox.d.ts +16 -13
  26. package/out/bbox.d.ts.map +1 -1
  27. package/out/bbox.js +32 -23
  28. package/out/bbox.js.map +1 -1
  29. package/out/{coordinate-bounds.d.ts → coordinate/bounds.d.ts} +3 -3
  30. package/out/coordinate/bounds.d.ts.map +1 -0
  31. package/out/{coordinate-bounds.js → coordinate/bounds.js} +3 -3
  32. package/out/coordinate/bounds.js.map +1 -0
  33. package/out/{coordinate-formats.d.ts → coordinate/formats.d.ts} +9 -12
  34. package/out/coordinate/formats.d.ts.map +1 -0
  35. package/out/{coordinate-formats.js → coordinate/formats.js} +12 -29
  36. package/out/coordinate/formats.js.map +1 -0
  37. package/out/distance.d.ts +48 -25
  38. package/out/distance.d.ts.map +1 -1
  39. package/out/distance.js +53 -27
  40. package/out/distance.js.map +1 -1
  41. package/out/feature.d.ts +2 -2
  42. package/out/feature.d.ts.map +1 -1
  43. package/out/geometries/point.d.ts +30 -22
  44. package/out/geometries/point.d.ts.map +1 -1
  45. package/out/geometries/point.js +35 -29
  46. package/out/geometries/point.js.map +1 -1
  47. package/out/geometries/polygon.d.ts +23 -122
  48. package/out/geometries/polygon.d.ts.map +1 -1
  49. package/out/geometries/polygon.js +13 -41
  50. package/out/geometries/polygon.js.map +1 -1
  51. package/out/geometries/ring-blob.d.ts +23 -109
  52. package/out/geometries/ring-blob.d.ts.map +1 -1
  53. package/out/geometries/ring-blob.js +29 -115
  54. package/out/geometries/ring-blob.js.map +1 -1
  55. package/out/{geometries/index.js → geometries.d.ts} +1 -1
  56. package/out/geometries.d.ts.map +1 -0
  57. package/out/{geometries/index.d.ts → geometries.js} +1 -1
  58. package/out/geometries.js.map +1 -0
  59. package/out/google/place-id.d.ts +4 -2
  60. package/out/google/place-id.d.ts.map +1 -1
  61. package/out/google/place-id.js.map +1 -1
  62. package/out/{google/index.d.ts → google.d.ts} +1 -1
  63. package/out/google.d.ts.map +1 -0
  64. package/out/{google/index.js → google.js} +1 -1
  65. package/out/google.js.map +1 -0
  66. package/out/h3/cell.d.ts +19 -73
  67. package/out/h3/cell.d.ts.map +1 -1
  68. package/out/h3/cell.js +20 -63
  69. package/out/h3/cell.js.map +1 -1
  70. package/out/h3/coverage.d.ts +11 -7
  71. package/out/h3/coverage.d.ts.map +1 -1
  72. package/out/h3/coverage.js +11 -7
  73. package/out/h3/coverage.js.map +1 -1
  74. package/out/h3/interior-cells.d.ts +9 -28
  75. package/out/h3/interior-cells.d.ts.map +1 -1
  76. package/out/h3/interior-cells.js +13 -32
  77. package/out/h3/interior-cells.js.map +1 -1
  78. package/out/h3/polygon-cells.d.ts +28 -120
  79. package/out/h3/polygon-cells.d.ts.map +1 -1
  80. package/out/h3/polygon-cells.js +48 -146
  81. package/out/h3/polygon-cells.js.map +1 -1
  82. package/out/{h3/index.d.ts → h3.d.ts} +2 -2
  83. package/out/h3.d.ts.map +1 -0
  84. package/out/{h3/index.js → h3.js} +2 -2
  85. package/out/h3.js.map +1 -0
  86. package/out/index.d.ts +6 -6
  87. package/out/index.d.ts.map +1 -1
  88. package/out/index.js +6 -6
  89. package/out/index.js.map +1 -1
  90. package/out/objects.d.ts +6 -6
  91. package/out/objects.d.ts.map +1 -1
  92. package/out/open-location-code.d.ts +11 -22
  93. package/out/open-location-code.d.ts.map +1 -1
  94. package/out/open-location-code.js +9 -40
  95. package/out/open-location-code.js.map +1 -1
  96. package/out/osgb36.d.ts +21 -97
  97. package/out/osgb36.d.ts.map +1 -1
  98. package/out/osgb36.js +12 -165
  99. package/out/osgb36.js.map +1 -1
  100. package/out/polyline.d.ts +5 -3
  101. package/out/polyline.d.ts.map +1 -1
  102. package/out/polyline.js +5 -3
  103. package/out/polyline.js.map +1 -1
  104. package/out/position.d.ts +17 -3
  105. package/out/position.d.ts.map +1 -1
  106. package/out/position.js +2 -2
  107. package/out/position.js.map +1 -1
  108. package/out/projection/transform.d.ts +70 -0
  109. package/out/projection/transform.d.ts.map +1 -0
  110. package/out/{projection-transform.js → projection/transform.js} +15 -17
  111. package/out/projection/transform.js.map +1 -0
  112. package/out/projection.d.ts +1 -1
  113. package/out/projection.d.ts.map +1 -1
  114. package/out/projection.js +1 -1
  115. package/out/regions/codes.d.ts.map +1 -1
  116. package/out/{regions/index.js → regions.d.ts} +1 -1
  117. package/out/regions.d.ts.map +1 -0
  118. package/out/{regions/index.d.ts → regions.js} +1 -1
  119. package/out/regions.js.map +1 -0
  120. package/out/tools/ogr/stream.d.ts +44 -0
  121. package/out/tools/ogr/stream.d.ts.map +1 -0
  122. package/out/tools/{ogr-stream.js → ogr/stream.js} +17 -10
  123. package/out/tools/ogr/stream.js.map +1 -0
  124. package/out/tools/ogr.d.ts +13 -10
  125. package/out/tools/ogr.d.ts.map +1 -1
  126. package/out/tools/ogr.js +18 -14
  127. package/out/tools/ogr.js.map +1 -1
  128. package/out/tools/tsconfig.tsbuildinfo +1 -0
  129. package/out/tsconfig.test.tsbuildinfo +1 -1
  130. package/out/well-known-text.d.ts +8 -8
  131. package/out/well-known-text.js +7 -7
  132. package/package.json +35 -127
  133. package/{lib/tools/ogr-stream.ts → tools/ogr/stream.ts} +25 -14
  134. package/{lib/tools → tools}/ogr.ts +26 -20
  135. package/lib/projection-transform.ts +0 -115
  136. package/out/coordinate-bounds.d.ts.map +0 -1
  137. package/out/coordinate-bounds.js.map +0 -1
  138. package/out/coordinate-formats.d.ts.map +0 -1
  139. package/out/coordinate-formats.js.map +0 -1
  140. package/out/geometries/index.d.ts.map +0 -1
  141. package/out/geometries/index.js.map +0 -1
  142. package/out/google/index.d.ts.map +0 -1
  143. package/out/google/index.js.map +0 -1
  144. package/out/h3/index.d.ts.map +0 -1
  145. package/out/h3/index.js.map +0 -1
  146. package/out/projection-transform.d.ts +0 -67
  147. package/out/projection-transform.d.ts.map +0 -1
  148. package/out/projection-transform.js.map +0 -1
  149. package/out/regions/index.d.ts.map +0 -1
  150. package/out/regions/index.js.map +0 -1
  151. package/out/tools/ogr-stream.d.ts +0 -34
  152. package/out/tools/ogr-stream.d.ts.map +0 -1
  153. package/out/tools/ogr-stream.js.map +0 -1
  154. /package/lib/{geometries/index.ts → geometries.ts} +0 -0
  155. /package/lib/{google/index.ts → google.ts} +0 -0
  156. /package/lib/{regions/index.ts → regions.ts} +0 -0
package/README.md CHANGED
@@ -21,7 +21,7 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
21
21
 
22
22
  | Capability | Implementation |
23
23
  | ---------------------- | --------------------------------------------------------------------------- |
24
- | **Coordinate math** | Haversine distance, bearing, bounding-box containment |
24
+ | **Coordinate math** | Haversine distance, direction, bounding-box containment |
25
25
  | **H3 indexing** | Hexagonal grid indexing via `h3-js` — spatial blocking for the matcher |
26
26
  | **WKB/WKT geometry** | Parse/format Well-Known Binary geometry via `wkx` |
27
27
  | **Coordinate parsing** | Parse decimal, DMS, and UTM coordinate strings via `geo-coordinates-parser` |
@@ -47,4 +47,4 @@ Optional: `@googlemaps/google-maps-services-js` for Google Maps API integration.
47
47
 
48
48
  ## License
49
49
 
50
- [AGPL-3.0-only](https://www.gnu.org/licenses/agpl-3.0.html)
50
+ [AGPL-3.0-only](https://www.gnu.org/licenses/AGPL-3.0.html)
package/lib/bbox.ts CHANGED
@@ -6,18 +6,21 @@
6
6
  * GeoJSON Bounding Boxes
7
7
  */
8
8
 
9
- import { toRad } from "#coordinate-formats"
9
+ import { toRad } from "#coordinate/formats"
10
10
  import type { PolygonLiteral, SolidPolygonPath } from "#geometries/polygon"
11
11
  import { clampLatitude, wrapLongitude } from "#position"
12
12
  import { CoordinateProjection } from "#projection"
13
13
 
14
14
  /**
15
- * Kilometres per degree of latitude, which is very nearly constant on a sphere.
15
+ * Kilometers per degree of latitude.
16
+ * The value is nearly constant on a sphere.
16
17
  */
17
18
  const KM_PER_DEGREE_LATITUDE = 111
18
19
 
19
20
  /**
20
- * Floor for cos(latitude) so a polar bbox stays finite. See {@linkcode bboxAround}.
21
+ * Floor for cos(latitude) so a polar bbox stays finite.
22
+ *
23
+ * See {@linkcode bboxAround}.
21
24
  */
22
25
  const MIN_COS_LATITUDE = 1e-6
23
26
 
@@ -31,7 +34,7 @@ const BBOX_2D_LENGTH = 4
31
34
  */
32
35
  const BBOX_3D_LENGTH = 6
33
36
 
34
- //#region Bounding Box Literals
37
+ // #region Bounding Box Literals
35
38
 
36
39
  /**
37
40
  * A 2-dimensional rectangular area that can be determined by two longitudes and two latitudes.
@@ -75,7 +78,8 @@ export type BBox2DLiteral = [
75
78
  ]
76
79
 
77
80
  /**
78
- * A 3-dimensional rectangular area that can be determined by two longitudes, two latitudes, and two altitudes.
81
+ * A 3-dimensional rectangular area bounded by two longitudes and two latitudes.
82
+ * Two altitudes set its vertical bounds.
79
83
  *
80
84
  * @category GeoJSON
81
85
  * @category Bounding Box
@@ -124,9 +128,9 @@ export type BBox3DLiteral = [
124
128
  maxAltitude: number,
125
129
  ]
126
130
 
127
- //#endregion
131
+ // #endregion
128
132
 
129
- //#region Type Predicates
133
+ // #region Type Predicates
130
134
 
131
135
  /**
132
136
  * Type-predicate for 2-dimensional bounding boxes (`[west, south, east, north]`).
@@ -165,8 +169,8 @@ export function isBBox(input: unknown): input is BBox2DLiteral | BBox3DLiteral {
165
169
  /**
166
170
  * Type-predicate to determine if the given input is a GeoBoundingBox instance.
167
171
  *
168
- * Note that this function only checks if the input is an instance of the GeoBoundingBox class. `instanceof` checks are
169
- * not reliable in JavaScript, so this function should be used with caution.
172
+ * Note that this function only checks if the input is an instance of the GeoBoundingBox class.
173
+ * `instanceof` checks are not reliable in JavaScript, so this function should be used with caution.
170
174
  *
171
175
  * @category GeoJSON
172
176
  */
@@ -174,7 +178,7 @@ export function isGeoBoundingBox(input: unknown): input is GeoBoundingBox {
174
178
  return input instanceof GeoBoundingBox
175
179
  }
176
180
 
177
- //#endregion
181
+ // #endregion
178
182
 
179
183
  /**
180
184
  * Input for creating a GeoBoundingBox instance.
@@ -186,11 +190,11 @@ export type GeoBoundingBoxInput = BBox2DLiteral | BBox3DLiteral | GeoBoundingBox
186
190
  /**
187
191
  * A bounding box to represent the coordinate range of a GeoJSON object.
188
192
  *
189
- * This is useful when defining the extent of a GeoJSON object, such as the minimum and maximum coordinates of the
190
- * object's Geometries, Features, or Feature Collections.
193
+ * This is useful when defining the extent of a GeoJSON object, such as the minimum
194
+ * and maximum coordinates of the object's Geometries, Features, or Feature Collections.
191
195
  */
192
196
  export class GeoBoundingBox {
193
- //#region Properties
197
+ // #region Properties
194
198
 
195
199
  public projection: CoordinateProjection
196
200
 
@@ -233,9 +237,9 @@ export class GeoBoundingBox {
233
237
  */
234
238
  #maxAltitude: number
235
239
 
236
- //#endregion
240
+ // #endregion
237
241
 
238
- //#region Accessors
242
+ // #region Accessors
239
243
 
240
244
  public get minLongitude() {
241
245
  return this.#minLongitude
@@ -332,9 +336,9 @@ export class GeoBoundingBox {
332
336
  return this.toJSON()[Symbol.iterator]()
333
337
  }
334
338
 
335
- //#endregion
339
+ // #endregion
336
340
 
337
- //#region Constructors
341
+ // #region Constructors
338
342
 
339
343
  /**
340
344
  * Creates a blank GeoBoundingBox instance.
@@ -381,9 +385,9 @@ export class GeoBoundingBox {
381
385
  this.projection = projection ?? CoordinateProjection.WGS84
382
386
  }
383
387
 
384
- //#endregion
388
+ // #endregion
385
389
 
386
- //#region Predicates
390
+ // #region Predicates
387
391
 
388
392
  public is3D() {
389
393
  return this.#minAltitude !== 0 || this.#maxAltitude !== 0
@@ -393,9 +397,9 @@ export class GeoBoundingBox {
393
397
  return !this.is3D()
394
398
  }
395
399
 
396
- //#endregion
400
+ // #endregion
397
401
 
398
- //#region Conversion
402
+ // #region Conversion
399
403
 
400
404
  /**
401
405
  * Converts the 2D GeoBoundingBox to an array literal.
@@ -461,16 +465,13 @@ export class GeoBoundingBox {
461
465
  return `SRID=${this.projection};POLYGON((${minLongitude} ${minLatitude}, ${maxLongitude} ${minLatitude}, ${maxLongitude} ${maxLatitude}, ${minLongitude} ${maxLatitude}, ${minLongitude} ${minLatitude}))`
462
466
  }
463
467
 
464
- //#endregion
468
+ // #endregion
465
469
  }
466
470
 
467
- //#region Equirectangular bbox around a point
471
+ // #region Equirectangular bbox around a point
468
472
 
469
473
  /**
470
- * A flat latitude/longitude range, as a query filter rather than a model.
471
- *
472
- * Distinct from {@linkcode GeoBoundingBox} on purpose: that is a class carrying a projection and private state, which an
473
- * SQL row cannot be. This is the plain shape a spatial-index query is built from — four numbers, no behaviour.
474
+ * Plain latitude and longitude bounds in decimal degrees.
474
475
  */
475
476
  export interface LatLonBounds {
476
477
  minLat: number
@@ -482,15 +483,21 @@ export interface LatLonBounds {
482
483
  /**
483
484
  * Approximate bounds `radiusKM` in each direction around a point.
484
485
  *
485
- * The spherical-Earth equirectangular approximation: 1° latitude ≈ 111 km globally, 1° longitude ≈ 111 km × cos(lat).
486
- * It is a FILTER, not an answer — it over-selects near the poles and along a long east-west span, and a caller is
487
- * expected to re-check survivors with an exact haversine distance. That is what makes the approximation safe: it may
488
- * admit a point it should not, and never excludes one it should keep.
486
+ * The spherical-Earth equirectangular approximation: 1° latitude ≈ 111 km globally,
487
+ * 1° longitude ≈ 111 km × cos(lat).
488
+ * It is a filter rather than an answer.
489
+ *
490
+ * It over-selects near the poles and along a long east-west span.
491
+ * Callers must re-check survivors with an exact haversine distance.
492
+ *
493
+ * The approximation may admit points outside the radius.
494
+ * It includes every point within the radius.
489
495
  */
490
496
  export function bboxAround(lat: number, lon: number, radiusKM: number): LatLonBounds {
491
497
  const latDelta = radiusKM / KM_PER_DEGREE_LATITUDE
492
- // cos(±90°) is 0, which would divide the longitude delta to Infinity and select the whole globe. Clamp so a polar
493
- // query stays a wide band rather than becoming unbounded.
498
+ // cos(±90°) is 0.
499
+ // A zero denominator would make the longitude delta infinite and select the whole globe.
500
+ // Clamp so a polar query stays a wide band rather than becoming unbounded.
494
501
  const cosLat = Math.max(Math.cos(toRad(lat)), MIN_COS_LATITUDE)
495
502
  const lonDelta = radiusKM / (KM_PER_DEGREE_LATITUDE * cosLat)
496
503
 
@@ -502,4 +509,4 @@ export function bboxAround(lat: number, lon: number, radiusKM: number): LatLonBo
502
509
  }
503
510
  }
504
511
 
505
- //#endregion
512
+ // #endregion
@@ -27,8 +27,8 @@ export const LONGITUDE_MIN = -180
27
27
  export const LONGITUDE_MAX = 180
28
28
 
29
29
  /**
30
- * Southern limit of the Web Mercator projection in WGS-84 degrees — the latitude at which the square projection's
31
- * vertical extent equals its horizontal one.
30
+ * Southern limit of the Web Mercator projection in WGS-84 degrees — the latitude at
31
+ * which the square projection's vertical extent equals its horizontal one.
32
32
  */
33
33
  export const MERCATOR_LATITUDE_MIN = -85.05112878
34
34
 
@@ -2,30 +2,16 @@
2
2
  * @copyright Sister Software
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- *
6
- * Coordinate-format conversions — the pure-math annotators OpenCage exposes (DMS, geohash,
7
- * Maidenhead, MGRS, Web Mercator, qibla bearing, sun times). No data, no I/O; each is a
8
- * deterministic function of a `{lat, lon}`. {@link coordinateFormatAnnotator} packages them as an
9
- * `@mailwoman/annotations` `Annotator`.
10
5
  */
11
6
 
12
7
  import type { AnnotationSet, Annotator } from "@mailwoman/annotations"
13
8
 
14
- import { MERCATOR_LATITUDE_MAX, MERCATOR_LATITUDE_MIN } from "#coordinate-bounds"
9
+ import { MERCATOR_LATITUDE_MAX, MERCATOR_LATITUDE_MIN } from "#coordinate/bounds"
15
10
 
16
- /**
17
- * Bits packed into each base-32 geohash character.
18
- */
19
11
  const GEOHASH_BITS_PER_CHAR = 5
20
12
 
21
- /**
22
- * Southern limit of the MGRS lettered bands. Below it MGRS is undefined and the UPS grid applies instead.
23
- */
24
13
  const MGRS_LAT_MIN = -80
25
14
 
26
- /**
27
- * Northern limit of the MGRS lettered bands. Band X is extended to 84°, so there is no 84–90° band.
28
- */
29
15
  const MGRS_LAT_MAX = 84
30
16
 
31
17
  /**
@@ -42,9 +28,6 @@ export function toDeg(rad: number): number {
42
28
  return (rad * 180) / Math.PI
43
29
  }
44
30
 
45
- /**
46
- * Render a single signed degree as `D° M′ S″ H` with the given hemisphere letters `[positive, negative]`.
47
- */
48
31
  function dmsComponent(value: number, hemispheres: [string, string], secondsDp = 2): string {
49
32
  const hemisphere = value >= 0 ? hemispheres[0] : hemispheres[1]
50
33
  const abs = Math.abs(value)
@@ -66,7 +49,7 @@ export function toDMS(lat: number, lon: number): { lat: string; lon: string } {
66
49
  const WEB_MERCATOR_R = 6_378_137
67
50
 
68
51
  /**
69
- * Web Mercator (EPSG:3857) projection of a coordinate.
52
+ * Web Mercator (epsg:3857) projection of a coordinate.
70
53
  */
71
54
  export function toMercator(lat: number, lon: number): { x: number; y: number } {
72
55
  const clampedLat = Math.max(MERCATOR_LATITUDE_MIN, Math.min(MERCATOR_LATITUDE_MAX, lat))
@@ -167,9 +150,10 @@ const J2000 = 2_451_545
167
150
  const unixEpochJulian = 2_440_587.5
168
151
 
169
152
  /**
170
- * Sunrise / solar-noon / sunset for a coordinate on a date, as UTC epoch seconds, via the standard sunrise equation.
171
- * `rise` and `set` are absent during polar day or polar night (the sun never crosses the horizon); `noon` (solar
172
- * transit) is always present.
153
+ * Computes sunrise, solar noon and sunset for a coordinate and date as UTC epoch
154
+ * seconds using the standard sunrise equation.
155
+ *
156
+ * `rise` and `set` are omitted during polar day or polar night, while `noon` is always present.
173
157
  */
174
158
  export function sunTimes(
175
159
  lat: number,
@@ -199,9 +183,6 @@ export function sunTimes(
199
183
  return { rise: toEpoch(transit - hourAngle / 360), set: toEpoch(transit + hourAngle / 360), noon }
200
184
  }
201
185
 
202
- /**
203
- * MGRS / UTM (WGS84). The forward Transverse Mercator series + the military grid lettering.
204
- */
205
186
  const UTM_A = 6_378_137
206
187
  const UTM_F = 1 / 298.257223563
207
188
  const UTM_K0 = 0.9996
@@ -249,11 +230,13 @@ const MGRS_COL_SETS = ["ABCDEFGH", "JKLMNPQR", "STUVWXYZ"]
249
230
  const MGRS_ROW_LETTERS = "ABCDEFGHJKLMNPQRSTUV"
250
231
 
251
232
  /**
252
- * Military Grid Reference System for a coordinate (`"18SUJ2340806479"`); `""` outside MGRS bands (±80°/84°).
233
+ * Formats a coordinate as a Military Grid Reference System string such as `"18SUJ2340806479"`,
234
+ * or returns `""` outside latitudes −80° to 84°.
253
235
  */
254
236
  export function toMGRS(lat: number, lon: number): string {
255
237
  if (lat < MGRS_LAT_MIN || lat > MGRS_LAT_MAX) return ""
256
- const band = MGRS_LAT_BANDS[Math.floor((lat + 80) / 8)]!
238
+ // Band X spans 72° to 84°, so 80° and above still maps to the last band.
239
+ const band = MGRS_LAT_BANDS[Math.min(Math.floor((lat + 80) / 8), MGRS_LAT_BANDS.length - 1)]!
257
240
  const { zone, easting, northing } = latLonToUtm(lat, lon)
258
241
  const colLetter = MGRS_COL_SETS[(zone - 1) % 3]![Math.floor(easting / 100_000) - 1]!
259
242
  let row = Math.floor(northing / 100_000) % 20
@@ -270,7 +253,7 @@ export function toMGRS(lat: number, lon: number): string {
270
253
  }
271
254
 
272
255
  /**
273
- * Fill the coordinate-format slice of an {@link AnnotationSet} from a `{lat, lon}`.
256
+ * Fill the coordinate-format fields of an {@link AnnotationSet} from a `{lat, lon}`.
274
257
  */
275
258
  export const coordinateFormatAnnotator: Annotator = ({ lat, lon, date }): Partial<AnnotationSet> => ({
276
259
  dms: toDMS(lat, lon),
package/lib/distance.ts CHANGED
@@ -38,8 +38,12 @@ const RADII = {
38
38
  export type PlanetaryBodyID = "earth" | "moon" | "mars"
39
39
 
40
40
  /**
41
- * Mean radii in kilometres. Earth's is the value `RADII` scales to its units; the Moon's and Mars's are the IAU mean
42
- * radii the USGS planetary products reference (the Moon as a sphere; Mars's mean radius, its DEM being on the areoid).
41
+ * Mean radii in kilometers.
42
+ *
43
+ * Earth's is the value `radii` scales to its units.
44
+ * The Moon's and Mars's are the IAU mean radii the usgs planetary products reference
45
+ * (the Moon as a sphere. Mars's mean radius, its DEM being on the areoid).
46
+ *
43
47
  * A distance on a body other than Earth is meaningless without one of these.
44
48
  */
45
49
  export const BODY_RADII_KM = {
@@ -57,7 +61,9 @@ export interface GreatCircleOptions {
57
61
  }
58
62
 
59
63
  /**
60
- * Shared great-circle math with no sentinel handling. `unit` selects the Earth radius.
64
+ * Shared great-circle math with no sentinel handling.
65
+ *
66
+ * `unit` selects the Earth radius.
61
67
  */
62
68
  function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
63
69
  const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
@@ -92,8 +98,10 @@ export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: Ea
92
98
  }
93
99
 
94
100
  /**
95
- * Great-circle distance on the named body. {@link haversine} is this function on Earth; a caller with a body passes it
96
- * here. The Null-Island sentinel of the object form applies: a `(0, 0)` input answers `NaN`.
101
+ * Great-circle distance on the selected body. {@link haversine} is this function on Earth.
102
+ * A caller with a body passes it here.
103
+ *
104
+ * The Null-Island sentinel of the object form applies: a `(0, 0)` input answers `NaN`.
97
105
  *
98
106
  * @category Position
99
107
  */
@@ -110,12 +118,15 @@ export function greatCircleDistance(
110
118
  }
111
119
 
112
120
  /**
113
- * Great-circle distance in kilometres between two lat/lon pairs given as raw scalars. The formula's one true home —
114
- * every resolver + eval consumer of the `(aLat, aLon, bLat, bLon)` shape imports this instead of re-declaring it.
121
+ * Returns the great-circle distance in kilometers between two coordinates on Earth.
122
+ *
123
+ * The formula's one true home — every resolver + eval consumer of the
124
+ * `(aLat, aLon, bLat, bLon)` shape imports this instead of re-declaring it.
115
125
  *
116
- * Unlike {@link haversine}, this is pure math with NO Null-Island sentinel: `(0, 0)` is the Gulf of Guinea — a real
117
- * point, not "missing coordinate". That sentinel convention belongs to the `GeoPointInput` object form (where a 0/0
118
- * input means "no coordinate"), not to a raw scalar distance.
126
+ * Unlike {@link haversine}, this is pure math with no Null-Island sentinel:
127
+ * `(0, 0)` is the Gulf of Guinea — a real point rather than "missing coordinate".
128
+ * That sentinel convention belongs to the `GeoPointInput` object form
129
+ * (where a 0/0 input means "no coordinate"), not to a raw scalar distance.
119
130
  *
120
131
  * @category Position
121
132
  */
@@ -124,26 +135,37 @@ export function haversineKm(aLat: number, aLon: number, bLat: number, bLon: numb
124
135
  }
125
136
 
126
137
  /**
127
- * Metres per degree of latitude — the scale {@link segmentDistanceMetres} reports in, and the constant the bounding-box
128
- * estimates in `#h3/polygon-cells` are built on.
138
+ * Meters per degree of latitude.
139
+ *
140
+ * The scale used by {@link segmentDistanceMetres}.
141
+ * It is also the constant the bounding-box estimates in `#h3/polygon-cells` are built on.
129
142
  */
130
143
  export const METRES_PER_DEGREE = 111_320
131
144
 
132
145
  /**
133
- * Square metres in a square kilometre — the conversion every polygon layer's area receipt reports through.
146
+ * Square meters in a square kilometer.
147
+ *
148
+ * The conversion every polygon layer's area receipt reports through.
134
149
  */
135
150
  export const M2_PER_KM2 = 1_000_000
136
151
 
137
152
  /**
138
- * Metres from a point to a line SEGMENT, with longitude scaled for the latitude so the two axes are comparable.
153
+ * Meters from a point to a line segment, with longitude scaled for the latitude
154
+ * so the two axes are comparable.
155
+ *
156
+ * Measure TO the edge rather than TO the nearest vertex.
157
+ * A point a centimeter from a long edge can be meters from every vertex of it,
158
+ * so a vertex distance overstates the gap without bound.
159
+ *
160
+ * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
161
+ * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
139
162
  *
140
- * MEASURE TO THE EDGE, NOT TO THE NEAREST VERTEX. A point a centimetre from a long edge can be metres from every vertex
141
- * of it, so a vertex distance overstates the gap without bound — measured on the flood layer's verification, one
142
- * near-miss read 1.58 m to vertices and 0.009 m to edges, a 9 mm difference overstated 175-fold. That is the difference
143
- * between "two channels rendered the same edge slightly differently" and "the conversion is wrong".
163
+ * That is the difference between "two channels rendered the same edge slightly
164
+ * differently" and "the conversion is wrong".
144
165
  *
145
- * The longitude scaling matters at the same scale: comparing raw degrees treats a degree of longitude as a degree of
146
- * latitude, which at 54°N overstates east-west distance by 70%.
166
+ * The longitude scaling matters at the same scale: comparing raw degrees treats
167
+ * a degree of longitude as a degree of latitude.
168
+ * At 54°N, this overstates east-west distance by 70%.
147
169
  *
148
170
  * @category Position
149
171
  */
@@ -160,20 +182,24 @@ export function segmentDistanceMetres(
160
182
  const vy = to[1]! - from[1]!
161
183
  const lengthSquared = vx * vx + vy * vy
162
184
 
163
- // A zero-length segment is a repeated vertex; the distance to it is the distance to the point.
185
+ // A zero-length segment is a repeated vertex.
186
+ // The distance to it is the distance to the point.
164
187
  const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, (px * vx + py * vy) / lengthSquared))
165
188
 
166
189
  return Math.hypot(px - t * vx, py - t * vy) * METRES_PER_DEGREE
167
190
  }
168
191
 
169
192
  /**
170
- * Metres from a point to the nearest ring EDGE of an areal geometry — `Infinity` when the geometry bounds no area or
171
- * carries no segment.
193
+ * Meters from a point to the nearest ring edge of an areal geometry.
194
+ *
195
+ * `Infinity` when the geometry bounds no area or contains no segment.
196
+ *
197
+ * To the edge rather than to the nearest vertex: a point a centimeter from a long edge can
198
+ * be meters from every vertex of it, so a vertex distance overstates the gap without bound.
199
+ * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
200
+ * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
172
201
  *
173
- * To the edge, not to the nearest vertex: a point a centimetre from a long edge can be metres from every vertex of it,
174
- * so a vertex distance overstates the gap without bound — measured on the flood layer's verification, one near-miss
175
- * read 1.58 m to vertices and 0.009 m to edges, a 9 mm difference overstated 175-fold. Every polygon layer's two-path
176
- * verification measures its boundary tolerance with this.
202
+ * Every polygon layer's two-path verification measures its boundary tolerance with this.
177
203
  *
178
204
  * @category Position
179
205
  */
package/lib/feature.ts CHANGED
@@ -5,7 +5,7 @@
5
5
  */
6
6
 
7
7
  /* eslint-disable @typescript-eslint/no-explicit-any */
8
- import type { GeometryLiteral } from "#geometries/index"
8
+ import type { GeometryLiteral } from "#geometries"
9
9
 
10
10
  export interface IdentifiableGeoFeature {
11
11
  GEOID: string | number
@@ -29,7 +29,7 @@ export interface GeoFeature<G = GeometryLiteral, P extends object | null = never
29
29
  geometry: G
30
30
 
31
31
  /**
32
- * A unique identifier for the feature, such as a UUID, a serial number, or a name.
32
+ * A unique identifier for the feature, such as a uuid, a serial number, or a name.
33
33
  */
34
34
  id?: P extends IdentifiableGeoFeature ? P["GEOID"] : never
35
35
 
@@ -6,7 +6,7 @@
6
6
 
7
7
  import type google from "@googlemaps/google-maps-services-js"
8
8
  import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
9
- import { tryParsingJSON } from "@mailwoman/core/json"
9
+ import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
10
10
  import { convert as convertCoords } from "geo-coordinates-parser"
11
11
  import { latLngToCell } from "h3-js"
12
12
 
@@ -27,7 +27,8 @@ import {
27
27
  } from "#position"
28
28
 
29
29
  /**
30
- * A JSON-serializeable single point geometry, such as a specific location, address, or longitude, latitude pair.
30
+ * A JSON-serializable single point geometry, such as a specific location,
31
+ * address, or longitude, latitude pair.
31
32
  *
32
33
  * ```js
33
34
  * {
@@ -79,8 +80,8 @@ export interface GeolocationCoordinatesLike {
79
80
  }
80
81
 
81
82
  /**
82
- * Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike} object — the
83
- * shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
83
+ * Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike}
84
+ * object — the shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
84
85
  */
85
86
  export function isGeolocationCoordinatesLike(input: unknown): input is GeolocationCoordinatesLike {
86
87
  if (!input || typeof input !== "object") return false
@@ -103,13 +104,13 @@ export type GeoPointInput =
103
104
  | LatLngLiteral
104
105
  | LatLng
105
106
 
106
- //#region GeoPoint
107
+ // #region GeoPoint
107
108
 
108
109
  /**
109
110
  * A single point geometry, such as a specific location, address, or longitude, latitude pair.
110
111
  */
111
112
  export class GeoPoint implements PointLiteral {
112
- //#region Properties
113
+ // #region Properties
113
114
 
114
115
  /**
115
116
  * Declares the type of GeoJSON object as a `Point` geometry.
@@ -148,9 +149,10 @@ export class GeoPoint implements PointLiteral {
148
149
  }
149
150
 
150
151
  /**
151
- * Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the contract, never inferred from
152
- * the magnitudes — and REJECTS a coordinate that is not on the globe. See {@link GeoPoint.from} for why both halves of
153
- * that sentence are required.
152
+ * Assigns the pair as GeoJSON [longitude, latitude(, altitude)] — the axis order is the interface,
153
+ * never inferred from the magnitudes — and rejects a coordinate that is not on the globe.
154
+ *
155
+ * See {@link GeoPoint.from} for why both halves of that sentence are required.
154
156
  *
155
157
  * @throws {RangeError} When longitude is outside [-180, 180] or latitude is outside [-90, 90].
156
158
  */
@@ -217,9 +219,9 @@ export class GeoPoint implements PointLiteral {
217
219
  this.#altitude = value
218
220
  }
219
221
 
220
- //#endregion
222
+ // #endregion
221
223
 
222
- //#region Constructors
224
+ // #region Constructors
223
225
 
224
226
  /**
225
227
  * Create a new GeoPoint object with default coordinates.
@@ -241,7 +243,8 @@ export class GeoPoint implements PointLiteral {
241
243
  constructor(geoLocationCoordinates: GeolocationCoordinatesLike, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
242
244
 
243
245
  /**
244
- * Creates a new GeoPoint instance from a Google Maps API {@linkcode google.LatLngLiteral | LatLngLiteral} object.
246
+ * Creates a new GeoPoint instance from a Google Maps API
247
+ * {@linkcode google.LatLngLiteral | LatLngLiteral} object.
245
248
  */
246
249
  constructor(latLngLiteral: google.LatLngLiteral, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
247
250
 
@@ -287,23 +290,28 @@ export class GeoPoint implements PointLiteral {
287
290
  }
288
291
 
289
292
  /**
290
- * Attempts to create a new GeoPoint instance from the given input, returning `null` rather than throwing when the
291
- * input is not a coordinate this class will stand behind.
293
+ * Attempts to create a new GeoPoint instance from the given input, returning `null`
294
+ * rather than throwing when the input is not a coordinate this class will stand behind.
292
295
  *
293
- * Two rules, both of which this constructor got wrong until 2026-08-05 (the defect was recorded in `e9bfd139` and
294
- * routed around rather than fixed):
296
+ * Two rules, both of which this constructor got wrong until 2026-08-05
297
+ * (the defect was recorded in `e9bfd139` and routed around rather than fixed):
295
298
  *
296
- * 1. **A 2-tuple is GeoJSON [longitude, latitude]. The axis order is never inferred.** The old path ran the pair through
297
- * `inferGeoJSONCoordOrder`, whose only signal is the [-90, 90] latitude range, so it transposed a pair exactly
298
- * when |the second magnitude| > 90. A caller handing it `[latitude, longitude]` therefore got the pair repaired in
299
- * Dallas and left corrupted in Berlin — behaviour selected by the data, from one code path. This change is a no-op
300
- * for every WELL-FORMED input: a valid `[longitude, latitude]` pair can never have an out-of-range second element,
301
- * so the heuristic never fired on one.
302
- * 2. **An out-of-range magnitude is rejected, not repaired.** `[999, 999]` used to produce a GeoPoint reporting latitude 999.
303
- * It now returns `null` here and throws a `RangeError` from the constructor. Note the deliberate asymmetry with
304
- * the scalar `longitude` / `latitude` setters, which still wrap and clamp: mutating a point is a pan gesture,
305
- * where 190° meaning -170° is right; PARSING one is a claim about the world, where an impossible magnitude means
306
- * the input was malformed and any repair invents a location.
299
+ * 1. **A 2-tuple is GeoJSON [longitude, latitude].
300
+ * The axis order is never inferred.** The old path ran the pair through
301
+ * `inferGeoJSONCoordOrder`, whose only signal is the [-90, 90] latitude range,
302
+ * so it transposed a pair exactly when |the second magnitude| > 90.
303
+ * A caller handing it `[latitude, longitude]` therefore got the pair repaired in Dallas
304
+ * and left corrupted in Berlin — behavior selected by the data, from one code path.
305
+ * This change is a no-op for every well-formed input: a valid `[longitude, latitude]` pair
306
+ * can never have an out-of-range second element, so the heuristic never fired on one.
307
+ * 2. **An out-of-range magnitude is rejected rather than repaired.** `[999, 999]`
308
+ * used to produce a GeoPoint reporting latitude 999.
309
+ * It now returns `null` here and throws a `RangeError` from the constructor.
310
+ * Note the deliberate asymmetry with the scalar `longitude` / `latitude` setters.
311
+ * which still wrap and clamp: mutating a point is a pan gesture, where 190°.
312
+ * This means -170° is right.
313
+ * A parse result is a claim about the world.
314
+ * An impossible magnitude means the input was malformed and any repair invents a location.
307
315
  *
308
316
  * A 0/0 result is treated as the "missing coordinate" sentinel (Null Island) and also returns `null`.
309
317
  */
@@ -331,9 +339,9 @@ export class GeoPoint implements PointLiteral {
331
339
  }
332
340
  }
333
341
 
334
- //#endregion
342
+ // #endregion
335
343
 
336
- //#region Predicates
344
+ // #region Predicates
337
345
 
338
346
  /**
339
347
  * Whether the GeoPoint is 2-dimensional.
@@ -356,9 +364,9 @@ export class GeoPoint implements PointLiteral {
356
364
  return this.#latitude === 0 && this.#longitude === 0
357
365
  }
358
366
 
359
- //#endregion
367
+ // #endregion
360
368
 
361
- //#region Conversion
369
+ // #region Conversion
362
370
 
363
371
  public toJSON(): PointLiteral {
364
372
  return {
@@ -413,9 +421,9 @@ export class GeoPoint implements PointLiteral {
413
421
  }
414
422
 
415
423
  public toString(): string {
416
- return JSON.stringify(this.toJSON())
424
+ return stringifyJSON(this.toJSON())
417
425
  }
418
- //#endregion
426
+ // #endregion
419
427
 
420
428
  public [Symbol.iterator](): Iterator<number> {
421
429
  return this.coordinates[Symbol.iterator]()