forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 27
/
api_asyncIterators.test.ts
124 lines (123 loc) · 4.95 KB
/
api_asyncIterators.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
import * as gdal from 'gdal-async'
import * as path from 'path'
import { assert } from 'chai'
import * as fileUtils from './utils/file'
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
import * as semver from 'semver'
chai.use(chaiAsPromised)
if (Symbol.asyncIterator) {
describe('gdal.Dataset', () => {
describe('instance', () => {
describe('"bands" propertry', () => {
describe('@@asyncIterator()', () => {
it('should support iterating over the values', async () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'multiband.tif'))
let count = 0
for await (const band of ds.bands) {
assert.instanceOf(band, gdal.RasterBand)
count++
}
assert.equal(count, ds.bands.count())
})
it('should throw error if dataset is destroyed', () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'multiband.tif'))
ds.close()
return assert.isRejected((async () => {
for await (const band of ds.bands) void band
})(), /already been destroyed/)
})
})
})
})
})
describe('gdal.Layer', () => {
describe('instance', () => {
describe('"features" propertry', () => {
describe('@@asyncIterator()', () => {
it('should support iterating over the values', async () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'park.geo.json'))
const layer = ds.layers.get(0)
let count = 0
for await (const feature of layer.features) {
assert.instanceOf(feature, gdal.Feature)
count++
}
assert.equal(count, layer.features.count())
})
it('should throw error if dataset is destroyed', () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'park.geo.json'))
const layer = ds.layers.get(0)
ds.close()
return assert.isRejected((async () => {
for (const l of layer.features) void l
})(), /already destroyed/)
})
})
})
})
})
describe('gdal.RasterBand', () => {
describe('"overviews" property', () => {
describe('@@asyncIterator()', () => {
if (!semver.satisfies(gdal.version, '^3.1.4')) {
// Work around https://github.com/OSGeo/gdal/issues/3746
// on Fedora 33
it('should iterate through the overviews', async () => {
const tempFile = fileUtils.clone(`${__dirname}/data/sample.tif`)
const ds = gdal.open(tempFile, 'r+')
const band = ds.bands.get(1)
ds.buildOverviews('NEAREST', [ 2, 4 ])
const w = []
for await (const overview of band.overviews) {
w.push(overview.size.x)
}
assert.sameMembers(w, [ ds.rasterSize.x / 2, ds.rasterSize.x / 4 ])
ds.close()
gdal.vsimem.release(tempFile)
})
it('should throw error if dataset already closed', () => {
const tempFile = fileUtils.clone(`${__dirname}/data/sample.tif`)
const ds = gdal.open(tempFile, 'r+')
const band = ds.bands.get(1)
ds.buildOverviews('NEAREST', [ 2 ])
ds.close()
gdal.vsimem.release(tempFile)
return assert.isRejected((async () => {
for await (const overview of band.overviews) void overview
})(), /already been destroyed/)
})
}
})
})
})
if (semver.gte(gdal.version, '3.1.0')) {
const tests = {
Group: { groups: gdal.Group, arrays: gdal.MDArray, dimensions: gdal.Dimension, attributes: gdal.Attribute },
MDArray: { dimensions: gdal.Dimension, attributes: gdal.Attribute }
} as Record<string, Record<string, new () => gdal.Attribute | gdal.Dimension | gdal.MDArray | gdal.Group>>
for (const tested of Object.keys(tests) as ('Group' | 'MDArray')[]) {
describe(`gdal.${tested}`, () => {
for (const prop of Object.keys(tests[tested]) as ('groups' | 'arrays' | 'dimensions' | 'attributes')[]) {
describe(`"${prop}" property`, () => {
describe('@@asyncIterator()', () => {
it('should iterate over the values', () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'gfs.t00z.alnsf.nc'), 'mr')
for (const i of ds.root[prop]) {
assert.instanceOf(i, tests[tested][prop])
}
})
it('should reject if the dataset is closed', () => {
const ds = gdal.open(path.resolve(__dirname, 'data', 'gfs.t00z.alnsf.nc'), 'mr')
ds.close()
return assert.isRejected((async () => {
for await (const i of ds.root[prop]) void i
})(), /already been destroyed/)
})
})
})
}
})
}
}
}