forked from mapnik/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
render_grid.test.js
279 lines (262 loc) · 9.96 KB
/
render_grid.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
"use strict";
var test = require('tape');
var mapnik = require('../');
var path = require('path');
var fs = require('fs');
var stylesheet = './test/stylesheet.xml';
var reference = './test/support/grid2.json';
var reference_view = './test/support/grid_view.json';
var reference__id__ = './test/support/grid__id__.json';
var reference__id__2 = './test/support/grid__id__2.json';
var reference__id__3 = './test/support/grid__id__3.json';
function _c(grid1,grid2) {
return grid1.replace(/\r/g, '') == grid2.replace(/\r/g, '');
}
if (mapnik.supports.grid) {
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'shape.input'));
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'geojson.input'));
var input_geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [0, 0]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [1, 1]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [2, 2]
},
"properties": {}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [3, 3]
},
"properties": {}
}
]
}
test('should match expected output (sync rendering)', (assert) => {
var map = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['NAME', 'LAT', 'LON', 'AREA', 'ISO2', 'ISO3', 'FIPS']
};
map.render(grid, options, function(err, grid) {
if (err) throw err;
var grid_utf = grid.encodeSync({resolution: 4, features:true});
// pull an identical view and compare it to original grid
var gv = grid.view(0, 0, 256, 256);
var gv_utf = gv.encodeSync({resolution: 4, features:true});
if (process.env.UPDATE) {
fs.writeFileSync(reference,JSON.stringify(grid_utf,null,1));
} else {
var expected = fs.readFileSync(reference,'utf8');
var equal = _c(JSON.stringify(grid_utf,null,1),expected);
assert.ok(equal);
equal = _c(JSON.stringify(gv_utf,null,1),expected);
assert.ok(equal);
}
// pull a subsetted view (greenland basically)
var gv2 = grid.view(64, 64, 64, 64);
assert.equal(gv2.width(), 64);
assert.equal(gv2.height(), 64);
var gv_utf2 = gv2.encodeSync({resolution: 4});
var expected_view = fs.readFileSync(reference_view,'utf8');
if (process.env.UPDATE) {
fs.writeFileSync(reference_view,JSON.stringify(gv_utf2,null,1));
} else {
assert.ok(_c(JSON.stringify(gv_utf2,null,1),expected_view));
}
assert.end();
});
});
test('should match expected output (async rendering)', (assert) => {
var map = new mapnik.Map(256, 256);
map.load(stylesheet, {strict: true}, function(err,map) {
if (err) throw err;
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['NAME', 'LAT', 'LON', 'AREA', 'ISO2', 'ISO3', 'FIPS']
};
map.render(grid, options, function(err, grid) {
if (err) throw err;
grid.encode({resolution: 4}, function(err,utf) {
var equal = _c(JSON.stringify(utf,null,1),fs.readFileSync(reference,'utf8'));
assert.ok(equal, 'Result should match fixture');
assert.end();
});
});
});
});
test('should match expected output (async rendering view)', (assert) => {
var map = new mapnik.Map(256, 256);
map.load(stylesheet, {strict: true}, function(err,map) {
if (err) throw err;
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['NAME', 'LAT', 'LON', 'AREA', 'ISO2', 'ISO3', 'FIPS']
};
map.render(grid, options, function(err, grid) {
assert.ok(!err);
var gv = grid.view(0, 0, 256, 256);
gv.encode({resolution: 4}, function(err,gv_utf1) {
var equal = _c(JSON.stringify(gv_utf1,null,1),fs.readFileSync(reference,'utf8'));
assert.ok(equal, 'Result should match fixture');
assert.end();
});
});
});
});
test('should match expected output (async rendering view subsetted)', (assert) => {
var map = new mapnik.Map(256, 256);
map.load(stylesheet, {strict: true}, function(err,map) {
if (err) throw err;
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['NAME', 'LAT', 'LON', 'AREA', 'ISO2', 'ISO3', 'FIPS']
};
map.render(grid, options, function(err, grid) {
assert.ok(!err);
// pull a subsetted view (greenland basically)
var gv2 = grid.view(64, 64, 64, 64);
assert.equal(gv2.width(), 64);
assert.equal(gv2.height(), 64);
gv2.encode({resolution: 4}, function(err,gv_utf2) {
if (process.env.UPDATE) {
fs.writeFileSync(reference_view,JSON.stringify(gv_utf2,null,1));
} else {
var equal = _c(JSON.stringify(gv_utf2,null,1),fs.readFileSync(reference_view,'utf8'));
assert.ok(equal, 'Result should match fixture');
}
assert.end();
});
});
});
});
test('should match expected output if __id__ is not the grid key', (assert) => {
var map = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: 'NAME'});
var options = {'layer': 0,
'fields': ['FIPS','__id__']
};
map.render(grid, options, function(err, grid) {
if (err) throw err;
var grid_utf = grid.encodeSync({resolution: 4});
if (process.env.UPDATE) {
fs.writeFileSync(reference__id__,JSON.stringify(grid_utf,null,1));
} else {
var equal = _c(JSON.stringify(grid_utf,null,1),fs.readFileSync(reference__id__,'utf8'));
assert.ok(equal);
}
assert.end();
});
});
test('should match expected output if __id__ both the grid key and in the attributes with others', (assert) => {
var map = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['__id__','NAME']
};
map.render(grid, options, function(err, grid) {
if (err) throw err;
var grid_utf = grid.encodeSync({resolution: 4});
if (process.env.UPDATE) {
fs.writeFileSync(reference__id__2,JSON.stringify(grid_utf,null,1));
} else {
var equal = _c(JSON.stringify(grid_utf,null,1),fs.readFileSync(reference__id__2,'utf8'));
assert.ok(equal);
}
assert.end();
});
});
test('should match expected output if __id__ the grid key and the only attributes', (assert) => {
var map = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
var ds = new mapnik.Datasource({ type:'geojson', inline: JSON.stringify(input_geojson) });
var l = new mapnik.Layer('test');
l.srs = map.srs;
l.datasource = ds;
map.add_layer(l);
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 'world',
'fields': ['__id__'],
variables: {pizza:'pie'}
};
map.render(grid, options, function(err, grid) {
if (err) throw err;
var grid_utf = grid.encodeSync({resolution: 4});
if (process.env.UPDATE) {
fs.writeFileSync(reference__id__3,JSON.stringify(grid_utf,null,1));
} else {
var equal = _c(JSON.stringify(grid_utf,null,1),fs.readFileSync(reference__id__3,'utf8'));
assert.ok(equal);
}
assert.end();
});
});
test('should fail to render two things at once', (assert) => {
var map = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
map.zoomAll();
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
var options = {'layer': 0,
'fields': ['__id__','NAME']
};
assert.throws(function() {
map.render(grid, options, function(err, result) {});
map.render(grid, options, function(err, result) {});
});
assert.end();
});
test('should fail to render grid', (assert) => {
var map = new mapnik.Map(256, 256);
var map2 = new mapnik.Map(256, 256);
map.loadSync(stylesheet, {strict: true});
var ds = new mapnik.Datasource({ type:'geojson', inline: JSON.stringify(input_geojson) });
var l = new mapnik.Layer('test');
l.srs = map.srs;
l.datasource = ds;
map.add_layer(l);
map.zoomAll();
map.srs = '+init=PIZZA';
var grid = new mapnik.Grid(map.width, map.height, {key: '__id__'});
// Requires options layer
assert.throws(function() { map.render(grid, {}, function(err, result) {}); });
assert.throws(function() { map.render(grid, {layer:null}, function(err, result) {}); });
assert.throws(function() { map.render(grid, {layer:'foo'}, function(err, result) {}); });
assert.throws(function() { map.render(grid, {layer:99}, function(err, result) {}); });
assert.throws(function() { map2.render(grid, {layer:0}, function(err, result) {}); });
assert.throws(function() { map.render(grid, {layer:0, fields:null}, function(err, result) {}); });
map.render(grid, {layer:0}, function(err, result) {
assert.throws(function() { if(err) throw err; });
assert.end();
});
});
}