-
Notifications
You must be signed in to change notification settings - Fork 0
/
updateData.ts
142 lines (127 loc) · 4.95 KB
/
updateData.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
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
import fs from "fs";
import fetch from "node-fetch";
import path from 'path';
import { deduplicate, filterBadResults, fixups } from "./deduplicate";
import { readJsonFile, writeJsonFile } from "./files";
import { SearchPlace } from "./searchPlace";
const OUT_FOLDER = path.join('public', 'data')
const outputFile = `${OUT_FOLDER}/min_nz_places.json`
const outputFileWithExclusions = `${OUT_FOLDER}/min_excluded_places.json`
interface DataSource {
name: string,
getData: () => Promise<any>,
transformData: (data: any) => SearchPlace[]
}
const sources: DataSource[] = [
{
name: 'huts',
getData: async () => fetch('https://api.doc.govt.nz/v2/huts?coordinates=wgs84', {
headers: {
'x-api-key': 'yNyjpuXvMJ1g2d0YEpUmW7VZhePMqbCv96GRjq8L'
}
}).then(r => r.json()),
transformData: (data) => data.map((hut: any) => ({
name: hut.name,
lat: hut.lat,
lon: hut.lon,
type: 'hut'
}))
},
{
name: 'mountains',
getData: async () => fetch('https://raw.githubusercontent.com/fallaciousreasoning/nz-mountains/main/mountains.json').then(r => r.json()),
transformData: data => {
return Object.values(data)
.filter((mountain: any) => mountain.latlng?.length >= 2)
.map((mountain: any) => ({
name: mountain.name,
type: 'peak',
lat: parseFloat(mountain.latlng[0]),
lon: parseFloat(mountain.latlng[1]),
}))
}
},
{
name: 'osm',
getData: () => fetch('https://www.overpass-api.de/api/interpreter?[out:json];node[natural](-47.9,165.9,-34.0,179.0);out;').then(r => r.json()),
transformData: (data: any) => data.elements.map((place: any) => ({
name: place.tags.name,
lat: place.lat,
lon: place.lon,
type: place.tags.natural
}))
},
{
name: 'gazetteer',
getData: async () => {
const text = await fetch('https://gazetteer.linz.govt.nz/gaz.csv').then(r => r.text())
const lines = text.split('\n');
const header = lines[0].split(',');
const result = [];
for (let i = 1; i < lines.length; ++i) {
const values = lines[i].split(',');
const place: any = {};
for (let j = 0; j < header.length; ++j) {
const key = header[j];
const value = values[j];
if (!key) continue;
if (!value) continue;
place[key] = value;
}
result.push(place);
}
return result
},
transformData: (data: any) => data.map((place: any) => ({
name: place.name,
lon: parseFloat(place.crd_longitude),
lat: parseFloat(place.crd_latitude),
type: place.feat_type
? place.feat_type.toLowerCase()
: undefined
})).filter((p: SearchPlace) => !isNaN(p.lat) && !isNaN(p.lon))
},
]
const processSource = async (source: DataSource) => {
console.log(`Processing ${source.name}`)
const baseName = path.join(OUT_FOLDER, source.name)
const name = baseName + '.json'
const minName = baseName + '.min.json'
let data: any
if (!fs.existsSync(name)) {
console.log(`Fetching ${source.name} data`)
data = await source.getData()
await writeJsonFile(name, data)
console.log(`Fetched ${source.name} data`)
} else {
data = await readJsonFile(name)
}
console.log(`Minifying ${source.name}`)
const minified = source.transformData(data)
await writeJsonFile(minName, minified)
console.log(`Minified ${source.name}`)
}
const joinOutputs = async () => {
console.log("Joining outputs and deduplicating");
const dataSources = await Promise.all(sources.map(async source => [source, await readJsonFile(path.join(OUT_FOLDER, source.name + '.min.json'))]))
const result = dataSources.flatMap(([, data]) => data)
.filter(p => !!p.name)
const fixed = fixups(result);
const filtered = filterBadResults(fixed);
const deduplicated = deduplicate(filtered);
await writeJsonFile(outputFile, deduplicated);
console.log("Wrote joined file", outputFile);
const excludeSources: (typeof sources)[number]['name'][] = ["huts", "mountains"]
const exclude = new Set(dataSources.filter(([source]) => excludeSources.includes(source.name))
.map(([, data]) => data)
.flat())
const noHutsOrMountains = deduplicated.filter(p => !exclude.has(p))
await writeJsonFile(outputFileWithExclusions, noHutsOrMountains)
console.log("Wrote excluded file")
}
(async () => {
if (!fs.existsSync(OUT_FOLDER)) fs.mkdirSync(OUT_FOLDER)
for (const source of sources)
await processSource(source)
await joinOutputs();
})();