-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathget_geodata.mjs
62 lines (50 loc) · 2.32 KB
/
get_geodata.mjs
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
import { exec } from 'child_process';
import fs from 'fs';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import config, { getNEDownloadUrl, getNEFilename } from './config.mjs';
const { resolutions, unDownloadUrl, unFilename, vectors } = config;
const tasksPath = './tasks/topojson';
const outputPath = './build/geodata';
// Download Natural Earth vector maps
for (const vector of Object.values(vectors)) {
for (const resolution of resolutions) {
const url = getNEDownloadUrl({ resolution, vector });
const filename = getNEFilename({ resolution, source: vector.source });
const archivePath = `${outputPath}/${filename}.zip`;
if (!fs.existsSync(archivePath)) {
try {
const response = await fetch(url);
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
const file = fs.createWriteStream(archivePath);
await pipeline(Readable.fromWeb(response.body), file);
// Use the shell to handle decompressing
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
exec(`unzip -o ${archivePath} -d ${outputPath}`);
} catch (error) {
console.error(`Error when downloading file '${archivePath}': ${error}`);
continue;
}
}
}
}
// Download UN GeoJSON file
const url = unDownloadUrl;
const archivePath = `${tasksPath}/${unFilename}.zip`;
const geojsonPath = `${outputPath}`;
const geojsonFilePath = `${geojsonPath}/${unFilename}.geojson`;
if (fs.existsSync(archivePath)) {
if (!fs.existsSync(geojsonFilePath)) exec(`unzip -o ${archivePath} -d ${geojsonPath}`);
} else {
try {
const response = await fetch(url);
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);
const file = fs.createWriteStream(geojsonFilePath);
await pipeline(Readable.fromWeb(response.body), file);
// Use the shell to handle compression
exec(`zip -j ${archivePath} ${geojsonFilePath}`);
} catch (error) {
console.error(`Error when downloading file '${geojsonFilePath}': ${error}`);
}
}