@mailwoman/spatial 10.6.0 → 10.8.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 (72) hide show
  1. package/README.md +11 -14
  2. package/lib/bbox.ts +20 -0
  3. package/lib/distance.ts +15 -66
  4. package/lib/gb/codepoint.ts +1 -1
  5. package/lib/geometries/collection.ts +2 -1
  6. package/lib/geometries/point/coordinates.ts +147 -0
  7. package/lib/geometries/point/distance.ts +115 -0
  8. package/lib/geometries/point.ts +16 -100
  9. package/lib/geometries.ts +2 -0
  10. package/lib/h3/cell.ts +10 -14
  11. package/lib/h3/coverage.ts +3 -3
  12. package/lib/h3/interior-cells.ts +6 -6
  13. package/lib/h3/polygon-cells.ts +17 -17
  14. package/lib/h3.ts +4 -3
  15. package/lib/polyline.ts +2 -2
  16. package/out/bbox.d.ts +8 -0
  17. package/out/bbox.d.ts.map +1 -1
  18. package/out/bbox.js +15 -0
  19. package/out/bbox.js.map +1 -1
  20. package/out/distance.d.ts +18 -33
  21. package/out/distance.d.ts.map +1 -1
  22. package/out/distance.js +15 -57
  23. package/out/distance.js.map +1 -1
  24. package/out/gb/codepoint.js +1 -1
  25. package/out/gb/codepoint.js.map +1 -1
  26. package/out/geometries/collection.d.ts +2 -1
  27. package/out/geometries/collection.d.ts.map +1 -1
  28. package/out/geometries/point/coordinates.d.ts +73 -0
  29. package/out/geometries/point/coordinates.d.ts.map +1 -0
  30. package/out/geometries/point/coordinates.js +84 -0
  31. package/out/geometries/point/coordinates.js.map +1 -0
  32. package/out/geometries/point/distance.d.ts +59 -0
  33. package/out/geometries/point/distance.d.ts.map +1 -0
  34. package/out/geometries/point/distance.js +41 -0
  35. package/out/geometries/point/distance.js.map +1 -0
  36. package/out/geometries/point.d.ts +3 -49
  37. package/out/geometries/point.d.ts.map +1 -1
  38. package/out/geometries/point.js +7 -51
  39. package/out/geometries/point.js.map +1 -1
  40. package/out/geometries.d.ts +2 -0
  41. package/out/geometries.d.ts.map +1 -1
  42. package/out/geometries.js +2 -0
  43. package/out/geometries.js.map +1 -1
  44. package/out/h3/cell.d.ts +9 -9
  45. package/out/h3/cell.d.ts.map +1 -1
  46. package/out/h3/cell.js +4 -4
  47. package/out/h3/cell.js.map +1 -1
  48. package/out/h3/coverage.d.ts +1 -1
  49. package/out/h3/coverage.d.ts.map +1 -1
  50. package/out/h3/coverage.js +1 -1
  51. package/out/h3/coverage.js.map +1 -1
  52. package/out/h3/interior-cells.d.ts +1 -1
  53. package/out/h3/interior-cells.d.ts.map +1 -1
  54. package/out/h3/interior-cells.js +1 -1
  55. package/out/h3/interior-cells.js.map +1 -1
  56. package/out/h3/polygon-cells.d.ts +3 -3
  57. package/out/h3/polygon-cells.d.ts.map +1 -1
  58. package/out/h3/polygon-cells.js +2 -2
  59. package/out/h3/polygon-cells.js.map +1 -1
  60. package/out/h3.d.ts +4 -2
  61. package/out/h3.d.ts.map +1 -1
  62. package/out/h3.js +1 -1
  63. package/out/h3.js.map +1 -1
  64. package/out/polyline.js +2 -2
  65. package/out/polyline.js.map +1 -1
  66. package/out/tools/ogr.d.ts.map +1 -1
  67. package/out/tools/ogr.js +1 -1
  68. package/out/tools/ogr.js.map +1 -1
  69. package/out/tools/tsconfig.tsbuildinfo +1 -1
  70. package/out/tsconfig.test.tsbuildinfo +1 -1
  71. package/package.json +8 -8
  72. package/tools/ogr.ts +3 -1
