forked from mapnik/node-mapnik
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datasource.test.js
242 lines (214 loc) · 6.85 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
"use strict";
var test = require('tape');
var mapnik = require('../');
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'));
let compare_proj_string = (str1, str2) => {
let a1 = str1.split(" ").sort();
let a2 = str2.split(" ").sort();
if (a1.length != a1.length)
return false;
for (let i = 0; i < a1.length; ++i) {
if (a1[i] != a2[i]) return false;
}
return true;
};
test('should throw with invalid usage', (assert) => {
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/);
assert.end();
});
test('should validate with known shapefile - ogr', (assert) => {
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.ok(compare_proj_string(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);
assert.end();
});
test('should validate with known shapefile', (assert) => {
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);
assert.end();
});
test('test empty geojson datasource', (assert) => {
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),'object');
assert.equal(empty_fs, null);
assert.end();
});
test('test empty geojson datasource due to invalid json string', (assert) => {
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 }); });
assert.end();
});
test('test empty geojson datasource due to invalid json file', (assert) => {
assert.throws(function() { new mapnik.Datasource({ type:'geojson', file: './test/data/parse.error.json', cache_features: false }); });
assert.end()
});
test('should validate with raster', (assert) => {
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);
assert.end();
});