-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathrasterio.jl
604 lines (556 loc) · 16.8 KB
/
rasterio.jl
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
"""
rasterio!(dataset::AbstractDataset, buffer::AbstractArray{<:Any, 3},
bands; <keyword arguments>)
rasterio!(dataset::AbstractDataset, buffer::AbstractArray{<:Any, 3}, bands, rows,
cols; <keyword arguments>)
rasterio!(rasterband::AbstractRasterBand, buffer::AbstractMatrix{<:Any};
<keyword arguments>)
rasterio!(rasterband::AbstractRasterBand, buffer::AbstractMatrix{<:Any}, rows,
cols; <keyword arguments>)
Read/write a region of image data from multiple bands.
This method allows reading a region of one or more `RasterBand`s from this
dataset into a buffer, or writing data from a buffer into a region of the
`RasterBand`s. It automatically takes care of data type translation if the
element type (`<:Any`) of the buffer is different than that of the
`RasterBand`. The method also takes care of image decimation / replication
if the buffer size (`xsz × ysz`) is different than the size of the
region being accessed (`xsize × ysize`).
The `pxspace`, `linespace` and `bandspace` parameters allow reading into
or writing from various organization of buffers.
For highest performance full resolution data access, read and write on \"block
boundaries\" as returned by `blocksize()`, or use the `readblock!()` and
`writeblock!()` methods.
### Parameters
* `rows` A continuous range of rows expressed as a
`UnitRange{<:Integer}`, such as 2:9.
* `cols` A continuous range of columns expressed as a
`UnitRange{<:Integer}`, such as 2:9.
* `access` Either `GF_Read` to read a region of data, or
`GF_Write` to write a region of data.
* `xoffset` The pixel offset to the top left corner of the region to be
accessed. It will be `0` (default) to start from the left.
* `yoffset` The line offset to the top left corner of the region to be
accessed. It will be `0` (default) to start from the top.
* `xsize` The width of the region of the band to be accessed in pixels.
* `ysize` The height of the region of the band to be accessed in lines.
* `buffer` The buffer into which the data should be read, or from which it
should be written. It must contain `≥ xsz * ysz * <# of bands>`
words of type `eltype(buffer)`. It is organized in left to
right, top to bottom pixel order. Spacing is controlled by the
`pxspace`, and `linespace` parameters
* `xsz` The width of the buffer into which the desired region is
to be read, or from which it is to be written.
* `ysz` The height of the buffer into which the desired region is
to be read, or from which it is to be written.
* `bands` The list of bands (`1`-based) to be read/written.
* `pxspace` The byte offset from the start of a pixel value in the `buffer`
to the start of the next pixel value within a scanline. By
default (i.e., `0`) the size of `eltype(buffer)` will be used.
* `linespace` The byte offset from the start of one scanline in pBuffer to
the start of the next. By default (i.e., `0`) the value of
`sizeof(eltype(buffer)) * xsz` will be used.
* `bandspace` The byte offset from the start of one bands data to the start
of the next. By default (`0`), it will be `linespace * ysz`
implying band sequential organization of the buffer.
### Returns
`CE_Failure` if the access fails, otherwise `CE_None`.
"""
function rasterio! end
function rasterio!(
dataset::AbstractDataset,
buffer::T,
bands,
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
bandspace::Integer = 0,
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(
dataset,
buffer,
bands,
0,
0,
size(buffer, 1),
size(buffer, 2),
access,
pxspace,
linespace,
bandspace,
)
return buffer
end
function rasterio!(
dataset::AbstractDataset,
buffer::AbstractArray{T,3},
bands,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
bandspace::Integer = 0,
)::Array{T,3} where {T<:Any}
xsize = cols[end] - cols[1] + 1
xsize < 0 && error("invalid window width")
ysize = rows[end] - rows[1] + 1
ysize < 0 && error("invalid window height")
rasterio!(
dataset,
buffer,
bands,
cols[1] - 1,
rows[1] - 1,
xsize,
ysize,
access,
pxspace,
linespace,
bandspace,
)
return buffer
end
function rasterio!(
dataset::AbstractDataset,
buffer::AbstractArray{T,3},
bands,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
bandspace::Integer = 0,
extraargs = Ptr{GDAL.GDALRasterIOExtraArg}(C_NULL),
)::Array{T,3} where {T<:Any}
# `psExtraArg` (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg
# structure with additional arguments to specify resampling and
# progress callback, or `NULL` for default behaviour. The
# `GDAL_RASTERIO_RESAMPLING` configuration option can also be
# defined to override the default resampling to one of `BILINEAR`,
# `CUBIC`, `CUBICSPLINE`, `LANCZOS`, `AVERAGE` or `MODE`.
(dataset == C_NULL) && error("Can't read NULL dataset")
xbsize, ybsize, zbsize = size(buffer)
nband = length(bands)
bands = isa(bands, Vector{Cint}) ? bands : Cint.(collect(bands))
@assert nband == zbsize
result = GDAL.gdaldatasetrasterioex(
dataset,
access,
xoffset,
yoffset,
xsize,
ysize,
pointer(buffer),
xbsize,
ybsize,
convert(GDALDataType, T),
nband,
pointer(bands),
pxspace,
linespace,
bandspace,
extraargs,
)
@cplerr result "Access in DatasetRasterIO failed."
return buffer
end
function rasterio!(
rasterband::AbstractRasterBand,
buffer::T,
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(
rasterband,
buffer,
0,
0,
width(rasterband),
height(rasterband),
access,
pxspace,
linespace,
)
return buffer
end
function rasterio!(
rasterband::AbstractRasterBand,
buffer::T,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
)::T where {T<:AbstractMatrix{<:Any}}
xsize = length(cols)
xsize < 1 && error("invalid window width")
ysize = length(rows)
ysize < 1 && error("invalid window height")
rasterio!(
rasterband,
buffer,
cols[1] - 1,
rows[1] - 1,
xsize,
ysize,
access,
pxspace,
linespace,
)
return buffer
end
function rasterio!(
rasterband::AbstractRasterBand,
buffer::AbstractMatrix{T},
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
extraargs = Ptr{GDAL.GDALRasterIOExtraArg}(C_NULL),
)::Matrix{T} where {T<:Any}
# `psExtraArg` (new in GDAL 2.0) pointer to a GDALRasterIOExtraArg
# structure with additional arguments to specify resampling and
# progress callback, or `NULL` for default behaviour. The
# `GDAL_RASTERIO_RESAMPLING` configuration option can also be
# defined to override the default resampling to one of `BILINEAR`,
# `CUBIC`, `CUBICSPLINE`, `LANCZOS`, `AVERAGE` or `MODE`.
(rasterband == C_NULL) && error("Can't read NULL rasterband")
xbsize, ybsize = size(buffer)
result = GDAL.gdalrasterioex(
rasterband,
access,
xoffset,
yoffset,
xsize,
ysize,
pointer(buffer),
xbsize,
ybsize,
convert(GDALDataType, T),
pxspace,
linespace,
extraargs,
)
@cplerr result "Access in RasterIO failed."
return buffer
end
function read!(
rb::AbstractRasterBand,
buffer::T,
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, GF_Read)
return buffer
end
function read!(
rb::AbstractRasterBand,
buffer::T,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, xoffset, yoffset, xsize, ysize)
return buffer
end
function read!(
rb::AbstractRasterBand,
buffer::T,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, rows, cols)
return buffer
end
function read(rb::AbstractRasterBand)
return rasterio!(rb, Array{pixeltype(rb)}(undef, width(rb), height(rb)))
end
function read(
rb::AbstractRasterBand,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::Matrix{pixeltype(rb)}
buffer = Matrix{pixeltype(rb)}(undef, xsize, ysize)
rasterio!(rb, buffer, xoffset, yoffset, xsize, ysize)
return buffer
end
function read(
rb::AbstractRasterBand,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::Matrix{pixeltype(rb)}
buffer = Matrix{pixeltype(rb)}(undef, length(cols), length(rows))
rasterio!(rb, buffer, rows, cols)
return buffer
end
function write!(
rb::AbstractRasterBand,
buffer::T,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, xoffset, yoffset, xsize, ysize, GF_Write)
return buffer
end
function write!(
rb::AbstractRasterBand,
buffer::T,
rows::UnitRange{<:Integer} = 1:height(rb),
cols::UnitRange{<:Integer} = 1:width(rb),
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, rows, cols, GF_Write)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
i::Integer,
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
indices,
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, GF_Read)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
)::T where {T<:AbstractArray{<:Any,3}}
nband = nraster(dataset)
@assert size(buffer, 3) == nband
rasterio!(dataset, buffer, collect(Cint, 1:nband), GF_Read)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
i::Integer,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer, xoffset, yoffset, xsize, ysize)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
indices,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, xoffset, yoffset, xsize, ysize)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
i::Integer,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer, rows, cols)
return buffer
end
function read!(
dataset::AbstractDataset,
buffer::T,
indices,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, rows, cols)
return buffer
end
read(dataset::AbstractDataset, i::Integer) = read(getband(dataset, i))
function read(dataset::AbstractDataset, indices)::Array{pixeltype(dataset),3}
buffer = Array{pixeltype(dataset)}(
undef,
width(dataset),
height(dataset),
length(indices),
)
rasterio!(dataset, buffer, indices)
return buffer
end
function read(dataset::AbstractDataset)::Array{pixeltype(dataset),3}
buffer = Array{pixeltype(dataset)}(
undef,
width(dataset),
height(dataset),
nraster(dataset),
)
read!(dataset, buffer)
return buffer
end
function read(
dataset::AbstractDataset,
i::Integer,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::Matrix{pixeltype(dataset)}
band = getband(dataset, i)
buffer = Matrix{pixeltype(band)}(undef, xsize, ysize)
read!(dataset, buffer, i, xoffset, yoffset, xsize, ysize)
return buffer
end
function read(
dataset::AbstractDataset,
indices,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::Array{pixeltype(dataset),3}
buffer = Array{pixeltype(dataset)}(undef, xsize, ysize, length(indices))
rasterio!(dataset, buffer, indices, xsize, ysize, xoffset, yoffset)
return buffer
end
function read(
dataset::AbstractDataset,
i::Integer,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::Matrix{pixeltype(dataset)}
buffer = read(getband(dataset, i), rows, cols)
return buffer
end
function read(
dataset::AbstractDataset,
indices,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::Array{pixeltype(dataset),3}
buffer = Array{pixeltype(dataset),3}(
undef,
length(cols),
length(rows),
length(indices),
)
rasterio!(dataset, buffer, indices, rows, cols)
return buffer
end
function write!(
dataset::T,
buffer::Matrix{<:Any},
i::Integer,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractDataset}
write!(getband(dataset, i), buffer, xoffset, yoffset, xsize, ysize)
return dataset
end
function write!(
dataset::T,
buffer::Array{<:Any,3},
indices,
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:AbstractDataset}
rasterio!(
dataset,
buffer,
indices,
xoffset,
yoffset,
xsize,
ysize,
GF_Write,
)
return dataset
end
function write!(
dataset::T,
buffer::Matrix{<:Any},
i::Integer,
rows::UnitRange{<:Integer} = 1:height(getband(dataset, i)),
cols::UnitRange{<:Integer} = 1:width(getband(dataset, i)),
)::T where {T<:AbstractDataset}
write!(getband(dataset, i), buffer, rows, cols)
return dataset
end
function write!(
dataset::T,
buffer::Array{<:Any,3},
indices,
rows::UnitRange{<:Integer} = 1:height(dataset),
cols::UnitRange{<:Integer} = 1:width(dataset),
)::T where {T<:AbstractDataset}
rasterio!(dataset, buffer, indices, rows, cols, GF_Write)
return dataset
end
"""
readblock!(rb::AbstractRasterBand, xoffset::Integer, yoffset::Integer,
buffer)
Read a block of image data efficiently.
This method accesses a "natural" block from the raster band without resampling,
or data type conversion. For a more generalized, but potentially less efficient
access use RasterIO().
### Parameters
* `xoffset` the horizontal block offset, with zero indicating the left most
block, 1 the next block and so forth.
* `yoffset` the vertical block offset, with zero indicating the top most block,
1 the next block and so forth.
* `buffer` the buffer into which the data will be read. The buffer must be
large enough to hold GetBlockXSize()*GetBlockYSize() words of type
GetRasterDataType().
"""
function readblock!(
rb::AbstractRasterBand,
xoffset::Integer,
yoffset::Integer,
buffer::T,
)::T where {T<:Any}
result = GDAL.gdalreadblock(rb, xoffset, yoffset, buffer)
@cplerr result "Failed to read block at ($xoffset,$yoffset)"
return buffer
end
"""
writeblock!(rb::AbstractRasterBand, xoffset::Integer, yoffset::Integer,
buffer)
Write a block of image data efficiently.
This method accesses a "natural" block from the raster band without resampling,
or data type conversion. For a more generalized, but potentially less efficient
access use RasterIO().
### Parameters
* `xoffset` the horizontal block offset, with zero indicating the left most
block, 1 the next block and so forth.
* `yoffset` the vertical block offset, with zero indicating the left most block,
1 the next block and so forth.
* `buffer` the buffer from which the data will be written. The buffer must be
large enough to hold GetBlockXSize()*GetBlockYSize() words of type
GetRasterDataType().
"""
function writeblock!(
rb::T,
xoffset::Integer,
yoffset::Integer,
buffer,
)::T where {T<:AbstractRasterBand}
result = GDAL.gdalwriteblock(rb, xoffset, yoffset, buffer)
@cplerr result "Failed to write block at ($xoffset, $yoffset)"
return rb
end