@mailwoman/resolver-wof-sqlite 12.0.0 → 12.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/address/keys/build.ts +35 -0
- package/lib/address/keys/lookup.ts +318 -6
- package/lib/address/keys/open.ts +1 -0
- package/lib/address/keys/point-lookup.ts +61 -2
- package/lib/address/keys/schema.ts +37 -0
- package/lib/address/number-forms.ts +32 -13
- package/lib/address/point/interpolation.ts +23 -6
- package/lib/address/point/schema.ts +198 -2
- package/lib/address/point/scope-search.ts +179 -0
- package/lib/address/point.ts +365 -32
- package/lib/build/candidate.ts +36 -9
- package/lib/candidate/ancestors/sidecar.ts +73 -4
- package/lib/candidate/del1.ts +218 -0
- package/lib/candidate/lookup.ts +159 -100
- package/lib/candidate/schema.ts +14 -2
- package/lib/fst/binary-matcher.ts +9 -1
- package/lib/fst/builder.ts +7 -0
- package/lib/fst/del1-sidecar.ts +782 -0
- package/lib/fst/deserialize-web.ts +14 -2
- package/lib/fst/matcher.ts +7 -1
- package/lib/fst/types.ts +8 -0
- package/lib/fst.ts +1 -0
- package/lib/index.ts +22 -2
- package/lib/labels/street/canonical.ts +333 -0
- package/lib/labels/street/centroid/schema.ts +23 -1
- package/lib/labels/street/interpolation.ts +32 -6
- package/lib/labels/street/morphology/fst/builder.ts +13 -0
- package/lib/labels/street/morphology/fst/loader.ts +32 -2
- package/lib/labels/street/normalize.ts +153 -4
- package/lib/labels/street.ts +1 -0
- package/lib/landmark.ts +43 -2
- package/lib/lookup/types.ts +0 -6
- package/lib/poi/lookup.ts +148 -52
- package/lib/poi/schema.ts +9 -1
- package/lib/schema/overture-match.ts +2 -1
- package/lib/weights/overlay-linker.ts +6 -2
- package/out/address/keys/build.d.ts.map +1 -1
- package/out/address/keys/build.js +28 -1
- package/out/address/keys/build.js.map +1 -1
- package/out/address/keys/lookup.d.ts +42 -0
- package/out/address/keys/lookup.d.ts.map +1 -1
- package/out/address/keys/lookup.js +182 -6
- package/out/address/keys/lookup.js.map +1 -1
- package/out/address/keys/open.d.ts.map +1 -1
- package/out/address/keys/open.js +1 -0
- package/out/address/keys/open.js.map +1 -1
- package/out/address/keys/point-lookup.d.ts +6 -1
- package/out/address/keys/point-lookup.d.ts.map +1 -1
- package/out/address/keys/point-lookup.js +43 -1
- package/out/address/keys/point-lookup.js.map +1 -1
- package/out/address/keys/schema.d.ts +21 -0
- package/out/address/keys/schema.d.ts.map +1 -1
- package/out/address/keys/schema.js +18 -0
- package/out/address/keys/schema.js.map +1 -1
- package/out/address/number-forms.d.ts +10 -1
- package/out/address/number-forms.d.ts.map +1 -1
- package/out/address/number-forms.js +20 -11
- package/out/address/number-forms.js.map +1 -1
- package/out/address/point/interpolation.d.ts.map +1 -1
- package/out/address/point/interpolation.js +15 -5
- package/out/address/point/interpolation.js.map +1 -1
- package/out/address/point/schema.d.ts +45 -2
- package/out/address/point/schema.d.ts.map +1 -1
- package/out/address/point/schema.js +129 -0
- package/out/address/point/schema.js.map +1 -1
- package/out/address/point/scope-search.d.ts +77 -0
- package/out/address/point/scope-search.d.ts.map +1 -0
- package/out/address/point/scope-search.js +107 -0
- package/out/address/point/scope-search.js.map +1 -0
- package/out/address/point.d.ts +24 -1
- package/out/address/point.d.ts.map +1 -1
- package/out/address/point.js +231 -30
- package/out/address/point.js.map +1 -1
- package/out/build/candidate.d.ts +8 -0
- package/out/build/candidate.d.ts.map +1 -1
- package/out/build/candidate.js +17 -7
- package/out/build/candidate.js.map +1 -1
- package/out/candidate/ancestors/sidecar.d.ts +7 -3
- package/out/candidate/ancestors/sidecar.d.ts.map +1 -1
- package/out/candidate/ancestors/sidecar.js +56 -4
- package/out/candidate/ancestors/sidecar.js.map +1 -1
- package/out/candidate/del1.d.ts +61 -0
- package/out/candidate/del1.d.ts.map +1 -0
- package/out/candidate/del1.js +158 -0
- package/out/candidate/del1.js.map +1 -0
- package/out/candidate/lookup.d.ts +2 -14
- package/out/candidate/lookup.d.ts.map +1 -1
- package/out/candidate/lookup.js +122 -84
- package/out/candidate/lookup.js.map +1 -1
- package/out/candidate/schema.d.ts +8 -2
- package/out/candidate/schema.d.ts.map +1 -1
- package/out/candidate/schema.js +8 -2
- package/out/candidate/schema.js.map +1 -1
- package/out/fst/binary-matcher.d.ts +7 -1
- package/out/fst/binary-matcher.d.ts.map +1 -1
- package/out/fst/binary-matcher.js +7 -1
- package/out/fst/binary-matcher.js.map +1 -1
- package/out/fst/builder.d.ts +2 -0
- package/out/fst/builder.d.ts.map +1 -1
- package/out/fst/builder.js +5 -0
- package/out/fst/builder.js.map +1 -1
- package/out/fst/del1-sidecar.d.ts +158 -0
- package/out/fst/del1-sidecar.d.ts.map +1 -0
- package/out/fst/del1-sidecar.js +581 -0
- package/out/fst/del1-sidecar.js.map +1 -0
- package/out/fst/deserialize-web.d.ts +3 -1
- package/out/fst/deserialize-web.d.ts.map +1 -1
- package/out/fst/deserialize-web.js +9 -2
- package/out/fst/deserialize-web.js.map +1 -1
- package/out/fst/matcher.d.ts +6 -1
- package/out/fst/matcher.d.ts.map +1 -1
- package/out/fst/matcher.js +6 -1
- package/out/fst/matcher.js.map +1 -1
- package/out/fst/types.d.ts +7 -0
- package/out/fst/types.d.ts.map +1 -1
- package/out/fst.d.ts +1 -0
- package/out/fst.d.ts.map +1 -1
- package/out/fst.js +1 -0
- package/out/fst.js.map +1 -1
- package/out/index.d.ts +4 -3
- package/out/index.d.ts.map +1 -1
- package/out/index.js +3 -2
- package/out/index.js.map +1 -1
- package/out/labels/street/canonical.d.ts +68 -0
- package/out/labels/street/canonical.d.ts.map +1 -0
- package/out/labels/street/canonical.js +234 -0
- package/out/labels/street/canonical.js.map +1 -0
- package/out/labels/street/centroid/schema.d.ts +10 -1
- package/out/labels/street/centroid/schema.d.ts.map +1 -1
- package/out/labels/street/centroid/schema.js +12 -0
- package/out/labels/street/centroid/schema.js.map +1 -1
- package/out/labels/street/interpolation.d.ts +9 -1
- package/out/labels/street/interpolation.d.ts.map +1 -1
- package/out/labels/street/interpolation.js +8 -5
- package/out/labels/street/interpolation.js.map +1 -1
- package/out/labels/street/morphology/fst/builder.d.ts +8 -0
- package/out/labels/street/morphology/fst/builder.d.ts.map +1 -1
- package/out/labels/street/morphology/fst/builder.js +5 -0
- package/out/labels/street/morphology/fst/builder.js.map +1 -1
- package/out/labels/street/morphology/fst/loader.d.ts +12 -0
- package/out/labels/street/morphology/fst/loader.d.ts.map +1 -1
- package/out/labels/street/morphology/fst/loader.js +18 -2
- package/out/labels/street/morphology/fst/loader.js.map +1 -1
- package/out/labels/street/normalize.d.ts +8 -0
- package/out/labels/street/normalize.d.ts.map +1 -1
- package/out/labels/street/normalize.js +138 -4
- package/out/labels/street/normalize.js.map +1 -1
- package/out/labels/street.d.ts +1 -0
- package/out/labels/street.d.ts.map +1 -1
- package/out/labels/street.js +1 -0
- package/out/labels/street.js.map +1 -1
- package/out/landmark.d.ts +8 -0
- package/out/landmark.d.ts.map +1 -1
- package/out/landmark.js +25 -2
- package/out/landmark.js.map +1 -1
- package/out/lookup/types.d.ts +0 -5
- package/out/lookup/types.d.ts.map +1 -1
- package/out/poi/lookup.d.ts +9 -1
- package/out/poi/lookup.d.ts.map +1 -1
- package/out/poi/lookup.js +111 -53
- package/out/poi/lookup.js.map +1 -1
- package/out/poi/schema.d.ts +7 -2
- package/out/poi/schema.d.ts.map +1 -1
- package/out/poi/schema.js +3 -0
- package/out/poi/schema.js.map +1 -1
- package/out/schema/overture-match.d.ts +2 -1
- package/out/schema/overture-match.d.ts.map +1 -1
- package/out/schema/overture-match.js.map +1 -1
- package/out/weights/overlay-linker.d.ts +1 -1
- package/out/weights/overlay-linker.d.ts.map +1 -1
- package/out/weights/overlay-linker.js +5 -1
- package/out/weights/overlay-linker.js.map +1 -1
- package/package.json +12 -12
package/lib/address/point.ts
CHANGED
|
@@ -5,14 +5,25 @@
|
|
|
5
5
|
* @file SQLite implementation of core's `AddressPointLookup`.
|
|
6
6
|
*/
|
|
7
7
|
|
|
8
|
-
import type { AddressPointHit, AddressPointLookup } from "@mailwoman/core/resolver"
|
|
8
|
+
import type { AddressPointHit, AddressPointLookup, StreetCentroidHit, StreetMatchKind } from "@mailwoman/core/resolver"
|
|
9
|
+
import { allRows } from "@mailwoman/core/utils"
|
|
10
|
+
import { haversine, shortCellToInt } from "@mailwoman/spatial"
|
|
9
11
|
import { latLonBoundsContain } from "@mailwoman/spatial/bbox"
|
|
10
12
|
import { DatabaseClient } from "@mailwoman/sqlite/client"
|
|
11
13
|
import { indexExists } from "@mailwoman/sqlite/introspection"
|
|
14
|
+
import { gridDisk, latLngToCell } from "@sister.software/h3"
|
|
12
15
|
import type { PathBuilderLike } from "path-ts"
|
|
13
16
|
|
|
14
|
-
import { houseNumberForms } from "#address/number-forms"
|
|
17
|
+
import { type HouseNumberForm, houseNumberForms } from "#address/number-forms"
|
|
15
18
|
import type { AddressPointDatabase, AddressPointTable } from "#address/point/schema"
|
|
19
|
+
import {
|
|
20
|
+
postcodeProbeForms,
|
|
21
|
+
postcodesAgree,
|
|
22
|
+
postcodeScopeKey,
|
|
23
|
+
prepareStreetScopeIndexQueries,
|
|
24
|
+
searchStreetScopeIndex,
|
|
25
|
+
type StreetScopeIndexQueries,
|
|
26
|
+
} from "#address/point/scope-search"
|
|
16
27
|
import {
|
|
17
28
|
normalizeHouseNumberForKey,
|
|
18
29
|
type NameKey,
|
|
@@ -38,6 +49,46 @@ type AddressPointRow = Pick<AddressPointTable, "lat" | "lon" | "source" | "relea
|
|
|
38
49
|
*/
|
|
39
50
|
const SELECT_COLS = "lat, lon, source, release, locality_norm, postcode"
|
|
40
51
|
|
|
52
|
+
/**
|
|
53
|
+
* H3 resolution stored in `address_point.h3_cell` by the layer sealing step.
|
|
54
|
+
*/
|
|
55
|
+
const ADDRESS_POINT_H3_RESOLUTION = 9
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Cell count of a hexagonal H3 1-ring (the center cell plus its six neighbors).
|
|
59
|
+
*/
|
|
60
|
+
const H3_RING_1_CELL_COUNT = 7
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Default search radius in meters when checking whether a street runs past a coordinate.
|
|
64
|
+
*/
|
|
65
|
+
const DEFAULT_STREET_NEAR_RADIUS_M = 200
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Maximum diagonal span in kilometers allowed for a street centroid aggregate.
|
|
69
|
+
*
|
|
70
|
+
* Prevents multi-city or corrupt point clusters from answering as one street.
|
|
71
|
+
*/
|
|
72
|
+
const MAX_STREET_CENTROID_SPAN_KM = 25
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Minimum reported uncertainty radius in meters for a street centroid derived from address points.
|
|
76
|
+
*/
|
|
77
|
+
const MIN_STREET_CENTROID_UNCERTAINTY_M = 50
|
|
78
|
+
|
|
79
|
+
interface StreetCentroidAggregateRow {
|
|
80
|
+
lat: number | null
|
|
81
|
+
lon: number | null
|
|
82
|
+
min_lat: number | null
|
|
83
|
+
max_lat: number | null
|
|
84
|
+
min_lon: number | null
|
|
85
|
+
max_lon: number | null
|
|
86
|
+
places: number
|
|
87
|
+
n: number
|
|
88
|
+
source: string | null
|
|
89
|
+
release: string | null
|
|
90
|
+
}
|
|
91
|
+
|
|
41
92
|
/**
|
|
42
93
|
* Finds an address point by exact normalized `(street, number)` within a postcode,
|
|
43
94
|
* a locality, or a bounding box.
|
|
@@ -78,12 +129,23 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
78
129
|
| PreparedGet<[street: StreetKey, number: string, tailPattern: string], AddressPointRow>
|
|
79
130
|
| undefined
|
|
80
131
|
readonly #hasStreetIndex: boolean = false
|
|
132
|
+
readonly #scopeIndex: StreetScopeIndexQueries | null = null
|
|
133
|
+
readonly #byH3Street: ReturnType<DatabaseClient<DB>["prepare"]> | undefined
|
|
81
134
|
readonly #byBbox:
|
|
82
135
|
| PreparedGet<
|
|
83
136
|
[street: StreetKey, number: string, minLat: number, maxLat: number, minLon: number, maxLon: number],
|
|
84
137
|
AddressPointRow
|
|
85
138
|
>
|
|
86
139
|
| undefined
|
|
140
|
+
readonly #centroidByPostcode:
|
|
141
|
+
| PreparedGet<[postcode: string, street: StreetKey], StreetCentroidAggregateRow>
|
|
142
|
+
| undefined
|
|
143
|
+
readonly #centroidByPostcodeLocality:
|
|
144
|
+
| PreparedGet<[postcode: string, locality: NameKey, street: StreetKey], StreetCentroidAggregateRow>
|
|
145
|
+
| undefined
|
|
146
|
+
readonly #centroidByLocality:
|
|
147
|
+
| PreparedGet<[locality: NameKey, street: StreetKey], StreetCentroidAggregateRow>
|
|
148
|
+
| undefined
|
|
87
149
|
|
|
88
150
|
/**
|
|
89
151
|
* @param dbPath Extract path.
|
|
@@ -124,13 +186,13 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
124
186
|
this.#byPostcode = prepareGet(
|
|
125
187
|
this.#db,
|
|
126
188
|
`SELECT ${SELECT_COLS} FROM address_point
|
|
127
|
-
WHERE postcode = ? AND street_norm = ? AND number = ? LIMIT 1`
|
|
189
|
+
WHERE postcode = ? AND street_norm = ? AND number = ? ORDER BY unit IS NOT NULL LIMIT 1`
|
|
128
190
|
)
|
|
129
191
|
|
|
130
192
|
this.#byPostcodeLocality = prepareGet(
|
|
131
193
|
this.#db,
|
|
132
194
|
`SELECT ${SELECT_COLS} FROM address_point
|
|
133
|
-
WHERE postcode = ? AND locality_norm = ? AND street_norm = ? AND number = ? LIMIT 1`
|
|
195
|
+
WHERE postcode = ? AND locality_norm = ? AND street_norm = ? AND number = ? ORDER BY unit IS NOT NULL LIMIT 1`
|
|
134
196
|
)
|
|
135
197
|
|
|
136
198
|
this.#postcodeKeyPlaces = prepareGet(
|
|
@@ -142,15 +204,22 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
142
204
|
this.#byLocality = prepareGet(
|
|
143
205
|
this.#db,
|
|
144
206
|
`SELECT ${SELECT_COLS} FROM address_point
|
|
145
|
-
WHERE locality_norm = ? AND street_norm = ? AND number = ? LIMIT 1`
|
|
207
|
+
WHERE locality_norm = ? AND street_norm = ? AND number = ? ORDER BY unit IS NOT NULL LIMIT 1`
|
|
146
208
|
)
|
|
147
209
|
|
|
148
210
|
this.#hasStreetIndex = indexExists(this.#db, "idx_ap_street")
|
|
149
211
|
|
|
212
|
+
if (indexExists(this.#db, "idx_ap_h3")) {
|
|
213
|
+
// The unary `+` keeps SQLite on `idx_ap_h3` rather than scanning a high-cardinality street name.
|
|
214
|
+
this.#byH3Street = this.#db.prepare(
|
|
215
|
+
"SELECT lat, lon FROM address_point WHERE h3_cell IN (?, ?, ?, ?, ?, ?, ?) AND +street_norm = ?"
|
|
216
|
+
)
|
|
217
|
+
}
|
|
218
|
+
|
|
150
219
|
this.#byBbox = prepareGet(
|
|
151
220
|
this.#db,
|
|
152
221
|
`SELECT ${SELECT_COLS} FROM address_point
|
|
153
|
-
WHERE street_norm = ? AND number = ? AND lat BETWEEN ? AND ? AND lon BETWEEN ? AND ? LIMIT 1`
|
|
222
|
+
WHERE street_norm = ? AND number = ? AND lat BETWEEN ? AND ? AND lon BETWEEN ? AND ? ORDER BY unit IS NOT NULL LIMIT 1`
|
|
154
223
|
)
|
|
155
224
|
|
|
156
225
|
this.#byLocalityTail = prepareGet(
|
|
@@ -158,6 +227,28 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
158
227
|
`SELECT ${SELECT_COLS} FROM address_point
|
|
159
228
|
WHERE street_norm = ? AND number = ? AND locality_norm LIKE ? LIMIT 1`
|
|
160
229
|
)
|
|
230
|
+
|
|
231
|
+
const centroidCols =
|
|
232
|
+
"AVG(lat) AS lat, AVG(lon) AS lon, MIN(lat) AS min_lat, MAX(lat) AS max_lat, " +
|
|
233
|
+
"MIN(lon) AS min_lon, MAX(lon) AS max_lon, COUNT(DISTINCT locality_norm) AS places, " +
|
|
234
|
+
"COUNT(*) AS n, MIN(source) AS source, MIN(release) AS release"
|
|
235
|
+
|
|
236
|
+
this.#centroidByPostcode = prepareGet(
|
|
237
|
+
this.#db,
|
|
238
|
+
`SELECT ${centroidCols} FROM address_point WHERE postcode = ? AND street_norm = ?`
|
|
239
|
+
)
|
|
240
|
+
|
|
241
|
+
this.#centroidByPostcodeLocality = prepareGet(
|
|
242
|
+
this.#db,
|
|
243
|
+
`SELECT ${centroidCols} FROM address_point WHERE postcode = ? AND locality_norm = ? AND street_norm = ?`
|
|
244
|
+
)
|
|
245
|
+
|
|
246
|
+
this.#centroidByLocality = prepareGet(
|
|
247
|
+
this.#db,
|
|
248
|
+
`SELECT ${centroidCols} FROM address_point WHERE locality_norm = ? AND street_norm = ?`
|
|
249
|
+
)
|
|
250
|
+
|
|
251
|
+
this.#scopeIndex = prepareStreetScopeIndexQueries(this.#db)
|
|
161
252
|
}
|
|
162
253
|
}
|
|
163
254
|
|
|
@@ -186,29 +277,142 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
186
277
|
: { ...query, localityTail: this.#rules.localityKey(query.subregion) }
|
|
187
278
|
: query
|
|
188
279
|
|
|
189
|
-
//
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
let row: AddressPointRow | null = null
|
|
280
|
+
// Step 1: exact / existing streetKeyVariants (zero overhead on exact hits).
|
|
281
|
+
const surfaceLocale = streetLocaleForSurface(query.street, this.#locale)
|
|
282
|
+
const variants = streetKeyVariants(query.street, surfaceLocale)
|
|
193
283
|
|
|
194
|
-
|
|
195
|
-
row
|
|
284
|
+
let found: {
|
|
285
|
+
row: AddressPointRow
|
|
286
|
+
form: HouseNumberForm
|
|
287
|
+
matchedStreet: StreetKey
|
|
288
|
+
streetMatch: StreetMatchKind
|
|
289
|
+
} | null = null
|
|
290
|
+
|
|
291
|
+
for (let i = 0; i < variants.length; i++) {
|
|
292
|
+
const streetNorm = variants[i]!
|
|
293
|
+
const hit = this.#findForKey(streetNorm, number, scoped)
|
|
196
294
|
|
|
197
|
-
if (
|
|
295
|
+
if (hit) {
|
|
296
|
+
found = { ...hit, matchedStreet: streetNorm, streetMatch: i === 0 ? "written" : "respelled" }
|
|
297
|
+
|
|
298
|
+
break
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
// Steps 2 & 3: canonical-alias and scoped 1-edit fuzzy recovery via `street_scope_index`.
|
|
303
|
+
if (!found && this.#scopeIndex) {
|
|
304
|
+
const postcodeScope = scoped.postcode ? postcodeScopeKey(scoped.postcode, this.#locale) : null
|
|
305
|
+
const localityScope = scoped.locality ? this.#localityKey(scoped.locality) : null
|
|
306
|
+
|
|
307
|
+
const recovered = searchStreetScopeIndex({
|
|
308
|
+
queries: this.#scopeIndex,
|
|
309
|
+
streetRaw: query.street,
|
|
310
|
+
locale: surfaceLocale,
|
|
311
|
+
tried: new Set(variants),
|
|
312
|
+
postcodeScope,
|
|
313
|
+
localityScope,
|
|
314
|
+
probeInScope: (scopeKind, streetNorm) => this.#findForKeyInScope(scopeKind, streetNorm, number, scoped),
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
if (recovered) {
|
|
318
|
+
found = {
|
|
319
|
+
...recovered.hit,
|
|
320
|
+
matchedStreet: recovered.matchedStreet,
|
|
321
|
+
streetMatch: recovered.streetMatch,
|
|
322
|
+
}
|
|
323
|
+
}
|
|
198
324
|
}
|
|
199
325
|
|
|
200
|
-
if (!
|
|
326
|
+
if (!found) return null
|
|
201
327
|
|
|
202
328
|
return {
|
|
203
|
-
lat: row.lat,
|
|
204
|
-
lon: row.lon,
|
|
205
|
-
source: row.source,
|
|
206
|
-
release: row.release,
|
|
207
|
-
localityNorm: row.locality_norm || null,
|
|
208
|
-
postcode: row.postcode || null,
|
|
329
|
+
lat: found.row.lat,
|
|
330
|
+
lon: found.row.lon,
|
|
331
|
+
source: found.row.source,
|
|
332
|
+
release: found.row.release,
|
|
333
|
+
localityNorm: found.row.locality_norm || null,
|
|
334
|
+
postcode: found.row.postcode || null,
|
|
335
|
+
matchedNumber: found.form.number,
|
|
336
|
+
numberMatch: found.form.match,
|
|
337
|
+
matchedStreet: found.matchedStreet,
|
|
338
|
+
streetMatch: found.streetMatch,
|
|
209
339
|
}
|
|
210
340
|
}
|
|
211
341
|
|
|
342
|
+
/**
|
|
343
|
+
* Probes one candidate street within a single scope (`postcode` or `locality`) using
|
|
344
|
+
* `houseNumberForms`, for Step 2 (`canonical-alias`) and Step 3 (`fuzzy-edit1`) confirmation.
|
|
345
|
+
*/
|
|
346
|
+
#findForKeyInScope(
|
|
347
|
+
scopeKind: "postcode" | "locality",
|
|
348
|
+
streetNorm: StreetKey,
|
|
349
|
+
number: string,
|
|
350
|
+
query: {
|
|
351
|
+
street: string
|
|
352
|
+
number: string
|
|
353
|
+
postcode?: string
|
|
354
|
+
locality?: string
|
|
355
|
+
localityTail?: NameKey
|
|
356
|
+
bbox: { minLat: number; maxLat: number; minLon: number; maxLon: number } | null
|
|
357
|
+
}
|
|
358
|
+
): { row: AddressPointRow; form: HouseNumberForm } | null {
|
|
359
|
+
for (const form of houseNumberForms(number, this.#locale)) {
|
|
360
|
+
const row =
|
|
361
|
+
scopeKind === "postcode"
|
|
362
|
+
? this.#probePostcodeScope(streetNorm, form.number, query)
|
|
363
|
+
: this.#probeLocalityScope(streetNorm, form.number, query)
|
|
364
|
+
|
|
365
|
+
if (row) return { row, form }
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return null
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
#probePostcodeScope(
|
|
372
|
+
streetNorm: StreetKey,
|
|
373
|
+
number: string,
|
|
374
|
+
query: {
|
|
375
|
+
postcode?: string
|
|
376
|
+
locality?: string
|
|
377
|
+
bbox: { minLat: number; maxLat: number; minLon: number; maxLon: number } | null
|
|
378
|
+
}
|
|
379
|
+
): AddressPointRow | null {
|
|
380
|
+
if (!query.postcode) return null
|
|
381
|
+
|
|
382
|
+
const localityKey = query.locality ? this.#localityKey(query.locality) : null
|
|
383
|
+
let heldBack: AddressPointRow | null = null
|
|
384
|
+
|
|
385
|
+
for (const postcode of postcodeProbeForms(query.postcode, this.#locale)) {
|
|
386
|
+
const agreeing = localityKey ? this.#byPostcodeLocality!(postcode, localityKey, streetNorm, number) : null
|
|
387
|
+
const candidate = agreeing ?? this.#byPostcode!(postcode, streetNorm, number)
|
|
388
|
+
|
|
389
|
+
if (candidate && !this.#scopeContradicts(candidate, query)) {
|
|
390
|
+
return candidate
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if (!heldBack && candidate && this.#isSublocalityRow(candidate, postcode, streetNorm, number, query.bbox)) {
|
|
394
|
+
heldBack = candidate
|
|
395
|
+
}
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
return heldBack
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
#probeLocalityScope(
|
|
402
|
+
streetNorm: StreetKey,
|
|
403
|
+
number: string,
|
|
404
|
+
query: {
|
|
405
|
+
postcode?: string
|
|
406
|
+
locality?: string
|
|
407
|
+
}
|
|
408
|
+
): AddressPointRow | null {
|
|
409
|
+
if (!query.locality) return null
|
|
410
|
+
|
|
411
|
+
const candidate = this.#byLocality!(this.#localityKey(query.locality), streetNorm, number)
|
|
412
|
+
|
|
413
|
+
return candidate && !this.#scopeContradicts(candidate, query) ? candidate : null
|
|
414
|
+
}
|
|
415
|
+
|
|
212
416
|
/**
|
|
213
417
|
* Tries one normalized street key with the exact number, then fallback number forms.
|
|
214
418
|
* Each fallback runs only after the previous forms missed.
|
|
@@ -224,11 +428,11 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
224
428
|
localityTail?: NameKey
|
|
225
429
|
bbox: { minLat: number; maxLat: number; minLon: number; maxLon: number } | null
|
|
226
430
|
}
|
|
227
|
-
) {
|
|
431
|
+
): { row: AddressPointRow; form: HouseNumberForm } | null {
|
|
228
432
|
for (const form of houseNumberForms(number, this.#locale)) {
|
|
229
|
-
const row = this.#probe(streetNorm, form, query)
|
|
433
|
+
const row = this.#probe(streetNorm, form.number, query)
|
|
230
434
|
|
|
231
|
-
if (row) return row
|
|
435
|
+
if (row) return { row, form }
|
|
232
436
|
}
|
|
233
437
|
|
|
234
438
|
return null
|
|
@@ -253,19 +457,25 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
253
457
|
let heldBack: AddressPointRow | null = null
|
|
254
458
|
|
|
255
459
|
if (query.postcode) {
|
|
256
|
-
const postcode = query.postcode.trim()
|
|
257
460
|
const localityKey = query.locality ? this.#localityKey(query.locality) : null
|
|
258
461
|
|
|
259
462
|
// One postcode can cover several places with the same street and number,
|
|
260
463
|
// so a row whose locality agrees with the query is tried first.
|
|
464
|
+
// Probe both raw trimmed and compact normalized forms (`4413 ND` → `4413ND`).
|
|
261
465
|
// The postcode-only row answers only if `#scopeContradicts` accepts it.
|
|
262
|
-
const
|
|
263
|
-
|
|
466
|
+
for (const postcode of postcodeProbeForms(query.postcode, this.#locale)) {
|
|
467
|
+
const agreeing = localityKey ? this.#byPostcodeLocality!(postcode, localityKey, streetNorm, number) : null
|
|
468
|
+
const candidate = agreeing ?? this.#byPostcode!(postcode, streetNorm, number)
|
|
264
469
|
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
470
|
+
if (candidate && !this.#scopeContradicts(candidate, query)) {
|
|
471
|
+
row = candidate
|
|
472
|
+
|
|
473
|
+
break
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
if (!heldBack && candidate && this.#isSublocalityRow(candidate, postcode, streetNorm, number, query.bbox)) {
|
|
477
|
+
heldBack = candidate
|
|
478
|
+
}
|
|
269
479
|
}
|
|
270
480
|
}
|
|
271
481
|
|
|
@@ -319,7 +529,7 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
319
529
|
* name that OSM's `addr:city` records, and a word boundary keeps `lauta` apart from `lautabach`.
|
|
320
530
|
*/
|
|
321
531
|
#scopeContradicts(row: AddressPointRow, query: { postcode?: string; locality?: string }): boolean {
|
|
322
|
-
if (query.postcode && row.postcode && row.postcode
|
|
532
|
+
if (query.postcode && row.postcode && !postcodesAgree(row.postcode, query.postcode, this.#locale)) return true
|
|
323
533
|
|
|
324
534
|
if (this.#localityKeys === "full" && query.locality && row.locality_norm) {
|
|
325
535
|
const queryKey = this.#localityKey(query.locality)
|
|
@@ -356,7 +566,7 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
356
566
|
): boolean {
|
|
357
567
|
if (this.#localityLevel !== "finest") return false
|
|
358
568
|
|
|
359
|
-
if (row.postcode && row.postcode
|
|
569
|
+
if (row.postcode && !postcodesAgree(row.postcode, postcode, this.#locale)) return false
|
|
360
570
|
|
|
361
571
|
if ((this.#postcodeKeyPlaces?.(postcode, streetNorm, number)?.places ?? 0) !== 1) return false
|
|
362
572
|
|
|
@@ -365,6 +575,129 @@ export class AddressPointSqliteLookup<DB extends AddressPointDatabase = AddressP
|
|
|
365
575
|
return latLonBoundsContain(bbox, row.lat, row.lon)
|
|
366
576
|
}
|
|
367
577
|
|
|
578
|
+
/**
|
|
579
|
+
* Reports whether `query.street` sits within `query.radiusM` m of the point.
|
|
580
|
+
*
|
|
581
|
+
* Returns `false` when the extract lacks `idx_ap_h3`.
|
|
582
|
+
* An unindexed database therefore never falls back to a table scan.
|
|
583
|
+
*/
|
|
584
|
+
hasStreetNear(query: { street: string; lat: number; lon: number; radiusM?: number }): boolean {
|
|
585
|
+
if (!this.#byH3Street) return false
|
|
586
|
+
|
|
587
|
+
const radiusM = query.radiusM ?? DEFAULT_STREET_NEAR_RADIUS_M
|
|
588
|
+
const disk = gridDisk(latLngToCell(query.lat, query.lon, ADDRESS_POINT_H3_RESOLUTION), 1).map(shortCellToInt)
|
|
589
|
+
|
|
590
|
+
if (!disk.length) return false
|
|
591
|
+
|
|
592
|
+
// A 1-ring around a pentagon holds 6 cells rather than 7.
|
|
593
|
+
// The loop pads with the center cell so one prepared statement serves every query.
|
|
594
|
+
while (disk.length < H3_RING_1_CELL_COUNT) {
|
|
595
|
+
disk.push(disk[0]!)
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
for (const streetNorm of streetKeyVariants(query.street, streetLocaleForSurface(query.street, this.#locale))) {
|
|
599
|
+
const rows = allRows<{ lat: number; lon: number }>(
|
|
600
|
+
this.#byH3Street,
|
|
601
|
+
disk[0]!,
|
|
602
|
+
disk[1]!,
|
|
603
|
+
disk[2]!,
|
|
604
|
+
disk[3]!,
|
|
605
|
+
disk[4]!,
|
|
606
|
+
disk[5]!,
|
|
607
|
+
disk[6]!,
|
|
608
|
+
streetNorm
|
|
609
|
+
)
|
|
610
|
+
|
|
611
|
+
if (rows.some((row) => haversine(query.lat, query.lon, row.lat, row.lon) * 1000 <= radiusM)) {
|
|
612
|
+
return true
|
|
613
|
+
}
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
return false
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
/**
|
|
620
|
+
* Aggregates a street-tier centroid inside `postcode` or `locality`.
|
|
621
|
+
*
|
|
622
|
+
* When `postcode` is given, only rows in that postcode are eligible.
|
|
623
|
+
* Returns `null` when no rows match or the span exceeds {@link MAX_STREET_CENTROID_SPAN_KM}.
|
|
624
|
+
*/
|
|
625
|
+
findStreetCentroid(query: { street: string; postcode?: string; locality?: string }): StreetCentroidHit | null {
|
|
626
|
+
if (!this.#centroidByPostcode || !this.#centroidByPostcodeLocality || !this.#centroidByLocality) return null
|
|
627
|
+
|
|
628
|
+
const postcodeRaw = query.postcode?.trim() || null
|
|
629
|
+
const localityRaw = query.locality?.trim() || null
|
|
630
|
+
|
|
631
|
+
if (!postcodeRaw && !localityRaw) return null
|
|
632
|
+
|
|
633
|
+
const surfaceLocale = streetLocaleForSurface(query.street, this.#locale)
|
|
634
|
+
const variants = streetKeyVariants(query.street, surfaceLocale)
|
|
635
|
+
const localityKey = localityRaw ? this.#localityKey(localityRaw) : null
|
|
636
|
+
|
|
637
|
+
for (const streetNorm of variants) {
|
|
638
|
+
let aggregate: StreetCentroidAggregateRow | null = null
|
|
639
|
+
|
|
640
|
+
if (postcodeRaw) {
|
|
641
|
+
for (const postcode of postcodeProbeForms(postcodeRaw, this.#locale)) {
|
|
642
|
+
const byBoth = localityKey ? this.#centroidByPostcodeLocality(postcode, localityKey, streetNorm) : null
|
|
643
|
+
|
|
644
|
+
if (byBoth && byBoth.n > 0) {
|
|
645
|
+
aggregate = byBoth
|
|
646
|
+
|
|
647
|
+
break
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
const byPC = this.#centroidByPostcode(postcode, streetNorm)
|
|
651
|
+
|
|
652
|
+
if (
|
|
653
|
+
byPC &&
|
|
654
|
+
byPC.n > 0 &&
|
|
655
|
+
(!localityKey ||
|
|
656
|
+
this.#localityKeys === "abbreviated" ||
|
|
657
|
+
(this.#localityLevel === "finest" && byPC.places <= 1))
|
|
658
|
+
) {
|
|
659
|
+
aggregate = byPC
|
|
660
|
+
|
|
661
|
+
break
|
|
662
|
+
}
|
|
663
|
+
}
|
|
664
|
+
} else if (localityKey) {
|
|
665
|
+
const byLoc = this.#centroidByLocality(localityKey, streetNorm)
|
|
666
|
+
|
|
667
|
+
if (byLoc && byLoc.n > 0) {
|
|
668
|
+
aggregate = byLoc
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
if (
|
|
673
|
+
!aggregate ||
|
|
674
|
+
aggregate.n <= 0 ||
|
|
675
|
+
aggregate.lat == null ||
|
|
676
|
+
aggregate.lon == null ||
|
|
677
|
+
aggregate.min_lat == null ||
|
|
678
|
+
aggregate.max_lat == null ||
|
|
679
|
+
aggregate.min_lon == null ||
|
|
680
|
+
aggregate.max_lon == null
|
|
681
|
+
) {
|
|
682
|
+
continue
|
|
683
|
+
}
|
|
684
|
+
|
|
685
|
+
const spanKm = haversine(aggregate.min_lat, aggregate.min_lon, aggregate.max_lat, aggregate.max_lon)
|
|
686
|
+
|
|
687
|
+
if (spanKm > MAX_STREET_CENTROID_SPAN_KM) continue
|
|
688
|
+
|
|
689
|
+
return {
|
|
690
|
+
lat: aggregate.lat,
|
|
691
|
+
lon: aggregate.lon,
|
|
692
|
+
uncertaintyM: Math.max(MIN_STREET_CENTROID_UNCERTAINTY_M, Math.round((spanKm * 1000) / 2)),
|
|
693
|
+
source: aggregate.source ?? "",
|
|
694
|
+
release: aggregate.release ?? "",
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
return null
|
|
699
|
+
}
|
|
700
|
+
|
|
368
701
|
[Symbol.dispose](): void {
|
|
369
702
|
this.#db[Symbol.dispose]()
|
|
370
703
|
}
|
package/lib/build/candidate.ts
CHANGED
|
@@ -17,6 +17,7 @@ import { stageBareAdminNames } from "#candidate/bare-admin-names"
|
|
|
17
17
|
import { createCapitalTable } from "#candidate/capital-schema"
|
|
18
18
|
import type { CapitalPoint } from "#candidate/capitals"
|
|
19
19
|
import { stageCountryDisplayNames } from "#candidate/country-display-names"
|
|
20
|
+
import { createCandidateDel1 } from "#candidate/del1"
|
|
20
21
|
import { stageExtractRows } from "#candidate/extract-rows"
|
|
21
22
|
import { createCandidateFTS } from "#candidate/fts"
|
|
22
23
|
import { IMPORTANCE_JOIN_RADIUS_KM, loadImportanceIndex } from "#candidate/importance"
|
|
@@ -289,6 +290,16 @@ export interface BuildCandidateResult {
|
|
|
289
290
|
* Count of rows written to `candidate_sitelinks`, or null when no sitelinks source was supplied.
|
|
290
291
|
*/
|
|
291
292
|
sitelinks: number | null
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Count of distinct prominent `name_key` values indexed in `candidate_del1`.
|
|
296
|
+
*/
|
|
297
|
+
del1Keys: number
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Count of `(del_key, name_key)` rows written to `candidate_del1`.
|
|
301
|
+
*/
|
|
302
|
+
del1Rows: number
|
|
292
303
|
}
|
|
293
304
|
|
|
294
305
|
/**
|
|
@@ -726,15 +737,7 @@ export async function buildCandidateTable(opts: BuildCandidateOptions): Promise<
|
|
|
726
737
|
progress("key-fold", `${fold}: ${rekeyed.toLocaleString()} rows rekeyed, ${merged.toLocaleString()} merged`)
|
|
727
738
|
}
|
|
728
739
|
|
|
729
|
-
|
|
730
|
-
createCandidateFTS(kdb)
|
|
731
|
-
|
|
732
|
-
const postcodePlacetypeID = ptcodes.get(POSTCODE_PLACETYPE)
|
|
733
|
-
|
|
734
|
-
if (postcodePlacetypeID != null) {
|
|
735
|
-
progress("postcode-index", `indexing postcode places by spr_id (placetype id ${postcodePlacetypeID})`)
|
|
736
|
-
await createCandidatePostcodePlaceIndex(kdb, postcodePlacetypeID)
|
|
737
|
-
}
|
|
740
|
+
const del1 = await buildCandidateIndexes(kdb, ptcodes, progress)
|
|
738
741
|
|
|
739
742
|
kdb.exec("PRAGMA page_size=8192")
|
|
740
743
|
kdb.exec("VACUUM")
|
|
@@ -767,9 +770,33 @@ export async function buildCandidateTable(opts: BuildCandidateOptions): Promise<
|
|
|
767
770
|
outcomes,
|
|
768
771
|
concordances: nConcordances,
|
|
769
772
|
sitelinks: nSitelinks,
|
|
773
|
+
del1Keys: del1.keys,
|
|
774
|
+
del1Rows: del1.rows,
|
|
770
775
|
}
|
|
771
776
|
}
|
|
772
777
|
|
|
778
|
+
async function buildCandidateIndexes(
|
|
779
|
+
kdb: DatabaseClient<CandidateDatabase>,
|
|
780
|
+
ptcodes: ReadonlyMap<string, number>,
|
|
781
|
+
progress: (stage: string, detail: string) => void
|
|
782
|
+
): Promise<{ keys: number; rows: number }> {
|
|
783
|
+
progress("fts", "building FTS5-trigram fuzzy index")
|
|
784
|
+
createCandidateFTS(kdb)
|
|
785
|
+
|
|
786
|
+
progress("del1", "building prominent-place SymSpell 1-delete fuzzy index")
|
|
787
|
+
const del1 = await createCandidateDel1(kdb)
|
|
788
|
+
progress("del1", `${del1.keys.toLocaleString()} keys, ${del1.rows.toLocaleString()} rows`)
|
|
789
|
+
|
|
790
|
+
const postcodePlacetypeID = ptcodes.get(POSTCODE_PLACETYPE)
|
|
791
|
+
|
|
792
|
+
if (postcodePlacetypeID != null) {
|
|
793
|
+
progress("postcode-index", `indexing postcode places by spr_id (placetype id ${postcodePlacetypeID})`)
|
|
794
|
+
await createCandidatePostcodePlaceIndex(kdb, postcodePlacetypeID)
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
return del1
|
|
798
|
+
}
|
|
799
|
+
|
|
773
800
|
async function stageCandidateConcordancesAndSitelinks(
|
|
774
801
|
src: DatabaseClient<WOFDatabase>,
|
|
775
802
|
kdb: DatabaseClient<CandidateDatabase>,
|