@mailwoman/spatial 10.0.0 → 10.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 (148) hide show
  1. package/README.md +2 -2
  2. package/lib/bbox.ts +41 -34
  3. package/lib/coordinate/bounds.ts +2 -2
  4. package/lib/coordinate/formats.ts +21 -33
  5. package/lib/distance.ts +56 -28
  6. package/lib/feature.ts +2 -2
  7. package/lib/geometries/point.ts +42 -35
  8. package/lib/geometries/polygon.ts +27 -135
  9. package/lib/geometries/ring-blob.ts +35 -132
  10. package/lib/google/place-id.ts +4 -2
  11. package/lib/h3/cell.ts +23 -78
  12. package/lib/h3/coverage.ts +13 -9
  13. package/lib/h3/interior-cells.ts +13 -34
  14. package/lib/h3/polygon-cells.ts +61 -167
  15. package/lib/{h3/index.ts → h3.ts} +1 -1
  16. package/lib/index.ts +6 -5
  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 +39 -20
  22. package/lib/projection/transform.ts +43 -44
  23. package/lib/{projection/index.ts → projection.ts} +1 -1
  24. package/lib/well-known-text.ts +9 -9
  25. package/out/bbox.d.ts +17 -14
  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 +2 -2
  30. package/out/coordinate/bounds.js +2 -2
  31. package/out/coordinate/formats.d.ts +17 -16
  32. package/out/coordinate/formats.d.ts.map +1 -1
  33. package/out/coordinate/formats.js +20 -32
  34. package/out/coordinate/formats.js.map +1 -1
  35. package/out/distance.d.ts +51 -26
  36. package/out/distance.d.ts.map +1 -1
  37. package/out/distance.js +53 -27
  38. package/out/distance.js.map +1 -1
  39. package/out/feature.d.ts +2 -2
  40. package/out/feature.d.ts.map +1 -1
  41. package/out/geometries/point.d.ts +30 -22
  42. package/out/geometries/point.d.ts.map +1 -1
  43. package/out/geometries/point.js +37 -30
  44. package/out/geometries/point.js.map +1 -1
  45. package/out/geometries/polygon.d.ts +27 -126
  46. package/out/geometries/polygon.d.ts.map +1 -1
  47. package/out/geometries/polygon.js +14 -42
  48. package/out/geometries/polygon.js.map +1 -1
  49. package/out/geometries/ring-blob.d.ts +24 -110
  50. package/out/geometries/ring-blob.d.ts.map +1 -1
  51. package/out/geometries/ring-blob.js +30 -116
  52. package/out/geometries/ring-blob.js.map +1 -1
  53. package/out/{geometries/index.js → geometries.d.ts} +1 -1
  54. package/out/geometries.d.ts.map +1 -0
  55. package/out/{geometries/index.d.ts → geometries.js} +1 -1
  56. package/out/geometries.js.map +1 -0
  57. package/out/google/place-id.d.ts +4 -2
  58. package/out/google/place-id.d.ts.map +1 -1
  59. package/out/google/place-id.js.map +1 -1
  60. package/out/{google/index.d.ts → google.d.ts} +1 -1
  61. package/out/google.d.ts.map +1 -0
  62. package/out/{google/index.js → google.js} +1 -1
  63. package/out/google.js.map +1 -0
  64. package/out/h3/cell.d.ts +19 -73
  65. package/out/h3/cell.d.ts.map +1 -1
  66. package/out/h3/cell.js +22 -65
  67. package/out/h3/cell.js.map +1 -1
  68. package/out/h3/coverage.d.ts +12 -8
  69. package/out/h3/coverage.d.ts.map +1 -1
  70. package/out/h3/coverage.js +12 -8
  71. package/out/h3/coverage.js.map +1 -1
  72. package/out/h3/interior-cells.d.ts +9 -28
  73. package/out/h3/interior-cells.d.ts.map +1 -1
  74. package/out/h3/interior-cells.js +13 -32
  75. package/out/h3/interior-cells.js.map +1 -1
  76. package/out/h3/polygon-cells.d.ts +28 -120
  77. package/out/h3/polygon-cells.d.ts.map +1 -1
  78. package/out/h3/polygon-cells.js +50 -148
  79. package/out/h3/polygon-cells.js.map +1 -1
  80. package/out/{h3/index.d.ts → h3.d.ts} +2 -2
  81. package/out/h3.d.ts.map +1 -0
  82. package/out/{h3/index.js → h3.js} +2 -2
  83. package/out/h3.js.map +1 -0
  84. package/out/index.d.ts +6 -5
  85. package/out/index.d.ts.map +1 -1
  86. package/out/index.js +6 -5
  87. package/out/index.js.map +1 -1
  88. package/out/objects.d.ts +6 -6
  89. package/out/open-location-code.d.ts +11 -22
  90. package/out/open-location-code.d.ts.map +1 -1
  91. package/out/open-location-code.js +9 -40
  92. package/out/open-location-code.js.map +1 -1
  93. package/out/osgb36.d.ts +21 -97
  94. package/out/osgb36.d.ts.map +1 -1
  95. package/out/osgb36.js +12 -165
  96. package/out/osgb36.js.map +1 -1
  97. package/out/polyline.d.ts +5 -3
  98. package/out/polyline.d.ts.map +1 -1
  99. package/out/polyline.js +5 -3
  100. package/out/polyline.js.map +1 -1
  101. package/out/position.d.ts +36 -20
  102. package/out/position.d.ts.map +1 -1
  103. package/out/position.js +0 -1
  104. package/out/position.js.map +1 -1
  105. package/out/projection/transform.d.ts +33 -30
  106. package/out/projection/transform.d.ts.map +1 -1
  107. package/out/projection/transform.js +15 -17
  108. package/out/projection/transform.js.map +1 -1
  109. package/out/{projection/index.d.ts → projection.d.ts} +2 -2
  110. package/out/projection.d.ts.map +1 -0
  111. package/out/{projection/index.js → projection.js} +2 -2
  112. package/out/projection.js.map +1 -0
  113. package/out/{regions/index.js → regions.d.ts} +1 -1
  114. package/out/regions.d.ts.map +1 -0
  115. package/out/{regions/index.d.ts → regions.js} +1 -1
  116. package/out/regions.js.map +1 -0
  117. package/out/tools/ogr/stream.d.ts +21 -11
  118. package/out/tools/ogr/stream.d.ts.map +1 -1
  119. package/out/tools/ogr/stream.js +17 -10
  120. package/out/tools/ogr/stream.js.map +1 -1
  121. package/out/tools/{ogr/index.d.ts → ogr.d.ts} +25 -15
  122. package/out/tools/ogr.d.ts.map +1 -0
  123. package/out/tools/{ogr/index.js → ogr.js} +25 -21
  124. package/out/tools/ogr.js.map +1 -0
  125. package/out/tools/tsconfig.tsbuildinfo +1 -0
  126. package/out/tsconfig.test.tsbuildinfo +1 -1
  127. package/out/well-known-text.d.ts +9 -9
  128. package/out/well-known-text.d.ts.map +1 -1
  129. package/out/well-known-text.js +7 -7
  130. package/out/well-known-text.js.map +1 -1
  131. package/package.json +37 -129
  132. package/{lib/tools → tools}/ogr/stream.ts +26 -15
  133. package/{lib/tools/ogr/index.ts → tools/ogr.ts} +44 -30
  134. package/out/geometries/index.d.ts.map +0 -1
  135. package/out/geometries/index.js.map +0 -1
  136. package/out/google/index.d.ts.map +0 -1
  137. package/out/google/index.js.map +0 -1
  138. package/out/h3/index.d.ts.map +0 -1
  139. package/out/h3/index.js.map +0 -1
  140. package/out/projection/index.d.ts.map +0 -1
  141. package/out/projection/index.js.map +0 -1
  142. package/out/regions/index.d.ts.map +0 -1
  143. package/out/regions/index.js.map +0 -1
  144. package/out/tools/ogr/index.d.ts.map +0 -1
  145. package/out/tools/ogr/index.js.map +0 -1
  146. /package/lib/{geometries/index.ts → geometries.ts} +0 -0
  147. /package/lib/{google/index.ts → google.ts} +0 -0
  148. /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
