forked from yocontra/node-gdal-next
-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathopen_vsimem.test.ts
153 lines (141 loc) · 5.36 KB
/
open_vsimem.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
import * as gdal from 'gdal-async'
import * as path from 'path'
import * as fs from 'fs'
import * as chai from 'chai'
import * as chaiAsPromised from 'chai-as-promised'
const assert: Chai.Assert = chai.assert
chai.use(chaiAsPromised)
describe('Open', () => {
afterEach(() => void global.gc!())
describe('vsimem/open', () => {
let filename, ds: gdal.Dataset, buffer: Buffer
it('should not throw', () => {
filename = path.join(__dirname, 'data/park.geo.json')
buffer = fs.readFileSync(filename)
ds = gdal.open(buffer)
})
it('should be able to read band count', () => {
assert.equal(ds.layers.count(), 1)
})
it('should keep the buffer in the dataset', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.instanceOf((ds as any).buffer, Buffer)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.equal((ds as any).buffer, buffer)
})
it('should throw on an empty buffer', () => {
const buffer2 = Buffer.alloc(0)
assert.throws(() => gdal.open(buffer2))
})
it('should throw on an invalid buffer', () => {
const buffer2 = Buffer.alloc(1024)
assert.throws(() => gdal.open(buffer2))
})
it('should be shareable across datasets', () => {
const ds2 = gdal.open(buffer)
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
assert.equal((ds2 as any).buffer, (ds as any).buffer)
ds2.close()
})
it('layer should have all fields defined', () => {
const layer = ds.layers.get(0)
assert.equal(layer.fields.count(), 3)
assert.deepEqual(layer.fields.getNames(), [
'kind',
'name',
'state'
])
})
})
describe('vsimem/openAsync', () => {
let filename, ds: Promise<gdal.Dataset>, buffer: Buffer
after(() => ds.then((r) => {
r.close()
global.gc!()
}))
afterEach(() => void global.gc!())
it('should not throw', () => {
filename = path.join(__dirname, 'data/park.geo.json')
buffer = fs.readFileSync(filename)
ds = gdal.openAsync(buffer)
})
it('should be able to read band count', () =>
assert.eventually.equal(ds.then((ds) => ds.layers.count()), 1)
)
it('should keep the buffer in the dataset', () =>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Promise.all([ assert.eventually.instanceOf(ds.then((ds) => (ds as any).buffer), Buffer),
// eslint-disable-next-line @typescript-eslint/no-explicit-any
assert.eventually.equal(ds.then((ds) => (ds as any).buffer), buffer)
])
)
it('should throw on an empty buffer', () => {
const buffer2 = Buffer.alloc(0)
return assert.isRejected(gdal.openAsync(buffer2))
})
it('should throw on an invalid buffer', () => {
const buffer2 = Buffer.alloc(1024)
return assert.isRejected(gdal.openAsync(buffer2))
})
})
})
describe('gdal.vsimem', () => {
afterEach(() => void global.gc!())
describe('set()', () => {
it('should create a vsimem file from a Buffer', () => {
const buffer_in = fs.readFileSync(path.join(__dirname, 'data/park.geo.json'))
gdal.vsimem.set(buffer_in, '/vsimem/park.geo.json')
const ds = gdal.open('/vsimem/park.geo.json')
ds.close()
const buffer_out = gdal.vsimem.release('/vsimem/park.geo.json')
assert.strictEqual(buffer_in, buffer_out)
})
it('should throw if the buffer is not a Buffer', () => {
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gdal.vsimem.set(({}) as any, '/vsimem/park.geo.json')
})
})
})
describe('copy()', () => {
it('should create a vsimem file from a Buffer', () => {
const buffer_in = fs.readFileSync(path.join(__dirname, 'data/park.geo.json'))
gdal.vsimem.copy(buffer_in, '/vsimem/park.geo.json')
const ds = gdal.open('/vsimem/park.geo.json')
ds.close()
const buffer_out = gdal.vsimem.release('/vsimem/park.geo.json')
assert.deepEqual(buffer_in, buffer_out)
})
it('should throw if the buffer is not a Buffer', () => {
assert.throws(() => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
gdal.vsimem.copy(({}) as any, '/vsimem/park.geo.json')
})
})
})
describe('release()', () => {
it('should allow to retrieve the contents from a vsimem', () => {
const size = 64
const ds = gdal.open('/vsimem/temp1.tiff', 'w', 'GTiff', size, size, 1, gdal.GDT_Byte)
const data = new Uint8Array(size * size)
for (let i = 0; i < data.length; i ++) data[i] = Math.round(Math.random() * 256)
ds.bands.get(1).pixels.write(0, 0, ds.rasterSize.x, ds.rasterSize.y, data)
ds.close()
const buffer = gdal.vsimem.release('/vsimem/temp1.tiff')
assert.instanceOf(buffer, Buffer)
const tmpName = path.join(__dirname, 'data', 'temp', `temp_vsimem_${Date.now()}.tiff`)
fs.writeFileSync(tmpName, buffer)
const tmp = gdal.open(tmpName)
assert.instanceOf(tmp, gdal.Dataset)
assert.deepEqual(tmp.rasterSize, { x: size, y: size })
assert.deepEqual(tmp.bands.get(1).pixels.read(0, 0, size, size), data)
tmp.close()
fs.unlinkSync(tmpName)
})
it('should throw if the file is not a vsimem file', () => {
assert.throws(() => {
gdal.vsimem.release('park.geo.json')
})
})
})
})