-
Notifications
You must be signed in to change notification settings - Fork 257
/
parseCountries.js
58 lines (49 loc) · 1.63 KB
/
parseCountries.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
const fs = require("fs");
const path = require("path");
const argv = process.argv;
const file = fs.readFileSync(argv[argv.length - 1], "utf8");
const geojson = JSON.parse(file);
const countriesA3 = {};
const regions = {
all: {},
};
geojson.features.forEach((feature) => {
// Some files have uppercase property names. This will not do.
const lowercaseProperties = {};
Object.keys(feature.properties).forEach((propName) => {
lowercaseProperties[propName.toLowerCase()] = feature.properties[propName];
});
feature.properties = lowercaseProperties;
// Pick out the country name and put it in a file.
const regionName = feature.properties.continent;
regions[regionName] = regions[regionName] || {};
const countryName = feature.properties.name;
let a3 = feature.properties.iso_a3;
if (!a3 || Number(a3) === -99) {
if (["Country", "Sovereign country"].includes(feature.properties.type)) {
a3 = feature.properties.gu_a3;
} else {
a3 = feature.properties.name.toLowerCase().replace(/\W/g, "");
}
}
console.log(
"country",
countryName,
feature.properties.iso_a3,
JSON.stringify(a3)
);
if (countriesA3[a3]) {
console.error(a3, countryName, "already exists");
console.log(JSON.stringify(feature.properties));
}
countriesA3[a3] = a3;
const filename = a3 + ".geojson";
feature.properties.filename = filename;
regions[regionName][countryName] = filename;
regions.all[a3] = { filename, countryName };
fs.writeFileSync(path.join(process.cwd(), filename), JSON.stringify(feature));
});
fs.writeFileSync(
path.join(process.cwd(), "index.json"),
JSON.stringify(regions)
);