@mailwoman/spatial 9.1.0 → 9.3.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/{bbox.ts → lib/bbox.ts} +54 -3
- package/lib/coordinate-bounds.ts +52 -0
- package/{coordinate-formats.ts → lib/coordinate-formats.ts} +16 -3
- package/lib/distance.ts +165 -0
- package/{feature.ts → lib/feature.ts} +1 -1
- package/{geometries → lib/geometries}/collection.ts +4 -4
- package/lib/geometries/index.ts +13 -0
- package/{geometries → lib/geometries}/line-string.ts +2 -2
- package/{geometries → lib/geometries}/point.ts +9 -9
- package/{geometries → lib/geometries}/polygon.ts +93 -89
- package/lib/geometries/ring-blob.ts +521 -0
- package/{google → lib/google}/index.ts +1 -1
- package/{h3 → lib/h3}/cell.ts +83 -1
- package/lib/h3/coverage.ts +45 -0
- package/{h3 → lib/h3}/index.ts +5 -3
- package/lib/h3/interior-cells.ts +138 -0
- package/lib/h3/polygon-cells.ts +462 -0
- package/lib/index.ts +21 -0
- package/{objects.ts → lib/objects.ts} +1 -1
- package/lib/open-location-code.ts +168 -0
- package/{osgb36.ts → lib/osgb36.ts} +2 -2
- package/{polyline.ts → lib/polyline.ts} +1 -1
- package/lib/position.ts +216 -0
- package/lib/projection-transform.ts +115 -0
- package/{regions → lib/regions}/codes.ts +1 -1
- package/{regions → lib/regions}/index.ts +2 -2
- package/lib/tools/ogr-stream.ts +99 -0
- package/lib/tools/ogr.ts +201 -0
- package/{sdk → lib}/well-known-text.ts +2 -1
- package/out/bbox.d.ts +23 -2
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +33 -2
- package/out/bbox.js.map +1 -1
- package/out/coordinate-bounds.d.ts +41 -0
- package/out/coordinate-bounds.d.ts.map +1 -0
- package/out/coordinate-bounds.js +45 -0
- package/out/coordinate-bounds.js.map +1 -0
- package/out/coordinate-formats.d.ts +8 -0
- package/out/coordinate-formats.d.ts.map +1 -1
- package/out/coordinate-formats.js +14 -3
- package/out/coordinate-formats.js.map +1 -1
- package/out/distance.d.ts +84 -0
- package/out/distance.d.ts.map +1 -0
- package/out/distance.js +132 -0
- package/out/distance.js.map +1 -0
- package/out/feature.d.ts +1 -1
- package/out/feature.d.ts.map +1 -1
- package/out/feature.js.map +1 -1
- package/out/geometries/collection.d.ts +4 -4
- package/out/geometries/collection.d.ts.map +1 -1
- package/out/geometries/collection.js.map +1 -1
- package/out/geometries/index.d.ts +5 -4
- package/out/geometries/index.d.ts.map +1 -1
- package/out/geometries/index.js +5 -4
- package/out/geometries/index.js.map +1 -1
- package/out/geometries/line-string.d.ts +2 -2
- package/out/geometries/line-string.d.ts.map +1 -1
- package/out/geometries/line-string.js.map +1 -1
- package/out/geometries/point.d.ts +9 -9
- package/out/geometries/point.d.ts.map +1 -1
- package/out/geometries/point.js +9 -9
- package/out/geometries/point.js.map +1 -1
- package/out/geometries/polygon.d.ts +60 -56
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +66 -63
- package/out/geometries/polygon.js.map +1 -1
- package/out/geometries/ring-blob.d.ts +183 -0
- package/out/geometries/ring-blob.d.ts.map +1 -0
- package/out/geometries/ring-blob.js +376 -0
- package/out/geometries/ring-blob.js.map +1 -0
- package/out/google/index.d.ts +1 -1
- package/out/google/index.d.ts.map +1 -1
- package/out/google/index.js +1 -1
- package/out/google/index.js.map +1 -1
- package/out/google/place-id.d.ts.map +1 -1
- package/out/google/place-id.js.map +1 -1
- package/out/h3/cell.d.ts +32 -0
- package/out/h3/cell.d.ts.map +1 -1
- package/out/h3/cell.js +68 -1
- package/out/h3/cell.js.map +1 -1
- package/out/h3/coverage.d.ts +32 -0
- package/out/h3/coverage.d.ts.map +1 -0
- package/out/h3/coverage.js +27 -0
- package/out/h3/coverage.js.map +1 -0
- package/out/h3/index.d.ts +5 -3
- package/out/h3/index.d.ts.map +1 -1
- package/out/h3/index.js +5 -3
- package/out/h3/index.js.map +1 -1
- package/out/h3/interior-cells.d.ts +54 -0
- package/out/h3/interior-cells.d.ts.map +1 -0
- package/out/h3/interior-cells.js +111 -0
- package/out/h3/interior-cells.js.map +1 -0
- package/out/h3/polygon-cells.d.ts +182 -0
- package/out/h3/polygon-cells.d.ts.map +1 -0
- package/out/h3/polygon-cells.js +339 -0
- package/out/h3/polygon-cells.js.map +1 -0
- package/out/index.d.ts +15 -12
- package/out/index.d.ts.map +1 -1
- package/out/index.js +15 -12
- package/out/index.js.map +1 -1
- package/out/objects.d.ts +1 -1
- package/out/objects.d.ts.map +1 -1
- package/out/objects.js +1 -1
- package/out/objects.js.map +1 -1
- package/out/open-location-code.d.ts +48 -0
- package/out/open-location-code.d.ts.map +1 -0
- package/out/open-location-code.js +137 -0
- package/out/open-location-code.js.map +1 -0
- package/out/osgb36.d.ts +2 -2
- package/out/osgb36.d.ts.map +1 -1
- package/out/osgb36.js +1 -1
- package/out/osgb36.js.map +1 -1
- package/out/polyline.d.ts.map +1 -1
- package/out/polyline.js +1 -1
- package/out/polyline.js.map +1 -1
- package/out/position.d.ts +4 -61
- package/out/position.d.ts.map +1 -1
- package/out/position.js +14 -126
- package/out/position.js.map +1 -1
- package/out/projection-transform.d.ts +67 -0
- package/out/projection-transform.d.ts.map +1 -0
- package/out/projection-transform.js +48 -0
- package/out/projection-transform.js.map +1 -0
- package/out/projection.d.ts.map +1 -1
- package/out/projection.js.map +1 -1
- package/out/regions/codes.d.ts.map +1 -1
- package/out/regions/codes.js.map +1 -1
- package/out/regions/index.d.ts +2 -2
- package/out/regions/index.d.ts.map +1 -1
- package/out/regions/index.js +2 -2
- package/out/regions/index.js.map +1 -1
- package/out/regions/names.d.ts.map +1 -1
- package/out/regions/names.js.map +1 -1
- package/out/tools/ogr-stream.d.ts +34 -0
- package/out/tools/ogr-stream.d.ts.map +1 -0
- package/out/tools/ogr-stream.js +74 -0
- package/out/tools/ogr-stream.js.map +1 -0
- package/out/tools/ogr.d.ts +87 -0
- package/out/tools/ogr.d.ts.map +1 -0
- package/out/tools/ogr.js +84 -0
- package/out/tools/ogr.js.map +1 -0
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/{sdk/well-known-text.d.ts → well-known-text.d.ts} +1 -1
- package/out/well-known-text.d.ts.map +1 -0
- package/out/well-known-text.js.map +1 -0
- package/package.json +143 -14
- package/geometries/index.ts +0 -12
- package/index.ts +0 -18
- package/out/sdk/index.d.ts +0 -7
- package/out/sdk/index.d.ts.map +0 -1
- package/out/sdk/index.js +0 -7
- package/out/sdk/index.js.map +0 -1
- package/out/sdk/well-known-text.d.ts.map +0 -1
- package/out/sdk/well-known-text.js.map +0 -1
- package/out/tsconfig.tsbuildinfo +0 -1
- package/position.ts +0 -345
- package/sdk/index.ts +0 -7
- /package/{google → lib/google}/place-id.ts +0 -0
- /package/{projection.ts → lib/projection.ts} +0 -0
- /package/{regions → lib/regions}/names.ts +0 -0
- /package/out/{sdk/well-known-text.js → well-known-text.js} +0 -0
package/position.ts
DELETED
|
@@ -1,345 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @copyright Sister Software.
|
|
3
|
-
* @license AGPL-3.0
|
|
4
|
-
* @author Teffen Ellis, et al.
|
|
5
|
-
*
|
|
6
|
-
* This file contains types and utilities for working with geographic positions.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
|
|
10
|
-
import { GeoPoint, type GeoPointInput } from "@mailwoman/spatial"
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Southern limit of latitude in WGS84 degrees. Outside it a value cannot be a latitude.
|
|
14
|
-
*/
|
|
15
|
-
const LATITUDE_MIN = -90
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Northern limit of latitude in WGS84 degrees.
|
|
19
|
-
*/
|
|
20
|
-
const LATITUDE_MAX = 90
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Western limit of longitude in WGS84 degrees (the antimeridian, which is itself a valid longitude).
|
|
24
|
-
*/
|
|
25
|
-
const LONGITUDE_MIN = -180
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Eastern limit of longitude in WGS84 degrees.
|
|
29
|
-
*/
|
|
30
|
-
const LONGITUDE_MAX = 180
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Whether a number is a latitude a point can actually sit at: finite, and within [-90, 90] inclusive (the poles count).
|
|
34
|
-
* Distinct from {@link clampLatitude}, which folds an out-of-range value onto the nearest pole — clamping is right for a
|
|
35
|
-
* viewport edge and wrong for a coordinate, where an out-of-range value means the input was malformed and the repaired
|
|
36
|
-
* point is fiction.
|
|
37
|
-
*
|
|
38
|
-
* @category Position
|
|
39
|
-
*/
|
|
40
|
-
export function isValidLatitude(value: number): boolean {
|
|
41
|
-
return Number.isFinite(value) && value >= LATITUDE_MIN && value <= LATITUDE_MAX
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Whether a number is a longitude a point can actually sit at: finite, and within [-180, 180] inclusive. Same
|
|
46
|
-
* distinction from {@link wrapLongitude} that {@link isValidLatitude} draws from `clampLatitude` — wrapping 190° to -170°
|
|
47
|
-
* is correct for a pan gesture and a silent relocation for a parsed coordinate.
|
|
48
|
-
*
|
|
49
|
-
* @category Position
|
|
50
|
-
*/
|
|
51
|
-
export function isValidLongitude(value: number): boolean {
|
|
52
|
-
return Number.isFinite(value) && value >= LONGITUDE_MIN && value <= LONGITUDE_MAX
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
/**
|
|
56
|
-
* Arity of a `[lon, lat]` coordinate tuple.
|
|
57
|
-
*/
|
|
58
|
-
const COORD_PAIR_LENGTH = 2
|
|
59
|
-
|
|
60
|
-
/**
|
|
61
|
-
* Arity of a `[lon, lat, elevation]` coordinate tuple.
|
|
62
|
-
*/
|
|
63
|
-
const COORD_TRIPLE_LENGTH = 3
|
|
64
|
-
|
|
65
|
-
/**
|
|
66
|
-
* An ordered pair of coordinates in the form of [longitude, latitude].
|
|
67
|
-
*
|
|
68
|
-
* Note that unlike the typical order, GeoJSON coordinates are in the order of [longitude, latitude] to match the order
|
|
69
|
-
* of [x, y] in Cartesian coordinates.
|
|
70
|
-
*
|
|
71
|
-
* @category Position
|
|
72
|
-
* @category GeoJSON
|
|
73
|
-
* @see {@linkcode Coordinates3D} for 3D coordinates.
|
|
74
|
-
*/
|
|
75
|
-
export type Coordinates2D = [
|
|
76
|
-
/**
|
|
77
|
-
* The longitude of the point, i.e. the x-coordinate.
|
|
78
|
-
*
|
|
79
|
-
* @minimum -180
|
|
80
|
-
* @maximum 180
|
|
81
|
-
*/
|
|
82
|
-
longitude: number,
|
|
83
|
-
/**
|
|
84
|
-
* The latitude of the point, i.e. the y-coordinate.
|
|
85
|
-
*
|
|
86
|
-
* @minimum -90
|
|
87
|
-
* @maximum 90
|
|
88
|
-
*/
|
|
89
|
-
latitude: number,
|
|
90
|
-
]
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* Orders the given coordinates as [longitude, latitude].
|
|
94
|
-
*
|
|
95
|
-
* This is useful when converting into GeoJSON format.
|
|
96
|
-
*
|
|
97
|
-
* @category GeoJSON
|
|
98
|
-
* @category Position
|
|
99
|
-
*/
|
|
100
|
-
export function orderCoordPairToGeoJSON([latitude, longitude]: [number, number]): Coordinates2D {
|
|
101
|
-
return [longitude, latitude]
|
|
102
|
-
}
|
|
103
|
-
|
|
104
|
-
/**
|
|
105
|
-
* Orders the given coordinates as [latitude, longitude].
|
|
106
|
-
*
|
|
107
|
-
* This is useful when converting into Google Maps format.
|
|
108
|
-
*
|
|
109
|
-
* @category GeoJSON
|
|
110
|
-
* @category Position
|
|
111
|
-
*/
|
|
112
|
-
export function orderGeoJSONToCoordPair([longitude, latitude]: Coordinates2D): [number, number] {
|
|
113
|
-
return [latitude, longitude]
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
/**
|
|
117
|
-
* Given an input which appears to be reversed GeoJSON coordinates (i.e. [latitude, longitude]), returns the coordinates
|
|
118
|
-
* in the correct order of [longitude, latitude].
|
|
119
|
-
*
|
|
120
|
-
* **This cannot do what its name promises, and nothing in this repo calls it.** The only signal it has is the [-90, 90]
|
|
121
|
-
* latitude range, so it transposes a pair only when the SECOND magnitude falls outside that range — i.e. only where
|
|
122
|
-
* |longitude| > 90. It is therefore inert across every longitude in [-90, 90]: all of Europe and Africa, western
|
|
123
|
-
* Russia, the Middle East, India, and the Atlantic. A caller who hands it `[latitude, longitude]` gets the pair
|
|
124
|
-
* repaired in Dallas and left corrupted in Berlin, from one code path, decided by the data. That is why `GeoPoint`
|
|
125
|
-
* stopped calling it on 2026-08-05 (recorded in `e9bfd139`; `registry/tools/geocoder-vs-provided-coords.ts` had already
|
|
126
|
-
* routed around it) and now reads a 2-tuple as GeoJSON [longitude, latitude], full stop.
|
|
127
|
-
*
|
|
128
|
-
* Retained as an explicit, opt-in escape hatch for a caller who knows their source is lat-first AND North American. If
|
|
129
|
-
* you know the axis order, use {@link orderCoordPairToGeoJSON}; if you do not, no function can tell you.
|
|
130
|
-
*
|
|
131
|
-
* @deprecated Axis order is metadata, not something a magnitude can recover. Prefer `orderCoordPairToGeoJSON`.
|
|
132
|
-
* @category GeoJSON
|
|
133
|
-
* @category Position
|
|
134
|
-
*/
|
|
135
|
-
export function inferGeoJSONCoordOrder([coordA, coordB]: [number, number]): Coordinates2D {
|
|
136
|
-
// Latitude values typically range from -90 to 90
|
|
137
|
-
const isCoordALat = coordA >= LATITUDE_MIN && coordA <= LATITUDE_MAX
|
|
138
|
-
const isCoordBLat = coordB >= LATITUDE_MIN && coordB <= LATITUDE_MAX
|
|
139
|
-
|
|
140
|
-
if (isCoordALat && !isCoordBLat) {
|
|
141
|
-
// coordA is latitude, coordB is longitude
|
|
142
|
-
return [coordB, coordA]
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
if (!isCoordALat && isCoordBLat) {
|
|
146
|
-
// coordB is latitude, coordA is longitude
|
|
147
|
-
return [coordA, coordB]
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
// In case both appear to be latitudes (unlikely) or longitudes (out of range for US),
|
|
151
|
-
// assume coordA is longitude and coordB is latitude as default.
|
|
152
|
-
return [coordA, coordB]
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* An ordered triple of coordinates in the form of [longitude, latitude, altitude].
|
|
157
|
-
*
|
|
158
|
-
* @category Position
|
|
159
|
-
* @category GeoJSON
|
|
160
|
-
* @see {@linkcode Coordinates2D} for 2D coordinates.
|
|
161
|
-
*/
|
|
162
|
-
export type Coordinates3D = [
|
|
163
|
-
/**
|
|
164
|
-
* The longitude of the point, i.e. the x-coordinate.
|
|
165
|
-
*
|
|
166
|
-
* @minimum -180
|
|
167
|
-
* @maximum 180
|
|
168
|
-
*/
|
|
169
|
-
longitude: number,
|
|
170
|
-
/**
|
|
171
|
-
* The latitude of the point, i.e. the y-coordinate.
|
|
172
|
-
*
|
|
173
|
-
* @minimum -90
|
|
174
|
-
* @maximum 90
|
|
175
|
-
*/
|
|
176
|
-
latitude: number,
|
|
177
|
-
/**
|
|
178
|
-
* The altitude of the point, i.e. the z-coordinate.
|
|
179
|
-
*/
|
|
180
|
-
altitude: number,
|
|
181
|
-
]
|
|
182
|
-
|
|
183
|
-
/**
|
|
184
|
-
* A record of internal coordinates, typically used by the US Census.
|
|
185
|
-
*/
|
|
186
|
-
export interface InternalPointCoordinates {
|
|
187
|
-
/**
|
|
188
|
-
* Internal Longitude (X) Coordinates
|
|
189
|
-
*
|
|
190
|
-
* @minimum -180
|
|
191
|
-
* @maximum 180
|
|
192
|
-
*/
|
|
193
|
-
x: number
|
|
194
|
-
/**
|
|
195
|
-
* Internal Latitude (Y) Coordinates
|
|
196
|
-
*
|
|
197
|
-
* @minimum -90
|
|
198
|
-
* @maximum 90
|
|
199
|
-
*/
|
|
200
|
-
y: number
|
|
201
|
-
}
|
|
202
|
-
|
|
203
|
-
/**
|
|
204
|
-
* Type-predicate to determine if the given input is a GeoJSON Point geometry.
|
|
205
|
-
*
|
|
206
|
-
* @category Type Predicates
|
|
207
|
-
* @category GeoJSON
|
|
208
|
-
*/
|
|
209
|
-
export function isCoordPairLiteral(input: unknown): input is [number, number] | [number, number, number] {
|
|
210
|
-
if (!Array.isArray(input)) return false
|
|
211
|
-
|
|
212
|
-
if (input.length !== COORD_PAIR_LENGTH && input.length !== COORD_TRIPLE_LENGTH) return false
|
|
213
|
-
|
|
214
|
-
return input.every((coord) => typeof coord === "number")
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
/**
|
|
218
|
-
* Type-predicate to determine if the given input is a {@linkcode LatLngLiteral} object.
|
|
219
|
-
*
|
|
220
|
-
* @category Position
|
|
221
|
-
* @category Type Predicates
|
|
222
|
-
* @see {@link https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral Google Maps API Documentation}
|
|
223
|
-
*/
|
|
224
|
-
export function isGoogleMapsLatLngLiteral(input: unknown): input is LatLngLiteral {
|
|
225
|
-
if (!input || typeof input !== "object") return false
|
|
226
|
-
|
|
227
|
-
if (!Object.hasOwn(input, "lat") || !Object.hasOwn(input, "lng")) return false
|
|
228
|
-
|
|
229
|
-
return true
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
/**
|
|
233
|
-
* Type-predicate to determine if the given input is a {@linkcode InternalPointCoordinates} object.
|
|
234
|
-
*
|
|
235
|
-
* @category Position
|
|
236
|
-
* @category Type Predicates
|
|
237
|
-
*/
|
|
238
|
-
export function isInterpolatedCoordinates(input: unknown): input is InternalPointCoordinates {
|
|
239
|
-
if (!input || typeof input !== "object") return false
|
|
240
|
-
|
|
241
|
-
if (!("x" in input)) return false
|
|
242
|
-
|
|
243
|
-
if (!("y" in input)) return false
|
|
244
|
-
|
|
245
|
-
return typeof input.x === "number" && typeof input.y === "number"
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* Given a longitude value, wraps it to the range of [-180, 180].
|
|
250
|
-
*
|
|
251
|
-
* This is useful when normalizing longitude values.
|
|
252
|
-
*
|
|
253
|
-
* @category Position
|
|
254
|
-
* @param longitude The longitude value to wrap.
|
|
255
|
-
*/
|
|
256
|
-
export function wrapLongitude(longitude: number): number {
|
|
257
|
-
return ((((longitude + 180) % 360) + 360) % 360) - 180
|
|
258
|
-
}
|
|
259
|
-
|
|
260
|
-
/**
|
|
261
|
-
* Given a latitude value, clamps it to the range of [-90, 90].
|
|
262
|
-
*
|
|
263
|
-
* This is useful when normalizing latitude values.
|
|
264
|
-
*
|
|
265
|
-
* @category Position
|
|
266
|
-
* @param value The latitude value to clamp.
|
|
267
|
-
*/
|
|
268
|
-
export function clampLatitude(value: number): number {
|
|
269
|
-
return Math.min(90, Math.max(-90, value))
|
|
270
|
-
}
|
|
271
|
-
|
|
272
|
-
/**
|
|
273
|
-
* Conversion factors for converting between degrees and radians.
|
|
274
|
-
*
|
|
275
|
-
* @category Position
|
|
276
|
-
* @see {@link https://en.wikipedia.org/wiki/Radian Wikipedia: Radian}
|
|
277
|
-
* @see {@link https://en.wikipedia.org/wiki/Degree_(angle) Wikipedia: Degree (angle)}
|
|
278
|
-
*/
|
|
279
|
-
export const ConversionFactor = {
|
|
280
|
-
DegreesToRadians: (Math.PI / 180) as unknown as 0.01745329251,
|
|
281
|
-
RadiansToDegrees: (180 / Math.PI) as unknown as 57.2957795131,
|
|
282
|
-
} as const
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Available conversion units for the radius of the Earth.
|
|
286
|
-
*/
|
|
287
|
-
export type EarthRadiusUnit = "km" | "miles" | "meters"
|
|
288
|
-
|
|
289
|
-
/**
|
|
290
|
-
* Radius of the Earth in various units
|
|
291
|
-
*/
|
|
292
|
-
const RADII = {
|
|
293
|
-
km: 6371,
|
|
294
|
-
miles: 3958.8,
|
|
295
|
-
meters: 6_371_000,
|
|
296
|
-
} as const satisfies Record<EarthRadiusUnit, number>
|
|
297
|
-
|
|
298
|
-
/**
|
|
299
|
-
* Calculate the distance between two points on the Earth's surface.
|
|
300
|
-
*
|
|
301
|
-
* @category Position
|
|
302
|
-
* @param point1 The first point to calculate the distance from.
|
|
303
|
-
* @param point2 The second point to calculate the distance to.
|
|
304
|
-
* @param unit The unit of measurement to return the distance in.
|
|
305
|
-
*
|
|
306
|
-
* @returns The distance between the two points in the specified unit.
|
|
307
|
-
*/
|
|
308
|
-
/**
|
|
309
|
-
* Shared great-circle math (no sentinel handling). `unit` selects the Earth radius.
|
|
310
|
-
*/
|
|
311
|
-
function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
|
|
312
|
-
const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
|
|
313
|
-
const dLon = (lon2 - lon1) * ConversionFactor.DegreesToRadians
|
|
314
|
-
|
|
315
|
-
const a =
|
|
316
|
-
Math.pow(Math.sin(dLat / 2), 2) +
|
|
317
|
-
Math.cos(lat1 * ConversionFactor.DegreesToRadians) *
|
|
318
|
-
Math.cos(lat2 * ConversionFactor.DegreesToRadians) *
|
|
319
|
-
Math.pow(Math.sin(dLon / 2), 2)
|
|
320
|
-
|
|
321
|
-
return RADII[unit] * 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a))
|
|
322
|
-
}
|
|
323
|
-
|
|
324
|
-
export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: EarthRadiusUnit = "km"): number {
|
|
325
|
-
const p1 = GeoPoint.from(point1)
|
|
326
|
-
const p2 = GeoPoint.from(point2)
|
|
327
|
-
|
|
328
|
-
if (!p1 || !p2) return Number.NaN
|
|
329
|
-
|
|
330
|
-
return greatCircle(p1.latitude, p1.longitude, p2.latitude, p2.longitude, unit)
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/**
|
|
334
|
-
* Great-circle distance in kilometres between two lat/lon pairs given as raw scalars. The formula's one true home —
|
|
335
|
-
* every resolver + eval consumer of the `(aLat, aLon, bLat, bLon)` shape imports this instead of re-declaring it.
|
|
336
|
-
*
|
|
337
|
-
* Unlike {@link haversine}, this is pure math with NO Null-Island sentinel: `(0, 0)` is the Gulf of Guinea — a real
|
|
338
|
-
* point, not "missing coordinate". That sentinel convention belongs to the `GeoPointInput` object form (where a 0/0
|
|
339
|
-
* input means "no coordinate"), not to a raw scalar distance.
|
|
340
|
-
*
|
|
341
|
-
* @category Position
|
|
342
|
-
*/
|
|
343
|
-
export function haversineKm(aLat: number, aLon: number, bLat: number, bLon: number): number {
|
|
344
|
-
return greatCircle(aLat, aLon, bLat, bLon, "km")
|
|
345
|
-
}
|
package/sdk/index.ts
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|