-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathtest.js
320 lines (288 loc) · 11.5 KB
/
test.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
var assert = require('assert');
var printer = require('../');
var fs = require('fs');
var path = require('path');
var mapnik = require('mapnik');
// defaults
var zoom = 5,
scale = 4,
x = 4096,
y = 4096,
quality = 256,
format = 'png',
limit = 19008;
// fixtures
var tiles = fs.readdirSync(path.resolve(__dirname + '/fixtures/')).reduce(function(memo, basename) {
var key = basename.split('.').slice(0, 4).join('.');
memo[key] = fs.readFileSync(path.resolve(__dirname + '/fixtures/' + basename));
return memo;
}, {});
describe('Get center from bbox', function() {
it('should fail if (x1, y1) and (x2,y2) are equal', function() {
var bbox = [0, 0, 0, 0];
assert.throws( function() {
printer.coordsFromBbox(zoom, scale, bbox, limit);
}, /Incorrect coordinates/);
});
it('should fail if the image is too large', function() {
var bbox = [-60, -60, 60, 60];
assert.throws( function() {
printer.coordsFromBbox(7, 2, bbox, limit);
}, /Desired image is too large./);
});
it('should return the correct coordinates', function() {
var bbox = [-60, -60, 60, 60];
var center = printer.coordsFromBbox(zoom, scale, bbox, limit);
assert.deepEqual(center.w, 10920);
assert.deepEqual(center.h, 13736);
assert.deepEqual(center.x, x);
assert.deepEqual(center.y, y);
});
});
describe('get coordinates from center', function() {
it('should should fail if the image is too large', function() {
var center = {
x: 0,
y: 0,
w: 4752,
h: 4752
};
assert.throws( function() {
printer.coordsFromCenter(zoom, scale, center, limit);
}, /Desired image is too large./);
});
it('should return correct origin coords', function() {
var center = {
x: 0,
y: 20,
w: 800,
h: 800
};
center = printer.coordsFromCenter(zoom, scale, center, limit);
assert.equal(center.x, x);
assert.equal(center.y, 3631);
});
it('should return correct origin coords for negative y', function() {
var zoom = 2,
center = {
x: 39,
y: -14,
w: 1000,
h: 1000
};
center = printer.coordsFromCenter(zoom, scale, center, limit);
assert.equal(center.x, 623);
assert.equal(center.y, 552);
});
});
describe('create list of tile coordinates', function() {
it('should return a tiles object with correct coords', function() {
var zoom = 5,
scale = 4,
width = 1824,
height = 1832,
center = { x: 4096, y: 4096, w: width, h: height };
var expectedCoords = {
tiles: [
{ z: zoom, x: 15, y: 15, px: -112, py: -108 },
{ z: zoom, x: 15, y: 16, px: -112, py: 916 },
{ z: zoom, x: 16, y: 15, px: 912, py: -108 },
{ z: zoom, x: 16, y: 16, px: 912, py: 916 }
],
dimensions: { x: width, y: height },
center: { row: 16, column: 16, zoom: zoom },
scale: scale
};
var coords = printer.tileList(zoom, scale, center);
assert.deepEqual(JSON.stringify(coords), JSON.stringify(expectedCoords));
});
it('should return a tiles object with correct coords when image exceeds y coords', function() {
var zoom = 2,
scale = 1,
width = 1000,
height = 1000,
center = {x: 623, y: 552, w: width, h: height};
var expectedCoords = {
tiles: [
{ z: zoom, x: 0, y: 0, px: -123, py: -52 },
{ z: zoom, x: 0, y: 1, px: -123, py: 204 },
{ z: zoom, x: 0, y: 2, px: -123, py: 460 },
{ z: zoom, x: 0, y: 3, px: -123, py: 716 },
{ z: zoom, x: 1, y: 0, px: 133, py: -52 },
{ z: zoom, x: 1, y: 1, px: 133, py: 204 },
{ z: zoom, x: 1, y: 2, px: 133, py: 460 },
{ z: zoom, x: 1, y: 3, px: 133, py: 716 },
{ z: zoom, x: 2, y: 0, px: 389, py: -52 },
{ z: zoom, x: 2, y: 1, px: 389, py: 204 },
{ z: zoom, x: 2, y: 2, px: 389, py: 460 },
{ z: zoom, x: 2, y: 3, px: 389, py: 716 },
{ z: zoom, x: 3, y: 0, px: 645, py: -52 },
{ z: zoom, x: 3, y: 1, px: 645, py: 204 },
{ z: zoom, x: 3, y: 2, px: 645, py: 460 },
{ z: zoom, x: 3, y: 3, px: 645, py: 716 },
{ z: zoom, x: 0, y: 0, px: 901, py: -52 },
{ z: zoom, x: 0, y: 1, px: 901, py: 204 },
{ z: zoom, x: 0, y: 2, px: 901, py: 460 },
{ z: zoom, x: 0, y: 3, px: 901, py: 716 }
],
dimensions: {x: width, y: height},
center: {row: 2, column: 2, zoom: zoom},
scale: scale
};
var coords = printer.tileList(zoom, scale, center);
assert.deepEqual(JSON.stringify(coords), JSON.stringify(expectedCoords));
});
it('should return a tiles object with correct coords when image is much bigger than world', function() {
var zoom = 1,
scale = 1,
width = 2000,
height = 2100,
center = {x: 100, y: 100, w: width, h: height};
var expectedCoords = {
tiles: [
{z: zoom, x: 0, y: 0, px: -124, py: 950},
{z: zoom, x: 0, y: 1, px: -124, py: 1206},
{z: zoom, x: 1, y: 0, px: 132, py: 950},
{z: zoom, x: 1, y: 1, px: 132, py: 1206},
{z: zoom, x: 0, y: 0, px: 388, py: 950},
{z: zoom, x: 0, y: 1, px: 388, py: 1206},
{z: zoom, x: 1, y: 0, px: 644, py: 950},
{z: zoom, x: 1, y: 1, px: 644, py: 1206},
{z: zoom, x: 0, y: 0, px: 900, py: 950},
{z: zoom, x: 0, y: 1, px: 900, py: 1206},
{z: zoom, x: 1, y: 0, px: 1156, py: 950},
{z: zoom, x: 1, y: 1, px: 1156, py: 1206},
{z: zoom, x: 0, y: 0, px: 1412, py: 950},
{z: zoom, x: 0, y: 1, px: 1412, py: 1206},
{z: zoom, x: 1, y: 0, px: 1668, py: 950},
{z: zoom, x: 1, y: 1, px: 1668, py: 1206},
{z: zoom, x: 0, y: 0, px: 1924, py: 950},
{z: zoom, x: 0, y: 1, px: 1924, py: 1206}
],
dimensions: {x: width, y: height},
center: {row: 0, column: 0, zoom: zoom},
scale: scale
};
var coords = printer.tileList(zoom, scale, center);
assert.deepEqual(JSON.stringify(coords), JSON.stringify(expectedCoords));
});
});
[256, 512, 1024].forEach(function(size) {
describe('stitch tiles into single png', function() {
var expectedCoords = {
tiles: [
{ z: 1, x: 0, y: 0, px: 0, py: 0 },
{ z: 1, x: 0, y: 1, px: 0, py: size },
{ z: 1, x: 1, y: 0, px: size, py: 0 },
{ z: 1, x: 1, y: 1, px: size, py: size }
],
dimensions: {
x: size * 2,
y: size * 2
},
center: { row: 1, column: 1, zoom: 1 },
scale: 1,
tileSize: size
};
it('should fail if no coordinates object', function(done) {
printer.stitchTiles(null, format, quality, function() {}, function(err) {
assert.equal(err.message, 'No coords object.');
done();
});
});
it('should return tiles and stitch them together', function(done) {
var expectedImage = fs.readFileSync(path.resolve(__dirname + '/expected/expected.' + size + '.png'));
printer.stitchTiles(expectedCoords, format, quality, getTileTest, function(err, image, header) {
fs.writeFile(__dirname + '/outputs/expected.' + size + '.png', image, function(err){
checkImage(image, expectedImage);
done();
});
});
});
});
describe('run entire function', function() {
it('stitches images with a center coordinate', function(done) {
var expectedImage = fs.readFileSync(path.resolve(__dirname + '/expected/center.' + size + '.png'));
var params = {
zoom: 1,
scale: 1,
center: {
x: 0,
y: 0,
w: 200,
h: 200
},
format: 'png',
quality: 50,
tileSize: size,
getTile: getTileTest
};
printer(params, function(err, image) {
assert.equal(err, null);
fs.writeFile(__dirname + '/outputs/center.' + size + '.png', image, function(err){
assert.equal(err, null);
console.log('\tVisually check image at '+ __dirname + '/outputs/center.' + size + '.png');
// byte by byte check of image:
checkImage(image, expectedImage);
done();
});
});
});
it('stitches images with a wsen bbox', function(done) {
var expectedImage = fs.readFileSync(path.resolve(__dirname + '/expected/bbox.' + size + '.png'));
var params = {
zoom: 1,
scale: 1,
bbox: [-140, -80, 140, 80],
format: 'png',
quality: 50,
tileSize: size,
getTile: getTileTest
};
printer(params, function(err, image, headers) {
assert.equal(err, null);
fs.writeFile(__dirname + '/outputs/bbox.' + size + '.png', image, function(err){
assert.equal(err, null);
console.log('\tVisually check image at '+ __dirname + '/outputs/bbox.'+ size +'.png');
// byte by byte check of image:
checkImage(image, expectedImage);
done();
});
});
})
});
// This approximates a tilelive's getTile function
// (https://github.com/mapbox/tilelive-vector/blob/master/index.js#L119-L218)
// by loading a series of local png tiles
// and returning the tile requested with the x, y, & z,
// parameters along with the appropriate headers
function getTileTest(z, x, y, callback) {
var key = [z, x, y, size].join('.');
// Headers.
var headers = {
'Last-Modified': new Date().toUTCString(),
'ETag':'73f12a518adef759138c142865287a18',
'Content-Type':'application/x-protobuf'
};
if (!tiles[key]) {
return callback(new Error('Tile does not exist'));
} else {
return callback(null, tiles[key], headers);
}
}
function checkImage(actual, expected) {
actual = new mapnik.Image.fromBytes(actual);
expected = new mapnik.Image.fromBytes(expected);
var max_diff_pixels = 0;
var compare_alpha = true;
var threshold = 16;
var diff_pixels = actual.compare(expected, {
threshold: threshold,
alpha: compare_alpha
});
if (diff_pixels > max_diff_pixels) {
expected.save('test/outputs/center.fail.png');
}
assert.equal(max_diff_pixels, diff_pixels);
}
});