@mailwoman/spatial 10.3.0 → 10.5.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/README.md +3 -3
- package/lib/coordinate/formats.ts +1 -1
- package/lib/distance.ts +32 -2
- package/lib/gb/codepoint.ts +210 -0
- package/lib/gb/os-downloads.ts +46 -0
- package/lib/geometries/point.ts +1 -1
- package/lib/h3/coverage.ts +1 -1
- package/lib/h3/polygon-cells.ts +2 -2
- package/lib/projection/transform.ts +1 -1
- package/out/coordinate/formats.d.ts +1 -1
- package/out/coordinate/formats.js +1 -1
- package/out/distance.d.ts +13 -1
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +23 -2
- package/out/distance.js.map +1 -1
- package/out/gb/codepoint.d.ts +126 -0
- package/out/gb/codepoint.d.ts.map +1 -0
- package/out/gb/codepoint.js +117 -0
- package/out/gb/codepoint.js.map +1 -0
- package/out/gb/os-downloads.d.ts +45 -0
- package/out/gb/os-downloads.d.ts.map +1 -0
- package/out/gb/os-downloads.js +15 -0
- package/out/gb/os-downloads.js.map +1 -0
- package/out/geometries/point.d.ts +1 -1
- package/out/geometries/point.js +1 -1
- package/out/h3/coverage.d.ts +1 -1
- package/out/h3/coverage.js +1 -1
- package/out/h3/polygon-cells.d.ts +2 -2
- package/out/h3/polygon-cells.js +2 -2
- package/out/projection/transform.d.ts +1 -1
- package/out/tools/tsconfig.tsbuildinfo +1 -1
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +5 -4
package/README.md
CHANGED
|
@@ -41,9 +41,9 @@ Optional: `@googlemaps/google-maps-services-js` for Google Maps API integration.
|
|
|
41
41
|
- [`@mailwoman/address-id`](../address-id) — uses H3 for stable address primary keys
|
|
42
42
|
- [`@mailwoman/core`](../core) — consumes spatial types
|
|
43
43
|
- [`@mailwoman/codex/country`](../codex/country) — ISO 3166-1 country codes and names. This
|
|
44
|
-
package once
|
|
45
|
-
from isp-nexus alongside the geometry code, never imported by
|
|
46
|
-
2026-08-04
|
|
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.
|
|
47
47
|
|
|
48
48
|
## License
|
|
49
49
|
|
|
@@ -63,7 +63,7 @@ export function toMercator(lat: number, lon: number): { x: number; y: number } {
|
|
|
63
63
|
const KAABA = { lat: 21.4225, lon: 39.8262 }
|
|
64
64
|
|
|
65
65
|
/**
|
|
66
|
-
* Initial great-circle
|
|
66
|
+
* Initial great-circle direction, in degrees clockwise from true north, from a coordinate toward the Kaaba.
|
|
67
67
|
*/
|
|
68
68
|
export function qiblaBearing(lat: number, lon: number): number {
|
|
69
69
|
const phi1 = toRad(lat)
|
package/lib/distance.ts
CHANGED
|
@@ -27,7 +27,7 @@ export const ConversionFactor = {
|
|
|
27
27
|
export type EarthRadiusUnit = "km" | "miles" | "meters"
|
|
28
28
|
|
|
29
29
|
/**
|
|
30
|
-
* Radius of the Earth in
|
|
30
|
+
* Radius of the Earth in kilometers, miles and meters.
|
|
31
31
|
*/
|
|
32
32
|
const RADII = {
|
|
33
33
|
km: 6371,
|
|
@@ -103,7 +103,7 @@ export function haversine(point1: GeoPointInput, point2: GeoPointInput, unit: Ea
|
|
|
103
103
|
* Great-circle distance on the selected body. {@link haversine} is this function on Earth.
|
|
104
104
|
* A caller with a body passes it here.
|
|
105
105
|
*
|
|
106
|
-
* The
|
|
106
|
+
* The object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
|
|
107
107
|
*
|
|
108
108
|
* @category Position
|
|
109
109
|
*/
|
|
@@ -191,6 +191,36 @@ export function segmentDistanceMetres(
|
|
|
191
191
|
return Math.hypot(px - t * vx, py - t * vy) * METRES_PER_DEGREE
|
|
192
192
|
}
|
|
193
193
|
|
|
194
|
+
/**
|
|
195
|
+
* The squared distance from a point to a line segment in a planar projection.
|
|
196
|
+
*
|
|
197
|
+
* The answer is in the projection's units squared.
|
|
198
|
+
* For EPSG:27700 those units are meters, and no latitude scaling applies.
|
|
199
|
+
*
|
|
200
|
+
* The squared form saves a square root per comparison.
|
|
201
|
+
* A caller compares it against a squared radius.
|
|
202
|
+
*
|
|
203
|
+
* @category Position
|
|
204
|
+
*/
|
|
205
|
+
export function planarSegmentDistanceSquared(
|
|
206
|
+
px: number,
|
|
207
|
+
py: number,
|
|
208
|
+
ax: number,
|
|
209
|
+
ay: number,
|
|
210
|
+
bx: number,
|
|
211
|
+
by: number
|
|
212
|
+
): number {
|
|
213
|
+
const vx = bx - ax
|
|
214
|
+
const vy = by - ay
|
|
215
|
+
const lengthSquared = vx * vx + vy * vy
|
|
216
|
+
// A zero-length segment is a repeated vertex.
|
|
217
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / lengthSquared))
|
|
218
|
+
const dx = ax + t * vx - px
|
|
219
|
+
const dy = ay + t * vy - py
|
|
220
|
+
|
|
221
|
+
return dx * dx + dy * dy
|
|
222
|
+
}
|
|
223
|
+
|
|
194
224
|
/**
|
|
195
225
|
* Meters from a point to the nearest ring edge of an areal geometry.
|
|
196
226
|
*
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software.
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Streams unit-postcode rows from extracted Code-Point Open CSVs and converts their OSGB36
|
|
7
|
+
* coordinates to WGS84.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { normalizePostcodeDisplay } from "@mailwoman/codex/postcode/display-form"
|
|
11
|
+
import { CSVSpliterator } from "spliterator"
|
|
12
|
+
|
|
13
|
+
import { osgb36ToWGS84 } from "#osgb36"
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The positional quality indicator for a row with no coordinate.
|
|
17
|
+
*
|
|
18
|
+
* These rows have zero eastings and northings.
|
|
19
|
+
* Conversion would place them at a real but wrong location.
|
|
20
|
+
*/
|
|
21
|
+
export const PQI_NO_COORDINATE = 90
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* The number of columns in a Code-Point Open CSV row.
|
|
25
|
+
*/
|
|
26
|
+
const CODEPOINT_COLUMNS = 10
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The ONS country codes in Code-Point Open, mapped to ISO 3166-2 subdivisions.
|
|
30
|
+
*
|
|
31
|
+
* Northern Ireland has no code because the product does not cover it.
|
|
32
|
+
*/
|
|
33
|
+
export const CODEPOINT_COUNTRY_CODES = {
|
|
34
|
+
E92000001: "ENG",
|
|
35
|
+
S92000003: "SCT",
|
|
36
|
+
W92000004: "WLS",
|
|
37
|
+
} as const
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* One subdivision code from {@link CODEPOINT_COUNTRY_CODES}.
|
|
41
|
+
*/
|
|
42
|
+
export type CodePointCountry = (typeof CODEPOINT_COUNTRY_CODES)[keyof typeof CODEPOINT_COUNTRY_CODES]
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* One unit postcode with a usable coordinate.
|
|
46
|
+
*/
|
|
47
|
+
export interface CodePointRecord {
|
|
48
|
+
/**
|
|
49
|
+
* The display form of the postcode, with one space between outward and inward codes, such as `SW1A 1AA`.
|
|
50
|
+
* The database builder derives the lookup form.
|
|
51
|
+
*/
|
|
52
|
+
postcode: string
|
|
53
|
+
/**
|
|
54
|
+
* The positional quality indicator, from 10 (best) to 60.
|
|
55
|
+
* Rows with 90 are dropped.
|
|
56
|
+
*/
|
|
57
|
+
quality: number
|
|
58
|
+
/**
|
|
59
|
+
* The OSGB36 easting in meters, as published.
|
|
60
|
+
*/
|
|
61
|
+
easting: number
|
|
62
|
+
/**
|
|
63
|
+
* The OSGB36 northing in meters, as published.
|
|
64
|
+
*/
|
|
65
|
+
northing: number
|
|
66
|
+
/**
|
|
67
|
+
* The WGS84 latitude converted from the grid reference, accurate to about 2 m.
|
|
68
|
+
*/
|
|
69
|
+
latitude: number
|
|
70
|
+
/**
|
|
71
|
+
* The WGS84 longitude.
|
|
72
|
+
*/
|
|
73
|
+
longitude: number
|
|
74
|
+
/**
|
|
75
|
+
* The ONS country code as published.
|
|
76
|
+
*/
|
|
77
|
+
countryCode: string
|
|
78
|
+
/**
|
|
79
|
+
* The subdivision for the country code, or null for an unknown code.
|
|
80
|
+
*/
|
|
81
|
+
country: CodePointCountry | null
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Counts from a parse run, including the reason for each skipped row.
|
|
86
|
+
*/
|
|
87
|
+
export interface CodePointParseStats {
|
|
88
|
+
/**
|
|
89
|
+
* Rows read, including skipped rows.
|
|
90
|
+
*/
|
|
91
|
+
read: number
|
|
92
|
+
/**
|
|
93
|
+
* Rows yielded.
|
|
94
|
+
*/
|
|
95
|
+
yielded: number
|
|
96
|
+
/**
|
|
97
|
+
* Rows dropped for positional quality 90.
|
|
98
|
+
*/
|
|
99
|
+
skippedNoCoordinate: number
|
|
100
|
+
/**
|
|
101
|
+
* Rows dropped for a malformed postcode, a non-numeric grid reference, or the wrong column count.
|
|
102
|
+
*/
|
|
103
|
+
skippedMalformed: number
|
|
104
|
+
/**
|
|
105
|
+
* Yielded rows per postcode area, such as `AB` or `B`.
|
|
106
|
+
*
|
|
107
|
+
* These counts can be compared with the archive's `Doc/metadata.txt`.
|
|
108
|
+
*/
|
|
109
|
+
yieldedByArea: Record<string, number>
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Matches a GB unit postcode with a single space.
|
|
114
|
+
*
|
|
115
|
+
* The pattern accepts any valid outward-code shape, such as `W1A`, `EC1A`, `B1`, or `DN55`.
|
|
116
|
+
* The inward code is always a digit followed by two letters.
|
|
117
|
+
*/
|
|
118
|
+
const UNIT_POSTCODE = /^[A-Z]{1,2}[0-9][A-Z0-9]?\s[0-9][A-Z]{2}$/
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Returns the postcode area.
|
|
122
|
+
* It consists of the leading one or two letters.
|
|
123
|
+
*
|
|
124
|
+
* For example, `SW1A 1AA` gives `SW` and `B33 8TH` gives `B`.
|
|
125
|
+
* `Doc/metadata.txt` counts rows by area.
|
|
126
|
+
*/
|
|
127
|
+
export function postcodeArea(postcode: string): string {
|
|
128
|
+
return /^[A-Z]{1,2}/.exec(postcode)?.[0] ?? ""
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Splits one CSV record into fields with RFC 4180 quoting.
|
|
133
|
+
*
|
|
134
|
+
* @deprecated Use `CSVSpliterator` directly.
|
|
135
|
+
* Callers that stream records should use {@linkcode readCodePointCSV}.
|
|
136
|
+
*/
|
|
137
|
+
export function splitCSVLine(line: string): string[] {
|
|
138
|
+
return CSVSpliterator.from<string[]>(line, { header: false }).next().value ?? []
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Streams every usable record from one extracted area CSV and updates `stats` as it reads.
|
|
143
|
+
*/
|
|
144
|
+
export async function* readCodePointCSV(csvPath: string, stats: CodePointParseStats): AsyncGenerator<CodePointRecord> {
|
|
145
|
+
// The CSVs have no header row.
|
|
146
|
+
// OS ships the column names in `Doc/Code-Point_Open_Column_Headers.csv`.
|
|
147
|
+
for await (const row of CSVSpliterator.fromAsync<string[]>(csvPath, {
|
|
148
|
+
header: false,
|
|
149
|
+
})) {
|
|
150
|
+
stats.read++
|
|
151
|
+
|
|
152
|
+
if (row.length !== CODEPOINT_COLUMNS) {
|
|
153
|
+
stats.skippedMalformed++
|
|
154
|
+
|
|
155
|
+
continue
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
const postcode = normalizeCodePointSpacing(row[0] ?? "")
|
|
159
|
+
const quality = Number(row[1])
|
|
160
|
+
const easting = Number(row[2])
|
|
161
|
+
const northing = Number(row[3])
|
|
162
|
+
const countryCode = row[4] ?? ""
|
|
163
|
+
|
|
164
|
+
if (quality === PQI_NO_COORDINATE) {
|
|
165
|
+
stats.skippedNoCoordinate++
|
|
166
|
+
|
|
167
|
+
continue
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
if (!UNIT_POSTCODE.test(postcode) || !Number.isFinite(easting) || !Number.isFinite(northing)) {
|
|
171
|
+
stats.skippedMalformed++
|
|
172
|
+
|
|
173
|
+
continue
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const { latitude, longitude } = osgb36ToWGS84({ easting, northing })
|
|
177
|
+
const area = postcodeArea(postcode)
|
|
178
|
+
|
|
179
|
+
stats.yielded++
|
|
180
|
+
stats.yieldedByArea[area] = (stats.yieldedByArea[area] ?? 0) + 1
|
|
181
|
+
|
|
182
|
+
yield {
|
|
183
|
+
postcode,
|
|
184
|
+
quality,
|
|
185
|
+
easting,
|
|
186
|
+
northing,
|
|
187
|
+
latitude,
|
|
188
|
+
longitude,
|
|
189
|
+
countryCode,
|
|
190
|
+
country: CODEPOINT_COUNTRY_CODES[countryCode as keyof typeof CODEPOINT_COUNTRY_CODES] ?? null,
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Normalizes Code-Point postcode spacing to the single-space display form.
|
|
197
|
+
*
|
|
198
|
+
* The product specification uses a fixed seven-character field that pads short postcodes with extra spaces.
|
|
199
|
+
* Normalization keeps a padded postcode from becoming a separate entry.
|
|
200
|
+
*/
|
|
201
|
+
export function normalizeCodePointSpacing(raw: string): string {
|
|
202
|
+
return normalizePostcodeDisplay(raw)
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Returns a stats object with every count at zero.
|
|
207
|
+
*/
|
|
208
|
+
export function createCodePointParseStats(): CodePointParseStats {
|
|
209
|
+
return { read: 0, yielded: 0, skippedNoCoordinate: 0, skippedMalformed: 0, yieldedByArea: {} }
|
|
210
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* The Ordnance Survey Downloads API's record of one downloadable archive.
|
|
7
|
+
*
|
|
8
|
+
* The API serves OS OpenData products without authentication. `GET /products/<id>/downloads`
|
|
9
|
+
* lists one record per archive, and every OS product fetcher in this repository reads that list.
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The root of the OS Downloads API.
|
|
14
|
+
*/
|
|
15
|
+
export const OS_DOWNLOADS_API_BASE = "https://api.os.uk/downloads/v1"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* One archive as `GET /products/<id>/downloads` lists it.
|
|
19
|
+
*/
|
|
20
|
+
export interface OSDownload {
|
|
21
|
+
/**
|
|
22
|
+
* The OS-published MD5 of the archive.
|
|
23
|
+
*/
|
|
24
|
+
md5: string
|
|
25
|
+
/**
|
|
26
|
+
* The archive size in bytes.
|
|
27
|
+
*/
|
|
28
|
+
size: number
|
|
29
|
+
/**
|
|
30
|
+
* The download URL.
|
|
31
|
+
* It redirects to a CDN object.
|
|
32
|
+
*/
|
|
33
|
+
url: string
|
|
34
|
+
/**
|
|
35
|
+
* The archive format, such as `CSV`, `GML` or `GeoPackage`.
|
|
36
|
+
*/
|
|
37
|
+
format: string
|
|
38
|
+
/**
|
|
39
|
+
* The coverage area, such as `GB` or a 100 km grid square.
|
|
40
|
+
*/
|
|
41
|
+
area: string
|
|
42
|
+
/**
|
|
43
|
+
* The archive file name, such as `codepo_gb.zip`.
|
|
44
|
+
*/
|
|
45
|
+
fileName: string
|
|
46
|
+
}
|
package/lib/geometries/point.ts
CHANGED
|
@@ -357,7 +357,7 @@ export class GeoPoint implements PointLiteral {
|
|
|
357
357
|
}
|
|
358
358
|
|
|
359
359
|
/**
|
|
360
|
-
* Whether the GeoPoint
|
|
360
|
+
* Whether the GeoPoint sits at 0, 0 (Null Island).
|
|
361
361
|
*/
|
|
362
362
|
public isNullIsland(): boolean {
|
|
363
363
|
return this.#latitude === 0 && this.#longitude === 0
|
package/lib/h3/coverage.ts
CHANGED
|
@@ -29,7 +29,7 @@ export interface CoverageRowProbe {
|
|
|
29
29
|
*
|
|
30
30
|
* A caller must treat it as an unknown value, separate from `{completeness: 0}`.
|
|
31
31
|
*
|
|
32
|
-
* The shared mapping owns the
|
|
32
|
+
* The shared mapping owns the rule for a `basis` column that holds NULL.
|
|
33
33
|
* A NULL column means the artifact predates `basis`.
|
|
34
34
|
*
|
|
35
35
|
* It recorded source presence and no stronger basis than the builder had.
|
package/lib/h3/polygon-cells.ts
CHANGED
|
@@ -104,8 +104,8 @@ export interface FeatureCells {
|
|
|
104
104
|
}
|
|
105
105
|
|
|
106
106
|
/**
|
|
107
|
-
*
|
|
108
|
-
* h3 reserves its buffer from, approximate on purpose.
|
|
107
|
+
* The count of cells at `resolution` that the feature's bounding box spans —
|
|
108
|
+
* the same quantity h3 reserves its buffer from, approximate on purpose.
|
|
109
109
|
*/
|
|
110
110
|
export function estimateCellCount(polygons: MultiPolygonRings, resolution: number): number {
|
|
111
111
|
const box = ringsBoundingBox(polygons)
|
|
@@ -34,7 +34,7 @@ import { TextSpliterator } from "spliterator"
|
|
|
34
34
|
*/
|
|
35
35
|
export interface DatumTransformationVerdict {
|
|
36
36
|
/**
|
|
37
|
-
*
|
|
37
|
+
* PROJ returned this candidate operation string, verbatim.
|
|
38
38
|
*
|
|
39
39
|
* Null when PROJ returned no candidate operation.
|
|
40
40
|
*/
|
|
@@ -27,7 +27,7 @@ export declare function toMercator(lat: number, lon: number): {
|
|
|
27
27
|
y: number;
|
|
28
28
|
};
|
|
29
29
|
/**
|
|
30
|
-
* Initial great-circle
|
|
30
|
+
* Initial great-circle direction, in degrees clockwise from true north, from a coordinate toward the Kaaba.
|
|
31
31
|
*/
|
|
32
32
|
export declare function qiblaBearing(lat: number, lon: number): number;
|
|
33
33
|
/**
|
|
@@ -47,7 +47,7 @@ export function toMercator(lat, lon) {
|
|
|
47
47
|
}
|
|
48
48
|
const KAABA = { lat: 21.4225, lon: 39.8262 };
|
|
49
49
|
/**
|
|
50
|
-
* Initial great-circle
|
|
50
|
+
* Initial great-circle direction, in degrees clockwise from true north, from a coordinate toward the Kaaba.
|
|
51
51
|
*/
|
|
52
52
|
export function qiblaBearing(lat, lon) {
|
|
53
53
|
const phi1 = toRad(lat);
|
package/out/distance.d.ts
CHANGED
|
@@ -60,7 +60,7 @@ export declare function haversine(point1: GeoPointInput, point2: GeoPointInput,
|
|
|
60
60
|
* Great-circle distance on the selected body. {@link haversine} is this function on Earth.
|
|
61
61
|
* A caller with a body passes it here.
|
|
62
62
|
*
|
|
63
|
-
* The
|
|
63
|
+
* The object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
|
|
64
64
|
*
|
|
65
65
|
* @category Position
|
|
66
66
|
*/
|
|
@@ -113,6 +113,18 @@ export declare const M2_PER_KM2 = 1000000;
|
|
|
113
113
|
* @category Position
|
|
114
114
|
*/
|
|
115
115
|
export declare function segmentDistanceMetres(lon: number, lat: number, from: readonly number[], to: readonly number[]): number;
|
|
116
|
+
/**
|
|
117
|
+
* The squared distance from a point to a line segment in a planar projection.
|
|
118
|
+
*
|
|
119
|
+
* The answer is in the projection's units squared.
|
|
120
|
+
* For EPSG:27700 those units are meters, and no latitude scaling applies.
|
|
121
|
+
*
|
|
122
|
+
* The squared form saves a square root per comparison.
|
|
123
|
+
* A caller compares it against a squared radius.
|
|
124
|
+
*
|
|
125
|
+
* @category Position
|
|
126
|
+
*/
|
|
127
|
+
export declare function planarSegmentDistanceSquared(px: number, py: number, ax: number, ay: number, bx: number, by: number): number;
|
|
116
128
|
/**
|
|
117
129
|
* Meters from a point to the nearest ring edge of an areal geometry.
|
|
118
130
|
*
|
package/out/distance.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{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AAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAE1F;AAED;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,SAAU,CAAA;AAExC;;;;GAIG;AACH,eAAO,MAAM,UAAU,UAAY,CAAA;AAEnC;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,qBAAqB,CACpC,GAAG,EAAE,MAAM,EACX,GAAG,EAAE,MAAM,EACX,IAAI,EAAE,SAAS,MAAM,EAAE,EACvB,EAAE,EAAE,SAAS,MAAM,EAAE,GACnB,MAAM,CAaR;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,4BAA4B,CAC3C,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,EACV,EAAE,EAAE,MAAM,GACR,MAAM,CAUR;AAED;;;;;;;;;;;;;GAaG;AACH,wBAAgB,qBAAqB,CACpC,QAAQ,EAAE;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,EAChD,SAAS,EAAE,MAAM,EACjB,QAAQ,EAAE,MAAM,GACd,MAAM,CAoBR"}
|
package/out/distance.js
CHANGED
|
@@ -19,7 +19,7 @@ export const ConversionFactor = {
|
|
|
19
19
|
RadiansToDegrees: 180 / Math.PI,
|
|
20
20
|
};
|
|
21
21
|
/**
|
|
22
|
-
* Radius of the Earth in
|
|
22
|
+
* Radius of the Earth in kilometers, miles and meters.
|
|
23
23
|
*/
|
|
24
24
|
const RADII = {
|
|
25
25
|
km: 6371,
|
|
@@ -75,7 +75,7 @@ export function haversine(point1, point2, unit = "km") {
|
|
|
75
75
|
* Great-circle distance on the selected body. {@link haversine} is this function on Earth.
|
|
76
76
|
* A caller with a body passes it here.
|
|
77
77
|
*
|
|
78
|
-
* The
|
|
78
|
+
* The object form's `(0, 0)` sentinel applies: a `(0, 0)` input answers `NaN`.
|
|
79
79
|
*
|
|
80
80
|
* @category Position
|
|
81
81
|
*/
|
|
@@ -146,6 +146,27 @@ export function segmentDistanceMetres(lon, lat, from, to) {
|
|
|
146
146
|
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, (px * vx + py * vy) / lengthSquared));
|
|
147
147
|
return Math.hypot(px - t * vx, py - t * vy) * METRES_PER_DEGREE;
|
|
148
148
|
}
|
|
149
|
+
/**
|
|
150
|
+
* The squared distance from a point to a line segment in a planar projection.
|
|
151
|
+
*
|
|
152
|
+
* The answer is in the projection's units squared.
|
|
153
|
+
* For EPSG:27700 those units are meters, and no latitude scaling applies.
|
|
154
|
+
*
|
|
155
|
+
* The squared form saves a square root per comparison.
|
|
156
|
+
* A caller compares it against a squared radius.
|
|
157
|
+
*
|
|
158
|
+
* @category Position
|
|
159
|
+
*/
|
|
160
|
+
export function planarSegmentDistanceSquared(px, py, ax, ay, bx, by) {
|
|
161
|
+
const vx = bx - ax;
|
|
162
|
+
const vy = by - ay;
|
|
163
|
+
const lengthSquared = vx * vx + vy * vy;
|
|
164
|
+
// A zero-length segment is a repeated vertex.
|
|
165
|
+
const t = lengthSquared === 0 ? 0 : Math.max(0, Math.min(1, ((px - ax) * vx + (py - ay) * vy) / lengthSquared));
|
|
166
|
+
const dx = ax + t * vx - px;
|
|
167
|
+
const dy = ay + t * vy - py;
|
|
168
|
+
return dx * dx + dy * dy;
|
|
169
|
+
}
|
|
149
170
|
/**
|
|
150
171
|
* Meters from a point to the nearest ring edge of an areal geometry.
|
|
151
172
|
*
|
package/out/distance.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"distance.js","sourceRoot":"","sources":["../lib/distance.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,QAAQ,EAAsB,MAAM,mBAAmB,CAAA;AAChE,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAA;AAEnD;;;;;;GAMG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC/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E3B,OAAO,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,CAAA;AACzB,CAAC;AAED;;;;;;;;;;;;;GAaG;AACH,MAAM,UAAU,qBAAqB,CACpC,QAAgD,EAChD,SAAiB,EACjB,QAAgB;IAEhB,MAAM,QAAQ,GAAG,aAAa,CAAC,QAAQ,CAAC,CAAA;IAExC,IAAI,OAAO,GAAG,QAAQ,CAAA;IAEtB,IAAI,CAAC,QAAQ;QAAE,OAAO,OAAO,CAAA;IAE7B,KAAK,MAAM,KAAK,IAAI,QAAQ,EAAE,CAAC;QAC9B,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;YAC1B,KAAK,IAAI,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,IAAI,CAAC,MAAM,EAAE,KAAK,EAAE,EAAE,CAAC;gBAClD,MAAM,QAAQ,GAAG,qBAAqB,CAAC,SAAS,EAAE,QAAQ,EAAE,IAAI,CAAC,KAAK,GAAG,CAAC,CAAE,EAAE,IAAI,CAAC,KAAK,CAAE,CAAC,CAAA;gBAE3F,IAAI,QAAQ,GAAG,OAAO,EAAE,CAAC;oBACxB,OAAO,GAAG,QAAQ,CAAA;gBACnB,CAAC;YACF,CAAC;QACF,CAAC;IACF,CAAC;IAED,OAAO,OAAO,CAAA;AACf,CAAC"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @copyright Sister Software.
|
|
3
|
+
* @license AGPL-3.0
|
|
4
|
+
* @author Teffen Ellis, et al.
|
|
5
|
+
*
|
|
6
|
+
* Streams unit-postcode rows from extracted Code-Point Open CSVs and converts their OSGB36
|
|
7
|
+
* coordinates to WGS84.
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* The positional quality indicator for a row with no coordinate.
|
|
11
|
+
*
|
|
12
|
+
* These rows have zero eastings and northings.
|
|
13
|
+
* Conversion would place them at a real but wrong location.
|
|
14
|
+
*/
|
|
15
|
+
export declare const PQI_NO_COORDINATE = 90;
|
|
16
|
+
/**
|
|
17
|
+
* The ONS country codes in Code-Point Open, mapped to ISO 3166-2 subdivisions.
|
|
18
|
+
*
|
|
19
|
+
* Northern Ireland has no code because the product does not cover it.
|
|
20
|
+
*/
|
|
21
|
+
export declare const CODEPOINT_COUNTRY_CODES: {
|
|
22
|
+
readonly E92000001: "ENG";
|
|
23
|
+
readonly S92000003: "SCT";
|
|
24
|
+
readonly W92000004: "WLS";
|
|
25
|
+
};
|
|
26
|
+
/**
|
|
27
|
+
* One subdivision code from {@link CODEPOINT_COUNTRY_CODES}.
|
|
28
|
+
*/
|
|
29
|
+
export type CodePointCountry = (typeof CODEPOINT_COUNTRY_CODES)[keyof typeof CODEPOINT_COUNTRY_CODES];
|
|
30
|
+
/**
|
|
31
|
+
* One unit postcode with a usable coordinate.
|
|
32
|
+
*/
|
|
33
|
+
export interface CodePointRecord {
|
|
34
|
+
/**
|
|
35
|
+
* The display form of the postcode, with one space between outward and inward codes, such as `SW1A 1AA`.
|
|
36
|
+
* The database builder derives the lookup form.
|
|
37
|
+
*/
|
|
38
|
+
postcode: string;
|
|
39
|
+
/**
|
|
40
|
+
* The positional quality indicator, from 10 (best) to 60.
|
|
41
|
+
* Rows with 90 are dropped.
|
|
42
|
+
*/
|
|
43
|
+
quality: number;
|
|
44
|
+
/**
|
|
45
|
+
* The OSGB36 easting in meters, as published.
|
|
46
|
+
*/
|
|
47
|
+
easting: number;
|
|
48
|
+
/**
|
|
49
|
+
* The OSGB36 northing in meters, as published.
|
|
50
|
+
*/
|
|
51
|
+
northing: number;
|
|
52
|
+
/**
|
|
53
|
+
* The WGS84 latitude converted from the grid reference, accurate to about 2 m.
|
|
54
|
+
*/
|
|
55
|
+
latitude: number;
|
|
56
|
+
/**
|
|
57
|
+
* The WGS84 longitude.
|
|
58
|
+
*/
|
|
59
|
+
longitude: number;
|
|
60
|
+
/**
|
|
61
|
+
* The ONS country code as published.
|
|
62
|
+
*/
|
|
63
|
+
countryCode: string;
|
|
64
|
+
/**
|
|
65
|
+
* The subdivision for the country code, or null for an unknown code.
|
|
66
|
+
*/
|
|
67
|
+
country: CodePointCountry | null;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* Counts from a parse run, including the reason for each skipped row.
|
|
71
|
+
*/
|
|
72
|
+
export interface CodePointParseStats {
|
|
73
|
+
/**
|
|
74
|
+
* Rows read, including skipped rows.
|
|
75
|
+
*/
|
|
76
|
+
read: number;
|
|
77
|
+
/**
|
|
78
|
+
* Rows yielded.
|
|
79
|
+
*/
|
|
80
|
+
yielded: number;
|
|
81
|
+
/**
|
|
82
|
+
* Rows dropped for positional quality 90.
|
|
83
|
+
*/
|
|
84
|
+
skippedNoCoordinate: number;
|
|
85
|
+
/**
|
|
86
|
+
* Rows dropped for a malformed postcode, a non-numeric grid reference, or the wrong column count.
|
|
87
|
+
*/
|
|
88
|
+
skippedMalformed: number;
|
|
89
|
+
/**
|
|
90
|
+
* Yielded rows per postcode area, such as `AB` or `B`.
|
|
91
|
+
*
|
|
92
|
+
* These counts can be compared with the archive's `Doc/metadata.txt`.
|
|
93
|
+
*/
|
|
94
|
+
yieldedByArea: Record<string, number>;
|
|
95
|
+
}
|
|
96
|
+
/**
|
|
97
|
+
* Returns the postcode area.
|
|
98
|
+
* It consists of the leading one or two letters.
|
|
99
|
+
*
|
|
100
|
+
* For example, `SW1A 1AA` gives `SW` and `B33 8TH` gives `B`.
|
|
101
|
+
* `Doc/metadata.txt` counts rows by area.
|
|
102
|
+
*/
|
|
103
|
+
export declare function postcodeArea(postcode: string): string;
|
|
104
|
+
/**
|
|
105
|
+
* Splits one CSV record into fields with RFC 4180 quoting.
|
|
106
|
+
*
|
|
107
|
+
* @deprecated Use `CSVSpliterator` directly.
|
|
108
|
+
* Callers that stream records should use {@linkcode readCodePointCSV}.
|
|
109
|
+
*/
|
|
110
|
+
export declare function splitCSVLine(line: string): string[];
|
|
111
|
+
/**
|
|
112
|
+
* Streams every usable record from one extracted area CSV and updates `stats` as it reads.
|
|
113
|
+
*/
|
|
114
|
+
export declare function readCodePointCSV(csvPath: string, stats: CodePointParseStats): AsyncGenerator<CodePointRecord>;
|
|
115
|
+
/**
|
|
116
|
+
* Normalizes Code-Point postcode spacing to the single-space display form.
|
|
117
|
+
*
|
|
118
|
+
* The product specification uses a fixed seven-character field that pads short postcodes with extra spaces.
|
|
119
|
+
* Normalization keeps a padded postcode from becoming a separate entry.
|
|
120
|
+
*/
|
|
121
|
+
export declare function normalizeCodePointSpacing(raw: string): string;
|
|
122
|
+
/**
|
|
123
|
+
* Returns a stats object with every count at zero.
|
|
124
|
+
*/
|
|
125
|
+
export declare function createCodePointParseStats(): CodePointParseStats;
|
|
126
|
+
//# sourceMappingURL=codepoint.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codepoint.d.ts","sourceRoot":"","sources":["../../lib/gb/codepoint.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAOH;;;;;GAKG;AACH,eAAO,MAAM,iBAAiB,KAAK,CAAA;AAOnC;;;;GAIG;AACH,eAAO,MAAM,uBAAuB;aACnC,SAAS,EAAE,KAAK;aAChB,SAAS,EAAE,KAAK;aAChB,SAAS,EAAE,KAAK;CACP,CAAA;AAEV;;GAEG;AACH,MAAM,MAAM,gBAAgB,GAAG,CAAC,OAAO,uBAAuB,CAAC,CAAC,MAAM,OAAO,uBAAuB,CAAC,CAAA;AAErG;;GAEG;AACH,MAAM,WAAW,eAAe;IAC/B;;;OAGG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;IAChB;;OAEG;IACH,SAAS,EAAE,MAAM,CAAA;IACjB;;OAEG;IACH,WAAW,EAAE,MAAM,CAAA;IACnB;;OAEG;IACH,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