package/README.md CHANGED
@@ -5,10 +5,10 @@ ecosystem. Provides coordinate math, H3 hexagonal grid indexing, WKB geometry
5
5
  parsing, and coordinate-string parsing.
6
6
 
7
7
  ```ts
8
- import { haversineKm, latLngToCell, parseCoordinateString } from "@mailwoman/spatial"
8
+ import { haversine, latLngToCell, parseCoordinateString } from "@mailwoman/spatial"
9
9
 
10
10
  // Great-circle distance
11
- const km = haversineKm(37.7749, -122.4194, 34.0522, -118.2437) // SF → LA
11
+ const km = haversine(37.7749, -122.4194, 34.0522, -118.2437) // SF → LA
12
12
 
13
13
  // H3 grid cell
14
14
  const cell = latLngToCell(37.7749, -122.4194, 9) // resolution-9 H3 index
@@ -19,17 +19,17 @@ const loc = parseCoordinateString("37.7749° N, 122.4194° W")
19
19
 
20
20
  ## What's inside
21
21
 
22
- | Capability | Implementation |
23
- | ---------------------- | --------------------------------------------------------------------------- |
24
- | **Coordinate math** | Haversine distance, direction, bounding-box containment |
25
- | **H3 indexing** | Hexagonal grid indexing via `h3-js` — spatial blocking for the matcher |
26
- | **WKB/WKT geometry** | Parse/format Well-Known Binary geometry via `wkx` |
27
- | **Coordinate parsing** | Parse decimal, DMS, and UTM coordinate strings via `geo-coordinates-parser` |
28
- | **GeoJSON** | GeoJSON Feature/FeatureCollection types and utilities |
22
+ | Capability | Implementation |
23
+ | ---------------------- | ------------------------------------------------------------------------------------ |
24
+ | **Coordinate math** | Haversine distance, direction, bounding-box containment |
25
+ | **H3 indexing** | Hexagonal grid indexing via `@sister.software/h3` — spatial blocking for the matcher |
26
+ | **WKB/WKT geometry** | Parse/format Well-Known Binary geometry via `wkx` |
27
+ | **Coordinate parsing** | Parse decimal, DMS, and UTM coordinate strings via `geo-coordinates-parser` |
28
+ | **GeoJSON** | GeoJSON Feature/FeatureCollection types and utilities |
29
29
 
30
30
  ## Dependencies
31
31
 
32
- - [`h3-js`](https://github.com/uber/h3-js) — Uber's hexagonal hierarchical geospatial indexing system
32
+ - [`@sister.software/h3`](https://github.com/sister-software/h3) — a Rust/WebAssembly implementation of Uber's H3 hexagonal hierarchical geospatial index
33
33
  - [`wkx`](https://github.com/cschwarz/wkx) — Well-Known Binary geometry parser
34
34
  - [`geo-coordinates-parser`](https://github.com/geops/geo-coordinates-parser) — coordinate string parsing
35
35
 
@@ -40,10 +40,7 @@ Optional: `@googlemaps/google-maps-services-js` for Google Maps API integration.
40
40
  - [`@mailwoman/match`](../match) — uses H3 cells for geo-first record blocking
41
41
  - [`@mailwoman/address-id`](../address-id) — uses H3 for stable address primary keys
42
42
  - [`@mailwoman/core`](../core) — consumes spatial types
43
- - [`@mailwoman/codex/country`](../codex/country) — ISO 3166-1 country codes and names. This
44
- package once held a byte-identical second copy of that table under `countries/` (salvaged
45
- from isp-nexus alongside the geometry code, never imported by any module). It was deleted
46
- 2026-08-04. Country reference data belongs to `codex`, not to the math package.
43
+ - [`@mailwoman/codex/country`](../codex/country) — ISO 3166-1 country codes and names.
47
44
 
48
45
  ## License
49
46
 
package/lib/bbox.ts CHANGED
@@ -480,6 +480,26 @@ export interface LatLonBounds {
480
480
  maxLon: number
481
481
  }
482
482
 
483
+ /**
484
+ * Type-predicate for a {@linkcode LatLonBounds} object with four numeric bounds.
485
+ */
486
+ export function isLatLonBounds(input: unknown): input is LatLonBounds {
487
+ if (!input || typeof input !== "object") return false
488
+
489
+ const { minLat, maxLat, minLon, maxLon } = input as Record<string, unknown>
490
+
491
+ return (
492
+ typeof minLat === "number" && typeof maxLat === "number" && typeof minLon === "number" && typeof maxLon === "number"
493
+ )
494
+ }
495
+
496
+ /**
497
+ * Whether the bounds contain the point, edges included.
498
+ */
499
+ export function latLonBoundsContain(bounds: LatLonBounds, lat: number, lon: number): boolean {
500
+ return lat >= bounds.minLat && lat <= bounds.maxLat && lon >= bounds.minLon && lon <= bounds.maxLon
501
+ }
502
+
483
503
  /**
484
504
  * Approximate bounds `radiusKM` in each direction around a point.
485
505
  *
package/lib/distance.ts CHANGED
@@ -2,11 +2,13 @@
2
2
  * @copyright Sister Software.
3
3
  * @license AGPL-3.0
4
4
  * @author Teffen Ellis, et al.
5
- * @file Great-circle distance utilities. This module imports geometry directly so spatial's
6
- * lowest-level math never reaches back through its public barrel.
5
+ * @file Great-circle and segment distance math over plain numbers.
6
+ *
7
+ * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, which embeds its WebAssembly
8
+ * binary, and a caller that only measures a distance must not load that. The object-form distances
9
+ * that take a `GeoPointInput` live in `#geometries/point/distance`.
7
10
  */
