-
Notifications
You must be signed in to change notification settings - Fork 0
/
2-geojson.js
73 lines (60 loc) · 2.06 KB
/
2-geojson.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
#!/usr/bin/env node --max-old-space-size=8192
import chalk from "chalk";
import { execSync } from "child_process";
import { existsSync, mkdirSync } from "fs";
import { sources } from "./sources.js";
["./2"].forEach((dir) => {
if (!existsSync(dir)) {
mkdirSync(dir);
}
});
function check(name, source, destination) {
if (existsSync(destination)) {
console.log(
chalk.yellow(
`Skipping ${name} because it exists already at ${destination}`
)
);
return false;
}
if (!existsSync(source)) {
console.log(
chalk.yellow(`Skipping ${name} because no source file at ${source}`)
);
return false;
}
return true;
}
let skipCount = 0;
async function execute() {
await Promise.all(
sources.map(async ({ name, extension }) => {
const source = `1/unzip/${name}.${extension}`;
const destination = `2/${name}.geojson`;
try {
if (!check(name, source, destination)) {
skipCount++;
return;
}
const openOptions =
extension === "geojson"
? ""
: `-oo GEOM_POSSIBLE_NAMES="SHAPE" -oo Y_POSSIBLE_NAMES="Latitude" -oo X_POSSIBLE_NAMES="Longitude"`;
const options = `-s_srs EPSG:4326 ${openOptions}`;
console.log(chalk.blue(`Converting ${name}`));
let cmd = `ogr2ogr -t_srs EPSG:4326 -gt 65536 ${options} -f GeoJSONSeq ${process.cwd()}/${destination} "${process.cwd()}/${source}"`;
console.log(cmd);
execSync(cmd);
console.log(chalk.green(`Loaded ${source.filename}`));
console.log(chalk.blue("Checking for null or bad geometry"));
cmd = `head ${destination} | ndjson-filter '!d.geometry || d.geometry.coordinates[0] < -180 || d.geometry.coordinates[0] > 180 || d.geometry.coordinates[1] < -80 || d.geometry.coordinates[1] > 80' 1>&2`;
console.log(cmd);
execSync(cmd);
} catch (error) {
console.error(chalk.red(`Error processing ${source.filename}`), error);
}
})
);
}
execute();
console.log(`Finished transforming trees. Skip count ${skipCount}.`);