-
Notifications
You must be signed in to change notification settings - Fork 4
/
update.js
54 lines (42 loc) · 1.44 KB
/
update.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
const fs = require('node:fs');
const readline = require('node:readline');
function parseRow(rowStr) {
const parts = rowStr.split("\t").map((str) => str.trim());
return {
country: parts[0],
zip: parts[1],
city: parts[2],
state: parts[4],
lat: parseFloat(parts[9]),
long: parseFloat(parts[10]),
};
}
const locTree = {
index: [],
zones: {},
};
const rl = readline.createInterface({ input: fs.createReadStream("./US.txt") });
rl.on("line", (line) => {
const place = parseRow(line);
const index = locTree.index.length;
locTree.index.push(place);
function addIndex(latIndex, longIndex, index) {
if (typeof locTree.zones[latIndex] == "undefined") {
locTree.zones[latIndex] = {};
}
if (typeof locTree.zones[latIndex][longIndex] == "undefined") {
locTree.zones[latIndex][longIndex] = [];
}
locTree.zones[latIndex][longIndex].push(index);
}
let latIndex = Math.floor(Math.abs(parseFloat(place.lat, 10)));
let longIndex = Math.floor(Math.abs(parseFloat(place.long, 10)));
addIndex(latIndex, longIndex, index);
latIndex = Math.ceil(Math.abs(parseFloat(place.lat, 10)));
longIndex = Math.ceil(Math.abs(parseFloat(place.long, 10)));
addIndex(latIndex, longIndex, index);
});
rl.on("close", () => {
fs.writeFileSync("data/loc-tree.json", JSON.stringify(locTree), "UTF8");
console.log("DONE");
});