-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyramid-builder.js
More file actions
161 lines (115 loc) · 3.24 KB
/
pyramid-builder.js
File metadata and controls
161 lines (115 loc) · 3.24 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
// ls -1 | parallel mogrify -format jpg -quality 90 {}/*.png "&&" rm {}/*.png
// NOTE set UV_THREADPOOL_SIZE env variable to higher value (eg. 50)
// CONFIG {
const maxParallel = 24;
const ext = "jpg";
const baseDir = "/home/martin/OSM/sh/";
const quality = 90;
const minZoom = 0;
const maxZoom = 19;
// } CONFIG
const { promises: fs } = require("fs");
const path = require("path");
const sharp = require("sharp");
async function generateZoom(sourceZoom) {
const base = baseDir + sourceZoom;
const baseLow = baseDir + (sourceZoom - 1);
const coords = new Set();
for (const file of await fs.readdir(base)) {
const name = path.join(base, file);
console.log(name);
const x = Number(file);
for (const file1 of await fs.readdir(name)) {
const y = Number(file1.slice(0, -4));
coords.add(`${Math.floor(x / 2)}/${Math.floor(y / 2)}`);
}
}
console.log("Let's go...");
let promises = [];
for (const coord of [...coords].sort()) {
console.log((sourceZoom - 1) + "/" + coord);
const [x, y] = coord.split("/");
const xx = x * 2;
const yy = y * 2;
const parts = [
`${base}/${xx}/${yy}.${ext}`,
`${base}/${xx}/${yy + 1}.${ext}`,
`${base}/${xx + 1}/${yy}.${ext}`,
`${base}/${xx + 1}/${yy + 1}.${ext}`,
];
async function stitch(check) {
chk: try {
const s = await fs.stat(`${baseLow}/${x}/${y}.${ext}`);
for (let i = 0; i < 4; i++) {
try {
const s1 = await fs.stat(parts[i]);
if (s1.mtime > s.mtime) {
break chk;
}
} catch {
// ignore
}
}
s.mtime
return;
} catch {
// ignore
}
if (check) {
await fs.mkdir(`${baseLow}/${x}`, { recursive: true });
for (let i = 0; i < 4; i++) {
try {
await fs.stat(parts[i]);
} catch {
parts[i] = null;
}
}
}
const files = ["northwest", "southwest", "northeast", "southeast"]
.map((gravity, i) => ({ input: parts[i], gravity }))
.filter((a) => a.input);
try {
const buff = await sharp({
create: {
width: 512,
height: 512,
channels: 3,
background: { r: 255, g: 255, b: 255 },
},
})
.composite(files)
.png({ compressionLevel: 0 })
.toBuffer();
await sharp(buff)
// .resize({ width: 256, kernel: sharp.kernel.cubic })
.resize({ width: 256 })
.jpeg({ quality, mozjpeg: true })
.toFile(`${baseLow}/${x}/${y}.${ext}`);
} catch (err) {
if (check) {
console.log("Files", files);
console.log(err);
throw err;
}
await stitch(true);
}
}
const p = stitch().then(() => {
promises = promises.filter((pomise) => pomise !== p);
});
promises.push(p);
if (promises.length > maxParallel) {
await Promise.race(promises);
}
}
await Promise.all(promises);
}
async function run() {
for (let z = maxZoom; z > minZoom; z--) {
console.log("Zoom:", z - 1);
await generateZoom(z);
}
}
run().then(() => {
console.log("DONE");
});