@mailwoman/spatial 10.0.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.
- package/README.md +2 -2
- package/lib/bbox.ts +41 -34
- package/lib/coordinate/bounds.ts +2 -2
- package/lib/coordinate/formats.ts +10 -27
- package/lib/distance.ts +53 -27
- package/lib/feature.ts +2 -2
- package/lib/geometries/point.ts +41 -33
- package/lib/geometries/polygon.ts +24 -128
- package/lib/geometries/ring-blob.ts +33 -130
- package/lib/google/place-id.ts +4 -2
- package/lib/h3/cell.ts +21 -76
- package/lib/h3/coverage.ts +11 -7
- package/lib/h3/interior-cells.ts +13 -34
- package/lib/h3/polygon-cells.ts +59 -165
- package/lib/{h3/index.ts → h3.ts} +1 -1
- package/lib/index.ts +5 -5
- package/lib/objects.ts +6 -6
- package/lib/open-location-code.ts +11 -42
- package/lib/osgb36.ts +28 -187
- package/lib/polyline.ts +5 -3
- package/lib/position.ts +18 -2
- package/lib/projection/transform.ts +39 -40
- package/lib/{projection/index.ts → projection.ts} +1 -1
- package/lib/well-known-text.ts +8 -8
- package/out/bbox.d.ts +17 -14
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +32 -23
- package/out/bbox.js.map +1 -1
- package/out/coordinate/bounds.d.ts +2 -2
- package/out/coordinate/bounds.js +2 -2
- package/out/coordinate/formats.d.ts +8 -11
- package/out/coordinate/formats.d.ts.map +1 -1
- package/out/coordinate/formats.js +10 -27
- package/out/coordinate/formats.js.map +1 -1
- package/out/distance.d.ts +48 -25
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +53 -27
- package/out/distance.js.map +1 -1
- package/out/feature.d.ts +2 -2
- package/out/feature.d.ts.map +1 -1
- package/out/geometries/point.d.ts +30 -22
- package/out/geometries/point.d.ts.map +1 -1
- package/out/geometries/point.js +35 -29
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +23 -122
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +13 -41
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +23 -109
- package/out/geometries/ring-blob.d.ts.map +1 -1
- package/out/geometries/ring-blob.js +29 -115
- package/out/geometries/ring-blob.js.map +1 -1
- package/out/{geometries/index.js → geometries.d.ts} +1 -1
- package/out/geometries.d.ts.map +1 -0
- package/out/{geometries/index.d.ts → geometries.js} +1 -1
- package/out/geometries.js.map +1 -0
- package/out/google/place-id.d.ts +4 -2
- package/out/google/place-id.d.ts.map +1 -1
- package/out/google/place-id.js.map +1 -1
- package/out/{google/index.d.ts → google.d.ts} +1 -1
- package/out/google.d.ts.map +1 -0
- package/out/{google/index.js → google.js} +1 -1
- package/out/google.js.map +1 -0
- package/out/h3/cell.d.ts +19 -73
- package/out/h3/cell.d.ts.map +1 -1
- package/out/h3/cell.js +20 -63
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +11 -7
- package/out/h3/coverage.d.ts.map +1 -1
- package/out/h3/coverage.js +11 -7
- package/out/h3/coverage.js.map +1 -1
- package/out/h3/interior-cells.d.ts +9 -28
- package/out/h3/interior-cells.d.ts.map +1 -1
- package/out/h3/interior-cells.js +13 -32
- package/out/h3/interior-cells.js.map +1 -1
- package/out/h3/polygon-cells.d.ts +28 -120
- package/out/h3/polygon-cells.d.ts.map +1 -1
- package/out/h3/polygon-cells.js +48 -146
- package/out/h3/polygon-cells.js.map +1 -1
- package/out/{h3/index.d.ts → h3.d.ts} +2 -2
- package/out/h3.d.ts.map +1 -0
- package/out/{h3/index.js → h3.js} +2 -2
- package/out/h3.js.map +1 -0
- package/out/index.d.ts +5 -5
- package/out/index.d.ts.map +1 -1
- package/out/index.js +5 -5
- package/out/index.js.map +1 -1
- package/out/objects.d.ts +6 -6
- package/out/open-location-code.d.ts +11 -22
- package/out/open-location-code.d.ts.map +1 -1
- package/out/open-location-code.js +9 -40
- package/out/open-location-code.js.map +1 -1
- package/out/osgb36.d.ts +21 -97
- package/out/osgb36.d.ts.map +1 -1
- package/out/osgb36.js +12 -165
- package/out/osgb36.js.map +1 -1
- package/out/polyline.d.ts +5 -3
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +5 -3
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +16 -2
- package/out/position.d.ts.map +1 -1
- package/out/position.js.map +1 -1
- package/out/projection/transform.d.ts +30 -27
- package/out/projection/transform.d.ts.map +1 -1
- package/out/projection/transform.js +14 -16
- package/out/projection/transform.js.map +1 -1
- package/out/{projection/index.d.ts → projection.d.ts} +2 -2
- package/out/projection.d.ts.map +1 -0
- package/out/{projection/index.js → projection.js} +2 -2
- package/out/projection.js.map +1 -0
- package/out/{regions/index.js → regions.d.ts} +1 -1
- package/out/regions.d.ts.map +1 -0
- package/out/{regions/index.d.ts → regions.js} +1 -1
- package/out/regions.js.map +1 -0
- package/out/tools/ogr/stream.d.ts +21 -11
- package/out/tools/ogr/stream.d.ts.map +1 -1
- package/out/tools/ogr/stream.js +16 -9
- package/out/tools/ogr/stream.js.map +1 -1
- package/out/tools/{ogr/index.d.ts → ogr.d.ts} +14 -11
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/{ogr/index.js → ogr.js} +18 -14
- package/out/tools/ogr.js.map +1 -0
- package/out/tools/tsconfig.tsbuildinfo +1 -0
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/well-known-text.d.ts +8 -8
- package/out/well-known-text.js +7 -7
- package/package.json +35 -127
- package/{lib/tools → tools}/ogr/stream.ts +25 -14
- package/{lib/tools/ogr/index.ts → tools/ogr.ts} +25 -19
- package/out/geometries/index.d.ts.map +0 -1
- package/out/geometries/index.js.map +0 -1
- package/out/google/index.d.ts.map +0 -1
- package/out/google/index.js.map +0 -1
- package/out/h3/index.d.ts.map +0 -1
- package/out/h3/index.js.map +0 -1
- package/out/projection/index.d.ts.map +0 -1
- package/out/projection/index.js.map +0 -1
- package/out/regions/index.d.ts.map +0 -1
- package/out/regions/index.js.map +0 -1
- package/out/tools/ogr/index.d.ts.map +0 -1
- package/out/tools/ogr/index.js.map +0 -1
- /package/lib/{geometries/index.ts → geometries.ts} +0 -0
- /package/lib/{google/index.ts → google.ts} +0 -0
- /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,
|
|
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/
|
|
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
|
|
12
|
+
import { CoordinateProjection } from "#projection"
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
|
-
*
|
|
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.
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
131
|
+
// #endregion
|
|
128
132
|
|
|
129
|
-
|
|
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.
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
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
|
-
|
|
240
|
+
// #endregion
|
|
237
241
|
|
|
238
|
-
|
|
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
|
-
|
|
339
|
+
// #endregion
|
|
336
340
|
|
|
337
|
-
|
|
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
|
-
|
|
388
|
+
// #endregion
|
|
385
389
|
|
|
386
|
-
|
|
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
|
-
|
|
400
|
+
// #endregion
|
|
397
401
|
|
|
398
|
-
|
|
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
|
-
|
|
468
|
+
// #endregion
|
|
465
469
|
}
|
|
466
470
|
|
|
467
|
-
|
|
471
|
+
// #region Equirectangular bbox around a point
|
|
468
472
|
|
|
469
473
|
/**
|
|
470
|
-
*
|
|
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,
|
|
486
|
-
*
|
|
487
|
-
*
|
|
488
|
-
*
|
|
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
|
|
493
|
-
//
|
|
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
|
-
|
|
512
|
+
// #endregion
|
package/lib/coordinate/bounds.ts
CHANGED
|
@@ -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
|
|
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 (
|
|
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
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
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
|
|
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
|
-
|
|
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
|
|
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
|
|
42
|
-
*
|
|
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.
|
|
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
|
|
96
|
-
*
|
|
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
|
-
*
|
|
114
|
-
*
|
|
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
|
|
117
|
-
*
|
|
118
|
-
*
|
|
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
|
-
*
|
|
128
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
141
|
-
*
|
|
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
|
|
146
|
-
*
|
|
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
|
|
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
|
-
*
|
|
171
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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
|
|
package/lib/geometries/point.ts
CHANGED
|
@@ -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-
|
|
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}
|
|
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
|
-
|
|
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
|
-
|
|
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
|
|
152
|
-
* the magnitudes — and
|
|
153
|
-
*
|
|
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
|
-
|
|
222
|
+
// #endregion
|
|
221
223
|
|
|
222
|
-
|
|
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
|
|
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`
|
|
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
|
|
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].
|
|
297
|
-
*
|
|
298
|
-
*
|
|
299
|
-
*
|
|
300
|
-
*
|
|
301
|
-
*
|
|
302
|
-
*
|
|
303
|
-
*
|
|
304
|
-
*
|
|
305
|
-
*
|
|
306
|
-
*
|
|
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
|
-
|
|
342
|
+
// #endregion
|
|
335
343
|
|
|
336
|
-
|
|
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
|
-
|
|
367
|
+
// #endregion
|
|
360
368
|
|
|
361
|
-
|
|
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
|
|
424
|
+
return stringifyJSON(this.toJSON())
|
|
417
425
|
}
|
|
418
|
-
|
|
426
|
+
// #endregion
|
|
419
427
|
|
|
420
428
|
public [Symbol.iterator](): Iterator<number> {
|
|
421
429
|
return this.coordinates[Symbol.iterator]()
|