Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions draftlogs/8188_fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
- 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)]
- Fix the `geo` base maps so that each country code has one feature [[#8188](https://github.com/plotly/plotly.js/pull/8188)]
- Before, a choropleth colored only the first part of PRT, ESP, ECU, PSE, MUS, and BES
- 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)]
Binary file modified test/image/baselines/distance_satellite.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_bubbles-colorscales.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_connectgaps.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_legendonly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_multi-geos.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_text_chart_arrays.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified test/image/baselines/geo_visible_false_override_template.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
104 changes: 63 additions & 41 deletions topojson/bin/get_geodata.mjs
Original file line number Diff line number Diff line change
@@ -1,59 +1,81 @@
import { exec } from 'child_process';
import { execFile } from 'child_process';
import fs from 'fs';
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';
import { promisify } from 'util';
import config, { getNEDownloadUrl, getNEFilename } from '../config.mjs';

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

const outputPath = inputDir;
fs.mkdirSync(inputDir, { recursive: true });

// Download Natural Earth vector maps
/**
* Download a URL to a file.
*
* The function writes to a temporary file and renames it at the end, so a failed download leaves no partial file.
*
* @param url - URL to download
* @param filePath - Path of the output file
*/
const download = async (url, filePath) => {
console.log(`Downloading '${url}'`);
const response = await fetch(url);
if (!response.ok || !response.body) throw new Error(`Bad response: ${response.status}`);

const partialFilePath = `${filePath}.part`;
try {
await pipeline(Readable.fromWeb(response.body), fs.createWriteStream(partialFilePath));
fs.renameSync(partialFilePath, filePath);
} finally {
fs.rmSync(partialFilePath, { force: true });
}
const { size } = fs.statSync(filePath);
const formattedSize = size < 1e6 ? `${(size / 1e3).toFixed(1)} kB` : `${(size / 1e6).toFixed(1)} MB`;
console.log(`Saved '${filePath}' (${formattedSize})`);
};

const extract = async (archivePath) => {
console.log(`Extracting '${archivePath}'`);
await execFileAsync('unzip', ['-o', archivePath, '-d', inputDir]);
};

// Download and extract 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;
const archivePath = `${inputDir}/${filename}.zip`;
try {
if (fs.existsSync(archivePath)) {
console.log(`Using the cached archive '${archivePath}'`);
} else {
await download(getNEDownloadUrl({ resolution, vector }), archivePath);
}
// Also extract an archive that an earlier run downloaded but did not extract
if (!fs.existsSync(`${inputDir}/${filename}.shp`)) await extract(archivePath);
} catch (error) {
console.error(`Failed to get '${archivePath}': ${error.message}`);
process.exitCode = 1;
}
}
}

// Download UN GeoJSON file
const url = unDownloadUrl;
const archivePath = `${outputPath}/${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}`);
// Download the UN GeoJSON file, and keep a zipped copy of it as the cache
const unArchivePath = `${inputDir}/${unFilename}.zip`;
const unGeojsonPath = `${inputDir}/${unFilename}.geojson`;
try {
if (fs.existsSync(unArchivePath)) {
console.log(`Using the cached archive '${unArchivePath}'`);
if (!fs.existsSync(unGeojsonPath)) await extract(unArchivePath);
} else {
await download(unDownloadUrl, unGeojsonPath);
console.log(`Saving a zipped copy as the cache at '${unArchivePath}'`);
await execFileAsync('zip', ['-j', unArchivePath, unGeojsonPath]);
}
} catch (error) {
console.error(`Failed to get '${unGeojsonPath}': ${error.message}`);
process.exitCode = 1;
}

if (process.exitCode) console.error('Finished with errors. Fix the errors above, then run the script again.');
else console.log(`Finished. The geodata is in '${inputDir}'.`);
Loading
Loading