@mailwoman/spatial 10.4.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/lib/distance.ts +30 -0
- package/lib/gb/codepoint.ts +210 -0
- package/lib/gb/os-downloads.ts +46 -0
- package/lib/projection/transform.ts +1 -1
- package/out/distance.d.ts +12 -0
- package/out/distance.d.ts.map +1 -1
- package/out/distance.js +21 -0
- 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/projection/transform.d.ts +1 -1
- package/out/tools/tsconfig.tsbuildinfo +1 -1
- package/out/tsconfig.test.tsbuildinfo +1 -1
- package/package.json +4 -3
package/lib/distance.ts
CHANGED
|
@@ -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
|
+
}
|
|
@@ -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
|
*/
|
package/out/distance.d.ts
CHANGED
|
@@ -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
|
-
{"version":3,"file":"distance.d.ts","sourceRoot":"","sources":["../lib/distance.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAY,KAAK,aAAa,EAAE,MAAM,mBAAmB,CAAA;AAGhE;;;;;;GAMG;AACH,eAAO,MAAM,gBAAgB;aAC5B,gBAAgB;aAChB,gBAAgB;CACP,CAAA;AAEV;;GAEG;AACH,MAAM,MAAM,eAAe,GAAG,IAAI,GAAG,OAAO,GAAG,QAAQ,CAAA;AAWvD,MAAM,MAAM,eAAe,GAAG,OAAO,GAAG,MAAM,GAAG,MAAM,CAAA;AAEvD;;;;;;;;GAQG;AACH,eAAO,MAAM,aAAa;;mBAEnB,MAAM;mBACN,MAAM;CACuC,CAAA;AAEpD,MAAM,WAAW,kBAAkB;IAClC,IAAI,CAAC,EAAE,eAAe,CAAA;IACtB;;;;OAIG;IACH,IAAI,CAAC,EAAE,eAAe,CAAA;CACtB;AAoBD;;;;;;;;;GASG;AACH,wBAAgB,SAAS,CAAC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,GAAE,eAAsB,GAAG,MAAM,CAO5G;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAClC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,kBAAuB,GAC9B,MAAM,CAMR;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,IAAI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CAAC,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,aAAa,EAAE,IAAI,GAAE,eAAsB,GAAG,MAAM,CAO5G;AAED;;;;;;;GAOG;AACH,wBAAgB,mBAAmB,CAClC,MAAM,EAAE,aAAa,EACrB,MAAM,EAAE,aAAa,EACrB,OAAO,GAAE,kBAAuB,GAC9B,MAAM,CAMR;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,WAAW,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,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
|
@@ -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/B,gBAAgB,EAAE,IAAI,CAAC,EAAE,GAAG,GAAG;IAC/B,gBAAgB,EAAE,GAAG,GAAG,IAAI,CAAC,EAAE;CACtB,CAAA;AAOV;;GAEG;AACH,MAAM,KAAK,GAAG;IACb,EAAE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AgB;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,OAAO,EAAE,gBAAgB,GAAG,IAAI,CAAA;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,mBAAmB;IACnC;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,OAAO,EAAE,MAAM,CAAA;IACf;;OAEG;IACH,mBAAmB,EAAE,MAAM,CAAA;IAC3B;;OAEG;IACH,gBAAgB,EAAE,MAAM,CAAA;IACxB;;;;OAIG;IACH,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAA;CACrC;AAUD;;;;;;GAMG;AACH,wBAAgB,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,CAErD;AAED;;;;;GAKG;AACH,wBAAgB,YAAY,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,EAAE,CAEnD;AAED;;GAEG;AACH,wBAAuB,gBAAgB,CAAC,OAAO,EAAE,MAAM,EAAE,KAAK,EAAE,mBAAmB,GAAG,cAAc,CAAC,eAAe,CAAC,CAiDpH;AAED;;;;;GAKG;AACH,wBAAgB,yBAAyB,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAE7D;AAED;;GAEG;AACH,wBAAgB,yBAAyB,IAAI,mBAAmB,CAE/D"}
|
|
@@ -0,0 +1,117 @@
|
|
|
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
|
+
import { normalizePostcodeDisplay } from "@mailwoman/codex/postcode/display-form";
|
|
10
|
+
import { CSVSpliterator } from "spliterator";
|
|
11
|
+
import { osgb36ToWGS84 } from "#osgb36";
|
|
12
|
+
/**
|
|
13
|
+
* The positional quality indicator for a row with no coordinate.
|
|
14
|
+
*
|
|
15
|
+
* These rows have zero eastings and northings.
|
|
16
|
+
* Conversion would place them at a real but wrong location.
|
|
17
|
+
*/
|
|
18
|
+
export const PQI_NO_COORDINATE = 90;
|
|
19
|
+
/**
|
|
20
|
+
* The number of columns in a Code-Point Open CSV row.
|
|
21
|
+
*/
|
|
22
|
+
const CODEPOINT_COLUMNS = 10;
|
|
23
|
+
/**
|
|
24
|
+
* The ONS country codes in Code-Point Open, mapped to ISO 3166-2 subdivisions.
|
|
25
|
+
*
|
|
26
|
+
* Northern Ireland has no code because the product does not cover it.
|
|
27
|
+
*/
|
|
28
|
+
export const CODEPOINT_COUNTRY_CODES = {
|
|
29
|
+
E92000001: "ENG",
|
|
30
|
+
S92000003: "SCT",
|
|
31
|
+
W92000004: "WLS",
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Matches a GB unit postcode with a single space.
|
|
35
|
+
*
|
|
36
|
+
* The pattern accepts any valid outward-code shape, such as `W1A`, `EC1A`, `B1`, or `DN55`.
|
|
37
|
+
* The inward code is always a digit followed by two letters.
|
|
38
|
+
*/
|
|
39
|
+
const UNIT_POSTCODE = /^[A-Z]{1,2}[0-9][A-Z0-9]?\s[0-9][A-Z]{2}$/;
|
|
40
|
+
/**
|
|
41
|
+
* Returns the postcode area.
|
|
42
|
+
* It consists of the leading one or two letters.
|
|
43
|
+
*
|
|
44
|
+
* For example, `SW1A 1AA` gives `SW` and `B33 8TH` gives `B`.
|
|
45
|
+
* `Doc/metadata.txt` counts rows by area.
|
|
46
|
+
*/
|
|
47
|
+
export function postcodeArea(postcode) {
|
|
48
|
+
return /^[A-Z]{1,2}/.exec(postcode)?.[0] ?? "";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Splits one CSV record into fields with RFC 4180 quoting.
|
|
52
|
+
*
|
|
53
|
+
* @deprecated Use `CSVSpliterator` directly.
|
|
54
|
+
* Callers that stream records should use {@linkcode readCodePointCSV}.
|
|
55
|
+
*/
|
|
56
|
+
export function splitCSVLine(line) {
|
|
57
|
+
return CSVSpliterator.from(line, { header: false }).next().value ?? [];
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Streams every usable record from one extracted area CSV and updates `stats` as it reads.
|
|
61
|
+
*/
|
|
62
|
+
export async function* readCodePointCSV(csvPath, stats) {
|
|
63
|
+
// The CSVs have no header row.
|
|
64
|
+
// OS ships the column names in `Doc/Code-Point_Open_Column_Headers.csv`.
|
|
65
|
+
for await (const row of CSVSpliterator.fromAsync(csvPath, {
|
|
66
|
+
header: false,
|
|
67
|
+
})) {
|
|
68
|
+
stats.read++;
|
|
69
|
+
if (row.length !== CODEPOINT_COLUMNS) {
|
|
70
|
+
stats.skippedMalformed++;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
const postcode = normalizeCodePointSpacing(row[0] ?? "");
|
|
74
|
+
const quality = Number(row[1]);
|
|
75
|
+
const easting = Number(row[2]);
|
|
76
|
+
const northing = Number(row[3]);
|
|
77
|
+
const countryCode = row[4] ?? "";
|
|
78
|
+
if (quality === PQI_NO_COORDINATE) {
|
|
79
|
+
stats.skippedNoCoordinate++;
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (!UNIT_POSTCODE.test(postcode) || !Number.isFinite(easting) || !Number.isFinite(northing)) {
|
|
83
|
+
stats.skippedMalformed++;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
const { latitude, longitude } = osgb36ToWGS84({ easting, northing });
|
|
87
|
+
const area = postcodeArea(postcode);
|
|
88
|
+
stats.yielded++;
|
|
89
|
+
stats.yieldedByArea[area] = (stats.yieldedByArea[area] ?? 0) + 1;
|
|
90
|
+
yield {
|
|
91
|
+
postcode,
|
|
92
|
+
quality,
|
|
93
|
+
easting,
|
|
94
|
+
northing,
|
|
95
|
+
latitude,
|
|
96
|
+
longitude,
|
|
97
|
+
countryCode,
|
|
98
|
+
country: CODEPOINT_COUNTRY_CODES[countryCode] ?? null,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
/**
|
|
103
|
+
* Normalizes Code-Point postcode spacing to the single-space display form.
|
|
104
|
+
*
|
|
105
|
+
* The product specification uses a fixed seven-character field that pads short postcodes with extra spaces.
|
|
106
|
+
* Normalization keeps a padded postcode from becoming a separate entry.
|
|
107
|
+
*/
|
|
108
|
+
export function normalizeCodePointSpacing(raw) {
|
|
109
|
+
return normalizePostcodeDisplay(raw);
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Returns a stats object with every count at zero.
|
|
113
|
+
*/
|
|
114
|
+
export function createCodePointParseStats() {
|
|
115
|
+
return { read: 0, yielded: 0, skippedNoCoordinate: 0, skippedMalformed: 0, yieldedByArea: {} };
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=codepoint.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"codepoint.js","sourceRoot":"","sources":["../../lib/gb/codepoint.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH,OAAO,EAAE,wBAAwB,EAAE,MAAM,wCAAwC,CAAA;AACjF,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAA;AAE5C,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAA;AAEvC;;;;;GAKG;AACH,MAAM,CAAC,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAEnC;;GAEG;AACH,MAAM,iBAAiB,GAAG,EAAE,CAAA;AAE5B;;;;GAIG;AACH,MAAM,CAAC,MAAM,uBAAuB,GAAG;IACtC,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,KAAK;IAChB,SAAS,EAAE,KAAK;CACP,CAAA;AA2EV;;;;;GAKG;AACH,MAAM,aAAa,GAAG,2CAA2C,CAAA;AAEjE;;;;;;GAMG;AACH,MAAM,UAAU,YAAY,CAAC,QAAgB;IAC5C,OAAO,aAAa,CAAC,IAAI,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,EAAE,CAAA;AAC/C,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,YAAY,CAAC,IAAY;IACxC,OAAO,cAAc,CAAC,IAAI,CAAW,IAAI,EAAE,EAAE,MAAM,EAAE,KAAK,EAAE,CAAC,CAAC,IAAI,EAAE,CAAC,KAAK,I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CAAC,CAAA;QACpE,MAAM,IAAI,GAAG,YAAY,CAAC,QAAQ,CAAC,CAAA;QAEnC,KAAK,CAAC,OAAO,EAAE,CAAA;QACf,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,aAAa,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAA;QAEhE,MAAM;YACL,QAAQ;YACR,OAAO;YACP,OAAO;YACP,QAAQ;YACR,QAAQ;YACR,SAAS;YACT,WAAW;YACX,OAAO,EAAE,uBAAuB,CAAC,WAAmD,CAAC,IAAI,IAAI;SAC7F,CAAA;IACF,CAAC;AACF,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,yBAAyB,CAAC,GAAW;IACpD,OAAO,wBAAwB,CAAC,GAAG,CAAC,CAAA;AACrC,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,yBAAyB;IACxC,OAAO,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,CAAC,EAAE,mBAAmB,EAAE,CAAC,EAAE,gBAAgB,EAAE,CAAC,EAAE,aAAa,EAAE,EAAE,EAAE,CAAA;AAC/F,CAAC"}
|
|
@@ -0,0 +1,45 @@
|
|
|
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
|
+
* The root of the OS Downloads API.
|
|
13
|
+
*/
|
|
14
|
+
export declare const OS_DOWNLOADS_API_BASE = "https://api.os.uk/downloads/v1";
|
|
15
|
+
/**
|
|
16
|
+
* One archive as `GET /products/<id>/downloads` lists it.
|
|
17
|
+
*/
|
|
18
|
+
export interface OSDownload {
|
|
19
|
+
/**
|
|
20
|
+
* The OS-published MD5 of the archive.
|
|
21
|
+
*/
|
|
22
|
+
md5: string;
|
|
23
|
+
/**
|
|
24
|
+
* The archive size in bytes.
|
|
25
|
+
*/
|
|
26
|
+
size: number;
|
|
27
|
+
/**
|
|
28
|
+
* The download URL.
|
|
29
|
+
* It redirects to a CDN object.
|
|
30
|
+
*/
|
|
31
|
+
url: string;
|
|
32
|
+
/**
|
|
33
|
+
* The archive format, such as `CSV`, `GML` or `GeoPackage`.
|
|
34
|
+
*/
|
|
35
|
+
format: string;
|
|
36
|
+
/**
|
|
37
|
+
* The coverage area, such as `GB` or a 100 km grid square.
|
|
38
|
+
*/
|
|
39
|
+
area: string;
|
|
40
|
+
/**
|
|
41
|
+
* The archive file name, such as `codepo_gb.zip`.
|
|
42
|
+
*/
|
|
43
|
+
fileName: string;
|
|
44
|
+
}
|
|
45
|
+
//# sourceMappingURL=os-downloads.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"os-downloads.d.ts","sourceRoot":"","sources":["../../lib/gb/os-downloads.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH;;GAEG;AACH,eAAO,MAAM,qBAAqB,mCAAmC,CAAA;AAErE;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B;;OAEG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;;OAGG;IACH,GAAG,EAAE,MAAM,CAAA;IACX;;OAEG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,IAAI,EAAE,MAAM,CAAA;IACZ;;OAEG;IACH,QAAQ,EAAE,MAAM,CAAA;CAChB"}
|