-
Notifications
You must be signed in to change notification settings - Fork 9
/
build.js
131 lines (113 loc) · 3.88 KB
/
build.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
const csv = require("csv-parser");
const https = require("https");
const path = require("path");
const fs = require("fs");
const resolve = (pathname) => {
return path.resolve(__dirname, pathname);
};
const parseCSV = (pathname) => {
const items = [];
return new Promise((resolve) => {
fs.createReadStream(pathname)
.pipe(csv())
.on("data", (data) => items.push(data))
.on("end", () => resolve(items));
});
};
const mapArrayByKey = (array, key, multiple = false) => {
const object = {};
for (const item of array) {
const keyValue = item[key];
if (multiple) {
if (!object[keyValue]) object[keyValue] = [];
object[keyValue].push(item);
} else {
object[keyValue] = item;
}
}
return object;
};
const downloadFile = (origin, destination) => {
return new Promise((resolve, reject) => {
const file = fs.createWriteStream(destination, { flags: "w" });
https.get(origin, function (response) {
response.pipe(file);
file.on("finish", function () {
file.close(resolve);
});
}).on("error", function (err) {
fs.unlink(dest);
reject(err);
});
});
};
const build = async () => {
// Download airports
await downloadFile(
"https://ourairports.com/data/airports.csv",
resolve("raw/airports.csv")
);
// Download runways
await downloadFile(
"https://ourairports.com/data/runways.csv",
resolve("raw/runways.csv")
);
// Download frequencies
await downloadFile(
"https://ourairports.com/data/airport-frequencies.csv",
resolve("raw/airport-frequencies.csv")
);
// Download countries
await downloadFile(
"https://ourairports.com/data/countries.csv",
resolve("raw/countries.csv")
);
// Download regions
await downloadFile(
"https://ourairports.com/data/regions.csv",
resolve("raw/regions.csv")
);
// Download navaids
await downloadFile(
"https://ourairports.com/data/navaids.csv",
resolve("raw/navaids.csv")
);
// Read raw airports
const airportsArray = await parseCSV(resolve("raw/airports.csv"));
// Read raw runways
const runwaysArray = await parseCSV(resolve("raw/runways.csv"));
// Map runways to object by airport ident
const runways = mapArrayByKey(runwaysArray, "airport_ident", true);
// Read raw frequencies
const freqsArray = await parseCSV(resolve("raw/airport-frequencies.csv"));
// Map frequencies to object by airport ident
const freqs = mapArrayByKey(freqsArray, "airport_ident", true);
// Read raw countries
const countriesArray = await parseCSV(resolve("raw/countries.csv"));
// Map countries to object by country code
const countries = mapArrayByKey(countriesArray, "code");
// Read raw regions
const regionsArray = await parseCSV(resolve("raw/regions.csv"));
// Map regions to object by code
const regions = mapArrayByKey(regionsArray, "code");
// Read raw navaids
const navaidsArray = await parseCSV(resolve("raw/navaids.csv"));
// Map navaids to object by code
const navaids = mapArrayByKey(navaidsArray, "associated_airport", true);
let index = 0;
for (const airport of airportsArray) {
console.log(airport.ident, index++, "/", airportsArray.length);
// Full fill
airport.runways = runways[airport.ident];
airport.freqs = freqs[airport.ident];
airport.country = countries[airport.iso_country] || null;
airport.region = regions[airport.iso_region] || null;
airport.navaids = navaids[airport.ident];
// Write to file
fs.writeFileSync(
resolve("icao/" + airport.ident + ".json"),
JSON.stringify(airport, null, 4)
);
}
};
build();