@mailwoman/spatial 9.1.0 → 9.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/bbox.ts +51 -0
- package/coordinate-bounds.ts +41 -0
- package/coordinate-formats.ts +13 -2
- package/distance.ts +84 -0
- package/geometries/polygon.ts +87 -7
- package/index.ts +3 -0
- package/open-location-code.ts +168 -0
- package/out/bbox.d.ts +21 -0
- package/out/bbox.d.ts.map +1 -1
- package/out/bbox.js +31 -0
- package/out/bbox.js.map +1 -1
- package/out/coordinate-bounds.d.ts +32 -0
- package/out/coordinate-bounds.d.ts.map +1 -0
- package/out/coordinate-bounds.js +36 -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 +12 -2
- package/out/coordinate-formats.js.map +1 -1
- package/out/distance.d.ts +46 -0
- package/out/distance.d.ts.map +1 -0
- package/out/distance.js +70 -0
- package/out/distance.js.map +1 -0
- package/out/geometries/polygon.d.ts +53 -0
- package/out/geometries/polygon.d.ts.map +1 -1
- package/out/geometries/polygon.js +40 -7
- package/out/geometries/polygon.js.map +1 -1
- package/out/index.d.ts +3 -0
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -0
- package/out/index.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/polyline.js +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/sdk/well-known-text.d.ts +1 -1
- package/out/sdk/well-known-text.d.ts.map +1 -1
- package/out/sdk/well-known-text.js.map +1 -1
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/out/tsconfig.tsbuildinfo +1 -1
- package/package.json +66 -6
- package/polyline.ts +1 -1
- package/position.ts +15 -144
- package/sdk/well-known-text.ts +2 -1
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mailwoman/spatial",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.2.0",
|
|
4
4
|
"description": "Spatial analysis, geocoding, and other geo-related utilities.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"geo",
|
|
@@ -16,7 +16,7 @@
|
|
|
16
16
|
"repository": {
|
|
17
17
|
"type": "git",
|
|
18
18
|
"url": "https://github.com/sister-software/mailwoman.git",
|
|
19
|
-
"directory": "spatial"
|
|
19
|
+
"directory": "packages/spatial"
|
|
20
20
|
},
|
|
21
21
|
"files": [
|
|
22
22
|
"out/",
|
|
@@ -28,11 +28,22 @@
|
|
|
28
28
|
"!*.test.tsx",
|
|
29
29
|
"!**/*.test.ts",
|
|
30
30
|
"!**/*.test.tsx",
|
|
31
|
-
"!__snapshots__/"
|
|
31
|
+
"!__snapshots__/",
|
|
32
|
+
"!test/**"
|
|
32
33
|
],
|
|
33
34
|
"type": "module",
|
|
35
|
+
"imports": {
|
|
36
|
+
"#index": {
|
|
37
|
+
"types": "./out/index.d.ts",
|
|
38
|
+
"default": "./out/index.js"
|
|
39
|
+
}
|
|
40
|
+
},
|
|
34
41
|
"exports": {
|
|
35
42
|
"./package.json": "./package.json",
|
|
43
|
+
"./coordinate-bounds": {
|
|
44
|
+
"types": "./out/coordinate-bounds.d.ts",
|
|
45
|
+
"default": "./out/coordinate-bounds.js"
|
|
46
|
+
},
|
|
36
47
|
"./sdk": {
|
|
37
48
|
"types": "./out/sdk/index.d.ts",
|
|
38
49
|
"default": "./out/sdk/index.js"
|
|
@@ -40,12 +51,36 @@
|
|
|
40
51
|
".": {
|
|
41
52
|
"types": "./out/index.d.ts",
|
|
42
53
|
"default": "./out/index.js"
|
|
54
|
+
},
|
|
55
|
+
"./coordinate-formats": {
|
|
56
|
+
"types": "./out/coordinate-formats.d.ts",
|
|
57
|
+
"default": "./out/coordinate-formats.js"
|
|
58
|
+
},
|
|
59
|
+
"./open-location-code": {
|
|
60
|
+
"types": "./out/open-location-code.d.ts",
|
|
61
|
+
"default": "./out/open-location-code.js"
|
|
62
|
+
},
|
|
63
|
+
"./geometries/polygon": {
|
|
64
|
+
"types": "./out/geometries/polygon.d.ts",
|
|
65
|
+
"default": "./out/geometries/polygon.js"
|
|
66
|
+
},
|
|
67
|
+
"./h3/cell": {
|
|
68
|
+
"types": "./out/h3/cell.d.ts",
|
|
69
|
+
"default": "./out/h3/cell.js"
|
|
70
|
+
},
|
|
71
|
+
"./sdk/well-known-text": {
|
|
72
|
+
"types": "./out/sdk/well-known-text.d.ts",
|
|
73
|
+
"default": "./out/sdk/well-known-text.js"
|
|
43
74
|
}
|
|
44
75
|
},
|
|
45
76
|
"publishConfig": {
|
|
46
77
|
"access": "public",
|
|
47
78
|
"exports": {
|
|
48
79
|
"./package.json": "./package.json",
|
|
80
|
+
"./coordinate-bounds": {
|
|
81
|
+
"types": "./out/coordinate-bounds.d.ts",
|
|
82
|
+
"default": "./out/coordinate-bounds.js"
|
|
83
|
+
},
|
|
49
84
|
"./sdk": {
|
|
50
85
|
"types": "./out/sdk/index.d.ts",
|
|
51
86
|
"default": "./out/sdk/index.js"
|
|
@@ -53,19 +88,44 @@
|
|
|
53
88
|
".": {
|
|
54
89
|
"types": "./out/index.d.ts",
|
|
55
90
|
"default": "./out/index.js"
|
|
91
|
+
},
|
|
92
|
+
"./coordinate-formats": {
|
|
93
|
+
"types": "./out/coordinate-formats.d.ts",
|
|
94
|
+
"default": "./out/coordinate-formats.js"
|
|
95
|
+
},
|
|
96
|
+
"./open-location-code": {
|
|
97
|
+
"types": "./out/open-location-code.d.ts",
|
|
98
|
+
"default": "./out/open-location-code.js"
|
|
99
|
+
},
|
|
100
|
+
"./geometries/polygon": {
|
|
101
|
+
"types": "./out/geometries/polygon.d.ts",
|
|
102
|
+
"default": "./out/geometries/polygon.js"
|
|
103
|
+
},
|
|
104
|
+
"./h3/cell": {
|
|
105
|
+
"types": "./out/h3/cell.d.ts",
|
|
106
|
+
"default": "./out/h3/cell.js"
|
|
107
|
+
},
|
|
108
|
+
"./sdk/well-known-text": {
|
|
109
|
+
"types": "./out/sdk/well-known-text.d.ts",
|
|
110
|
+
"default": "./out/sdk/well-known-text.js"
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
"imports": {
|
|
114
|
+
"#index": {
|
|
115
|
+
"types": "./out/index.d.ts",
|
|
116
|
+
"default": "./out/index.js"
|
|
56
117
|
}
|
|
57
118
|
}
|
|
58
119
|
},
|
|
59
120
|
"dependencies": {
|
|
60
|
-
"@mailwoman/core": "9.
|
|
121
|
+
"@mailwoman/core": "9.2.0",
|
|
61
122
|
"geo-coordinates-parser": "^1.7.4",
|
|
62
123
|
"h3-js": "^4.5.0",
|
|
63
124
|
"wkx": "^0.5.0"
|
|
64
125
|
},
|
|
65
126
|
"devDependencies": {
|
|
66
127
|
"@googlemaps/google-maps-services-js": "^3.4.2",
|
|
67
|
-
"@mailwoman/annotations": "9.
|
|
68
|
-
"@types/google.maps": "^3.65.3",
|
|
128
|
+
"@mailwoman/annotations": "9.2.0",
|
|
69
129
|
"type-fest": "^5.8.0"
|
|
70
130
|
},
|
|
71
131
|
"optionalDependencies": {
|
package/polyline.ts
CHANGED
package/position.ts
CHANGED
|
@@ -7,50 +7,10 @@
|
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
9
|
import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
|
|
10
|
-
import { GeoPoint, type GeoPointInput } from "@mailwoman/spatial"
|
|
11
10
|
|
|
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
|
-
}
|
|
11
|
+
import { isValidLatitude, isValidLongitude } from "./coordinate-bounds.ts"
|
|
43
12
|
|
|
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
|
-
}
|
|
13
|
+
export { isValidLatitude, isValidLongitude } from "./coordinate-bounds.ts"
|
|
54
14
|
|
|
55
15
|
/**
|
|
56
16
|
* Arity of a `[lon, lat]` coordinate tuple.
|
|
@@ -114,42 +74,28 @@ export function orderGeoJSONToCoordPair([longitude, latitude]: Coordinates2D): [
|
|
|
114
74
|
}
|
|
115
75
|
|
|
116
76
|
/**
|
|
117
|
-
*
|
|
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.
|
|
77
|
+
* Infers GeoJSON `[longitude, latitude]` order from coordinate bounds.
|
|
130
78
|
*
|
|
131
|
-
* @
|
|
132
|
-
* @
|
|
133
|
-
* @category Position
|
|
79
|
+
* @returns The coordinates ordered as [longitude, latitude] when exactly one possible ordering is valid.
|
|
80
|
+
* @throws {RangeError} When axis order is ambiguous or coordinates are invalid.
|
|
134
81
|
*/
|
|
135
82
|
export function inferGeoJSONCoordOrder([coordA, coordB]: [number, number]): Coordinates2D {
|
|
136
|
-
|
|
137
|
-
const
|
|
138
|
-
|
|
83
|
+
const geoJSONIsValid = isValidLongitude(coordA) && isValidLatitude(coordB)
|
|
84
|
+
const latLonIsValid = isValidLatitude(coordA) && isValidLongitude(coordB)
|
|
85
|
+
|
|
86
|
+
if (geoJSONIsValid && !latLonIsValid) {
|
|
87
|
+
return [coordA, coordB]
|
|
88
|
+
}
|
|
139
89
|
|
|
140
|
-
if (
|
|
141
|
-
// coordA is latitude, coordB is longitude
|
|
90
|
+
if (latLonIsValid && !geoJSONIsValid) {
|
|
142
91
|
return [coordB, coordA]
|
|
143
92
|
}
|
|
144
93
|
|
|
145
|
-
if (
|
|
146
|
-
|
|
147
|
-
return [coordA, coordB]
|
|
94
|
+
if (geoJSONIsValid && latLonIsValid) {
|
|
95
|
+
throw new RangeError(`Cannot infer coordinate order: [${coordA}, ${coordB}] is valid in both axis orders.`)
|
|
148
96
|
}
|
|
149
97
|
|
|
150
|
-
|
|
151
|
-
// assume coordA is longitude and coordB is latitude as default.
|
|
152
|
-
return [coordA, coordB]
|
|
98
|
+
throw new RangeError(`Invalid coordinate pair: [${coordA}, ${coordB}].`)
|
|
153
99
|
}
|
|
154
100
|
|
|
155
101
|
/**
|
|
@@ -268,78 +214,3 @@ export function wrapLongitude(longitude: number): number {
|
|
|
268
214
|
export function clampLatitude(value: number): number {
|
|
269
215
|
return Math.min(90, Math.max(-90, value))
|
|
270
216
|
}
|
|
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/well-known-text.ts
CHANGED
|
@@ -6,9 +6,10 @@
|
|
|
6
6
|
* Provides functions for parsing and converting extended Well-Known text and binary data.
|
|
7
7
|
*/
|
|
8
8
|
|
|
9
|
-
import type { GeometryCollection, GeometryLiteral } from "@mailwoman/spatial"
|
|
10
9
|
import wkx from "wkx"
|
|
11
10
|
|
|
11
|
+
import type { GeometryCollection, GeometryLiteral } from "#index"
|
|
12
|
+
|
|
12
13
|
/**
|
|
13
14
|
* Given an extended Well-Known Binary (EWKB) buffer, parse it into a GeoJSON geometry object.
|
|
14
15
|
*
|