-
Notifications
You must be signed in to change notification settings - Fork 125
/
Copy pathapi_algorithms.test.js
244 lines (224 loc) · 5.98 KB
/
api_algorithms.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
var gdal = require('../lib/gdal.js');
var assert = require('chai').assert;
describe('gdal', function() {
afterEach(gc);
describe('contourGenerate()', function() {
var src, srcband, dst, lyr;
before(function() {
// create a simple ramp in memory
var w = 64;
var h = 64;
src = gdal.open('temp', 'w', 'MEM', w, h, 1);
srcband = src.bands.get(1);
for (var y = 0; y < h; y++) {
var buf = Buffer.alloc(w);
buf.fill(y * 4);
srcband.pixels.write(0, y, w, 1, new Uint8Array(buf));
}
});
after(function() {
src.close();
});
beforeEach(function() {
dst = gdal.open('temp', 'w', 'Memory');
lyr = dst.layers.create('temp', null, gdal.Linestring);
lyr.fields.add(new gdal.FieldDefn('id', gdal.OFTInteger));
lyr.fields.add(new gdal.FieldDefn('elev', gdal.OFTReal));
});
afterEach(function() {
try {
dst.close();
} catch (err) {
/* ignore */
}
});
it('should generate contours when passed an interval / base', function() {
var offset = 7;
var interval = 32;
gdal.contourGenerate({
src: srcband,
dst: lyr,
offset: offset,
interval: interval,
idField: 0,
elevField: 1
});
assert(lyr.features.count() > 0, 'features were created');
lyr.features.forEach(function(feature) {
assert((feature.fields.get('elev') - offset) % interval === 0, 'contour used correct interval / base');
assert.isFalse(feature.getGeometry().isEmpty());
});
});
it('should accept an array of fixed levels', function() {
var levels = [53, 43, 193].sort();
gdal.contourGenerate({
src: srcband,
dst: lyr,
fixedLevels: levels,
idField: 0,
elevField: 1
});
assert(lyr.features.count() > 0, 'features were created');
// this behavior changed in gdal 2.4 (not a binding problem)
/*
var actual_levels = [];
lyr.features.forEach(function(feature) {
var elev = feature.fields.get('elev');
assert.include(levels, elev, 'contour elevation in array of fixed levels');
assert.isFalse(feature.getGeometry().isEmpty());
if (actual_levels.indexOf(elev) === -1) actual_levels.push(elev);
});
assert.deepEqual(actual_levels.sort(), levels, 'all fixed levels used');
*/
});
});
describe('fillNodata()', function() {
var src, srcband;
var holes_x = [53, 61, 61, 1, 43, 44, 5];
var holes_y = [11, 5, 6, 33, 22, 11, 0];
var nodata = 33;
beforeEach(function() {
var w = 64;
var h = 64;
src = gdal.open('temp', 'w', 'MEM', w, h, 1);
srcband = src.bands.get(1);
srcband.noDataValue = nodata;
// sprinkle a solid fill with nodata
var buf = Buffer.alloc(w * h);
buf.fill(128);
srcband.pixels.write(0, 0, w, h, new Uint8Array(buf));
for (var i = 0; i < holes_x.length; i++) {
srcband.pixels.set(holes_x[i], holes_y[i], nodata);
assert.equal(srcband.pixels.get(holes_x[i], holes_y[i]), nodata);
}
});
afterEach(function() {
try {
src.close();
} catch (err) {
/* ignore */
}
});
it('should fill nodata values', function() {
gdal.fillNodata({
src: srcband,
searchDist: 3,
smoothingIterations: 2
});
for (var i = 0; i < holes_x.length; i++) {
assert.notEqual(srcband.pixels.get(holes_x[i], holes_y[i]), nodata);
}
});
});
describe('checksumImage()', function() {
var src, band;
var w = 16;
var h = 16;
beforeEach(function() {
src = gdal.open('temp', 'w', 'MEM', w, h, 1);
band = src.bands.get(1);
});
afterEach(function() {
try {
src.close();
} catch (err) {
/* ignore */
}
});
it('should generate unique checksum for the given region', function() {
for (var x = 0; x < w; x++) {
for (var y = 0; y < h; y++) {
band.pixels.set(x, y, (x * h + y) % 255);
}
}
band.pixels.set(4, 4, 25);
var a = gdal.checksumImage(band);
band.pixels.set(4, 4, 93);
var b = gdal.checksumImage(band);
var c = gdal.checksumImage(band, 8, 0, w / 2, h);
assert.notEqual(a, b);
assert.notEqual(b, c);
});
});
describe('sieveFilter()', function() {
var src, band;
var w = 64;
var h = 64;
beforeEach(function() {
src = gdal.open('temp', 'w', 'MEM', w, h, 1);
band = src.bands.get(1);
// create two rectangles next to eachother of differing sizes
var small_buffer = Buffer.alloc(4 * 4);
small_buffer.fill(10);
var big_buffer = Buffer.alloc(32 * 32);
big_buffer.fill(20);
band.pixels.write(5, 5, 32, 32, new Uint8Array(big_buffer));
band.pixels.write(7, 7, 4, 4, new Uint8Array(small_buffer));
});
afterEach(function() {
try {
src.close();
} catch (err) {
/* ignore */
}
});
it('should fill smaller polygons with value from neighboring bigger polygon', function() {
assert.equal(band.pixels.get(8, 8), 10);
gdal.sieveFilter({
src: band,
dst: band, // in place
threshold: 4 * 4 + 1,
connectedness: 8
});
assert.equal(band.pixels.get(8, 8), 20);
});
});
describe('polygonize()', function() {
var src, srcband, dst, lyr;
before(function() {
// create two identical rectangles
var w = 64;
var h = 64;
src = gdal.open('temp', 'w', 'MEM', w, h, 1);
srcband = src.bands.get(1);
for (var y = 0; y < h; y++) {
var buf = Buffer.alloc(w);
buf.fill(y & 32);
srcband.pixels.write(0, y, w, 1, new Uint8Array(buf));
}
});
after(function() {
try {
src.close();
} catch (err) {
/* ignore */
}
});
beforeEach(function() {
dst = gdal.open('temp', 'w', 'Memory');
lyr = dst.layers.create('temp', null, gdal.Polygon);
lyr.fields.add(new gdal.FieldDefn('val', gdal.OFTInteger));
});
afterEach(function() {
try {
dst.close();
} catch (err) {
/* ignore */
}
});
it('should generate polygons from a RasterBand', function() {
gdal.polygonize({
src: srcband,
dst: lyr,
pixValField: 0,
connectedness: 8
});
assert.equal(lyr.features.count(), 2);
lyr.features.forEach(function(f) {
var geom = f.getGeometry();
assert.isFalse(geom.isEmpty());
assert.instanceOf(geom, gdal.Polygon);
});
});
});
});