forked from CartoDB/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.test.js
185 lines (165 loc) · 7.08 KB
/
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
"use strict";
var mapnik = require('../');
var assert = require('assert');
if (mapnik.supports.grid) {
describe('mapnik.Grid ', function() {
it('should throw with invalid usage', function() {
// no 'new' keyword
assert.throws(function() { mapnik.Grid(1, 1); });
// invalid args
assert.throws(function() { new mapnik.Grid(); });
assert.throws(function() { new mapnik.Grid(1); });
assert.throws(function() { new mapnik.Grid('foo'); });
assert.throws(function() { new mapnik.Grid('a', 'b', 'c'); });
assert.throws(function() { new mapnik.Grid(256,256,null); });
assert.throws(function() { new mapnik.Grid(256,256,{key:null}); });
});
it('should be initialized properly', function() {
var grid = new mapnik.Grid(256, 256);
assert.ok(grid instanceof mapnik.Grid);
assert.equal(grid.width(), 256);
assert.equal(grid.height(), 256);
var v = grid.view(0, 0, 256, 256);
assert.ok(v instanceof mapnik.GridView);
assert.equal(v.width(), 256);
assert.equal(v.height(), 256);
assert.equal(grid.encodeSync({features:true}).length, v.encodeSync().length);
});
it('should fail to encode properly', function() {
var grid = new mapnik.Grid(256, 256);
assert.throws(function() { grid.encodeSync("foo"); });
assert.throws(function() { grid.encode("foo"); });
assert.throws(function() { grid.encode("foo", function(err, result) {}); });
assert.throws(function() { grid.encodeSync({features:null}); });
assert.throws(function() { grid.encodeSync({resolution:null}); });
assert.throws(function() { grid.encodeSync({resolution:0}); });
assert.throws(function() { grid.encode({features:null}, function(err, result) {}); });
assert.throws(function() { grid.encode({resolution:null}, function(err, result) {}); });
assert.throws(function() { grid.encode({resolution:0}, function(err, result) {}); });
assert.throws(function() { grid.encode({resolution:4}, null); });
});
it('should encode properly', function(done) {
var grid = new mapnik.Grid(256, 256);
grid.encode({resolution:4, features:true}, function(err, result) {
if (err) throw err;
assert.ok(result);
done();
});
});
it('should not be painted after rendering', function(done) {
var grid_blank = new mapnik.Grid(4, 4);
assert.equal(grid_blank.painted(), false);
assert.equal(grid_blank.background, undefined);
var m = new mapnik.Map(4, 4);
var l = new mapnik.Layer('test');
m.add_layer(l);
m.render(grid_blank, {layer: 0},function(err,grid_blank) {
assert.equal(grid_blank.painted(), false);
// TODO - expose grid background
//assert.equal(grid_blank.background, undefined);
done();
});
});
it('should be have background applied after rendering', function(done) {
var grid_blank = new mapnik.Grid(4, 4);
var m = new mapnik.Map(4, 4);
var l = new mapnik.Layer('test');
m.add_layer(l);
m.background = new mapnik.Color('green');
m.render(grid_blank, {layer: 0},function(err,grid_blank2) {
assert.equal(grid_blank2.painted(), false);
// TODO - expose grid background
//assert.ok(grid_blank2.background);
done();
});
});
it('should be painted after rendering 2', function(done) {
var grid_blank3 = new mapnik.Grid(4, 4);
assert.equal(grid_blank3.painted(), false);
assert.equal(grid_blank3.background, undefined);
var m3 = new mapnik.Map(4, 4);
var s = '<Map>';
s += '<Style name="points">';
s += ' <Rule>';
s += ' <PointSymbolizer />';
s += ' </Rule>';
s += '</Style>';
s += '</Map>';
m3.fromStringSync(s);
var mem_datasource = new mapnik.MemoryDatasource({'extent': '-180,-90,180,90'});
mem_datasource.add({ 'x': 0, 'y': 0 });
mem_datasource.add({ 'x': 1, 'y': 1 });
mem_datasource.add({ 'x': 2, 'y': 2 });
mem_datasource.add({ 'x': 3, 'y': 3 });
var l = new mapnik.Layer('test');
l.srs = m3.srs;
l.styles = ['points'];
l.datasource = mem_datasource;
m3.add_layer(l);
m3.zoomAll();
m3.render(grid_blank3, {layer: 0},function(err,grid_blank3) {
assert.equal(grid_blank3.painted(), true);
// TODO - expose grid background
//assert.equal(grid_blank3.background,undefined);
grid_blank3.key = "stuff";
assert.throws(function() { grid_blank3.key = null; });
assert.throws(function() { grid_blank3.clear(null); });
grid_blank3.clear(function(err, grid_blank4) {
assert.equal(grid_blank4.key, "stuff");
assert.equal(grid_blank4.painted(), false);
done();
});
});
});
it('should be painted after rendering 3', function(done) {
var grid_blank3 = new mapnik.Grid(4, 4);
assert.equal(grid_blank3.painted(), false);
assert.equal(grid_blank3.background, undefined);
var m3 = new mapnik.Map(4, 4);
var s = '<Map>';
s += '<Style name="points">';
s += ' <Rule>';
s += ' <PointSymbolizer />';
s += ' </Rule>';
s += '</Style>';
s += '</Map>';
m3.fromStringSync(s);
var mem_datasource = new mapnik.MemoryDatasource({extent: '-180,-90,180,90', somefield:'value'});
mem_datasource.add({ 'x': 0, 'y': 0, properties: {feat_id:1,null_val:null,name:"name"}});
mem_datasource.add({ 'x': 1, 'y': 1 });
mem_datasource.add({ 'x': 2, 'y': 2 });
mem_datasource.add({ 'x': 3, 'y': 3 });
var l = new mapnik.Layer('test');
l.srs = m3.srs;
l.styles = ['points'];
l.datasource = mem_datasource;
m3.add_layer(l);
m3.zoomAll();
m3.render(grid_blank3, {layer: 0},function(err,grid_blank3) {
assert.equal(grid_blank3.painted(), true);
assert.throws(function() { grid_blank3.addField(null); });
assert.throws(function() { grid_blank3.addField(); });
grid_blank3.addField("foo");
assert.deepEqual(grid_blank3.fields(), ["foo"]);
// TODO - expose grid background
//assert.equal(grid_blank3.background,undefined);
grid_blank3.clear();
assert.equal(grid_blank3.painted(), false);
grid_blank3.clearSync();
assert.equal(grid_blank3.painted(), false);
done();
});
});
it('works with metrics', function(done) {
var g = new mapnik.Grid(256, 256);
g.metrics_enabled = false;
assert.equal(g.metrics_enabled, false);
if (mapnik.supports.metrics) {
g.metrics_enabled = true;
assert.equal(g.metrics_enabled, true);
assert(JSON.stringify(g.get_metrics()) === JSON.stringify({}));
}
done();
});
});
}