@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
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Open Location Code (plus code) decode + nearest-recovery — the coordinate system Google Maps
|
|
7
|
+
* prints on every place card, which makes it a first-class user register in countries with sparse
|
|
8
|
+
* street addressing (the Nicaraguan board rows arrived exactly this way). Pure arithmetic over the
|
|
9
|
+
* published spec (https://github.com/google/open-location-code/blob/main/docs/specification.md);
|
|
10
|
+
* no dependency, no I/O.
|
|
11
|
+
*
|
|
12
|
+
* A FULL code (8 digits, a `+`, then 2–3 more) decodes directly. A SHORT code (2–6 leading digits
|
|
13
|
+
* removed, e.g. `VFQ6+92P`) is only meaningful near a reference point — the removed prefix is
|
|
14
|
+
* recovered from the reference, then the candidate cell is shifted by whole prefix-resolutions if
|
|
15
|
+
* a neighboring cell sits closer (the spec's `recoverNearest`). The reference in an address is the
|
|
16
|
+
* RESOLVED LOCALITY, which is why the geocode wiring recovers after the admin walk.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The 20-character OLC digit set. Deliberately excludes letters that read as words or digits (no A/E/I/L/N/O/S/T…), so
|
|
21
|
+
* a matched token is very unlikely to be ordinary text.
|
|
22
|
+
*/
|
|
23
|
+
const OLC_ALPHABET = "23456789CFGHJMPQRVWX"
|
|
24
|
+
|
|
25
|
+
const OLC_DIGIT_VALUE = new Map<string, number>([...OLC_ALPHABET].map((c, i) => [c, i]))
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Degree width of each pair-position, most significant first: the pair at index i spans `20^(2-i)` degrees. Ten pair
|
|
29
|
+
* digits (five lat/lon pairs) take a cell to 1/400° ≈ 275 m; grid digits refine further.
|
|
30
|
+
*/
|
|
31
|
+
const PAIR_RESOLUTIONS = [20, 1, 1 / 20, 1 / 400, 1 / 8000] as const
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Grid refinement past ten digits: each digit subdivides the cell into 4 columns × 5 rows.
|
|
35
|
+
*/
|
|
36
|
+
const GRID_COLUMNS = 4
|
|
37
|
+
const GRID_ROWS = 5
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* A decoded plus-code cell: the center (the coordinate consumers want) plus the cell's span, from which callers price
|
|
41
|
+
* the claim (`uncertaintyM` ≈ the half-diagonal).
|
|
42
|
+
*/
|
|
43
|
+
export interface DecodedPlusCode {
|
|
44
|
+
lat: number
|
|
45
|
+
lon: number
|
|
46
|
+
latSpanDeg: number
|
|
47
|
+
lonSpanDeg: number
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* A syntactically-valid FULL plus code: exactly 8 digits, `+`, then 2 or 3 digits. (The spec allows padded and longer
|
|
52
|
+
* forms; addresses carry the 10–11 digit register, which is all this reader accepts.)
|
|
53
|
+
*/
|
|
54
|
+
export function isFullPlusCode(token: string): boolean {
|
|
55
|
+
return /^[23456789CFGHJMPQRVWX]{8}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A syntactically-valid SHORT plus code: 2, 4, or 6 leading digits removed — so 6, 4, or 2 digits before the `+`. The
|
|
60
|
+
* 4-before-`+` form (`VFQ6+92P`) is the one Google prints on place cards.
|
|
61
|
+
*/
|
|
62
|
+
export function isShortPlusCode(token: string): boolean {
|
|
63
|
+
return /^[23456789CFGHJMPQRVWX]{2,6}\+[23456789CFGHJMPQRVWX]{2,3}$/i.test(token) && token.indexOf("+") % 2 === 0
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Decode a FULL plus code to its cell. Returns null on anything `isFullPlusCode` rejects.
|
|
68
|
+
*/
|
|
69
|
+
export function decodePlusCode(code: string): DecodedPlusCode | null {
|
|
70
|
+
if (!isFullPlusCode(code)) return null
|
|
71
|
+
const digits = code.toUpperCase().replace("+", "")
|
|
72
|
+
|
|
73
|
+
let latLo = -90
|
|
74
|
+
let lonLo = -180
|
|
75
|
+
let latSpan = 400
|
|
76
|
+
let lonSpan = 400
|
|
77
|
+
|
|
78
|
+
// The first ten digits arrive in (lat, lon) pairs.
|
|
79
|
+
const pairCount = Math.min(digits.length, 10)
|
|
80
|
+
|
|
81
|
+
for (let i = 0; i < pairCount; i += 2) {
|
|
82
|
+
const resolution = PAIR_RESOLUTIONS[i / 2]!
|
|
83
|
+
|
|
84
|
+
latLo += OLC_DIGIT_VALUE.get(digits[i]!)! * resolution
|
|
85
|
+
lonLo += OLC_DIGIT_VALUE.get(digits[i + 1]!)! * resolution
|
|
86
|
+
latSpan = resolution
|
|
87
|
+
lonSpan = resolution
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// Grid refinement: each further digit indexes a 4×5 (columns × rows) subdivision.
|
|
91
|
+
for (let i = 10; i < digits.length; i++) {
|
|
92
|
+
const value = OLC_DIGIT_VALUE.get(digits[i]!)!
|
|
93
|
+
|
|
94
|
+
latSpan /= GRID_ROWS
|
|
95
|
+
lonSpan /= GRID_COLUMNS
|
|
96
|
+
latLo += Math.floor(value / GRID_COLUMNS) * latSpan
|
|
97
|
+
lonLo += (value % GRID_COLUMNS) * lonSpan
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
return {
|
|
101
|
+
lat: latLo + latSpan / 2,
|
|
102
|
+
lon: lonLo + lonSpan / 2,
|
|
103
|
+
latSpanDeg: latSpan,
|
|
104
|
+
lonSpanDeg: lonSpan,
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Encode the pair digits of a coordinate to `length` digits (length ≤ 10, even) — the prefix implementation
|
|
110
|
+
* {@link recoverNearestPlusCode} needs; not a general encoder.
|
|
111
|
+
*/
|
|
112
|
+
function encodePairDigits(lat: number, lon: number, length: number): string {
|
|
113
|
+
let latVal = Math.min(Math.max(lat + 90, 0), 180 - 1e-12)
|
|
114
|
+
let lonVal = lon + 180
|
|
115
|
+
|
|
116
|
+
lonVal -= Math.floor(lonVal / 360) * 360
|
|
117
|
+
|
|
118
|
+
let out = ""
|
|
119
|
+
|
|
120
|
+
for (let i = 0; i < length / 2; i++) {
|
|
121
|
+
const resolution = PAIR_RESOLUTIONS[i]!
|
|
122
|
+
const latDigit = Math.min(Math.floor(latVal / resolution), 19)
|
|
123
|
+
const lonDigit = Math.min(Math.floor(lonVal / resolution), 19)
|
|
124
|
+
|
|
125
|
+
out += OLC_ALPHABET[latDigit]! + OLC_ALPHABET[lonDigit]!
|
|
126
|
+
latVal -= latDigit * resolution
|
|
127
|
+
lonVal -= lonDigit * resolution
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
return out
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Recover a SHORT plus code against a reference coordinate, per the spec's `recoverNearest`: prepend the reference's
|
|
135
|
+
* prefix at the missing precision, then shift the candidate cell by whole prefix-resolutions when a neighboring cell
|
|
136
|
+
* center sits closer to the reference. Returns the decoded nearest cell, or null for an invalid short code.
|
|
137
|
+
*/
|
|
138
|
+
export function recoverNearestPlusCode(shortCode: string, refLat: number, refLon: number): DecodedPlusCode | null {
|
|
139
|
+
if (!isShortPlusCode(shortCode)) return null
|
|
140
|
+
const upper = shortCode.toUpperCase()
|
|
141
|
+
const missing = 8 - upper.indexOf("+")
|
|
142
|
+
const prefix = encodePairDigits(refLat, refLon, missing)
|
|
143
|
+
const candidate = decodePlusCode(prefix + upper)
|
|
144
|
+
|
|
145
|
+
if (!candidate) return null
|
|
146
|
+
|
|
147
|
+
// The prefix pins the cell modulo its own resolution; the nearest bearer of the short code may sit
|
|
148
|
+
// one prefix-cell away (the reference near a cell edge). Shift by whole prefix-resolutions, never
|
|
149
|
+
// past the poles.
|
|
150
|
+
const LAT_MIN = -90
|
|
151
|
+
const LAT_MAX = 90
|
|
152
|
+
const prefixResolution = PAIR_RESOLUTIONS[missing / 2 - 1]!
|
|
153
|
+
const result = { ...candidate }
|
|
154
|
+
|
|
155
|
+
if (refLat + prefixResolution / 2 < result.lat && result.lat - prefixResolution >= LAT_MIN) {
|
|
156
|
+
result.lat -= prefixResolution
|
|
157
|
+
} else if (refLat - prefixResolution / 2 > result.lat && result.lat + prefixResolution <= LAT_MAX) {
|
|
158
|
+
result.lat += prefixResolution
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (refLon + prefixResolution / 2 < result.lon) {
|
|
162
|
+
result.lon -= prefixResolution
|
|
163
|
+
} else if (refLon - prefixResolution / 2 > result.lon) {
|
|
164
|
+
result.lon += prefixResolution
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return result
|
|
168
|
+
}
|
|
@@ -54,14 +54,14 @@
|
|
|
54
54
|
*
|
|
55
55
|
* **Do not use this module for anything that does.** Surveying, cadastral work, or any consumer
|
|
56
56
|
* comparing our coordinates against another OSGB36-derived dataset at sub-10 m tolerance needs
|
|
57
|
-
* OSTN15, which means shipping the shift grid. If that day comes, the
|
|
57
|
+
* OSTN15, which means shipping the shift grid. If that day comes, the swap point is
|
|
58
58
|
* {@link osgb36AiryToWGS84} — swap the Helmert for a grid interpolation and the projection step
|
|
59
59
|
* above it is unchanged.
|
|
60
60
|
*
|
|
61
61
|
* @see https://www.ordnancesurvey.co.uk/documents/resources/guide-coordinate-systems-great-britain.pdf
|
|
62
62
|
*/
|
|
63
63
|
|
|
64
|
-
import type { Coordinates2D } from "
|
|
64
|
+
import type { Coordinates2D } from "#position"
|
|
65
65
|
|
|
66
66
|
/**
|
|
67
67
|
* Semi-major axis of the Airy 1830 ellipsoid, in metres — the figure of the Earth OSGB36 is referenced to.
|
package/lib/position.ts
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
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
|
+
|
|
11
|
+
import { isValidLatitude, isValidLongitude } from "#coordinate-bounds"
|
|
12
|
+
|
|
13
|
+
export { isValidLatitude, isValidLongitude } from "#coordinate-bounds"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Arity of a `[lon, lat]` coordinate tuple.
|
|
17
|
+
*/
|
|
18
|
+
const COORD_PAIR_LENGTH = 2
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Arity of a `[lon, lat, elevation]` coordinate tuple.
|
|
22
|
+
*/
|
|
23
|
+
const COORD_TRIPLE_LENGTH = 3
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* An ordered pair of coordinates in the form of [longitude, latitude].
|
|
27
|
+
*
|
|
28
|
+
* Note that unlike the typical order, GeoJSON coordinates are in the order of [longitude, latitude] to match the order
|
|
29
|
+
* of [x, y] in Cartesian coordinates.
|
|
30
|
+
*
|
|
31
|
+
* @category Position
|
|
32
|
+
* @category GeoJSON
|
|
33
|
+
* @see {@linkcode Coordinates3D} for 3D coordinates.
|
|
34
|
+
*/
|
|
35
|
+
export type Coordinates2D = [
|
|
36
|
+
/**
|
|
37
|
+
* The longitude of the point, i.e. the x-coordinate.
|
|
38
|
+
*
|
|
39
|
+
* @minimum -180
|
|
40
|
+
* @maximum 180
|
|
41
|
+
*/
|
|
42
|
+
longitude: number,
|
|
43
|
+
/**
|
|
44
|
+
* The latitude of the point, i.e. the y-coordinate.
|
|
45
|
+
*
|
|
46
|
+
* @minimum -90
|
|
47
|
+
* @maximum 90
|
|
48
|
+
*/
|
|
49
|
+
latitude: number,
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Orders the given coordinates as [longitude, latitude].
|
|
54
|
+
*
|
|
55
|
+
* This is useful when converting into GeoJSON format.
|
|
56
|
+
*
|
|
57
|
+
* @category GeoJSON
|
|
58
|
+
* @category Position
|
|
59
|
+
*/
|
|
60
|
+
export function orderCoordPairToGeoJSON([latitude, longitude]: [number, number]): Coordinates2D {
|
|
61
|
+
return [longitude, latitude]
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Orders the given coordinates as [latitude, longitude].
|
|
66
|
+
*
|
|
67
|
+
* This is useful when converting into Google Maps format.
|
|
68
|
+
*
|
|
69
|
+
* @category GeoJSON
|
|
70
|
+
* @category Position
|
|
71
|
+
*/
|
|
72
|
+
export function orderGeoJSONToCoordPair([longitude, latitude]: Coordinates2D): [number, number] {
|
|
73
|
+
return [latitude, longitude]
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Infers GeoJSON `[longitude, latitude]` order from coordinate bounds.
|
|
78
|
+
*
|
|
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.
|
|
81
|
+
*/
|
|
82
|
+
export function inferGeoJSONCoordOrder([coordA, coordB]: [number, number]): Coordinates2D {
|
|
83
|
+
const geoJSONIsValid = isValidLongitude(coordA) && isValidLatitude(coordB)
|
|
84
|
+
const latLonIsValid = isValidLatitude(coordA) && isValidLongitude(coordB)
|
|
85
|
+
|
|
86
|
+
if (geoJSONIsValid && !latLonIsValid) {
|
|
87
|
+
return [coordA, coordB]
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (latLonIsValid && !geoJSONIsValid) {
|
|
91
|
+
return [coordB, coordA]
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (geoJSONIsValid && latLonIsValid) {
|
|
95
|
+
throw new RangeError(`Cannot infer coordinate order: [${coordA}, ${coordB}] is valid in both axis orders.`)
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
throw new RangeError(`Invalid coordinate pair: [${coordA}, ${coordB}].`)
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* An ordered triple of coordinates in the form of [longitude, latitude, altitude].
|
|
103
|
+
*
|
|
104
|
+
* @category Position
|
|
105
|
+
* @category GeoJSON
|
|
106
|
+
* @see {@linkcode Coordinates2D} for 2D coordinates.
|
|
107
|
+
*/
|
|
108
|
+
export type Coordinates3D = [
|
|
109
|
+
/**
|
|
110
|
+
* The longitude of the point, i.e. the x-coordinate.
|
|
111
|
+
*
|
|
112
|
+
* @minimum -180
|
|
113
|
+
* @maximum 180
|
|
114
|
+
*/
|
|
115
|
+
longitude: number,
|
|
116
|
+
/**
|
|
117
|
+
* The latitude of the point, i.e. the y-coordinate.
|
|
118
|
+
*
|
|
119
|
+
* @minimum -90
|
|
120
|
+
* @maximum 90
|
|
121
|
+
*/
|
|
122
|
+
latitude: number,
|
|
123
|
+
/**
|
|
124
|
+
* The altitude of the point, i.e. the z-coordinate.
|
|
125
|
+
*/
|
|
126
|
+
altitude: number,
|
|
127
|
+
]
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* A record of internal coordinates, typically used by the US Census.
|
|
131
|
+
*/
|
|
132
|
+
export interface InternalPointCoordinates {
|
|
133
|
+
/**
|
|
134
|
+
* Internal Longitude (X) Coordinates
|
|
135
|
+
*
|
|
136
|
+
* @minimum -180
|
|
137
|
+
* @maximum 180
|
|
138
|
+
*/
|
|
139
|
+
x: number
|
|
140
|
+
/**
|
|
141
|
+
* Internal Latitude (Y) Coordinates
|
|
142
|
+
*
|
|
143
|
+
* @minimum -90
|
|
144
|
+
* @maximum 90
|
|
145
|
+
*/
|
|
146
|
+
y: number
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Type-predicate to determine if the given input is a GeoJSON Point geometry.
|
|
151
|
+
*
|
|
152
|
+
* @category Type Predicates
|
|
153
|
+
* @category GeoJSON
|
|
154
|
+
*/
|
|
155
|
+
export function isCoordPairLiteral(input: unknown): input is [number, number] | [number, number, number] {
|
|
156
|
+
if (!Array.isArray(input)) return false
|
|
157
|
+
|
|
158
|
+
if (input.length !== COORD_PAIR_LENGTH && input.length !== COORD_TRIPLE_LENGTH) return false
|
|
159
|
+
|
|
160
|
+
return input.every((coord) => typeof coord === "number")
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Type-predicate to determine if the given input is a {@linkcode LatLngLiteral} object.
|
|
165
|
+
*
|
|
166
|
+
* @category Position
|
|
167
|
+
* @category Type Predicates
|
|
168
|
+
* @see {@link https://developers.google.com/maps/documentation/javascript/reference/coordinates#LatLngLiteral Google Maps API Documentation}
|
|
169
|
+
*/
|
|
170
|
+
export function isGoogleMapsLatLngLiteral(input: unknown): input is LatLngLiteral {
|
|
171
|
+
if (!input || typeof input !== "object") return false
|
|
172
|
+
|
|
173
|
+
if (!Object.hasOwn(input, "lat") || !Object.hasOwn(input, "lng")) return false
|
|
174
|
+
|
|
175
|
+
return true
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Type-predicate to determine if the given input is a {@linkcode InternalPointCoordinates} object.
|
|
180
|
+
*
|
|
181
|
+
* @category Position
|
|
182
|
+
* @category Type Predicates
|
|
183
|
+
*/
|
|
184
|
+
export function isInterpolatedCoordinates(input: unknown): input is InternalPointCoordinates {
|
|
185
|
+
if (!input || typeof input !== "object") return false
|
|
186
|
+
|
|
187
|
+
if (!("x" in input)) return false
|
|
188
|
+
|
|
189
|
+
if (!("y" in input)) return false
|
|
190
|
+
|
|
191
|
+
return typeof input.x === "number" && typeof input.y === "number"
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Given a longitude value, wraps it to the range of [-180, 180].
|
|
196
|
+
*
|
|
197
|
+
* This is useful when normalizing longitude values.
|
|
198
|
+
*
|
|
199
|
+
* @category Position
|
|
200
|
+
* @param longitude The longitude value to wrap.
|
|
201
|
+
*/
|
|
202
|
+
export function wrapLongitude(longitude: number): number {
|
|
203
|
+
return ((((longitude + 180) % 360) + 360) % 360) - 180
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Given a latitude value, clamps it to the range of [-90, 90].
|
|
208
|
+
*
|
|
209
|
+
* This is useful when normalizing latitude values.
|
|
210
|
+
*
|
|
211
|
+
* @category Position
|
|
212
|
+
* @param value The latitude value to clamp.
|
|
213
|
+
*/
|
|
214
|
+
export function clampLatitude(value: number): number {
|
|
215
|
+
return Math.min(90, Math.max(-90, value))
|
|
216
|
+
}
|
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { runFile } from "@mailwoman/core/process"
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @copyright Sister Software
|
|
5
|
+
* @license AGPL-3.0
|
|
6
|
+
* @author Teffen Ellis, et al.
|
|
7
|
+
*
|
|
8
|
+
* Ask PROJ which datum transformation it would choose, and refuse a ballpark one.
|
|
9
|
+
*
|
|
10
|
+
* PROJ SUBSTITUTES A BALLPARK DATUM SHIFT WHEN THE ACCURATE GRID IS NOT ON DISK, AND IT DOES SO SILENTLY.
|
|
11
|
+
* Measured on the EA flood product: with the OSGB36→WGS84 grid missing, ogr2ogr placed the first
|
|
12
|
+
* feature's first vertex at `1.698151293, 52.648130027`; with `uk_os_OSTN15_NTv2_OSGBtoETRS.tif` present
|
|
13
|
+
* it placed it at `1.698174628, 52.648157259` — 3.4 m apart. Both look like perfectly ordinary WGS84
|
|
14
|
+
* coordinates, both pass a bounding-box check, and the whole layer is offset. It surfaced as eight
|
|
15
|
+
* disagreements out of 59 against the authority's own OGC service, every one a point that fell into a
|
|
16
|
+
* NEIGHBOURING sliver.
|
|
17
|
+
*
|
|
18
|
+
* `--config PROJ_NETWORK ON` does not reach PROJ through GDAL 3.8, and `PROJ_ONLY_BEST=ON` was observed
|
|
19
|
+
* not to refuse, so neither is a usable guard. What is usable is asking PROJ what it would do: `projinfo`
|
|
20
|
+
* names the best candidate operation and says when a grid is missing.
|
|
21
|
+
*
|
|
22
|
+
* IT IS RUN EVEN WHERE NO TRANSFORMATION IS NEEDED, AND THAT IS THE POINT. A source already in EPSG:4326
|
|
23
|
+
* gets `Null geographic offset from WGS 84 to WGS 84, 0 m, World.` — trivially usable, and the check costs
|
|
24
|
+
* one process. Skipping it on the reasoning that a source needs no shift makes the guard fire on the day
|
|
25
|
+
* a source arrives that does, which is the day nobody is looking.
|
|
26
|
+
*
|
|
27
|
+
* SHARED BY EVERY VECTOR INGEST, because the failure is a property of PROJ rather than of any product. The
|
|
28
|
+
* parse is split from the spawn so it can be pinned against captured output: the two states it
|
|
29
|
+
* distinguishes were observed from the same command on the same machine, before and after the grid was
|
|
30
|
+
* installed, and they are the difference between a metre-accurate layer and a 3 m-offset one.
|
|
31
|
+
*/
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* What PROJ would do, and whether it can actually do it.
|
|
35
|
+
*/
|
|
36
|
+
export interface DatumTransformationVerdict {
|
|
37
|
+
/**
|
|
38
|
+
* The candidate operation line PROJ named, verbatim. Absent when it named none.
|
|
39
|
+
*/
|
|
40
|
+
best?: string
|
|
41
|
+
usable: boolean
|
|
42
|
+
reason: string
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Read `projinfo --summary` output: which operation PROJ would choose, and whether it can actually run.
|
|
47
|
+
*/
|
|
48
|
+
export function assessDatumTransformation(summary: string): DatumTransformationVerdict {
|
|
49
|
+
// The first line naming a candidate operation is the one PROJ will choose. Everything before it is a header, and the
|
|
50
|
+
// `Note:` line about `--spatial-test` is not a candidate.
|
|
51
|
+
//
|
|
52
|
+
// `projinfo --summary` prints a header plus one line per candidate operation: two to nine lines, bounded by PROJ's own
|
|
53
|
+
// candidate enumeration rather than by input size. Reaching for a streaming reader would put a `spliterator` dependency
|
|
54
|
+
// on this package for a fixed handful of lines.
|
|
55
|
+
// oxlint-disable-next-line mailwoman/prefer-spliterator -- bounded output, see above
|
|
56
|
+
const lines = summary.split("\n")
|
|
57
|
+
|
|
58
|
+
const best = lines
|
|
59
|
+
.map((line) => line.trim())
|
|
60
|
+
.find((line) => line.includes(", ") && !line.startsWith("Note:") && !line.startsWith("Candidate operations"))
|
|
61
|
+
|
|
62
|
+
if (!best) return { usable: false, reason: "projinfo named no candidate operation" }
|
|
63
|
+
|
|
64
|
+
if (best.includes("grid missing")) return { best, usable: false, reason: "its grid is not installed" }
|
|
65
|
+
|
|
66
|
+
if (best.toLowerCase().includes("ballpark")) return { best, usable: false, reason: "it is a ballpark offset" }
|
|
67
|
+
|
|
68
|
+
return { best, usable: true, reason: "the best operation is available" }
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* The target every layer ingest reprojects to — H3 takes WGS84 latitude and longitude.
|
|
73
|
+
*/
|
|
74
|
+
export const WGS84_EPSG = 4326
|
|
75
|
+
|
|
76
|
+
export interface AssertDatumTransformationOptions {
|
|
77
|
+
/**
|
|
78
|
+
* Names the caller in the refusal, so a build log says which ingest stopped.
|
|
79
|
+
*/
|
|
80
|
+
context: string
|
|
81
|
+
targetEPSG?: number
|
|
82
|
+
/**
|
|
83
|
+
* The area-of-use to name in the `projsync` remedy the message prints.
|
|
84
|
+
*/
|
|
85
|
+
areaOfUse?: string
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Refuse an ingest whose best available datum transformation is a ballpark one, or is missing its grid.
|
|
90
|
+
*
|
|
91
|
+
* @throws {Error} When PROJ names no candidate, would use a ballpark offset, or would use an operation whose grid is
|
|
92
|
+
* not installed.
|
|
93
|
+
*/
|
|
94
|
+
export async function assertDatumTransformationAvailable(
|
|
95
|
+
sourceEPSG: number,
|
|
96
|
+
options: AssertDatumTransformationOptions
|
|
97
|
+
): Promise<DatumTransformationVerdict> {
|
|
98
|
+
const targetEPSG = options.targetEPSG ?? WGS84_EPSG
|
|
99
|
+
|
|
100
|
+
const { stdout } = await runFile("projinfo", ["-s", `EPSG:${sourceEPSG}`, "-t", `EPSG:${targetEPSG}`, "--summary"])
|
|
101
|
+
|
|
102
|
+
const verdict = assessDatumTransformation(stdout)
|
|
103
|
+
|
|
104
|
+
if (verdict.usable) return verdict
|
|
105
|
+
|
|
106
|
+
const remedy = options.areaOfUse
|
|
107
|
+
? `Install the grid with \`projsync --area-of-use "${options.areaOfUse}"\` and re-run.`
|
|
108
|
+
: "Install the grid with `projsync` for the source's area of use and re-run."
|
|
109
|
+
|
|
110
|
+
throw new Error(
|
|
111
|
+
`${options.context}: the best EPSG:${sourceEPSG} → EPSG:${targetEPSG} transformation is unusable — ${verdict.reason} ` +
|
|
112
|
+
`(${verdict.best ?? "projinfo named no candidate"}). PROJ falls back to a ballpark datum shift, which is metres ` +
|
|
113
|
+
`wrong and looks exactly like a correct answer. ${remedy}`
|
|
114
|
+
)
|
|
115
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
* @file One ogr2ogr harness for every streaming extraction.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { tryParsingJSON } from "@mailwoman/core/json"
|
|
9
|
+
import { spawnProcess } from "@mailwoman/core/process"
|
|
10
|
+
import { TextSpliterator } from "spliterator"
|
|
11
|
+
|
|
12
|
+
const RECORD_SEPARATOR = 0x1e
|
|
13
|
+
const STDERR_TAIL_CHARS = 800
|
|
14
|
+
|
|
15
|
+
export interface OGRProcess {
|
|
16
|
+
stdout: NodeJS.ReadableStream
|
|
17
|
+
/**
|
|
18
|
+
* Resolves on a clean exit; rejects with the exit code and the stderr tail otherwise. A truncated stream reads as a
|
|
19
|
+
* short but well-formed feature list, which is exactly the partial result that must throw rather than be reported as
|
|
20
|
+
* a smaller extract — so consume the stream fully, then await this.
|
|
21
|
+
*/
|
|
22
|
+
settled: Promise<void>
|
|
23
|
+
/**
|
|
24
|
+
* Stop the child if it is still running — the `finally` companion for a consumer that throws mid-stream.
|
|
25
|
+
*/
|
|
26
|
+
kill: () => void
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Spawn `ogr2ogr` with stderr accumulated for the failure message. The caller owns the stdout format (GeoJSONSeq, CSV,
|
|
31
|
+
* …) and its parsing; {@link ogr2ogrGeoJSONSeq} is the GeoJSONSeq reading over this.
|
|
32
|
+
*/
|
|
33
|
+
export function spawnOGR2OGR(args: readonly string[], context: string): OGRProcess {
|
|
34
|
+
const child = spawnProcess("ogr2ogr", [...args], { stdio: ["ignore", "pipe", "pipe"] })
|
|
35
|
+
let stderr = ""
|
|
36
|
+
|
|
37
|
+
child.stderr.setEncoding("utf8")
|
|
38
|
+
|
|
39
|
+
child.stderr.on("data", (chunk: string) => {
|
|
40
|
+
stderr += chunk
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
const settled = new Promise<void>((resolve, reject) => {
|
|
44
|
+
child.on("error", reject)
|
|
45
|
+
|
|
46
|
+
child.on("close", (code) => {
|
|
47
|
+
if (code === 0) {
|
|
48
|
+
resolve()
|
|
49
|
+
} else {
|
|
50
|
+
reject(new Error(`${context}: ogr2ogr exited ${code}: ${stderr.slice(-STDERR_TAIL_CHARS)}`))
|
|
51
|
+
}
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
// A failed spawn rejects `settled` BEFORE any consumer awaits it — the consumer is still draining the stream on a
|
|
56
|
+
// later tick, and a consumer that abandons the stream never awaits it at all — so an unobserved rejection would trip
|
|
57
|
+
// the process's unhandled-rejection hook. Observed at birth instead; every consumer still awaits the real verdict.
|
|
58
|
+
settled.catch(() => undefined)
|
|
59
|
+
|
|
60
|
+
return {
|
|
61
|
+
stdout: child.stdout,
|
|
62
|
+
settled,
|
|
63
|
+
kill: () => {
|
|
64
|
+
if (child.exitCode === null && child.signalCode === null) {
|
|
65
|
+
child.kill()
|
|
66
|
+
}
|
|
67
|
+
},
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Stream a GeoJSONSeq extraction as parsed features.
|
|
73
|
+
*
|
|
74
|
+
* Strips the RFC-8142 record separator (U+001E) GDAL MAY prefix records with — `.trim()` does not remove it (not
|
|
75
|
+
* whitespace), so an RS-framed record would fail to parse and be silently skipped, all of them, and an empty extract
|
|
76
|
+
* would read as a real absence. A malformed record is tolerated (skipped) rather than thrown; a non-zero exit throws
|
|
77
|
+
* after the stream drains.
|
|
78
|
+
*/
|
|
79
|
+
export async function* ogr2ogrGeoJSONSeq<T>(args: readonly string[], context: string): AsyncGenerator<T> {
|
|
80
|
+
const proc = spawnOGR2OGR(args, context)
|
|
81
|
+
|
|
82
|
+
try {
|
|
83
|
+
for await (const raw of TextSpliterator.fromAsync(proc.stdout)) {
|
|
84
|
+
const line = (raw.charCodeAt(0) === RECORD_SEPARATOR ? raw.slice(1) : raw).trim()
|
|
85
|
+
|
|
86
|
+
if (!line) continue
|
|
87
|
+
|
|
88
|
+
const feature = tryParsingJSON<T>(line)
|
|
89
|
+
|
|
90
|
+
if (feature !== null) {
|
|
91
|
+
yield feature
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
} finally {
|
|
95
|
+
proc.kill()
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
await proc.settled
|
|
99
|
+
}
|