Skip to content

Commit 5a64d8d

Browse files
authored
Merge pull request #8188 from plotly/cam/7922/update-topojson-deps
chore: Update topojson deps
2 parents df22b38 + e06c786 commit 5a64d8d

31 files changed

Lines changed: 2553 additions & 638 deletions

‎draftlogs/8188_fix.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
- Fix the `geo` base maps so that the eastern tip of Vanua Levu in Fiji connects to the rest of the island [[#8188](https://github.com/plotly/plotly.js/pull/8188)]
2+
- Fix the `geo` base maps so that each country code has one feature [[#8188](https://github.com/plotly/plotly.js/pull/8188)]
3+
- Before, a choropleth colored only the first part of PRT, ESP, ECU, PSE, MUS, and BES
4+
- With the default `fitbounds: 'locations'`, a map that shows one of these countries now zooms out to include all of the parts of that country
5+
- Fix the `oceania` base maps so that the Fiji islands that cross the antimeridian draw without a seam [[#8188](https://github.com/plotly/plotly.js/pull/8188)]
-29 Bytes
Loading
16 Bytes
Loading
1 Byte
Loading
10 Bytes
Loading
28 Bytes
Loading
-253 Bytes
Loading
15 Bytes
Loading

‎topojson/bin/get_geodata.mjs‎

Lines changed: 63 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,81 @@
1-
import { exec } from 'child_process';
1+
import { execFile } from 'child_process';
22
import fs from 'fs';
33
import { Readable } from 'stream';
44
import { pipeline } from 'stream/promises';
5+
import { promisify } from 'util';
56
import config, { getNEDownloadUrl, getNEFilename } from '../config.mjs';
67

8+
const execFileAsync = promisify(execFile);
79
const { inputDir, resolutions, unDownloadUrl, unFilename, vectors } = config;
810

9-
const outputPath = inputDir;
11+
fs.mkdirSync(inputDir, { recursive: true });
1012

11-
// Download Natural Earth vector maps
13+
/**
14+
* Download a URL to a file.
15+
*
16+
* The function writes to a temporary file and renames it at the end, so a failed download leaves no partial file.
17+
*
18+
* @param url - URL to download
19+
* @param filePath - Path of the output file
20+
*/
21+
const download = async (url, filePath) => {
22+
console.log(`Downloading '${url}'`);
23+
const response = await fetch(url);
24+
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);
25+
26+
const partialFilePath = `${filePath}.part`;
27+
try {
28+
await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(partialFilePath));
29+
fs.renameSync(partialFilePath, filePath);
30+
} finally {
31+
fs.rmSync(partialFilePath, { force: true });
32+
}
33+
const { size } = fs.statSync(filePath);
34+
const formattedSize = size < 1e6 ? `${(size / 1e3).toFixed(1)} kB` : `${(size / 1e6).toFixed(1)} MB`;
35+
console.log(`Saved '${filePath}' (${formattedSize})`);
36+
};
37+
38+
const extract = async (archivePath) => {
39+
console.log(`Extracting '${archivePath}'`);
40+
await execFileAsync('unzip', ['-o', archivePath, '-d', inputDir]);
41+
};
42+
43+
// Download and extract Natural Earth vector maps
1244
for (const vector of Object.values(vectors)) {
1345
for (const resolution of resolutions) {
14-
const url = getNEDownloadUrl({ resolution, vector });
1546
const filename = getNEFilename({ resolution, source: vector.source });
16-
const archivePath = `${outputPath}/${filename}.zip`;
17-
18-
if (!fs.existsSync(archivePath)) {
19-
try {
20-
const response = await fetch(url);
21-
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);
22-
23-
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
24-
const file = fs.createWriteStream(archivePath);
25-
await pipeline(Readable.fromWeb(response.body), file);
26-
27-
// Use the shell to handle decompressing
28-
if (!fs.existsSync(outputPath)) fs.mkdirSync(outputPath, { recursive: true });
29-
exec(`unzip -o ${archivePath} -d ${outputPath}`);
30-
} catch (error) {
31-
console.error(`Error when downloading file '${archivePath}': ${error}`);
32-
continue;
47+
const archivePath = `${inputDir}/${filename}.zip`;
48+
try {
49+
if (fs.existsSync(archivePath)) {
50+
console.log(`Using the cached archive '${archivePath}'`);
51+
} else {
52+
await download(getNEDownloadUrl({ resolution, vector }), archivePath);
3353
}
54+
// Also extract an archive that an earlier run downloaded but did not extract
55+
if (!fs.existsSync(`${inputDir}/${filename}.shp`)) await extract(archivePath);
56+
} catch (error) {
57+
console.error(`Failed to get '${archivePath}': ${error.message}`);
58+
process.exitCode = 1;
3459
}
3560
}
3661
}
3762

38-
// Download UN GeoJSON file
39-
const url = unDownloadUrl;
40-
const archivePath = `${outputPath}/${unFilename}.zip`;
41-
const geojsonPath = `${outputPath}`;
42-
const geojsonFilePath = `${geojsonPath}/${unFilename}.geojson`;
43-
44-
if (fs.existsSync(archivePath)) {
45-
if (!fs.existsSync(geojsonFilePath)) exec(`unzip -o ${archivePath} -d ${geojsonPath}`);
46-
} else {
47-
try {
48-
const response = await fetch(url);
49-
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);
50-
51-
const file = fs.createWriteStream(geojsonFilePath);
52-
await pipeline(Readable.fromWeb(response.body), file);
53-
54-
// Use the shell to handle compression
55-
exec(`zip -j ${archivePath} ${geojsonFilePath}`);
56-
} catch (error) {
57-
console.error(`Error when downloading file '${geojsonFilePath}': ${error}`);
63+
// Download the UN GeoJSON file, and keep a zipped copy of it as the cache
64+
const unArchivePath = `${inputDir}/${unFilename}.zip`;
65+
const unGeojsonPath = `${inputDir}/${unFilename}.geojson`;
66+
try {
67+
if (fs.existsSync(unArchivePath)) {
68+
console.log(`Using the cached archive '${unArchivePath}'`);
69+
if (!fs.existsSync(unGeojsonPath)) await extract(unArchivePath);
70+
} else {
71+
await download(unDownloadUrl, unGeojsonPath);
72+
console.log(`Saving a zipped copy as the cache at '${unArchivePath}'`);
73+
await execFileAsync('zip', ['-j', unArchivePath, unGeojsonPath]);
5874
}
75+
} catch (error) {
76+
console.error(`Failed to get '${unGeojsonPath}': ${error.message}`);
77+
process.exitCode = 1;
5978
}
79+
80+
if (process.exitCode) console.error('Finished with errors. Fix the errors above, then run the script again.');
81+
else console.log(`Finished. The geodata is in '${inputDir}'.`);

0 commit comments

Comments
 (0)