@mailwoman/spatial 8.0.0 → 8.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.
Files changed (55) hide show
  1. package/bbox.ts +14 -3
  2. package/coordinate-formats.ts +58 -21
  3. package/feature.ts +1 -1
  4. package/geometries/point.ts +7 -5
  5. package/geometries/polygon.ts +5 -0
  6. package/h3/cell.ts +72 -0
  7. package/h3/index.ts +2 -60
  8. package/index.ts +1 -0
  9. package/out/bbox.d.ts.map +1 -1
  10. package/out/bbox.js +11 -3
  11. package/out/bbox.js.map +1 -1
  12. package/out/coordinate-formats.d.ts +18 -6
  13. package/out/coordinate-formats.d.ts.map +1 -1
  14. package/out/coordinate-formats.js +47 -21
  15. package/out/coordinate-formats.js.map +1 -1
  16. package/out/feature.d.ts +1 -1
  17. package/out/feature.d.ts.map +1 -1
  18. package/out/feature.js +1 -2
  19. package/out/feature.js.map +1 -1
  20. package/out/geometries/point.d.ts +2 -2
  21. package/out/geometries/point.d.ts.map +1 -1
  22. package/out/geometries/point.js +4 -2
  23. package/out/geometries/point.js.map +1 -1
  24. package/out/geometries/polygon.d.ts +4 -0
  25. package/out/geometries/polygon.d.ts.map +1 -1
  26. package/out/geometries/polygon.js +4 -0
  27. package/out/geometries/polygon.js.map +1 -1
  28. package/out/h3/cell.d.ts +37 -0
  29. package/out/h3/cell.d.ts.map +1 -0
  30. package/out/h3/cell.js +44 -0
  31. package/out/h3/cell.js.map +1 -0
  32. package/out/h3/index.d.ts +2 -26
  33. package/out/h3/index.d.ts.map +1 -1
  34. package/out/h3/index.js +2 -33
  35. package/out/h3/index.js.map +1 -1
  36. package/out/index.d.ts +1 -0
  37. package/out/index.d.ts.map +1 -1
  38. package/out/index.js +1 -0
  39. package/out/index.js.map +1 -1
  40. package/out/polyline.d.ts +28 -0
  41. package/out/polyline.d.ts.map +1 -0
  42. package/out/polyline.js +58 -0
  43. package/out/polyline.js.map +1 -0
  44. package/out/position.d.ts +3 -3
  45. package/out/position.d.ts.map +1 -1
  46. package/out/position.js +24 -7
  47. package/out/position.js.map +1 -1
  48. package/out/sdk/well-known-text.d.ts.map +1 -1
  49. package/out/sdk/well-known-text.js.map +1 -1
  50. package/out/tsconfig.test.tsbuildinfo +1 -0
  51. package/out/tsconfig.tsbuildinfo +1 -1
  52. package/package.json +3 -3
  53. package/polyline.ts +73 -0
  54. package/position.ts +30 -8
  55. package/sdk/well-known-text.ts +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mailwoman/spatial",
3
- "version": "8.0.0",
3
+ "version": "8.2.0",
4
4
  "description": "Spatial analysis, geocoding, and other geo-related utilities.",
5
5
  "keywords": [
6
6
  "geo",
@@ -57,14 +57,14 @@
57
57
  }
58
58
  },
59
59
  "dependencies": {
60
- "@mailwoman/core": "8.0.0",
60
+ "@mailwoman/core": "8.2.0",
61
61
  "geo-coordinates-parser": "^1.7.4",
62
62
  "h3-js": "^4.5.0",
63
63
  "wkx": "^0.5.0"
64
64
  },
65
65
  "devDependencies": {
66
66
  "@googlemaps/google-maps-services-js": "^3.4.2",
67
- "@mailwoman/annotations": "8.0.0",
67
+ "@mailwoman/annotations": "8.2.0",
68
68
  "@types/google.maps": "^3.65.3",
69
69
  "type-fest": "^5.8.0"
70
70
  },
package/polyline.ts ADDED
@@ -0,0 +1,73 @@
1
+ /**
2
+ * @copyright Sister Software
3
+ * @license AGPL-3.0
4
+ * @author Teffen Ellis, et al.
5
+ *
6
+ * Position along a polyline, by arc length.
7
+ *
8
+ * This is what house-number interpolation stands on: given a street segment's geometry and how far
9
+ * along its house-number range an address sits, it answers where that is on the ground. It lived in
10
+ * two byte-identical copies — `resolver-wof-sqlite/interpolation.ts` and the browser twin in
11
+ * `docs/src/shared/httpvfs-street.ts` — under a header telling both to be kept in lockstep by hand.
12
+ * The geometry never differed between them; only the I/O around it did.
13
+ */
14
+
15
+ import { haversineKm } from "./position.ts"
16
+
17
+ /**
18
+ * Clamp a fraction into `[0, 1]`.
19
+ *
20
+ * NaN passes through, which is deliberate: an interpolation whose inputs were not numbers should produce NaN
21
+ * coordinates a caller can detect, not silently snap to a segment's start.
22
+ */
23
+ export function clampFraction(t: number): number {
24
+ return t < 0 ? 0 : Math.min(1, t)
25
+ }
26
+
27
+ /**
28
+ * The point at fraction `t` of a polyline's total arc length (haversine), plus that total in km.
29
+ *
30
+ * `t` is assumed clamped to `[0, 1]`. A zero-length polyline yields its first vertex and a length of 0, so a degenerate
31
+ * segment still produces a usable coordinate rather than NaN.
32
+ */
33
+ export function pointAlong(
34
+ polyline: readonly [number, number][],
35
+ t: number
36
+ ): [lon: number, lat: number, lengthKm: number] {
37
+ const legs: number[] = []
38
+ let total = 0
39
+
40
+ for (let i = 1; i < polyline.length; i++) {
41
+ const [aLon, aLat] = polyline[i - 1]!
42
+ const [bLon, bLat] = polyline[i]!
43
+ const d = haversineKm(aLat, aLon, bLat, bLon)
44
+ legs.push(d)
45
+ total += d
46
+ }
47
+
48
+ if (total === 0) {
49
+ const [lon, lat] = polyline[0]!
50
+
51
+ return [lon, lat, 0]
52
+ }
53
+
54
+ let remaining = t * total
55
+
56
+ for (let i = 0; i < legs.length; i++) {
57
+ const leg = legs[i]!
58
+
59
+ if (remaining <= leg || i === legs.length - 1) {
60
+ const f = leg === 0 ? 0 : clampFraction(remaining / leg)
61
+ const [aLon, aLat] = polyline[i]!
62
+ const [bLon, bLat] = polyline[i + 1]!
63
+
64
+ return [aLon + (bLon - aLon) * f, aLat + (bLat - aLat) * f, total]
65
+ }
66
+
67
+ remaining -= leg
68
+ }
69
+
70
+ const [lon, lat] = polyline.at(-1)!
71
+
72
+ return [lon, lat, total]
73
+ }
package/position.ts CHANGED
@@ -6,9 +6,29 @@
6
6
  * This file contains types and utilities for working with geographic positions.