8
11
 
9
- import { GeoPoint, type GeoPointInput } from "#geometries/point"
10
12
  import { arealPolygons } from "#geometries/polygon"
11
13
 
12
14
  /**
@@ -29,7 +31,7 @@ export type EarthRadiusUnit = "km" | "miles" | "meters"
29
31
  /**
30
32
  * Radius of the Earth in kilometers, miles and meters.
31
33
  */
32
- const RADII = {
34
+ export const EARTH_RADII = {
33
35
  km: 6371,
34
36
  miles: 3958.8,
35
37
  meters: 6_371_000,
@@ -40,14 +42,14 @@ export type PlanetaryBodyID = "earth" | "moon" | "mars"
40
42
  /**
41
43
  * Mean radii in kilometers.
42
44
  *
43
- * Earth's is the value `radii` scales to its units.
45
+ * Earth's is the value {@link EARTH_RADII} scales to its units.
44
46
  * The Moon's and Mars's are the IAU mean radii the usgs planetary products reference
45
47
  * (the Moon as a sphere. Mars's mean radius, its DEM being on the areoid).
46
48
  *
47
49
  * A distance on a body other than Earth is meaningless without one of these.
48
50
  */
49
51
  export const BODY_RADII_KM = {
50
- earth: RADII.km,
52
+ earth: EARTH_RADII.km,
51
53
  moon: 1737.4,
52
54
  mars: 3389.5,
53
55
  } as const satisfies Record<PlanetaryBodyID, number>
@@ -63,11 +65,14 @@ export interface GreatCircleOptions {
63
65
  }
64
66
 
65
67
  /**
66
- * Shared great-circle math with no sentinel handling.
68
+ * Returns the central angle in radians between two coordinates in decimal degrees, by the haversine formula.
69
+ *
70
+ * It applies no sentinel handling.
71
+ * Multiply by a radius from {@link EARTH_RADII} or {@link BODY_RADII_KM} for a distance.
67
72
  *
68
- * `unit` selects the Earth radius.
73
+ * @category Position
69
74
  */
70
- function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
75
+ export function centralAngle(lat1: number, lon1: number, lat2: number, lon2: number): number {
71
76
  const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
72
77
  const dLon = (lon2 - lon1) * ConversionFactor.DegreesToRadians
73
78
 
@@ -77,63 +82,7 @@ function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, uni
77
82
  Math.cos(lat2 * ConversionFactor.DegreesToRadians) *
78
83
  Math.pow(Math.sin(dLon / 2), 2)
79
84
 
80
- return RADII[unit] * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
81
- }
82
-
83
- /**
84
- * Calculate the distance between two points on the Earth's surface.
85
- *
86
- * @category Position
87
- * @param point1 The first point to calculate the distance from.
88
- * @param point2 The second point to calculate the distance to.
89
- * @param unit The unit of measurement to return the distance in.
90
- *
91
- * @returns The distance between the two points in the specified unit.
92
- */
93
- export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: EarthRadiusUnit = "km"): number {
94
- const p1 = GeoPoint.from(point1)
95
- const p2 = GeoPoint.from(point2)
96
-
97
- if (!p1 || !p2) return Number.NaN
98
-
99
- return greatCircle(p1.latitude, p1.longitude, p2.latitude, p2.longitude, unit)
100
- }
101
-
102
- /**
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 object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
107
- *
108
- * @category Position
109
- */
110
- export function greatCircleDistance(
111
- point1: GeoPointInput,
112
- point2: GeoPointInput,
113
- options: GreatCircleOptions = {}
114
- ): number {
115
- const unit = options.unit ?? "km"
116
- const body = options.body ?? "earth"
117
- const scale = BODY_RADII_KM[body] / BODY_RADII_KM.earth
118
-
119
- return haversine(point1, point2, unit) * scale
120
- }
121
-
122
- /**
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.
127
- *
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.
132
- *
133
- * @category Position
134
- */
135
- export function haversineKm(aLat: number, aLon: number, bLat: number, bLon: number): number {
136
- return greatCircle(aLat, aLon, bLat, bLon, "km")
85
+ return 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
137
86
  }
138
87
 
139
88
  /**
@@ -7,7 +7,7 @@
7
7
  * coordinates to WGS84.
8
8
  */
9
9
 
10
- import { normalizePostcodeDisplay } from "@mailwoman/codex/postcode/display-form"
10
+ import { normalizePostcodeDisplay } from "@mailwoman/postcode/display-form"
11
11
  import { CSVSpliterator } from "spliterator"
12
12
 
13
13
  import { osgb36ToWGS84 } from "#osgb36"
@@ -7,7 +7,8 @@
7
7
  */
8
8
 
9
9
  import type { LineStringLiteral, MultiLineStringLiteral } from "#geometries/line-string"
10
- import type { MultiPointLiteral, PointLiteral } from "#geometries/point"
10
+ import type { MultiPointLiteral } from "#geometries/point"
11
+ import type { PointLiteral } from "#geometries/point/coordinates"
11
12
  import type { MultiPolygonLiteral, PolygonLiteral } from "#geometries/polygon"
12
13
  import type { GeoObjectLiteral } from "#objects"
13
14
 
@@ -0,0 +1,147 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file The shapes a point can arrive in, and the reading of their coordinates.
6
+ *
7
+ * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a caller that only needs
8
+ * the coordinates of a `GeoPointInput` must not load its embedded WebAssembly binary.
9
+ */
10
+
11
+ import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
12
+ import { tryParsingJSON } from "@mailwoman/core/json"
13
+
14
+ import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
15
+ import { type GeoObjectLiteral, GeometryType } from "#objects"
16
+ import {
17
+ type InternalPointCoordinates,
18
+ type Coordinates2D as Point2DCoordinates,
19
+ type Coordinates3D as Point3DCoordinates,
20
+ isCoordPairLiteral,
21
+ isGoogleMapsLatLngLiteral,
22
+ isInterpolatedCoordinates,
23
+ } from "#position"
24
+
25
+ /**
26
+ * A JSON-serializable single point geometry, such as a specific location,
27
+ * address, or longitude, latitude pair.
28
+ *
29
+ * ```js
30
+ * {
31
+ * "type": "Point",
32
+ * "coordinates": [100, 0]
33
+ * }
34
+ * ```
35
+ *
36
+ * @title Point Geometry
37
+ * @public
38
+ */
39
+ export interface PointLiteral extends GeoObjectLiteral {
40
+ /**
41
+ * Declares the type of GeoJSON object as a `Point` geometry.
42
+ */
43
+ type: "Point"
44
+ /**
45
+ * A pair of coordinates in the form of [longitude, latitude].
46
+ *
47
+ * @see {@linkcode Point2DCoordinates} for more information.
48
+ */
49
+ coordinates: Point2DCoordinates | Point3DCoordinates
50
+ }
51
+
52
+ /**
53
+ * Type-predicate to determine if the given input is a GeoJSON Point geometry.
54
+ */
55
+ export function isPointLiteral(input: PointLiteral | null | undefined | unknown): input is PointLiteral {
56
+ if (!input || typeof input !== "object") return false
57
+
58
+ if (!("type" in input)) return false
59
+
60
+ if (!("coordinates" in input)) return false
61
+
62
+ if (input.type !== GeometryType.Point) return false
63
+
64
+ return isCoordPairLiteral(input.coordinates)
65
+ }
66
+
67
+ /**
68
+ * Common interface for Browser Geolocation API coordinates.
69
+ *
70
+ * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates MDN Web Docs}
71
+ */
72
+ export interface GeolocationCoordinatesLike {
73
+ latitude: number
74
+ longitude: number
75
+ altitude: number
76
+ }
77
+
78
+ /**
79
+ * Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike}
80
+ * object — the shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
81
+ */
82
+ export function isGeolocationCoordinatesLike(input: unknown): input is GeolocationCoordinatesLike {
83
+ if (!input || typeof input !== "object") return false
84
+
85
+ if (!("latitude" in input)) return false
86
+
87
+ if (!("longitude" in input)) return false
88
+
89
+ if (!("altitude" in input)) return false
90
+
91
+ return true
92
+ }
93
+
94
+ export type GeoPointInput =
95
+ | PointLiteral
96
+ | Point2DCoordinates
97
+ | Point3DCoordinates
98
+ | GeolocationCoordinatesLike
99
+ | InternalPointCoordinates
100
+ | LatLngLiteral
101
+ | LatLng
102
+
103
+ /**
104
+ * Reads the `[longitude, latitude(, altitude)]` coordinates out of a point of any accepted shape.
105
+ *
106
+ * It only dispatches on shape: no range check applies, and `null` answers an input
107
+ * that is none of them. {@link readPointCoordinates} adds the validation.
108
+ */
109
+ export function pointInputCoordinates(input: unknown): Point2DCoordinates | Point3DCoordinates | null {
110
+ if (isCoordPairLiteral(input)) return input
111
+
112
+ if (isPointLiteral(input)) return input.coordinates
113
+
114
+ if (isGoogleMapsLatLngLiteral(input)) return [input.lng, input.lat]
115
+
116
+ if (isGeolocationCoordinatesLike(input)) return [input.longitude, input.latitude, input.altitude || 0]
117
+
118
+ if (isInterpolatedCoordinates(input)) return [input.x, input.y]
119
+
120
+ return null
121
+ }
122
+
123
+ /**
124
+ * The coordinates of a point a caller can stand behind, or `null`.
125
+ *
126
+ * Applies the rules of `GeoPoint.from` without constructing one: a string is parsed as JSON, an
127
+ * out-of-range magnitude is rejected rather than repaired, and `(0, 0)` is read as a missing coordinate.
128
+ */
129
+ export function readPointCoordinates(input: unknown): Point2DCoordinates | Point3DCoordinates | null {
130
+ if (!input) return null
131
+
132
+ if (typeof input === "string") {
133
+ input = tryParsingJSON<GeoPointInput>(input) || tryParsingJSON<GeoPointInput>(`[${input}]`) || input
134
+ }
135
+
136
+ const coordinates = pointInputCoordinates(input)
137
+
138
+ if (!coordinates) return null
139
+
140
+ const [longitude, latitude] = coordinates
141
+
142
+ if (!isValidLongitude(longitude) || !isValidLatitude(latitude)) return null
143
+
144
+ if (latitude === 0 && longitude === 0) return null
145
+
146
+ return coordinates
147
+ }
@@ -0,0 +1,115 @@
1
+ /**
2
+ * @copyright Sister Software.
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ * @file Great-circle distances between `GeoPointInput` values or plain coordinates.
6
+ *
7
+ * This module imports no runtime geometry class. `GeoPoint` imports `@sister.software/h3`, and a distance must not pay for that.
8
+ * Points are read through `#geometries/point/coordinates` instead.
9
+ */
10
+
11
+ import { BODY_RADII_KM, centralAngle, EARTH_RADII, type EarthRadiusUnit, type GreatCircleOptions } from "#distance"
12
+ import { type GeoPointInput, readPointCoordinates } from "#geometries/point/coordinates"
13
+
14
+ /**
15
+ * A coordinate component that may be absent, such as a geocoder's `lat` and `lon` when it returned no point.
16
+ */
17
+ type MaybeCoordinate = number | null | undefined
18
+
19
+ /**
20
+ * Calculate the distance between two points on the Earth's surface.
21
+ *
22
+ * The object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
23
+ *
24
+ * @category Position
25
+ * @param point1 The first point to calculate the distance from.
26
+ * @param point2 The second point to calculate the distance to.
27
+ * @param unit The unit of measurement to return the distance in.
28
+ *
29
+ * @returns The distance between the two points in the specified unit.
30
+ */
31
+ export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit?: EarthRadiusUnit): number
32
+
33
+ /**
34
+ * Calculate the distance between two coordinates in decimal degrees on the Earth's surface.
35
+ *
36
+ * Unlike the object form, this is pure math with no Null-Island sentinel: `(0, 0)` is
37
+ * the Gulf of Guinea — a real point rather than a "missing coordinate".
38
+ *
39
+ * @category Position
40
+ * @returns The distance between the two coordinates in the specified unit.
41
+ */
42
+ export function haversine(aLat: number, aLon: number, bLat: number, bLon: number, unit?: EarthRadiusUnit): number
43
+
44
+ /**
45
+ * Calculate the distance between two coordinates, either of which may be absent.
46
+ *
47
+ * Answers `null` when any component is `null` or `undefined`, so a geocoder's
48
+ * `{ lat: number | null, lon: number | null }` pair needs no guard or non-null assertion.
49
+ *
50
+ * @category Position
51
+ * @returns The distance in the specified unit, or `null` when a component is absent.
52
+ */
53
+ export function haversine(
54
+ aLat: MaybeCoordinate,
55
+ aLon: MaybeCoordinate,
56
+ bLat: MaybeCoordinate,
57
+ bLon: MaybeCoordinate,
58
+ unit?: EarthRadiusUnit
59
+ ): number | null
60
+
61
+ export function haversine(
62
+ ...args:
63
+ | [point1: GeoPointInput, point2: GeoPointInput, unit?: EarthRadiusUnit]
64
+ | [
65
+ aLat: MaybeCoordinate,
66
+ aLon: MaybeCoordinate,
67
+ bLat: MaybeCoordinate,
68
+ bLon: MaybeCoordinate,
69
+ unit?: EarthRadiusUnit,
70
+ ]
71
+ ): number | null {
72
+ const [first] = args
73
+
74
+ if (typeof first === "object" && first !== null) {
75
+ const [point1, point2, unit = "km"] = args as [GeoPointInput, GeoPointInput, EarthRadiusUnit?]
76
+ const p1 = readPointCoordinates(point1)
77
+ const p2 = readPointCoordinates(point2)
78
+
79
+ if (!p1 || !p2) return Number.NaN
80
+
81
+ return EARTH_RADII[unit] * centralAngle(p1[1], p1[0], p2[1], p2[0])
82
+ }
83
+
84
+ const [aLat, aLon, bLat, bLon, unit = "km"] = args as [
85
+ MaybeCoordinate,
86
+ MaybeCoordinate,
87
+ MaybeCoordinate,
88
+ MaybeCoordinate,
89
+ EarthRadiusUnit?,
90
+ ]
91
+
92
+ if (aLat == null || aLon == null || bLat == null || bLon == null) return null
93
+
94
+ return EARTH_RADII[unit] * centralAngle(aLat, aLon, bLat, bLon)
95
+ }
96
+
97
+ /**
98
+ * Great-circle distance on the selected body. {@link haversine} is this function on Earth.
99
+ * A caller with a body passes it here.
100
+ *
101
+ * The object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
102
+ *
103
+ * @category Position
104
+ */
105
+ export function greatCircleDistance(
106
+ point1: GeoPointInput,
107
+ point2: GeoPointInput,
108
+ options: GreatCircleOptions = {}
109
+ ): number {
110
+ const unit = options.unit ?? "km"
111
+ const body = options.body ?? "earth"
112
+ const scale = BODY_RADII_KM[body] / BODY_RADII_KM.earth
113
+
114
+ return haversine(point1, point2, unit) * scale
115
+ }
@@ -5,104 +5,30 @@
5
5
  */
