-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathapi_rasterband-async.test.js
303 lines (293 loc) · 10.8 KB
/
api_rasterband-async.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
const chaiAsPromised = require('chai-as-promised')
const chai = require('chai')
const assert = chai.assert
const gdal = require('../lib/gdal.js')
chai.use(chaiAsPromised)
const expect = chai.expect
/* Without this, unhandledRejections are silently ignored!!!
*/
process.on('unhandledRejection', (e) => {
console.error(e); process.exit(1)
})
// Not supported on GDAL 1.x
if (parseFloat(gdal.version) < 2) return
describe('gdal.RasterBand', () => {
afterEach(gc)
it('should not be instantiable', () => {
assert.throws(() => {
new gdal.RasterBand()
})
})
describe('instance', () => {
describe('"description" property', () => {
describe('getter', () => {
it('should return string', () => {
gdal.openAsync(`${__dirname}/data/dem_azimuth50_pa.img`).then((ds) => {
const band = ds.bands.get(1)
assert.equal(band.description, 'hshade17')
})
})
it('should throw error if dataset already closed', () => {
gdal.openAsync(`${__dirname}/data/dem_azimuth50_pa.img`).then((ds) => {
const band = ds.bands.get(1)
ds.close()
assert.throws(() => {
console.log(band.description)
})
})
})
})
describe('setter', () => {
it('should throw error', () => {
gdal.openAsync(`${__dirname}/data/dem_azimuth50_pa.img`,
(e, ds) => {
const band = ds.bands.get(1)
assert.throws(() => {
band.description = 'test'
})
})
})
})
})
describe('"readOnly" property', () => {
describe('getter', () => {
it('should return true on readOnly dataset', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
assert.isTrue(band.readOnly)
})
})
})
})
describe('"pixels" property', () => {
describe('readAsync() w/cb', () => {
it('should return a TypedArray', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`, (e, ds) => {
const band = ds.bands.get(1)
const w = 20
const h = 30
band.pixels.readAsync(190, 290, w, h, (e, data) => {
assert.instanceOf(data, Uint8Array)
assert.equal(data.length, w * h)
assert.equal(data[10 * 20 + 10], 10)
})
})
})
})
describe('readAsync() w/Promise', () => {
it('should return a TypedArray', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
const w = 20
const h = 30
band.pixels.readAsync(190, 290, w, h).then((data) => {
assert.instanceOf(data, Uint8Array)
assert.equal(data.length, w * h)
assert.equal(data[10 * 20 + 10], 10)
})
})
})
describe('w/data argument', () => {
it('should put the data in the existing array', () => {
gdal.openAsync('temp',
'w',
'MEM',
256,
256,
1,
gdal.GDT_Byte).then((ds) => {
const band = ds.bands.get(1)
const data = new Uint8Array(new ArrayBuffer(20 * 30))
data[15] = 31
band.pixels.readAsync(0, 0, 20, 30, data).then((result) => {
assert.equal(data, result)
assert.equal(data[15], 0)
})
})
})
it('should create new array if null', () => {
gdal.openAsync(
'temp',
'w',
'MEM',
256,
256,
1,
gdal.GDT_Byte
).then((ds) => {
const band = ds.bands.get(1)
band.pixels.readAsync(0, 0, 20, 30, null).then((data) => {
assert.instanceOf(data, Uint8Array)
assert.equal(data.length, 20 * 30)
})
})
})
it('should throw error if array is too small', () => {
gdal.openAsync('temp',
'w',
'MEM',
256,
256,
1,
gdal.GDT_Byte).then(async (ds) => {
const band = ds.bands.get(1)
const data = new Uint8Array(new ArrayBuffer(20 * 30))
await expect(band.pixels.readAsync(0, 0, 20, 31, data)).to.be.rejectedWith(Error)
})
})
it('should automatically translate data to array data type', () => {
gdal.openAsync('temp',
'w',
'MEM',
256,
256,
1,
gdal.GDT_Byte
).then((ds) => {
const band = ds.bands.get(1)
band.pixels.set(1, 1, 30)
const data = new Float64Array(new ArrayBuffer(20 * 30 * 8))
band.pixels.readAsync(1, 1, 20, 30, data).then((data) => {
assert.equal(data[0], 30)
})
})
})
})
describe('w/options', () => {
describe('"buffer_width", "buffer_height"', () => {
it('should default to width, height when not present', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
band.pixels.readAsync(0, 0, 20, 30).then((data) => {
assert.equal(data.length, 20 * 30)
})
})
})
it("should create new array with given dimensions if array isn't given", () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
band.pixels.readAsync(0, 0, 20, 30, null, {
buffer_width: 10,
buffer_height: 15
}).then((data) => {
assert.equal(data.length, 10 * 15)
})
})
})
it('should throw error if given array is smaller than given dimensions', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then(async (ds) => {
const band = ds.bands.get(1)
const data = new Float64Array(new ArrayBuffer(8 * 10 * 14))
await expect(band.pixels.readAsync(0, 0, 20, 30, data, {
buffer_width: 10,
buffer_height: 15
} /*Array length must be greater than.*/)).to.be.rejectedWith(Error)
})
})
})
describe('"type"', () => {
it('should be ignored if typed array is given', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
const data = new Float64Array(new ArrayBuffer(20 * 30 * 8))
const result = band.pixels.read(0, 0, 20, 30, data, {
type: gdal.GDT_Byte
})
assert.instanceOf(result, Float64Array)
})
})
it('should create output array with given type', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then((ds) => {
const band = ds.bands.get(1)
const data = band.pixels.read(0, 0, 20, 30, null, {
type: gdal.GDT_Float64
})
assert.instanceOf(data, Float64Array)
})
})
})
describe('"pixel_space", "line_space"', () => {
it('should read data with space between values', () => {
const w = 16,
h = 16
gdal.openAsync('temp', 'w', 'MEM', w, h, 2, gdal.GDT_Byte).then((ds) => {
const red = ds.bands.get(1)
const blue = ds.bands.get(2)
red.fill(1)
blue.fill(2)
const interleaved = new Uint8Array(new ArrayBuffer(w * h * 2))
const read_options = {
buffer_width: w,
buffer_height: h,
type: gdal.GDT_Byte,
pixel_space: 2,
line_space: 2 * w
}
red.pixels.readAsync(0, 0, w, h, interleaved, read_options).then((interleaved) => {
blue.pixels.readAsync(
0,
0,
w,
h,
interleaved.subarray(1),
read_options
).then(() => {
for (let y = 0; y < h; y++) {
for (let x = 0; x < w; x++) {
const r = interleaved[x * 2 + 0 + y * w * 2]
const b = interleaved[x * 2 + 1 + y * w * 2]
assert.equal(r, 1)
assert.equal(b, 2)
}
}
})
})
})
})
it('should throw error if array is not long enough to store result', () => {
const w = 16,
h = 16
gdal.openAsync('temp', 'w', 'MEM', w, h, 2, gdal.GDT_Byte).then((ds) => {
const red = ds.bands.get(1)
const blue = ds.bands.get(2)
red.fill(1)
blue.fill(2)
const interleaved = new Uint8Array(new ArrayBuffer(w * h * 2))
const read_options = {
buffer_width: w,
buffer_height: h,
type: gdal.GDT_Byte,
pixel_space: 2,
line_space: 2 * w
}
red.pixels.readAsync(0, 0, w, h, interleaved, read_options).then(async (interleaved) => {
await expect(blue.pixels.readAsync(
0,
0,
w,
h,
interleaved.subarray(2),
read_options
)).to.be.rejectedWith(Error)
})
})
})
})
it('should throw an error if region is out of bounds', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then(async (ds) => {
const band = ds.bands.get(1)
await expect(band.pixels.readAsync(2000, 2000, 16, 16)).to.be.rejectedWith(3)
})
})
it('should throw error if dataset already closed', () => {
gdal.openAsync(`${__dirname}/data/sample.tif`).then(async (ds) => {
const band = ds.bands.get(1)
ds.close()
await expect(band.pixels.readAsync(0, 0, 16, 16)).to.be.rejectedWith(Error)
})
})
})
})
})
})
})