mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 09:42:21 +00:00
Merge branch 'main' into samehkhamis/kit-styles
This commit is contained in:
14 files changed
+628
-38
No files matched your search
@@ -39,6 +39,8 @@ import { mkdir, readdir, readFile, rm, writeFile } from 'node:fs/promises';
|
||||
import path from 'node:path';
|
||||
import { fileURLToPath } from 'node:url';
|
||||
import {
|
||||
keepBuiltIfUnchanged,
|
||||
keepPackBuiltIfUnchanged,
|
||||
placeTileFiles,
|
||||
withoutCities,
|
||||
withoutNearTwins,
|
||||
@@ -68,6 +70,8 @@ const outDir = path.join(
|
||||
: 'src/data/local_data/wikidata_cities',
|
||||
);
|
||||
|
||||
/** A file's text, or undefined when there is none yet. */
|
||||
const readIfThere = (file) => readFile(file, 'utf8').catch(() => undefined);
|
||||
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||
const qid = (uri) => uri.slice(uri.lastIndexOf('/') + 1);
|
||||
|
||||
@@ -499,17 +503,30 @@ if (tilesMode) {
|
||||
count: tileRows.length,
|
||||
maxPopulation,
|
||||
});
|
||||
// Tiles from an earlier build that this one has no rows for go.
|
||||
// The new files are all written before tiles from an earlier build that
|
||||
// this one has no rows for go, so a build that stops part way never leaves
|
||||
// an index naming a removed tile.
|
||||
const earlier = new Map();
|
||||
for (const name of await readdir(outDir))
|
||||
if (/^-?\d+_-?\d+\.json$|^index\.json$|^files\.js$/.test(name))
|
||||
earlier.set(name, await readFile(path.join(outDir, name), 'utf8'));
|
||||
keepPackBuiltIfUnchanged(files, earlier);
|
||||
for (const [name, text] of files)
|
||||
await writeFile(path.join(outDir, name), text);
|
||||
for (const name of await readdir(outDir))
|
||||
if (/^-?\d+_-?\d+\.json$/.test(name) && !files.has(name))
|
||||
await rm(path.join(outDir, name));
|
||||
for (const [name, text] of files)
|
||||
await writeFile(path.join(outDir, name), text);
|
||||
console.log(
|
||||
`Wrote ${tileRows.length} places in ${files.size - 2} tiles to ${path.relative(root, outDir)}`,
|
||||
);
|
||||
process.exit(0);
|
||||
}
|
||||
const file = path.join(outDir, 'cities.json');
|
||||
await writeFile(file, `${JSON.stringify({ meta, rows })}\n`);
|
||||
await writeFile(
|
||||
file,
|
||||
keepBuiltIfUnchanged(
|
||||
`${JSON.stringify({ meta, rows })}\n`,
|
||||
await readIfThere(file),
|
||||
),
|
||||
);
|
||||
console.log(`Wrote ${rows.length} places to ${path.relative(root, file)}`);
|
||||
@@ -89,6 +89,51 @@ export function placeTileFiles(rows, meta) {
|
||||
return files;
|
||||
}
|
||||
|
||||
/**
|
||||
* A pack file's new text, keeping the earlier build's `meta.built` when
|
||||
* nothing else in the file changed, so rebuilding unchanged data leaves the
|
||||
* file as it was. A file that does not hold all of its pack's data (the
|
||||
* places index) is passed through this only when the rest of the pack is
|
||||
* unchanged too.
|
||||
* @param {string} text The new file, JSON with a `meta.built` stamp.
|
||||
* @param {string | undefined} earlier The file as it is now, if there is one.
|
||||
*/
|
||||
export function keepBuiltIfUnchanged(text, earlier) {
|
||||
if (!earlier) return text;
|
||||
let before;
|
||||
try {
|
||||
before = JSON.parse(earlier);
|
||||
} catch {
|
||||
return text;
|
||||
}
|
||||
const after = JSON.parse(text);
|
||||
if (typeof before?.meta?.built !== 'string' || !after.meta) return text;
|
||||
const unstamped = (value) =>
|
||||
JSON.stringify({ ...value, meta: { ...value.meta, built: null } });
|
||||
if (unstamped(before) !== unstamped(after)) return text;
|
||||
return `${JSON.stringify({ ...after, meta: { ...after.meta, built: before.meta.built } })}\n`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Keeps the earlier build's `meta.built` in the places pack's index when the
|
||||
* rebuild changed nothing: every tile and `files.js` the same text, no tile
|
||||
* of the earlier build left out, and nothing else in the index changed. The
|
||||
* index holds only row counts, so it alone cannot tell.
|
||||
* @param {Map<string, string>} files The new pack, as placeTileFiles gives it; its index is replaced in place.
|
||||
* @param {Map<string, string>} earlier The pack's files on disk now, by name.
|
||||
*/
|
||||
export function keepPackBuiltIfUnchanged(files, earlier) {
|
||||
for (const name of earlier.keys())
|
||||
if (name !== 'index.json' && !files.has(name)) return files;
|
||||
for (const [name, text] of files)
|
||||
if (name !== 'index.json' && earlier.get(name) !== text) return files;
|
||||
files.set(
|
||||
'index.json',
|
||||
keepBuiltIfUnchanged(files.get('index.json'), earlier.get('index.json')),
|
||||
);
|
||||
return files;
|
||||
}
|
||||
|
||||
const kmBetween = (a, b) =>
|
||||
greatCircleKm({ lat: a[1], lon: a[2] }, { lat: b[1], lon: b[2] });
|
||||
|
||||
|
||||
Reference in new issue
Block a user