6
6
 
7
7
  import type google from "@googlemaps/google-maps-services-js"
8
- import type { LatLng, LatLngLiteral } from "@googlemaps/google-maps-services-js"
8
+ import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
9
9
  import { tryParsingJSON, stringifyJSON } from "@mailwoman/core/json"
10
+ import { latLngToCell } from "@sister.software/h3"
10
11
  import { convert as convertCoords } from "geo-coordinates-parser"
11
- import { latLngToCell } from "h3-js"
12
12
 
13
13
  import { type BBox2DLiteral, type BBox3DLiteral, GeoBoundingBox, isBBox } from "#bbox"
14
14
  import { isValidLatitude, isValidLongitude } from "#coordinate/bounds"
15
- import { type H3Cell, shortenH3Cell } from "#h3/cell"
15
+ import {
16
+ type GeoPointInput,
17
+ type GeolocationCoordinatesLike,
18
+ type PointLiteral,
19
+ isPointLiteral,
20
+ pointInputCoordinates,
21
+ } from "#geometries/point/coordinates"
22
+ import { shortenH3Cell } from "#h3/cell"
16
23
  import { type GeoObjectLiteral, GeometryType } from "#objects"
17
24
  import {
18
25
  type InternalPointCoordinates,
19
26
  type Coordinates2D as Point2DCoordinates,
20
27
  type Coordinates3D as Point3DCoordinates,
21
28
  clampLatitude,
22
- isCoordPairLiteral,
23
- isGoogleMapsLatLngLiteral,
24
- isInterpolatedCoordinates,
25
29
  wrapLongitude,
26
30
  } from "#position"
