forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
api_algorithms.test.ts
710 lines (637 loc) · 22 KB
/
api_algorithms.test.ts
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
import * as gdal from 'gdal-async'
import * as chai from 'chai'
import * as path from 'path'
import * as semver from 'semver'
const assert: Chai.Assert = chai.assert
import * as chaiAsPromised from 'chai-as-promised'
chai.use(chaiAsPromised)
describe('gdal', () => {
afterEach(global.gc!)
describe('contourGenerate()', () => {
let src: gdal.Dataset, srcband: gdal.RasterBand, dst: gdal.Dataset, lyr: gdal.Layer
before(() => {
// create a simple ramp in memory
const w = 64
const h = 64
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
srcband = src.bands.get(1)
for (let y = 0; y < h; y++) {
const buf = Buffer.alloc(w)
buf.fill(y * 4)
srcband.pixels.write(0, y, w, 1, new Uint8Array(buf))
}
})
after(() => {
src.close()
})
beforeEach(() => {
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(() => {
try {
dst.close()
} catch (_err) {
/* ignore */
}
})
it('should generate contours when passed an interval / base', () => {
const offset = 7
const interval = 32
gdal.contourGenerate({
src: srcband,
dst: lyr,
offset: offset,
interval: interval,
idField: 0,
elevField: 1
})
assert.isTrue(lyr.features.count() > 0, 'features were created')
lyr.features.forEach((feature) => {
assert.isTrue(
(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', () => {
const order = (a: number, b: number) => a - b
const levels = [ 53, 43, 193 ].sort(order)
gdal.contourGenerate({
src: srcband,
dst: lyr,
fixedLevels: levels,
idField: 0,
elevField: 1
})
assert.isTrue(lyr.features.count() > 0, 'features were created')
const actual_levels = [] as number[]
lyr.features.forEach((feature) => {
const 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(levels, actual_levels.sort(order), 'all fixed levels used')
})
it('should accept a "progress_cb"', () => {
const offset = 7
const interval = 32
let calls = 0
gdal.contourGenerate({
src: srcband,
dst: lyr,
offset: offset,
interval: interval,
idField: 0,
elevField: 1,
progress_cb: () => {
calls++
}
})
assert.isAbove(calls, 0)
})
})
describe('contourGenerateAsync()', () => {
let src: gdal.Dataset, srcband: gdal.RasterBand, dst: gdal.Dataset, lyr: gdal.Layer
before(() => {
// create a simple ramp in memory
const w = 64
const h = 64
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
srcband = src.bands.get(1)
for (let y = 0; y < h; y++) {
const buf = Buffer.alloc(w)
buf.fill(y * 4)
srcband.pixels.write(0, y, w, 1, new Uint8Array(buf))
}
})
beforeEach(() => {
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))
})
it('should generate contours when passed an interval / base', () => {
const offset = 7
const interval = 32
const p = gdal.contourGenerateAsync({
src: srcband,
dst: lyr,
offset: offset,
interval: interval,
idField: 0,
elevField: 1
})
return assert.eventually.isTrue(p.then(() => lyr.features.count() > 0), 'features were created')
})
})
describe('fillNodata()', () => {
let src: gdal.Dataset, srcband: gdal.RasterBand
const holes_x = [ 53, 61, 61, 1, 43, 44, 5 ]
const holes_y = [ 11, 5, 6, 33, 22, 11, 0 ]
const nodata = 33
beforeEach(() => {
const w = 64
const 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
const buf = Buffer.alloc(w * h)
buf.fill(128)
srcband.pixels.write(0, 0, w, h, new Uint8Array(buf))
for (let 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(() => {
try {
src.close()
} catch (_err) {
/* ignore */
}
})
it('should fill nodata values', () => {
gdal.fillNodata({
src: srcband,
searchDist: 3,
smoothingIterations: 2
})
for (let i = 0; i < holes_x.length; i++) {
assert.notEqual(srcband.pixels.get(holes_x[i], holes_y[i]), nodata)
}
})
})
describe('fillNodataAsync()', () => {
let src: gdal.Dataset, srcband: gdal.RasterBand
const holes_x = [ 53, 61, 61, 1, 43, 44, 5 ]
const holes_y = [ 11, 5, 6, 33, 22, 11, 0 ]
const nodata = 33
beforeEach(() => {
const w = 64
const 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
const buf = Buffer.alloc(w * h)
buf.fill(128)
srcband.pixels.write(0, 0, w, h, new Uint8Array(buf))
for (let 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)
}
})
it('should fill nodata values', () => {
const p = gdal.fillNodataAsync({
src: srcband,
searchDist: 3,
smoothingIterations: 2
})
const r = []
for (let i = 0; i < holes_x.length; i++) {
r.push(assert.eventually.notEqual(p.then(() => srcband.pixels.get(holes_x[i], holes_y[i])), nodata))
}
return assert.isFulfilled(Promise.all(r))
})
})
describe('checksumImage()', () => {
let src: gdal.Dataset, band: gdal.RasterBand
const w = 16
const h = 16
beforeEach(() => {
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
band = src.bands.get(1)
})
afterEach(() => {
try {
src.close()
} catch (_err) {
/* ignore */
}
})
it('should generate unique checksum for the given region', () => {
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
band.pixels.set(x, y, (x * h + y) % 255)
}
}
band.pixels.set(4, 4, 25)
const a = gdal.checksumImage(band)
band.pixels.set(4, 4, 93)
const b = gdal.checksumImage(band)
const c = gdal.checksumImage(band, 8, 0, w / 2, h)
assert.notEqual(a, b)
assert.notEqual(b, c)
})
})
describe('checksumImageAsync()', () => {
let src: gdal.Dataset, band: gdal.RasterBand
const w = 16
const h = 16
beforeEach(() => {
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
band = src.bands.get(1)
})
it('should generate unique checksum for the given region', () => {
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
band.pixels.set(x, y, (x * h + y) % 255)
}
}
band.pixels.set(4, 4, 25)
const a = gdal.checksumImageAsync(band)
band.pixels.set(4, 4, 93)
const b = gdal.checksumImageAsync(band)
const c = gdal.checksumImageAsync(band, 8, 0, w / 2, h)
return assert.isFulfilled(Promise.all([ assert.eventually.notEqual(a, b), assert.eventually.notEqual(b, c) ]))
})
})
describe('sieveFilter()', () => {
let src: gdal.Dataset, band: gdal.RasterBand
const w = 64
const h = 64
beforeEach(() => {
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
band = src.bands.get(1)
// create two rectangles next to eachother of differing sizes
const small_buffer = Buffer.alloc(4 * 4)
small_buffer.fill(10)
const 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(() => {
try {
src.close()
} catch (_err) {
/* ignore */
}
})
it('should fill smaller polygons with value from neighboring bigger polygon', () => {
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)
})
it('accept "progress_cb"', () => {
assert.equal(band.pixels.get(8, 8), 10)
let calls = 0
gdal.sieveFilter({
src: band,
dst: band, // in place
threshold: 4 * 4 + 1,
connectedness: 8,
progress_cb: () => {
calls++
}
})
assert.isAbove(calls, 0)
assert.equal(band.pixels.get(8, 8), 20)
})
})
describe('sieveFilterAsync()', () => {
let src: gdal.Dataset, band: gdal.RasterBand
const w = 64
const h = 64
beforeEach(() => {
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
band = src.bands.get(1)
// create two rectangles next to eachother of differing sizes
const small_buffer = Buffer.alloc(4 * 4)
small_buffer.fill(10)
const 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))
})
it('should fill smaller polygons with value from neighboring bigger polygon', () => {
assert.equal(band.pixels.get(8, 8), 10)
const p = gdal.sieveFilterAsync({
src: band,
dst: band, // in place
threshold: 4 * 4 + 1,
connectedness: 8
})
return assert.eventually.equal(p.then(() => band.pixels.get(8, 8)), 20)
})
})
describe('polygonize()', () => {
let src: gdal.Dataset, srcband: gdal.RasterBand, dst: gdal.Dataset, lyr: gdal.Layer
before(() => {
// create two identical rectangles
const w = 64
const h = 64
src = gdal.open('temp', 'w', 'MEM', w, h, 1)
srcband = src.bands.get(1)
for (let y = 0; y < h; y++) {
const buf = Buffer.alloc(w)
buf.fill(y & 32)
srcband.pixels.write(0, y, w, 1, new Uint8Array(buf))
}
})
after(() => {
try {
src.close()
} catch (_err) {
/* ignore */
}
})
beforeEach(() => {
dst = gdal.open('temp', 'w', 'Memory')
lyr = dst.layers.create('temp', null, gdal.Polygon)
lyr.fields.add(new gdal.FieldDefn('val', gdal.OFTInteger))
})
afterEach(() => {
try {
dst.close()
} catch (_err) {
/* ignore */
}
})
it('should generate polygons from a RasterBand', () => {
gdal.polygonize({
src: srcband,
dst: lyr,
pixValField: 0,
connectedness: 8
})
assert.equal(lyr.features.count(), 2)
lyr.features.forEach((f) => {
const geom = f.getGeometry()
assert.isFalse(geom.isEmpty())
assert.instanceOf(geom, gdal.Polygon)
})
})
it('should accept a "progress_cb"', () => {
let calls = 0
gdal.polygonize({
src: srcband,
dst: lyr,
pixValField: 0,
connectedness: 8,
progress_cb: () => {
calls++
}
})
assert.isAbove(calls, 0)
})
})
describe('addPixelFunc()', () => {
it('should throw with invalid arguments', () => {
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(gdal.addPixelFunc as any)(1, 2)
}, /name must be a string/)
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(gdal.addPixelFunc as any)('func', 2)
}, /pixelFn must be an object/)
assert.throws(() => {
gdal.addPixelFunc('func', new Uint8Array(48))
}, /pixelFn must be a native code pixel function/)
})
})
describe('toPixelFunc()', () => {
let band1: gdal.RasterBand, band2: gdal.RasterBand
before(() => {
band1 = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff')).bands.get(1)
band2 = gdal.open(path.resolve(__dirname, 'data', 'AROME_D2m_10.tiff')).bands.get(1)
})
it('should produce a binary pixel function from a JS function', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const sum2 = (sources: gdal.TypedArray[], buffer: gdal.TypedArray, args: Record<string, string|number>) => {
assert.equal(sources.length, 2)
assert.instanceOf(sources[0], Float64Array)
assert.instanceOf(sources[1], Float64Array)
assert.instanceOf(buffer, Float64Array)
assert.isEmpty(args)
for (let i = 0; i < buffer.length; i++) {
buffer[i] = sources[0][i] + sources[1][i] + 1
}
}
gdal.addPixelFunc('sum2', gdal.toPixelFunc(sum2))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'sum2'
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const q = ds.bands.get(1).pixels.readAsync(0, 0, ds.rasterSize.x, ds.rasterSize.y).then((result) => {
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + 1, 1e-6)
}
})
return assert.isFulfilled(q)
})
it('should propagate exceptions to the calling code', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const fail = () => {
throw new Error('pixel function failed')
}
gdal.addPixelFunc('fail', gdal.toPixelFunc(fail))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'fail'
}
]
})
const ds = gdal.open(vrt)
return assert.isRejected(ds.bands.get(1).pixels.readAsync(0, 0, ds.rasterSize.x, ds.rasterSize.y),
/pixel function failed/)
})
it('should support being called synchronously', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const sync = (sources: gdal.TypedArray[], buffer: gdal.TypedArray) => {
for (let i = 0; i < buffer.length; i++) {
buffer[i] = sources[0][i] + sources[1][i] + 2
}
}
gdal.addPixelFunc('sync', gdal.toPixelFunc(sync))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'sync'
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const result = ds.bands.get(1).pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + 2, 1e-6)
}
})
it('should support converting the data type', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const sum2 = (sources: gdal.TypedArray[], buffer: gdal.TypedArray) => {
assert.equal(sources.length, 2)
assert.instanceOf(sources[0], Float32Array)
assert.instanceOf(sources[1], Float32Array)
assert.instanceOf(buffer, Int32Array)
for (let i = 0; i < buffer.length; i++) {
buffer[i] = sources[0][i] + sources[1][i] + 3
}
}
gdal.addPixelFunc('sum2int', gdal.toPixelFunc(sum2))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'sum2int',
dataType: gdal.GDT_Int32,
sourceTransferType: gdal.GDT_Float32
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
assert.equal(ds.bands.get(1).dataType, gdal.GDT_Int32)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const q = ds.bands.get(1).pixels.readAsync(0, 0, ds.rasterSize.x, ds.rasterSize.y).then((result) => {
assert.instanceOf(result, Int32Array)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + 3, 1)
}
})
return assert.isFulfilled(q)
})
it('should pass any additional arguments', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const withArgs = (sources: gdal.TypedArray[], buffer: gdal.TypedArray, args: Record<string, string|number>) => {
assert.deepEqual(args, { s: 'stringArg', k: 20, t: 15, pi: 3.14 })
assert.isString(args.s)
assert.isNumber(args.k)
assert.isNumber(args.t)
assert.isNumber(args.pi)
for (let i = 0; i < buffer.length; i++) {
buffer[i] = +args.k + sources[0][i] + sources[1][i]
}
}
gdal.addPixelFunc('withArgs', gdal.toPixelFunc(withArgs))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'withArgs',
pixelFuncArgs: { s: 'stringArg', k: 20, t: '15', pi: '3.14' }
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const result = ds.bands.get(1).pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + 20, 1e-6)
}
})
})
describe('createPixelFunc()', () => {
let band1: gdal.RasterBand, band2: gdal.RasterBand
before(() => {
band1 = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff')).bands.get(1)
band2 = gdal.open(path.resolve(__dirname, 'data', 'AROME_D2m_10.tiff')).bands.get(1)
})
it('should create a pixel function from a JS function for a single pixel', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
gdal.addPixelFunc('createPxFn', gdal.createPixelFunc((a, b) => a + b + 4))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'createPxFn'
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const result = ds.bands.get(1).pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + 4, 1e-6)
}
})
it('should support converting the data type', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
gdal.addPixelFunc('createPxFnInt', gdal.createPixelFunc((a, b) => Math.round(a + b + 5)))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'createPxFnInt',
dataType: gdal.GDT_Int32,
sourceTransferType: gdal.GDT_Float32
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
assert.equal(ds.bands.get(1).dataType, gdal.GDT_Int32)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const result = ds.bands.get(1).pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.instanceOf(result, Int32Array)
assert.closeTo(result[i], input1[i] + input2[i] + 5, 0.5, `${input1[i]} + ${input2[i]}`)
}
})
})
describe('createPixelFuncWithArgs()', () => {
let band1: gdal.RasterBand, band2: gdal.RasterBand
before(() => {
band1 = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff')).bands.get(1)
band2 = gdal.open(path.resolve(__dirname, 'data', 'AROME_D2m_10.tiff')).bands.get(1)
})
it('should create a pixel function with arguments from a JS function for a single pixel', function () {
if (!semver.gte(gdal.version, '3.5.0-git')) this.skip()
const k = 42
gdal.addPixelFunc('createPxFn', gdal.createPixelFuncWithArgs((args, a, b) => a + b + +args.k))
const vrt = gdal.wrapVRT({
bands: [
{
sources: [ band1, band2 ],
pixelFunc: 'createPxFn',
pixelFuncArgs: { k }
}
]
})
const ds = gdal.open(vrt)
assert.equal(ds.bands.count(), 1)
const input1 = band1.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const input2 = band2.pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
const result = ds.bands.get(1).pixels.read(0, 0, ds.rasterSize.x, ds.rasterSize.y)
for (let i = 0; i < ds.rasterSize.x * ds.rasterSize.y; i += 256) {
assert.closeTo(result[i], input1[i] + input2[i] + k, 1e-6)
}
})
})
})