7
7
  */
8
8
 
9
- import { type LatLngLiteral } from "@googlemaps/google-maps-services-js"
9
+ import type { LatLngLiteral } from "@googlemaps/google-maps-services-js"
10
10
  import { GeoPoint, type GeoPointInput } from "@mailwoman/spatial"
11
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
+ * Arity of a `[lon, lat]` coordinate tuple.
24
+ */
25
+ const COORD_PAIR_LENGTH = 2
26
+
27
+ /**
28
+ * Arity of a `[lon, lat, elevation]` coordinate tuple.
29
+ */
30
+ const COORD_TRIPLE_LENGTH = 3
31
+
12
32
  /**
13
33
  * An ordered pair of coordinates in the form of [longitude, latitude].
14
34
  *
@@ -71,8 +91,8 @@ export function orderGeoJSONToCoordPair([longitude, latitude]: Coordinates2D): [
71
91
  */
72
92
  export function inferGeoJSONCoordOrder([coordA, coordB]: [number, number]): Coordinates2D {
73
93
  // Latitude values typically range from -90 to 90
74
- const isCoordALat = coordA >= -90 && coordA <= 90
75
- const isCoordBLat = coordB >= -90 && coordB <= 90
94
+ const isCoordALat = coordA >= LATITUDE_MIN && coordA <= LATITUDE_MAX
95
+ const isCoordBLat = coordB >= LATITUDE_MIN && coordB <= LATITUDE_MAX
76
96
 
77
97
  if (isCoordALat && !isCoordBLat) {
78
98
  // coordA is latitude, coordB is longitude
@@ -120,7 +140,7 @@ export type Coordinates3D = [
120
140
  /**
121
141
  * A record of internal coordinates, typically used by the US Census.
122
142
  */
123
- export type InternalPointCoordinates = {
143
+ export interface InternalPointCoordinates {
124
144
  /**
125
145
  * Internal Longitude (X) Coordinates
126
146
  *
@@ -146,7 +166,7 @@ export type InternalPointCoordinates = {
146
166
  export function isCoordPairLiteral(input: unknown): input is [number, number] | [number, number, number] {
147
167
  if (!Array.isArray(input)) return false
148
168
 
149
- if (input.length !== 2 && input.length !== 3) return false
169
+ if (input.length !== COORD_PAIR_LENGTH && input.length !== COORD_TRIPLE_LENGTH) return false
150
170
 
151
171
  return input.every((coord) => typeof coord === "number")
152
172
  }
@@ -229,7 +249,7 @@ export type EarthRadiusUnit = "km" | "miles" | "meters"
229
249
  const RADII = {
230
250
  km: 6371,
231
251
  miles: 3958.8,
232
- meters: 6371000,
252
+ meters: 6_371_000,
233
253
  } as const satisfies Record<EarthRadiusUnit, number>
234
254
 
235
255
  /**
@@ -242,7 +262,9 @@ const RADII = {
242
262
  *
243
263
  * @returns The distance between the two points in the specified unit.
244
264
  */
245
- /** Shared great-circle math (no sentinel handling). `unit` selects the Earth radius. */
265
+ /**
266
+ * Shared great-circle math (no sentinel handling). `unit` selects the Earth radius.
267
+ */
246
268
  function greatCircle(lat1: number, lon1: number, lat2: number, lon2: number, unit: EarthRadiusUnit): number {
247
269
  const dLat = (lat2 - lat1) * ConversionFactor.DegreesToRadians
248
270
  const dLon = (lon2 - lon1) * ConversionFactor.DegreesToRadians
@@ -260,7 +282,7 @@ export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: Ea
260
282
  const p1 = GeoPoint.from(point1)
261
283
  const p2 = GeoPoint.from(point2)
262
284
 
263
- if (!p1 || !p2) return NaN
285
+ if (!p1 || !p2) return Number.NaN
264
286
 
265
287
  return greatCircle(p1.latitude, p1.longitude, p2.latitude, p2.longitude, unit)
266
288
  }
@@ -27,6 +27,7 @@ export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(wellKnownText: s
27
27
  * @category Geo
28
28
  */
29
29
  export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(input: Buffer | string): T
30
+
30
31
  export function wellKnownGeometryToGeoJSON<T = GeometryLiteral>(input: Buffer | string) {
31
32
  return wkx.Geometry.parse(input).toGeoJSON() as T
32
33
  }