27
31
 
28
- /**
29
- * A JSON-serializable single point geometry, such as a specific location,
30
- * address, or longitude, latitude pair.
31
- *
32
- * ```js
33
- * {
34
- * "type": "Point",
35
- * "coordinates": [100, 0]
36
- * }
37
- * ```
38
- *
39
- * @title Point Geometry
40
- * @public
41
- */
42
- export interface PointLiteral extends GeoObjectLiteral {
43
- /**
44
- * Declares the type of GeoJSON object as a `Point` geometry.
45
- */
46
- type: "Point"
47
- /**
48
- * A pair of coordinates in the form of [longitude, latitude].
49
- *
50
- * @see {@linkcode Point2DCoordinates} for more information.
51
- */
52
- coordinates: Point2DCoordinates | Point3DCoordinates
53
- }
54
-
55
- /**
56
- * Type-predicate to determine if the given input is a GeoJSON Point geometry.
57
- */
58
- export function isPointLiteral(input: PointLiteral | null | undefined | unknown): input is PointLiteral {
59
- if (!input || typeof input !== "object") return false
60
-
61
- if (!("type" in input)) return false
62
-
63
- if (!("coordinates" in input)) return false
64
-
65
- if (input.type !== GeometryType.Point) return false
66
-
67
- return isCoordPairLiteral(input.coordinates)
68
- }
69
-
70
- /**
71
- * Common interface for Browser Geolocation API coordinates.
72
- *
73
- * @see {@link https://developer.mozilla.org/en-US/docs/Web/API/GeolocationCoordinates MDN Web Docs}
74
- */
75
- export interface GeolocationCoordinatesLike {
76
- latitude: number
77
- longitude: number
78
- altitude: number
79
- }
80
-
81
- /**
82
- * Type-predicate to determine if the given input appears to be a {@linkcode GeolocationCoordinatesLike}
83
- * object — the shape the browser's `GeolocationCoordinates` presents, without depending on the DOM type.
84
- */
85
- export function isGeolocationCoordinatesLike(input: unknown): input is GeolocationCoordinatesLike {
86
- if (!input || typeof input !== "object") return false
87
-
88
- if (!("latitude" in input)) return false
89
-
90
- if (!("longitude" in input)) return false
91
-
92
- if (!("altitude" in input)) return false
93
-
94
- return true
95
- }
96
-
97
- export type GeoPointInput =
98
- | PointLiteral
99
- | Point2DCoordinates
100
- | Point3DCoordinates
101
- | GeolocationCoordinatesLike
102
- | InternalPointCoordinates
103
- | LatLngLiteral
104
- | LatLng
105
-
106
32
  // #region GeoPoint
