generated from the-pudding/lambda-node-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
makeSprite.js
53 lines (42 loc) · 1.67 KB
/
makeSprite.js
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
import fs from "fs";
import Spritesmith from "spritesmith";
function getSrc(geo){
return new Promise((resolve, reject) => {
fs.readdir(`../../../Volumes/My Passport/raw_images/${geo}`, (err, files) => {
let filesToSend = files.slice(0,10).map(file => {
return `../../../Volumes/My Passport/raw_images/${geo}/${file}`
});
resolve(filesToSend);
});
});
}
export default async function sprite(courts,chunk){
return new Promise((resolve, reject) => {
console.log("sprite chunk",chunk)
// if(chunk == 8){
// let path = `../../../Volumes/My Passport/small/${geo}`
let src = courts.map(d => {
return `../../../Volumes/My Passport/small/${d.properties.geo}/google_way_${d.properties.id}.png`
})//.slice(0,3510)
// console.log(src.slice(-1))
Spritesmith.run({src: src}, function handleResult (err, result) {
let ids = Object.keys(result.coordinates);
let coords = result.coordinates;
let forSave = [];
for (let id of ids){
let idClean = id.split("way_")[1].replace(".png","");
let row = {id:idClean, coords: result.coordinates[id]};
forSave.push(row);
}
fs.writeFile(`sprite_data/${chunk}.json`, JSON.stringify(forSave), 'utf8', function(err){
if (err) throw err
console.log('File saved.',chunk)
resolve();
})
fs.writeFileSync(`../../../Volumes/My Passport/sprites/${chunk}.jpg`, result.image);
});
// } else {
// resolve()
// }
})
}