Repository navigation
Expand file tree
/
Copy pathdata.js
More file actions
75 lines (67 loc) · 1.8 KB
/
Copy pathdata.js
File metadata and controls
75 lines (67 loc) · 1.8 KB
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
const fs = require("fs/promises");
const raw = require("./raw-data.json");
const exec = require("node-exec-promise").exec;
function makeName(e) {
if (e === "Dfa Cold-Withouth_dry_season-Very_Cold_Winter") {
return "Dfd";
}
const code = e.split(" ")[0];
return code;
}
const boundary = 400;
const transform = (func) => (coordinates) => {
const projected = coordinates.map(([l, r]) => [func(l), r]);
const removeBadData = projected.filter(([l]) => {
return l >= -boundary && l <= boundary;
});
return removeBadData;
};
function duplicate360(feature, code) {
const coordinates = feature.geometry.coordinates || [];
const makeCords = (c) => ({
type: feature.type,
geometry: {
...feature.geometry,
coordinates: c,
},
properties: {
code,
},
});
const added = coordinates.map(transform((l) => l + 360));
const removed = coordinates.map(transform((l) => l - 360));
return [makeCords(coordinates), makeCords(added), makeCords(removed)];
}
const newFeatures = raw.features
.map((e) => {
const climate = makeName(e.properties.climate);
return duplicate360(e, climate);
})
.flat(1);
function jsonStringWithFixDfaDfd(code, json) {
const str = JSON.stringify(json);
if (code === "Dfd") {
return str.replace(/Dfa/g, "Dfd");
}
return str;
}
function makeGeoJson(name, features) {
const gj = {
type: "FeatureCollection",
features,
};
return fs.writeFile(`./data/${name}.json`, jsonStringWithFixDfaDfd(name, gj));
}
async function main() {
try {
await fs.stat("./data");
} catch (_) {
await fs.mkdir("./data");
}
await makeGeoJson("all", newFeatures);
await exec(
`npx mapshaper ./data/all.json -snap -split code -o ./data format=topojson id-field=code singles`
);
await fs.unlink("./data/all.json");
}
main();