107
33
 
108
34
  /**
@@ -265,22 +191,12 @@ export class GeoPoint implements PointLiteral {
265
191
  */
266
192
  constructor(input: GeoPointInput, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox)
267
193
  constructor(input?: GeoPointInput, bbox?: BBox2DLiteral | BBox3DLiteral | GeoBoundingBox) {
268
- if (isCoordPairLiteral(input)) {
269
- this.coordinates = input
270
- } else if (isPointLiteral(input)) {
271
- this.coordinates = [...input.coordinates]
194
+ const coordinates = pointInputCoordinates(input)
272
195
 
273
- if (isBBox(input.bbox)) {
274
- this.bbox = [...input.bbox]
275
- }
276
- } else if (isGoogleMapsLatLngLiteral(input)) {
277
- this.coordinates = [input.lng, input.lat]
278
- } else if (isGeolocationCoordinatesLike(input)) {
279
- this.coordinates = [input.longitude, input.latitude, input.altitude || 0]
280
- } else if (isInterpolatedCoordinates(input)) {
281
- this.coordinates = [input.x, input.y]
282
- } else {
283
- this.coordinates = [0, 0]
196
+ this.coordinates = coordinates ? [...coordinates] : [0, 0]
197
+
198
+ if (isPointLiteral(input) && isBBox(input.bbox)) {
199
+ this.bbox = [...input.bbox]
284
200
  }
285
201
 
286
202
  if (isBBox(bbox)) {
@@ -405,7 +321,7 @@ export class GeoPoint implements PointLiteral {
405
321
  * Converts the GeoPoint to a H3 short cell address.
406
322
  */
407
323
  public toH3Cell(resolution = 15) {
408
- const cell = latLngToCell(this.#latitude, this.#longitude, resolution) as H3Cell
324
+ const cell = latLngToCell(this.#latitude, this.#longitude, resolution)
409
325
 
410
326
  return cell
411
327
  }
package/lib/geometries.ts CHANGED
@@ -9,5 +9,7 @@
9
9
  export * from "#geometries/collection"
10
10
  export * from "#geometries/line-string"
11
11
  export * from "#geometries/point"
12
+ export * from "#geometries/point/coordinates"
13
+ export * from "#geometries/point/distance"
12
14
  export * from "#geometries/polygon"
13
15
  export * from "#geometries/ring-blob"