Skip to content

Instantly share code, notes, and snippets.

@zdila
Last active June 22, 2020 15:59
Show Gist options
  • Save zdila/b4a817ae5a2fd208ee87a6f33544db56 to your computer and use it in GitHub Desktop.
Save zdila/b4a817ae5a2fd208ee87a6f33544db56 to your computer and use it in GitHub Desktop.
Generate TMS pyramid
import * as path from "https://deno.land/std/path/mod.ts";
const encoder = new TextEncoder();
const dot = encoder.encode(".");
const dash = encoder.encode("-");
for (let zz = 18; zz >= 0; zz--) {
const base = `/home/martin/ofmozaika/${zz + 1}`;
const zoom = zz;
const coords = new Set();
for await (const file of Deno.readDir(base)) {
const name = path.join(base, file.name);
console.log(name);
const x = Number(file.name);
for await (const file1 of Deno.readDir(name)) {
const name1 = path.join(name, file1.name);
const y = Number(file1.name.slice(0, -4));
// if (y > 300000) {
// await Deno.rename(name1, `${name}/${524287 - y}.jpg`);
// } else {
// console.log("???", y);
// }
coords.add(`${Math.floor(x / 2)}/${Math.floor(y / 2)}`);
}
}
console.log("Let's go...");
let promises = [];
for (const coord of coords) {
const [x, y] = coord.split("/");
const xx = x * 2;
const yy = y * 2;
const parts = [
`${base}/${xx}/${yy}.jpg`,
`${base}/${xx}/${yy + 1}.jpg`,
`${base}/${xx + 1}/${yy}.jpg`,
`${base}/${xx + 1}/${yy + 1}.jpg`,
];
try {
await Deno.stat(`${zoom}/${x}/${y}.jpg`);
Deno.stdout.write(dash);
continue;
} catch {
// ignore
}
const fn = async () => {
Deno.stdout.write(dot);
await Deno.mkdir(`${zoom}/${x}`, { recursive: true });
for (let i = 0; i < 4; i++) {
try {
await Deno.stat(parts[i]);
} catch {
parts[i] = "white.png";
}
}
const proc = Deno.run({
cmd: [
"convert",
"(",
parts[0],
parts[2],
"+append",
")",
"(",
parts[1],
parts[3],
"+append",
")",
"-append",
"-resize",
"256x256",
`${zoom}/${x}/${y}.jpg`,
],
stdout: "null",
stderr: "null",
stdin: "null",
});
await proc.status();
proc.close();
};
const p = fn();
p.then(() => {
p.done = true;
});
promises.push(p);
if (promises.length > 24) {
await Promise.race(promises);
promises = promises.filter((p) => !p.done);
}
}
console.log("FLUSH");
await Promise.all(promises);
}
console.log("FIN");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment