@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.
- package/README.md +2 -2
- package/lib/bbox.ts +41 -34
- package/lib/coordinate/bounds.ts +2 -2
- package/lib/coordinate/formats.ts +21 -33
- package/lib/distance.ts +56 -28
- package/lib/feature.ts +2 -2
- package/lib/geometries/point.ts +42 -35
- package/lib/geometries/polygon.ts +27 -135
- package/lib/geometries/ring-blob.ts +35 -132
- package/lib/google/place-id.ts +4 -2
- package/lib/h3/cell.ts +23 -78
- package/lib/h3/coverage.ts +13 -9
- package/lib/h3/interior-cells.ts +13 -34
- package/lib/h3/polygon-cells.ts +61 -167
- package/lib/{h3/index.ts → h3.ts} +1 -1
- package/lib/index.ts +6 -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 +39 -20
- package/lib/projection/transform.ts +43 -44
- package/lib/{projection/index.ts → projection.ts} +1 -1
- package/lib/well-known-text.ts +9 -9
- 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 +17 -16
- package/out/coordinate/formats.d.ts.map +1 -1
- package/out/coordinate/formats.js +20 -32
- package/out/coordinate/formats.js.map +1 -1
- package/out/distance.d.ts +51 -26
- 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 +37 -30
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +27 -126
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +14 -42
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +24 -110
- package/out/geometries/ring-blob.d.ts.map +1 -1
- package/out/geometries/ring-blob.js +30 -116
- 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 +22 -65
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +12 -8
- package/out/h3/coverage.d.ts.map +1 -1
- package/out/h3/coverage.js +12 -8
- 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 +50 -148
- 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 +6 -5
- package/out/index.d.ts.map +1 -1
- package/out/index.js +6 -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 +36 -20
- package/out/position.d.ts.map +1 -1
- package/out/position.js +0 -1
- package/out/position.js.map +1 -1
- package/out/projection/transform.d.ts +33 -30
- package/out/projection/transform.d.ts.map +1 -1
- package/out/projection/transform.js +15 -17
- 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 +17 -10
- package/out/tools/ogr/stream.js.map +1 -1
- package/out/tools/{ogr/index.d.ts → ogr.d.ts} +25 -15
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/{ogr/index.js → ogr.js} +25 -21
- 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 +9 -9
- package/out/well-known-text.d.ts.map +1 -1
- package/out/well-known-text.js +7 -7
- package/out/well-known-text.js.map +1 -1
- package/package.json +37 -129
- package/{lib/tools → tools}/ogr/stream.ts +26 -15
- package/{lib/tools/ogr/index.ts → tools/ogr.ts} +44 -30
- 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))
|
|
@@ -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
|
|
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
|
|
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
|
-
*
|
|
171
|
-
*
|
|
172
|
-
*
|
|
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 =
|
|
178
|
-
): { rise
|
|
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
|
|
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
|
-
|
|
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
|
|
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) ||
|
|
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
|
|
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 = {
|
|
@@ -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.
|
|
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.
|
|
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
|
|
96
|
-
*
|
|
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
|
-
*
|
|
114
|
-
*
|
|
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
|
|
117
|
-
*
|
|
118
|
-
*
|
|
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
|
-
*
|
|
128
|
-
*
|
|
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
|
|
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
|
-
*
|
|
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
|
-
*
|
|
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".
|
|
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
|
|
146
|
-
*
|
|
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
|
|
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
|
-
*
|
|
171
|
-
*
|
|
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
|
-
*
|
|
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
|
|
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
|
|