forked from CartoDB/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.test.js
259 lines (228 loc) · 9.16 KB
/
datasource.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
"use strict";
var mapnik = require('../');
var assert = require('assert');
var path = require('path');
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'geojson.input'));
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'ogr.input'));
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'shape.input'));
mapnik.register_datasource(path.join(mapnik.settings.paths.input_plugins,'gdal.input'));
describe('mapnik.Datasource', function() {
it('should throw with invalid usage', function() {
assert.throws(function() { mapnik.Datasource('foo'); });
assert.throws(function() { mapnik.Datasource({ 'foo': 1 }); });
assert.throws(function() { mapnik.Datasource({ 'type': 'foo' }); });
assert.throws(function() { mapnik.Datasource({ 'type': 'shape' }); });
assert.throws(function() { new mapnik.Datasource('foo'); },
/Must provide an object, eg \{type: 'shape', file : 'world.shp'\}/);
assert.throws(function() { new mapnik.Datasource(); });
assert.throws(function() { new mapnik.Datasource({ 'foo': 1 }); });
assert.throws(function() { new mapnik.Datasource({ 'type': 'foo' }); });
assert.throws(function() { new mapnik.Datasource({ 'type': 'shape' }); },
/Shape Plugin: missing <file> parameter/);
});
it('should validate with known shapefile - ogr', function() {
var options = {
type: 'ogr',
file: './test/data/world_merc.shp',
layer: 'world_merc'
};
var ds = new mapnik.Datasource(options);
assert.ok(ds);
assert.deepEqual(ds.parameters(), options);
var features = [];
var featureset = ds.featureset();
var feature;
while ((feature = featureset.next())) {
features.push(feature);
}
assert.equal(features.length, 245);
assert.deepEqual(features[244].attributes(), {
AREA: 1638094,
FIPS: 'RS',
ISO2: 'RU',
ISO3: 'RUS',
LAT: 61.988,
LON: 96.689,
NAME: 'Russia',
POP2005: 143953092,
REGION: 150,
SUBREGION: 151,
UN: 643
});
var expected = {
type: 'vector',
extent: [
-20037508.342789248,
-8283343.693882697,
20037508.342789244,
18365151.363070473
],
encoding: 'utf-8',
fields: {
FIPS: 'String',
ISO2: 'String',
ISO3: 'String',
UN: 'Number',
NAME: 'String',
AREA: 'Number',
POP2005: 'Number',
REGION: 'Number',
SUBREGION: 'Number',
LON: 'Number',
LAT: 'Number'
},
geometry_type: 'polygon',
proj4:'+proj=merc +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs'
};
var actual = ds.describe();
assert.equal(actual.proj4, expected.proj4);
assert.deepEqual(actual.type, expected.type);
assert.deepEqual(actual.encoding, expected.encoding);
assert.deepEqual(actual.fields, expected.fields);
assert.deepEqual(actual.geometry_type, expected.geometry_type);
assert.deepEqual(ds.extent(), expected.extent);
assert.deepEqual(ds.fields(), expected.fields);
});
it('should validate with known shapefile', function() {
var options = {
type: 'shape',
file: './test/data/world_merc.shp'
};
var ds = new mapnik.Datasource(options);
assert.ok(ds);
assert.deepEqual(ds.parameters(), options);
var features = [];
var featureset = ds.featureset();
var feature;
while ((feature = featureset.next())) {
features.push(feature);
}
assert.equal(features.length, 245);
assert.deepEqual(features[244].attributes(), {
AREA: 1638094,
FIPS: 'RS',
ISO2: 'RU',
ISO3: 'RUS',
LAT: 61.988,
LON: 96.689,
NAME: 'Russia',
POP2005: 143953092,
REGION: 150,
SUBREGION: 151,
UN: 643
});
var expected = {
type: 'vector',
extent: [
-20037508.342789248,
-8283343.693882697,
20037508.342789244,
18365151.363070473
],
encoding: 'utf-8',
fields: {
FIPS: 'String',
ISO2: 'String',
ISO3: 'String',
UN: 'Number',
NAME: 'String',
AREA: 'Number',
POP2005: 'Number',
REGION: 'Number',
SUBREGION: 'Number',
LON: 'Number',
LAT: 'Number'
},
geometry_type: 'polygon'
};
var actual = ds.describe();
assert.deepEqual(actual.type, expected.type);
assert.deepEqual(actual.encoding, expected.encoding);
assert.deepEqual(actual.fields, expected.fields);
assert.deepEqual(actual.geometry_type, expected.geometry_type);
assert.deepEqual(ds.extent(), expected.extent);
assert.deepEqual(ds.fields(), expected.fields);
});
it('test invalid use of memory datasource', function() {
var ds = new mapnik.MemoryDatasource({'extent': '-180,-90,180,90'});
assert.throws(function() { ds.add(); });
assert.throws(function() { ds.add(null); });
assert.throws(function() { ds.add({}, null); });
assert.throws(function() { ds.add({'wkt': '1234'}); });
assert.equal(false, ds.add({}));
});
it('test empty memory datasource', function() {
var ds = new mapnik.MemoryDatasource({'extent': '-180,-90,180,90'});
var empty_fs = ds.featureset();
assert.equal(typeof(empty_fs),'undefined');
assert.equal(empty_fs, null);
});
it('test empty geojson datasource', function() {
var input = {
"type": "Feature",
"properties": {
"something": []
},
"geometry": {
"type": "Point",
"coordinates": [ 1, 1 ]
}
};
var ds = new mapnik.Datasource({ type:'geojson', inline: JSON.stringify(input) });
var fs = ds.featureset()
var feat = fs.next();
var feature = JSON.parse(feat.toJSON());
// pass invalid extent to filter all features out
// resulting in empty featureset that should be returned
// as a null object
var empty_fs = ds.featureset({extent:[-1,-1,0,0]});
assert.equal(typeof(empty_fs),'undefined');
assert.equal(empty_fs, null);
});
it('test empty geojson datasource due to invalid json string', function() {
var input = "{ \"type\": \"FeatureCollection\", \"features\": [{ \"oofda\" } ] }";
// from string will fail to parse
assert.throws(function() { new mapnik.Datasource({ type:'geojson', inline: inline, cache_features: false }); });
assert.throws(function() { new mapnik.Datasource({ type:'geojson', inline: fs.readFileSync('./test/data/parse.error.json').toString(), cache_features: false }); });
});
it('test empty geojson datasource due to invalid json file', function() {
assert.throws(function() { new mapnik.Datasource({ type:'geojson', file: './test/data/parse.error.json', cache_features: true }); });
});
it('test valid use of memory datasource', function() {
var ds = new mapnik.MemoryDatasource({'extent': '-180,-90,180,90'});
assert.equal(true, ds.add({ 'x': 0, 'y': 0 }));
assert.equal(true, ds.add({ 'x': 0.23432, 'y': 0.234234 }));
assert.equal(true, ds.add({ 'x': 1, 'y': 1 , 'properties': {'a':'b', 'c':1, 'd':0.23 }}));
var expected_describe = {
type: 'vector',
encoding: 'utf-8',
fields: {},
geometry_type: 'collection'
};
assert.deepEqual(expected_describe, ds.describe());
// Currently descriptors can not be added to memory datasource so will always be empty object
assert.deepEqual({},ds.fields());
});
it('should validate with raster', function() {
var options = {
type: 'gdal',
file: './test/data/images/sat_image.tif'
};
var ds = new mapnik.Datasource(options);
assert.ok(ds);
assert.deepEqual(ds.parameters(), options);
var describe = ds.describe();
var expected = { type: 'raster',
encoding: 'utf-8',
fields: { nodata: 'Number' },
geometry_type: 'raster'
};
assert.deepEqual(expected,describe);
// Test that if added to layer, can get datasource back
var layer = new mapnik.Layer('foo', '+init=epsg:4326');
layer.datasource = ds;
var ds2 = layer.datasource;
assert.ok(ds2);
assert.deepEqual(ds2.parameters(), options);
});
});