-
Notifications
You must be signed in to change notification settings - Fork 0
/
getUkraineGeo.js
executable file
·70 lines (60 loc) · 1.87 KB
/
getUkraineGeo.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
import fs from "fs";
import * as turf from "@turf/turf";
import { geoJsonToGltf } from "./geoJsonToGltf.js";
import { simplifyFeatures } from "./simplify.js";
const files = fs.readdirSync("./ukraine-data");
const labels = JSON.parse(
fs.readFileSync("./ukraine-data/labels.json"),
).features;
const regions = [];
files
.filter((f) => !f.includes("labels"))
.forEach((fileName) => {
const geometry = JSON.parse(fs.readFileSync(`./ukraine-data/${fileName}`));
regions.push(turf.feature(geometry, { id: fileName.split(".")[0] }));
});
const ukraineGeoJson = turf.featureCollection(
regions.map((r) => turf.rewind(r)),
);
fs.writeFileSync("./ukraine.json", JSON.stringify(ukraineGeoJson, null, 2));
const simpleRegions = simplifyFeatures(ukraineGeoJson, 0.03, 0.01);
const labelsGeoJson = turf.featureCollection([
...simpleRegions.features.map(
(f) =>
labels.find((l) => l.properties.id == f.properties.id) ||
turf.centroid(f, { properties: f.properties }),
),
]);
const cleanRegions = simpleRegions.features.map((f) => {
if (f.geometry.type === "MultiPolygon") {
return turf.polygon(
f.geometry.coordinates.sort((a, b) => b[0].length - a[0].length)[0],
f.properties,
);
} else return f;
});
// fs.writeFileSync(
// "./ukraine-labels.json",
// JSON.stringify(labelsGeoJson, null, 2),
// );
fs.writeFileSync(
`./simple-ukraine.json`,
JSON.stringify(simpleRegions, null, 2),
);
const mobileLabels = JSON.parse(
fs.readFileSync("./ukraine-data/mobile-labels.json"),
);
const desktopLabels = JSON.parse(
fs.readFileSync("./ukraine-data/desktop-labels.json"),
);
await geoJsonToGltf(
turf.featureCollection([
...cleanRegions,
...labelsGeoJson.features.filter((f) => f.geometry.type === "Point"),
]),
"./out/ukraine-regions.glb",
{
mobileLabels: mobileLabels.features,
desktopLabels: desktopLabels.features,
},
);