@@ -9,15 +9,18 @@
9
9
  import { toRad } from "#coordinate/formats"
10
10
  import type { PolygonLiteral, SolidPolygonPath } from "#geometries/polygon"
11
11
  import { clampLatitude, wrapLongitude } from "#position"
12
- import { CoordinateProjection } from "#projection/index"
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
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))
@@ -95,7 +78,9 @@ export function qiblaBearing(lat: number, lon: number): number {
95
78
  const GEOHASH_BASE32 = "0123456789bcdefghjkmnpqrstuvwxyz"
96
79
 
97
80
  /**
98
- * Encode a coordinate as a geohash of the given precision (default 9 ≈ 4.8 m).
81
+ * Encode a coordinate as a geohash of the given precision.
82
+ *
83
+ * @param precision [9] Geohash length. 9 characters is about 4.8 m.
99
84
  */
100
85
  export function toGeohash(lat: number, lon: number, precision = 9): string {
101
86
  let latMin = -90
@@ -145,7 +130,9 @@ export function toGeohash(lat: number, lon: number, precision = 9): string {
145
130
  const A_CODE = "A".charCodeAt(0)
146
131
 
147
132
  /**
148
- * Maidenhead grid locator (default 6-char: field uppercase, square digits, subsquare lowercase).
133
+ * Maidenhead grid locator: field uppercase, square digits, subsquare lowercase.
134
+ *
135
+ * @param pairs [3] Character pairs in the locator. 3 pairs is the 6-character form.
149
136
  */
150
137
  export function toMaidenhead(lat: number, lon: number, pairs = 3): string {
151
138
  const lonAdj = lon + 180
@@ -167,15 +154,17 @@ const J2000 = 2_451_545
167
154
  const unixEpochJulian = 2_440_587.5
168
155
 
169
156
  /**
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.
157
+ * Computes sunrise, solar noon and sunset for a coordinate and date as UTC epoch
158
+ * seconds using the standard sunrise equation.
159
+ *
160
+ * `rise` and `set` are omitted during polar day or polar night, while `noon` is always present.
173
161
  */
174
162
  export function sunTimes(
175
163
  lat: number,
176
164
  lon: number,
177
- date: Date = new Date()
178
- ): { rise?: number; set?: number; noon: number } {
165
+ date: Date | null = null
166
+ ): { rise: number | null; set: number | null; noon: number } {
167
+ date ??= new Date()
179
168
  const julian = date.getTime() / 86_400_000 + unixEpochJulian
180
169
  const n = Math.round(julian - J2000 - 0.0009 + lon / 360)
181
170
  const meanSolarTime = n + 0.0009 - lon / 360
@@ -193,15 +182,12 @@ export function sunTimes(
193
182
  const toEpoch = (j: number): number => Math.round((j - unixEpochJulian) * 86_400)
194
183
  const noon = toEpoch(transit)
195
184
 
196
- if (cosH >= 1 || cosH <= -1) return { noon }
185
+ if (cosH >= 1 || cosH <= -1) return { rise: null, set: null, noon }
197
186
  const hourAngle = toDeg(Math.acos(cosH))
198
187
 
199
188
  return { rise: toEpoch(transit - hourAngle / 360), set: toEpoch(transit + hourAngle / 360), noon }
200
189
  }
201
190
 
202
- /**
203
- * MGRS / UTM (WGS84). The forward Transverse Mercator series + the military grid lettering.
204
- */
205
191
  const UTM_A = 6_378_137
206
192
  const UTM_F = 1 / 298.257223563
207
193
  const UTM_K0 = 0.9996
@@ -249,11 +235,13 @@ const MGRS_COL_SETS = ["ABCDEFGH", "JKLMNPQR", "STUVWXYZ"]
249
235
  const MGRS_ROW_LETTERS = "ABCDEFGHJKLMNPQRSTUV"
250
236
 
251
237
  /**
252
- * Military Grid Reference System for a coordinate (`"18SUJ2340806479"`); `""` outside MGRS bands (±80°/84°).
238
+ * Formats a coordinate as a Military Grid Reference System string such as `"18SUJ2340806479"`,
239
+ * or returns `""` outside latitudes −80° to 84°.
253
240
  */
254
241
  export function toMGRS(lat: number, lon: number): string {
255
242
  if (lat < MGRS_LAT_MIN || lat > MGRS_LAT_MAX) return ""
256
- const band = MGRS_LAT_BANDS[Math.floor((lat + 80) / 8)]!
243
+ // Band X spans 72° to 84°, so 80° and above still maps to the last band.
244
+ const band = MGRS_LAT_BANDS[Math.min(Math.floor((lat + 80) / 8), MGRS_LAT_BANDS.length - 1)]!
257
245
  const { zone, easting, northing } = latLonToUtm(lat, lon)
258
246
  const colLetter = MGRS_COL_SETS[(zone - 1) % 3]![Math.floor(easting / 100_000) - 1]!
259
247
  let row = Math.floor(northing / 100_000) % 20
@@ -270,13 +258,13 @@ export function toMGRS(lat: number, lon: number): string {
270
258
  }
271
259
 
272
260
  /**
273
- * Fill the coordinate-format slice of an {@link AnnotationSet} from a `{lat, lon}`.
261
+ * Fill the coordinate-format fields of an {@link AnnotationSet} from a `{lat, lon}`.
274
262
  */
275
263
  export const coordinateFormatAnnotator: Annotator = ({ lat, lon, date }): Partial<AnnotationSet> => ({
276
264
  dms: toDMS(lat, lon),
277
265
  geohash: toGeohash(lat, lon),
278
266
  maidenhead: toMaidenhead(lat, lon),
279
- mgrs: toMGRS(lat, lon) || undefined,
267
+ mgrs: toMGRS(lat, lon) || null,
280
268
  mercator: toMercator(lat, lon),
281
269
  qiblaBearing: qiblaBearing(lat, lon),
282
270
  sun: sunTimes(lat, lon, date),
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 = {
@@ -51,13 +55,17 @@ export const BODY_RADII_KM = {
51
55
  export interface GreatCircleOptions {
52
56
  unit?: EarthRadiusUnit
53
57
  /**
54
- * Which body's mean radius scales the arc. @default "earth"
58
+ * Which body's mean radius scales the arc.
59
+ *
60
+ * @defaultValue `"earth"`
55
61
  */
56
62
  body?: PlanetaryBodyID
57
63
  }
58
64
 
59
65
  /**
60
- * Shared great-circle math with no sentinel handling. `unit` selects the Earth radius.
66
+ * Shared great-circle math with no sentinel handling.
67
+ *
68
+ * `unit` selects the Earth radius.
61
69
  */
62
70
  function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
63
71
  const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
@@ -92,8 +100,10 @@ export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: Ea
92
100
  }
93
101
 
94
102
  /**
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`.
103
+ * Great-circle distance on the selected body. {@link haversine} is this function on Earth.
104
+ * A caller with a body passes it here.
105
+ *
106
+ * The Null-Island sentinel of the object form applies: a `(0, 0)` input answers `NaN`.
97
107
  *
98
108
  * @category Position
99
109
  */
@@ -110,12 +120,15 @@ export function greatCircleDistance(
110
120
  }
111
121
 
112
122
  /**
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.
123
+ * Returns the great-circle distance in kilometers between two coordinates on Earth.
124
+ *
125
+ * The formula's one true home — every resolver + eval consumer of the
126
+ * `(aLat, aLon, bLat, bLon)` shape imports this instead of re-declaring it.
115
127
  *
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.
128
+ * Unlike {@link haversine}, this is pure math with no Null-Island sentinel:
129
+ * `(0, 0)` is the Gulf of Guinea — a real point rather than "missing coordinate".
130
+ * That sentinel convention belongs to the `GeoPointInput` object form
131
+ * (where a 0/0 input means "no coordinate"), not to a raw scalar distance.
119
132
  *
120
133
  * @category Position
121
134
  */
@@ -124,26 +137,37 @@ export function haversineKm(aLat: number, aLon: number, bLat: number, bLon: numb
124
137
  }
125
138
 
126
139
  /**
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.
140
+ * Meters per degree of latitude.
141
+ *
142
+ * The scale used by {@link segmentDistanceMetres}.
143
+ * It is also the constant the bounding-box estimates in `#h3/polygon-cells` are built on.
129
144
  */
130
145
  export const METRES_PER_DEGREE = 111_320
131
146
 
132
147
  /**
133
- * Square metres in a square kilometre — the conversion every polygon layer's area receipt reports through.
148
+ * Square meters in a square kilometer.
149
+ *
150
+ * The conversion every polygon layer's area receipt reports through.
134
151
  */
135
152
  export const M2_PER_KM2 = 1_000_000
136
153
 
137
154
  /**
138
- * Metres from a point to a line SEGMENT, with longitude scaled for the latitude so the two axes are comparable.
155
+ * Meters from a point to a line segment, with longitude scaled for the latitude
156
+ * so the two axes are comparable.
157
+ *
158
+ * Measure TO the edge rather than TO the nearest vertex.
159
+ * A point a centimeter from a long edge can be meters from every vertex of it,
160
+ * so a vertex distance overstates the gap without bound.
161
+ *
162
+ * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
163
+ * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
139
164
  *
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".
165
+ * That is the difference between "two channels rendered the same edge slightly
166
+ * differently" and "the conversion is wrong".
144
167
  *
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%.
168
+ * The longitude scaling matters at the same scale: comparing raw degrees treats
169
+ * a degree of longitude as a degree of latitude.
170
+ * At 54°N, this overstates east-west distance by 70%.
147
171
  *
148
172
  * @category Position
149
173
  */
@@ -160,20 +184,24 @@ export function segmentDistanceMetres(
160
184
  const vy = to[1]! - from[1]!
161
185
  const lengthSquared = vx * vx + vy * vy
162
186
 
163
- // A zero-length segment is a repeated vertex; the distance to it is the distance to the point.
187
+ // A zero-length segment is a repeated vertex.
188
+ // The distance to it is the distance to the point.
164
189
  const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, (px * vx + py * vy) / lengthSquared))
165
190
 
166
191
  return Math.hypot(px - t * vx, py - t * vy) * METRES_PER_DEGREE
167
192
  }
168
193
 
169
194
  /**
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.
195
+ * Meters from a point to the nearest ring edge of an areal geometry.
196
+ *
197
+ * `Infinity` when the geometry bounds no area or contains no segment.
198
+ *
199
+ * To the edge rather than to the nearest vertex: a point a centimeter from a long edge can
200
+ * be meters from every vertex of it, so a vertex distance overstates the gap without bound.
201
+ * Measured on the flood layer's verification, one near-miss read 1.58 m to vertices
202
+ * and 0.009 m to edges, a 9 mm difference overstated 175-fold.
172
203
  *
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.
204
+ * Every polygon layer's two-path verification measures its boundary tolerance with this.
177
205
  *
178
206
  * @category Position
179
207
  */
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