-
Notifications
You must be signed in to change notification settings - Fork 8
/
make-tiles.js
169 lines (149 loc) · 5.2 KB
/
make-tiles.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
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
162
163
164
165
166
167
168
169
var Canvas = require('canvas');
var Image = Canvas.Image;
var moment = require('moment');
var fs = require('fs');
var util = require('util');
var mkdirp = require('mkdirp');
var getDirName = require('path').dirname;
var cp = require('child_process').exec;
let img = new Image;
let imageCache = {};
console.log(process.argv);
var regionName = process.argv[2];
var min = +process.argv[3];
var max = +process.argv[4];
var debug = +process.argv[5];
var val = JSON.parse(fs.readFileSync('./regions/'+regionName+'.json')).bounds;
var imgp = './region-raw-img/' + JSON.parse(fs.readFileSync('./regions/'+regionName+'.json')).image;
console.log(val)
console.log('generate tiles:', regionName, min+'x', '~', max+'x');
// get tms bounds for each zoom level
var total = 0;
var ranges = [];
for(var i = min; i <= max ; i++) {
var lt = getTileAtLatLng(val[0], i);
var rb = getTileAtLatLng(val[2], i);
var w = Math.abs(rb.x-lt.x);
var h = Math.abs(rb.y-lt.y);
ranges.push({
zoom : i,
lt : lt,
rb : rb,
width : w,
height : h,
number : w*h
});
total += (w+1)*(h+1);
}
console.log('tiles needs to be generated :', total);
var now = 0;
var start = Date.now();
for(var i=min;i<=max;i++) {
var lt = getTileAtLatLng(val[0], i);
var rb = getTileAtLatLng(val[2], i);
for(var j = lt.x; j <= rb.x;j++){
for(var k=lt.y; k <= rb.y;k++) {
createTileFromRawImage(regionName, j,k,i, function() {
now++;
var time = Math.floor((total-now)*((Date.now() - start)/now)/1000);
process.stdout.write('Processing .. ' + (now) + '/' + total + ' --> ' + (now/total*100).toFixed(2) + '% [' +time + '] seconds remains \033[0G' );
});
}
}
}
console.log('Task finished,', Math.floor((Date.now() - start)/1000), 'seconds elapsed ');
function tileToLatLng(x,y,z) {
var result = { lat : tileToLat(y,z), lng : tileToLong(x,z) };;
return result;
}
function tileToLong(x,z) { return (x/Math.pow(2,z)*360-180); }
function tileToLat(y,z) {
var n = Math.PI-2*Math.PI*y/Math.pow(2,z);
return (180/Math.PI*Math.atan(0.5*(Math.exp(n)-Math.exp(-n))));
}
function createTileFromRawImage(region, x, y, z, cb) {
x = Math.floor(x);
y = Math.floor(y);
z = Math.floor(z);
let data = fs.readFileSync(`./regions/${region}.json`);
let {bounds} = JSON.parse(data);
let sign = bounds[0].lat < 0 ? 1 : -1;
let tileBounds = [
tileToLatLng(x,y,z),
tileToLatLng(x+1,y,z),
tileToLatLng(x+1,y+1,z),
tileToLatLng(x,y+1,z)
];
var raw = imageCache[region] ? imageCache[region] : fs.readFileSync(imgp);
let img = new Image;
img.src = raw;
let regionWidth = Math.abs(Math.abs(bounds[0].lng) - Math.abs(bounds[1].lng)),
regionHeight = Math.abs(Math.abs(bounds[1].lat) - Math.abs(bounds[2].lat));
let tileWidth = Math.abs(tileBounds[0].lng - tileBounds[1].lng) * img.width /regionWidth,
tileHeight = Math.abs(tileBounds[1].lat - tileBounds[2].lat) * img.height /regionHeight;
let tileImg = new Canvas(256, 256);
let originX = (Math.abs(tileBounds[0].lng) - Math.abs(bounds[0].lng))/regionWidth * img.width;
let originY = (Math.abs(tileBounds[0].lat) - Math.abs(bounds[0].lat))/regionHeight * img.height;
let ctx = tileImg.getContext('2d');
if( originX > img.width + tileWidth ||
originY > img.height + tileHeight ||
originY < -tileHeight -1 || originX < -tileWidth -1 ) {
return
}
else {
if(sign<0) {
originY = img.height - originY - 512 - img.height%256;
}
ctx.drawImage(img, originX, originY, tileWidth, tileHeight, 0, 0, 256, 256);
ctx.strokeStyle = 'transparent';
ctx.strokeRect(0, 0, 256, 256);
if(debug) {
drawText(ctx,{x,y,z}, originX,originY,tileWidth, tileHeight, z);
}
}
var bytes = tileImg.toBuffer(undefined, 3, ctx.PNG_FILTER_NONE);
var dir = `./regions/output/${debug?'debug/':''}${region}/${z}/${x}${y}.png`;
mkdirp.sync(getDirName(dir));
fs.writeFileSync(dir, bytes);
cb();
}
var deleteFolderRecursive = function(path, cb) {
if( fs.existsSync(path) ) {
fs.readdirSync(path).forEach(function(file,index){
var curPath = path + "/" + file;
if(fs.lstatSync(curPath).isDirectory()) { // recurse
deleteFolderRecursive(curPath);
} else { // delete file
fs.unlinkSync(curPath);
}
});
fs.rmdirSync(path);
}
cb()
};
function drawText(ctx,param, ox,oy, dx, dy, z) {
var {region,x,y,z} = param;
var info = `${z}/${x}/${y}`;
var coords = 'from (' + [Math.round(ox), Math.floor(oy)].join(', ') + ')';
var coords2 = 'to (' + [Math.round(ox+dx), Math.floor(oy + dy)].join(', ') + ')';
ctx.font = '16px Arial';
ctx.fillStyle = '#FFF';
ctx.fillText(info, 8, 24);
ctx.fillText(coords, 8, 48);
ctx.fillText(coords2, 8, 64);
ctx.font = '64px Arial';
ctx.fillText(z + 'x', 144, 255);
}
function fromLatLngToPoint (latLng){
var siny = Math.min(Math.max(Math.sin(latLng.lat* (Math.PI / 180)), -.9999),.9999);
return {
x: 128 + latLng.lng * (256/360),
y: 128 + 0.5 * Math.log((1 + siny) / (1 - siny)) * -(256 / (2 * Math.PI))
};
}
function getTileAtLatLng(latLng,zoom){
var t=Math.pow(2,zoom),
s=256/t,
p=fromLatLngToPoint(latLng);
return {x:Math.floor(p.x/s),y:Math.floor(p.y/s),z:zoom};
}