-
Notifications
You must be signed in to change notification settings - Fork 0
/
deduplicate.ts
101 lines (78 loc) · 2.61 KB
/
deduplicate.ts
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
import { SearchPlace } from "./searchPlace";
const closenessThreshold = 0.05;
const getName = (p: SearchPlace) => p.name.toLowerCase()
.replace("mt", "mount")
.replace(" / ", "/");
const getDist = (p1: SearchPlace, p2: SearchPlace) => {
const dlat = p1.lat - p2.lat;
const dlon = p1.lon - p2.lon;
return Math.sqrt(dlat ** 2 + dlon ** 2);
}
export const findClose = (p1: SearchPlace, places: SearchPlace[], threshold: number=0.0000001) => {
return places.filter(p => getDist(p1, p) < threshold)
}
const minimalSet = (places: SearchPlace[]) => {
places = [...places];
const result: SearchPlace[] = [];
const getMatchIndex = (place: SearchPlace) => {
for (let i = 0; i < result.length; ++i) {
if (getDist(result[i], place) < closenessThreshold)
return i;
}
return -1;
}
while (places.length) {
const place = places.pop();
const matchIndex = getMatchIndex(place);
if (matchIndex === -1) {
result.push(place);
continue;
}
result[matchIndex] = {
...place,
...result[matchIndex]
}
}
return result;
}
export const deduplicate = (places: SearchPlace[]) => {
const dict: { [name: string]: SearchPlace[] } = {};
for (const place of places) {
const name = getName(place);
if (!dict[name])
dict[name] = [];
dict[name].push(place);
}
const deduplicated: SearchPlace[] = [];
for (const name in dict) {
const minSet = minimalSet(dict[name]);
deduplicated.push(...minSet);
}
const result: SearchPlace[] = []
const seen = new Set<SearchPlace>()
for (const place of deduplicated) {
if (seen.has(place)) continue
const close = findClose(place, deduplicated).filter(c => c.type === place.type)
for (const place of close) seen.add(place)
result.push(place)
}
console.log(`Went from ${places.length} places to ${result.length} after deduplication`)
return result;
}
export const fixups = (places: SearchPlace[]) => {
const typeOverrides: { [type: string]: string } = {
"reserve (non-cpa)": 'reserve',
"hill": "peak",
"pinnacle": "peak"
}
for (const place of places) {
if (typeOverrides[place.type]) {
place.type = typeOverrides[place.type]
}
}
const isGood = /^[A-Z a-z_]+$/g
const fixed = places.filter(p => p.type.match(isGood))
return fixed;
}
export const filterBadResults = (places: SearchPlace[]) => places
.filter(c => c.lat !== 0 && c.lon !== 0)