forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
api_streams.test.ts
331 lines (296 loc) · 12.8 KB
/
api_streams.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
import * as gdal from 'gdal-async'
import { Transform, finished as _finished } from 'stream'
import { promisify } from 'util'
import * as chai from 'chai'
import * as path from 'path'
import * as semver from 'semver'
const assert: Chai.Assert = chai.assert
const finished = promisify(_finished)
type doneCb = (err?: unknown) => void
describe('gdal.RasterReadStream', () => {
function noDataTest(done: doneCb, file: string, convert?: boolean) {
const ds = gdal.open(path.resolve(__dirname, 'data', file))
const band = ds.bands.get(1)
const noData = ds.bands.get(1).noDataValue
let test: (v: number) => boolean
if (convert) test = isNaN
else test = (v) => v === noData
const rs = band.pixels.createReadStream({
convertNoData: convert,
type: Float64Array
})
rs.once('data', (chunk) => {
try {
assert.instanceOf(chunk, Float64Array)
assert.isTrue(chunk.subarray(0, 50).every(test))
done()
} catch (e) {
done(e)
}
})
}
function readTest(done: doneCb, file: string, blockOptimize: boolean) {
const ds = gdal.open(path.resolve(__dirname, 'data', file))
const band = ds.bands.get(1)
const expected = band.pixels.read(0, 0, band.size.x, band.size.y)
const type = gdal.fromDataType(band.dataType)
const actual = new type(band.size.x * band.size.y)
const rs = band.pixels.createReadStream({ blockOptimize })
assert.instanceOf(rs, gdal.RasterReadStream)
let length = 0
rs.on('data', (chunk) => {
try {
assert.instanceOf(chunk, type)
actual.set(chunk, length)
length += chunk.length
} catch (e) {
rs.removeAllListeners('data')
rs.removeAllListeners('end')
done(e)
}
})
rs.on('end', () => {
try {
assert.equal(length, band.size.x * band.size.y)
assert.deepEqual(actual, expected)
done()
} catch (e) {
done(e)
}
})
}
const inputFiles = [
'gfs.t00z.alnsf.nc',
'h5ex_d_gzip.h5',
'sample.tif',
'a39se10.jpg'
]
if (semver.gte(gdal.version, '3.0.0')) {
inputFiles.push('gfs.t00z.pgrb2b.4p.f000.grb2')
inputFiles.push('France_120km_WRF_WAM_210525-00.grb')
}
it('should accept a raster band', (done) => readTest(done, 'sample.tif', true))
it('should accept a raster band w/o blockOptimize', (done) => readTest(done, 'sample.tif', false))
it('should accept a raster band w/Float', (done) => readTest(done, 'AROME_T2m_10.tiff', true))
it('should accept a raster band w/Float w/o blockOptimize', (done) => readTest(done, 'AROME_T2m_10.tiff', false))
it('should support on the fly conversion w/ noData', (done) => noDataTest(done, 'dem_azimuth50_pa.img', undefined))
it('should support noData conversion', (done) => noDataTest(done, 'dem_azimuth50_pa.img', true))
for (const file of inputFiles) {
it(`should accept various formats (${file})`, (done) => readTest(done, file, true))
}
})
describe('gdal.RasterWriteStream', () => {
function writeTest(done: doneCb, w: number, h: number, len: number, blockSize: number, blockOptimize: boolean, convertNoData?: boolean) {
const filename = `/vsimem/ds_ws_test.${String(
Math.random()
).substring(2)}.tmp.tiff`
const ds = gdal.open(filename, 'w', 'GTiff', w, h, 1, gdal.GDT_Float64, { BLOCKXSIZE: w, BLOCKYSIZE: blockSize })
const band = ds.bands.get(1)
if (convertNoData) band.noDataValue = 1e38
const ws = band.pixels.createWriteStream({ blockOptimize, convertNoData })
const pattern = new Float64Array(len)
for (let i = 0; i < len; i++) {
if (i % 10 == 0 && convertNoData) pattern[i] = NaN
else pattern[i] = i
}
let written = 0
// this is the callback for the final write
function endCheck() {
try {
ds.flush()
ds.close()
const testDS = gdal.open(filename)
const testData = testDS.bands.get(1).pixels.read(0, 0, w, h)
if (convertNoData) {
for (let i = 0; i < pattern.length; i++) if (isNaN(pattern[i])) pattern[i] = 1e38
}
for (let i = 0; i < w*h; i += len) {
assert.deepEqual(pattern, testData.subarray(i, i + len))
}
testDS.close()
gdal.vsimem.release(filename)
done()
} catch (e) {
// mocha has a complicated relationship with async exceptions
// we have to explicitly signal errors
done(e)
}
}
function write() {
while (written < w*h) {
written += pattern.length
// write until finished or write returns false because the buffer is full
if (!ws.write(pattern, written == w*h ? endCheck : undefined)) break
}
}
// continue writing if we had to stop because the buffer was full
ws.on('drain', write)
// mocha can't catch this
ws.on('error', (e) => {
done(e)
})
// start writing
write()
}
function writeTestOverflow(done: doneCb, w: number, h: number, len: number, blockSize: number, blockOptimize: boolean) {
const doneInverted = (err: Error) => {
if (!err) done('did not throw')
else if (err.toString().match(/beyond the end/)) done()
else done(err)
}
writeTest(doneInverted as doneCb, w, h, len, blockSize, blockOptimize, undefined)
}
it('should write a raster band in zero-copy mode w/ edge block',
(done) => writeTest(done, 801, 600, 1602, 2, true, undefined))
it('should write a raster band in zero-copy mode w/o edge block',
(done) => writeTest(done, 801, 601, 1803, 3, true, undefined))
it('should write a raster band in block consolidation mode w/ big chunks w/ edge block',
(done) => writeTest(done, 801, 601, 1803, 2, true, undefined))
it('should write a raster band in block consolidation mode w/ small chunks w/ edge block',
(done) => writeTest(done, 801, 601, 267, 2, true, undefined))
it('should write a raster band in block consolidation mode w/ big chunks w/o edge block',
(done) => writeTest(done, 801, 601, 1803, 6, true, undefined))
it('should write a raster band in block consolidation mode w/ small chunks w/o edge block',
(done) => writeTest(done, 801, 601, 267, 6, true, undefined))
it('should write a raster band in line mode w/ big chunks',
(done) => writeTest(done, 801, 601, 1803, 2, false, undefined))
it('should write a raster band in line mode w/ small chunks',
(done) => writeTest(done, 801, 601, 267, 2, false, undefined))
it('should throw on writing beyond the end in zero-copy mode w/ edge block',
(done) => writeTestOverflow(done, 801, 601, 1804, 2, true))
it('should throw on writing beyond the end in zero-copy mode w/o edge block',
(done) => writeTestOverflow(done, 801, 601, 1804, 3, true))
it('should throw on writing beyond the end in block consolidation mode w/ big chunks w/ edge block',
(done) => writeTestOverflow(done, 801, 601, 1804, 2, true))
it('should throw on writing beyond the end in block consolidation mode w/ small chunks w/ edge block',
(done) => writeTestOverflow(done, 801, 601, 268, 2, true))
it('should throw on writing beyond the end in block consolidation mode w/ big chunks w/o edge block',
(done) => writeTestOverflow(done, 801, 601, 1804, 6, true))
it('should throw on writing beyond the end in block consolidation mode w/ small chunks w/o edge block',
(done) => writeTestOverflow(done, 801, 601, 268, 6, true))
it('should throw on writing beyond the end in line mode w/ big chunks',
(done) => writeTestOverflow(done, 801, 601, 1804, 2, false))
it('should throw on writing beyond the end in line mode w/ small chunks',
(done) => writeTestOverflow(done, 801, 601, 268, 2, false))
it('should support noData conversion', (done) => writeTest(done, 801, 601, 1803, 2, true, true))
it('should support an ill-behaved user application', (done) => {
const filename = `/vsimem/ds_pressure_test.${String(
Math.random()
).substring(2)}.tmp.tiff`
const ds = gdal.open(filename, 'w', 'GTiff', 64, 128, 1, gdal.GDT_Float64)
const band = ds.bands.get(1)
const ws = band.pixels.createWriteStream()
const data = new Float64Array(64)
for (let i = 0; i < 64; i++) data[i] = i
for (let i = 0; i < 127; i++) ws.write(data)
ws.write(data, () => {
try {
ds.flush()
ds.close()
const testDS = gdal.open(filename)
const testData = testDS.bands.get(1).pixels.read(0, 0, 64, 128)
for (let i = 0; i < 128; i += 128) {
assert.deepEqual(data, testData.subarray(i * 64, (i+1) * 64))
}
testDS.close()
gdal.vsimem.release(filename)
done()
} catch (e) {
done(e)
}
})
})
})
describe('gdal.RasterReadStream + gdal.RasterWriteStream', () => {
it('should support piping', () => {
const dsIn = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff'))
const filename = `/vsimem/ds_pipe_test.${String(
Math.random()
).substring(2)}.tmp.tiff`
const dsOut = gdal.open(filename, 'w', 'GTiff', dsIn.rasterSize.x, dsIn.rasterSize.y, 1, gdal.GDT_Float64)
const bandIn = dsIn.bands.get(1)
const bandOut = dsOut.bands.get(1)
const rs = bandIn.pixels.createReadStream()
const ws = bandOut.pixels.createWriteStream()
rs.pipe(ws)
return assert.isFulfilled(finished(ws).then(() => {
dsOut.close()
const dataOrig = bandIn.pixels.read(0, 0, bandIn.size.x, bandIn.size.y)
const dsTest = gdal.open(filename)
const dataTest = dsTest.bands.get(1).pixels.read(0, 0, bandIn.size.x, bandIn.size.y)
assert.deepEqual(dataOrig, dataTest)
dsTest.close()
gdal.vsimem.release(filename)
}))
})
it('should support transforms', () => {
const dsIn = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff'))
const filename = `/vsimem/ds_transform_test.${String(
Math.random()
).substring(2)}.tmp.tiff`
const dsOut = gdal.open(filename, 'w', 'GTiff', dsIn.rasterSize.x, dsIn.rasterSize.y, 1, gdal.GDT_Float64)
const bandIn = dsIn.bands.get(1)
const bandOut = dsOut.bands.get(1)
const rs = bandIn.pixels.createReadStream()
const ws = bandOut.pixels.createWriteStream()
const scaleTransform = new Transform({
objectMode: true,
transform(chunk, _, cb) {
for (let i = 0; i < chunk.length; i++) {
chunk[i] = Math.min(chunk[i], 6000) / 250
}
cb(null, chunk)
}
})
rs.pipe(scaleTransform).pipe(ws)
return assert.isFulfilled(finished(ws).then(() => {
dsOut.close()
const dataOrig = bandIn.pixels.read(0, 0, bandIn.size.x, bandIn.size.y)
for (let i = 0; i < dataOrig.length; i++) {
dataOrig[i] = Math.min(dataOrig[i], 6000) / 250
}
const dsTest = gdal.open(filename)
const dataTest = dsTest.bands.get(1).pixels.read(0, 0, bandIn.size.x, bandIn.size.y)
assert.deepEqual(dataOrig, dataTest)
dsTest.close()
gdal.vsimem.release(filename)
}))
})
})
describe('gdal.RasterMuxStream', () => {
function testMux(blockOptimize?: boolean) {
const dsT2m = gdal.open(path.resolve(__dirname, 'data', 'AROME_T2m_10.tiff'))
const dsD2m = gdal.open(path.resolve(__dirname, 'data', 'AROME_D2m_10.tiff'))
const filename = `/vsimem/ds_mux_test.${String(
Math.random()
).substring(2)}.tmp.tiff`
const dsCloudBase = gdal.open(filename, 'w', 'GTiff', dsT2m.rasterSize.x, dsD2m.rasterSize.y, 1, gdal.GDT_Float64)
const mux = new gdal.RasterMuxStream({
T2m: dsT2m.bands.get(1).pixels.createReadStream(),
D2m: dsD2m.bands.get(1).pixels.createReadStream()
}, { blockOptimize })
const ws = dsCloudBase.bands.get(1).pixels.createWriteStream()
// Espy's estimation for cloud base height (lifted condensation level)
// LCL = 125 * (T2m - Td2m)
// where T2m is the temperature at 2m and Td2m is the dew point at 2m
const fn = (t: number, td: number) => 125 * (t - td)
const espyEstimation = new gdal.RasterTransform({ type: Float64Array, fn })
mux.pipe(espyEstimation).pipe(ws)
return assert.isFulfilled(finished(ws).then(() => {
dsCloudBase.close()
const dataOrigT2m = dsT2m.bands.get(1).pixels.read(0, 0, dsT2m.rasterSize.x, dsT2m.rasterSize.y)
const dataOrigD2m = dsD2m.bands.get(1).pixels.read(0, 0, dsD2m.rasterSize.x, dsD2m.rasterSize.y)
const dataCloudBase = new Float64Array(dsD2m.rasterSize.x * dsD2m.rasterSize.y)
for (let i = 0; i < dataOrigT2m.length; i++) {
dataCloudBase[i] = 125 * (dataOrigT2m[i] - dataOrigD2m[i])
}
const dsTest = gdal.open(filename)
const dataTest = dsTest.bands.get(1).pixels.read(0, 0, dsTest.rasterSize.x, dsTest.rasterSize.y)
assert.deepEqual(dataCloudBase, dataTest)
dsTest.close()
gdal.vsimem.release(filename)
}))
}
it('should accept multiple inputs', () => testMux(undefined))
it('should support different block sizes', () => testMux(false))
})