-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocoder.js
432 lines (354 loc) · 11.4 KB
/
geocoder.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
import * as fuzz from "fuzzball";
import { ofnBeProfile } from "./configs/ofnBe";
import {
photonSearch,
nominatimGetDetails,
nominatimLookup,
extractOsmUuids,
} from "./framework.js";
const layersMapping = {
house: ["house"],
street: ["street"],
locality: ["locality", "district", "city"],
region: ["county", "state"],
country: ["country"],
};
const untilPropertyMapping = {
house: "housenumber",
street: "street",
locality: "district",
region: "county",
country: "countrycode",
};
const levelsMajorToMinor = ["country", "region", "locality", "street", "house"];
export async function photonLocate(input, config) {
const defaultConfig = {
untilLevel: "house",
};
config = Object.assign(defaultConfig, config);
// TODO a bit of a hack to build a strategy per
const strategy = buildStrategy(input, config);
const geocoder = new Geocoder(strategy);
return await geocoder.locate(input);
}
function addressLevelsAsc(input) {
const sortedKeys = Object.keys(input).sort(
(a, b) => levelsMajorToMinor.indexOf(a) - levelsMajorToMinor.indexOf(b)
);
return sortedKeys;
}
function addressLevelsDesc(input) {
return addressLevelsAsc(input).reverse();
}
function addressLevelsDecreasing(input) {
const sortedKeys = Object.keys(input).sort(
(a, b) => levelsMajorToMinor.indexOf(a) - levelsMajorToMinor.indexOf(b)
);
return sortedKeys;
}
function addressTagsForSpecificLevel(input, level) {
const slicedKeys = addressLevelsDesc(input).filter(
(k) => levelsMajorToMinor.indexOf(level) >= levelsMajorToMinor.indexOf(k)
);
const tags = slicedKeys.map((k) => input[k]).flat();
return tags;
}
// TODO input variable confusing with anonymous function input parameter
export function buildStrategy(input, config) {
// TODO : a bit of a hack
const postalCode = input.postalCode;
delete input.postalCode;
for (const [key, value] of Object.entries(input)) {
if (Array.isArray(value) == false) input[key] = [value];
}
if (config?.untilLevel != null && input[config.untilLevel] == null)
input[config.untilLevel] = [];
const postalCodeLevel = config?.postalCodeLevel ?? "locality";
if (postalCode != null) {
if (input[postalCodeLevel] == null) input[postalCodeLevel] = [];
input[postalCodeLevel].push(postalCode);
}
const strategy = {
key: "default",
validationThreshold: 80,
skipBreakingAtLayers: ["street"],
};
const tactics = [];
for (const level of addressLevelsDecreasing(input)) {
const layers = layersMapping[level];
const untilProperty = untilPropertyMapping[level];
const tactic = {
key: "global_" + level,
layers: layers,
untilProperty: untilProperty,
searchQuery: (input) => {
return addressTagsForSpecificLevel(input, level);
},
};
if (level == "country") {
tactic.postTransform = (input, features) => {
// If a unique country is found, replace input country name with this one
if (features?.length == 1)
input.country = [features[0].properties.country];
};
}
if (["house", "street"].includes(level) && input.street?.[0] != null) {
tactic.validateAgainst = (input) => {
return {
street: input.street[0],
};
};
}
tactics.push(tactic);
}
strategy.tactics = tactics.map((t) => t.key);
return {
properties: [
"countrycode",
"state",
"county",
"city",
"district",
"postcode",
"street",
"housenumber",
],
selectStrategy: (input) => {
return "default";
},
strategies: [strategy],
tactics: tactics,
};
}
// give services (their url), mapping configurations
export class Geocoder {
constructor(profile, config) {
this.photonUrl = config?.photon?.url ?? "https://photon.komoot.io/api";
this.profile = profile;
}
async locate(input) {
const properties = this.profile.properties;
const selectedStrategy = this.profile.selectStrategy(input);
const currentStrategy = this.profile.strategies.find(
(s) => s.key == selectedStrategy
);
if (currentStrategy.preTransform) currentStrategy.preTransform(input);
const currentTactics = currentStrategy.tactics.map((t1) =>
this.profile.tactics.find((t2) => t2.key == t1)
);
for (const t of currentTactics) {
t.properties = properties.slice(
0,
properties.indexOf(t.untilProperty) + 1
);
}
const runs = await this.makeAndExecuteRuns(input, currentTactics);
const matrix = this.getPropertyValues(runs, input, currentStrategy);
let groupedMatrix = this.getGroupedPropertyValues(
matrix,
currentTactics.map((t) => t.key)
);
const exactMatch = {};
const previousMatch = [];
for (const p of properties) {
const myTactics = currentTactics
.filter((t) => t.properties.includes(p))
// skip tactics where run didn't give any result ...
.filter(
(t) =>
runs.find((r) => r.features?.length == 0 && r.tactic.key == t) ==
false
)
.map((t) => t.key);
const bestMatches = groupedMatrix.filter(
(g) => g.property == p && myTactics.every((t) => g.keys.includes(t))
);
let bestMatch = null;
for (const best of bestMatches) {
if (
previousMatch.every((p) =>
best.uuids.some((u) => p.uuids.includes(u))
)
) {
bestMatch = best;
break;
}
}
if (bestMatch != null) {
exactMatch[p] = bestMatch.value;
previousMatch.push(bestMatch);
}
}
const arrays = previousMatch.map((p) => p.uuids);
if (arrays.length == 0) return;
for (const pp of previousMatch) {
let indexToKeep = pp.isExactTypes.reduce(
(acc, curr, index) => (curr ? [...acc, index] : acc),
[]
);
const indexToKeepNodes = indexToKeep.filter(
(ik) => pp.uuids[ik].startsWith("N") // node has priority over way
);
if (pp.property == "housenumber" && indexToKeepNodes.length == 1) {
indexToKeep = indexToKeepNodes;
}
const singleIndexToKeep = indexToKeep[0];
if (indexToKeep.length == 1) {
pp.exactType = pp.exactTypes[singleIndexToKeep];
pp.key = pp.keys[singleIndexToKeep];
pp.uuid = pp.uuids[singleIndexToKeep];
}
delete pp.isExactTypes;
delete pp.uniqueKeys;
delete pp.exactTypes;
delete pp.keys;
delete pp.uuids;
}
const features = runs.map((r) => r.features).flat();
// reset feature as it was at origin
features.forEach((f) => {
if (f.properties.type == "house") return;
delete f.properties[f.properties.type];
});
for (const pp of previousMatch.filter((p) => p.uuid != null)) {
pp.feature = features.find(
(f) =>
f.properties.osm_id == pp.uuid.slice(1) &&
f.properties.osm_type == pp.uuid[0]
);
}
const levelToAchieve = "global_house";
const result = {};
result.match = exactMatch;
const exactFeatureIndex = previousMatch.findIndex(
(p) => p.key == levelToAchieve && p.feature != null
);
if (exactFeatureIndex >= 0) {
result.exactFeature = previousMatch[exactFeatureIndex].feature;
delete previousMatch[exactFeatureIndex];
}
result.upperFeatures = previousMatch
.filter((p) => p.feature != null)
.map((p) => p.feature)
.reverse();
return result;
}
async makeAndExecuteRuns(initialData, currentTactics) {
const runs = [];
// TODO get strategy tactics
for (const tactic of currentTactics) {
const addressTags = tactic.searchQuery(initialData);
const response = await photonSearch(
{
addressTags: addressTags,
layers: tactic.layers,
limit: tactic.limit,
},
{
url: this.photonUrl,
}
);
// TODO this is transformin photon response
const features = response.features.map((f) => {
if (f.properties.type == "house") return f;
f.properties[f.properties.type] = f.properties.name;
return f;
});
if (tactic.postTransform) tactic.postTransform(initialData, features);
runs.push({
tactic: tactic,
features: features,
});
}
return runs;
}
getGroupedPropertyValues(matrix, tactics) {
let groupedMatrix = [];
const sortedMatrix = matrix.sort(
(a, b) => tactics.indexOf(b.key) - tactics.indexOf(a.key)
);
for (const m of sortedMatrix) {
const e = groupedMatrix.find(
(g) => g.value == m.value && g.property == m.property
);
if (e == null) {
groupedMatrix.push({
value: m.value,
property: m.property,
keys: [m.key],
uuids: [m.uuid],
exactTypes: [m.exactType],
isExactTypes: [m.isExactType],
});
} else {
e.keys.push(m.key);
e.uuids.push(m.uuid);
e.exactTypes.push(m.exactType);
e.isExactTypes.push(m.isExactType);
}
}
groupedMatrix.forEach((g) => (g.uniqueKeys = [...new Set(g.keys)]));
const group = groupedMatrix.sort((a, b) => {
if (a.uniqueKeys.length == b.uniqueKeys.length) {
// prefer values obtained more precise layers
// TODO be recursive
if (a.keys == b.keys && a.uniqueKeys > 1 && b.uniqueKeys > 1)
return (
tactics.indexOf(b.uniqueKeys[1]) - tactics.indexOf(a.uniqueKeys[1])
);
return (
tactics.indexOf(b.uniqueKeys[0]) - tactics.indexOf(a.uniqueKeys[0])
);
}
return b.uniqueKeys.length - a.uniqueKeys.length;
});
return group;
}
getPropertyValues(runs, input, strategy) {
const matrix = [];
const threshold = strategy.validationThreshold; // should remove house number
for (const r of runs) {
let atLeastOne = false;
for (const f of r.features) {
if (r.tactic.validateAgainst != null) {
const against = r.tactic.validateAgainst(input);
let leave = false;
for (const againstProp of Object.keys(against)) {
if (f.properties[againstProp] == null) continue;
// OSM seperates name by - for multiple languages
const scores = f.properties[againstProp]
.split(" - ")
.map((v) => fuzz.ratio(v, against[againstProp]));
const score = Math.max.apply(null, scores);
if (score < threshold) leave = true;
}
if (leave == true) continue;
}
atLeastOne = true;
for (const p of r.tactic.properties) {
if (f.properties[p] != null)
matrix.push({
key: r.tactic.key,
property: p,
value: f.properties[p],
uuid: f.properties.osm_type + f.properties.osm_id,
exactType: f.properties.type,
isExactType:
f.properties.type == p ||
(p == "housenumber" && f.properties.type == "house") || // TODO house number is a pain
(p == "countrycode" && f.properties.type == "country"), // TODO country is a pain
});
}
}
if (
atLeastOne == false &&
r.tactic.layers.some((l) =>
strategy.skipBreakingAtLayers.includes(l)
) == false
)
break;
}
return matrix;
}
}
export { ofnBeProfile, nominatimGetDetails, nominatimLookup, extractOsmUuids };