Skip to content

Commit

Permalink
Make buffer for read! an AbstractArray (#411)
Browse files Browse the repository at this point in the history
* Make buffer for `read!` an AbstractArray

* Add isa SubArray checks
  • Loading branch information
felixcremer authored Feb 2, 2024
1 parent 3bef4db commit 0f76a51
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 22 deletions.
44 changes: 22 additions & 22 deletions src/raster/rasterio.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@

"""
rasterio!(dataset::AbstractDataset, buffer::Array{<:Any, 3},
rasterio!(dataset::AbstractDataset, buffer::AbstractArray{<:Any, 3},
bands; <keyword arguments>)
rasterio!(dataset::AbstractDataset, buffer::Array{<:Any, 3}, bands, rows,
rasterio!(dataset::AbstractDataset, buffer::AbstractArray{<:Any, 3}, bands, rows,
cols; <keyword arguments>)
rasterio!(rasterband::AbstractRasterBand, buffer::Matrix{<:Any};
rasterio!(rasterband::AbstractRasterBand, buffer::AbstractMatrix{<:Any};
<keyword arguments>)
rasterio!(rasterband::AbstractRasterBand, buffer::Matrix{<:Any}, rows,
rasterio!(rasterband::AbstractRasterBand, buffer::AbstractMatrix{<:Any}, rows,
cols; <keyword arguments>)
Expand Down Expand Up @@ -73,7 +73,7 @@ function rasterio!(
pxspace::Integer = 0,
linespace::Integer = 0,
bandspace::Integer = 0,
)::T where {T<:Array{<:Any,3}}
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(
dataset,
buffer,
Expand All @@ -92,7 +92,7 @@ end

function rasterio!(
dataset::AbstractDataset,
buffer::Array{T,3},
buffer::AbstractArray{T,3},
bands,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
Expand Down Expand Up @@ -123,7 +123,7 @@ end

function rasterio!(
dataset::AbstractDataset,
buffer::Array{T,3},
buffer::AbstractArray{T,3},
bands,
xoffset::Integer,
yoffset::Integer,
Expand Down Expand Up @@ -174,7 +174,7 @@ function rasterio!(
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(
rasterband,
buffer,
Expand All @@ -197,7 +197,7 @@ function rasterio!(
access::GDALRWFlag = GF_Read,
pxspace::Integer = 0,
linespace::Integer = 0,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
xsize = length(cols)
xsize < 1 && error("invalid window width")
ysize = length(rows)
Expand All @@ -218,7 +218,7 @@ end

function rasterio!(
rasterband::AbstractRasterBand,
buffer::Matrix{T},
buffer::AbstractMatrix{T},
xoffset::Integer,
yoffset::Integer,
xsize::Integer,
Expand Down Expand Up @@ -255,7 +255,7 @@ function rasterio!(
return buffer
end

function read!(rb::AbstractRasterBand, buffer::T)::T where {T<:Matrix{<:Any}}
function read!(rb::AbstractRasterBand, buffer::T)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, GF_Read)
return buffer
end
Expand All @@ -267,7 +267,7 @@ function read!(
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, xoffset, yoffset, xsize, ysize)
return buffer
end
Expand All @@ -277,7 +277,7 @@ function read!(
buffer::T,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, rows, cols)
return buffer
end
Expand Down Expand Up @@ -315,7 +315,7 @@ function write!(
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, xoffset, yoffset, xsize, ysize, GF_Write)
return buffer
end
Expand All @@ -325,7 +325,7 @@ function write!(
buffer::T,
rows::UnitRange{<:Integer} = 1:height(rb),
cols::UnitRange{<:Integer} = 1:width(rb),
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
rasterio!(rb, buffer, rows, cols, GF_Write)
return buffer
end
Expand All @@ -334,7 +334,7 @@ function read!(
dataset::AbstractDataset,
buffer::T,
i::Integer,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer)
return buffer
end
Expand All @@ -343,12 +343,12 @@ function read!(
dataset::AbstractDataset,
buffer::T,
indices,
)::T where {T<:Array{<:Any,3}}
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, GF_Read)
return buffer
end

function read!(dataset::AbstractDataset, buffer::T)::T where {T<:Array{<:Any,3}}
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)
Expand All @@ -363,7 +363,7 @@ function read!(
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer, xoffset, yoffset, xsize, ysize)
return buffer
end
Expand All @@ -376,7 +376,7 @@ function read!(
yoffset::Integer,
xsize::Integer,
ysize::Integer,
)::T where {T<:Array{<:Any,3}}
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, xoffset, yoffset, xsize, ysize)
return buffer
end
Expand All @@ -387,7 +387,7 @@ function read!(
i::Integer,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:Matrix{<:Any}}
)::T where {T<:AbstractMatrix{<:Any}}
read!(getband(dataset, i), buffer, rows, cols)
return buffer
end
Expand All @@ -398,7 +398,7 @@ function read!(
indices,
rows::UnitRange{<:Integer},
cols::UnitRange{<:Integer},
)::T where {T<:Array{<:Any,3}}
)::T where {T<:AbstractArray{<:Any,3}}
rasterio!(dataset, buffer, indices, rows, cols)
return buffer
end
Expand Down
185 changes: 185 additions & 0 deletions test/test_rasterio.jl
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,191 @@ import ArchGDAL as AG
end
end


@testset "Non standard buffer rasterio " begin
AG.read("ospy/data4/aster.img") do ds
@testset "version 1" begin
band = AG.getband(ds, 1)
count = 0
total = 0
abuffer = Array{AG.pixeltype(band)}(undef, AG.blocksize(band)..., 1)
buffer = view(abuffer, :,:,:)
for (cols, rows) in AG.windows(band)
AG.rasterio!(ds, buffer, [1], rows, cols)
data = buffer[1:length(cols), 1:length(rows)]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 2" begin
band = AG.getband(ds, 1)
count = 0
total = 0
abuffer = Array{AG.pixeltype(band)}(undef, AG.blocksize(band)..., 1)
buffer = view(abuffer, :,:,:)

for (cols, rows) in AG.windows(band)
AG.read!(ds, buffer, [1], rows, cols)
data = buffer[1:length(cols), 1:length(rows)]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 3" begin
band = AG.getband(ds, 1)
count = 0
total = 0
abuffer = Matrix{AG.pixeltype(band)}(undef, AG.blocksize(band)...)
buffer = view(abuffer, :,:)

for (cols, rows) in AG.windows(band)
AG.read!(ds, buffer, 1, rows, cols)
data = buffer[1:length(cols), 1:length(rows)]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 4" begin
band = AG.getband(ds, 1)
count = 0
total = 0
abuffer = Matrix{AG.pixeltype(band)}(undef, AG.blocksize(band)...)
buffer = view(abuffer, :,:)

for (cols, rows) in AG.windows(band)
AG.read!(band, buffer, rows, cols)
data = buffer[1:length(cols), 1:length(rows)]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 5" begin
band = AG.getband(ds, 1)
count = 0
total = 0
xbsize, ybsize = AG.blocksize(band)
abuffer = Matrix{AG.pixeltype(band)}(undef, ybsize, xbsize)
buffer = view(abuffer, :,:)

for ((i, j), (nrows, ncols)) in AG.blocks(band)
# AG.rasterio!(ds,buffer,[1],i,j,nrows,ncols)
# AG.read!(band, buffer, j, i, ncols, nrows)
AG.readblock!(band, j, i, buffer)
data = buffer[1:nrows, 1:ncols]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 6" begin
band = AG.getband(ds, 1)
abuffer = Array{AG.pixeltype(band)}(undef, AG.width(ds), AG.height(ds), 1)
buffer = view(abuffer, :,:,:)

AG.rasterio!(ds, buffer, [1])
count = sum(buffer .> 0)
total = sum(buffer)
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 7" begin
band = AG.getband(ds, 1)
abuffer =
Matrix{AG.pixeltype(band)}(undef, AG.width(ds), AG.height(ds))
buffer = view(abuffer, :,:)

AG.read!(band, buffer)
count = sum(buffer .> 0)
total = sum(buffer)
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 8" begin
band = AG.getband(ds, 1)
abuffer =
Matrix{AG.pixeltype(band)}(undef, AG.width(ds), AG.height(ds))
buffer = view(abuffer, :,:)

AG.read!(ds, buffer, 1)
count = sum(buffer .> 0)
total = sum(buffer)
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 9" begin
band = AG.getband(ds, 1)
abuffer =
Array{AG.pixeltype(band)}(undef, AG.width(ds), AG.height(ds), 1)
buffer = view(abuffer, :,:,:)

AG.read!(ds, buffer, [1])
count = sum(buffer .> 0)
total = sum(buffer)
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

@testset "version 10" begin
band = AG.getband(ds, 1)
abuffer =
Array{AG.pixeltype(band)}(undef, AG.width(ds), AG.height(ds), 3)
buffer = view(abuffer, :,:, :)

AG.read!(ds, buffer)
count = sum(buffer[:, :, 1] .> 0)
total = sum(buffer[:, :, 1])
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end

# check for calling with Tuple
@testset "version 11" begin
band = AG.getband(ds, 1)
count = 0
total = 0
abuffer = Array{AG.pixeltype(band)}(undef, AG.blocksize(band)..., 1)
buffer = view(abuffer, :,:,:)

for (cols, rows) in AG.windows(band)
AG.rasterio!(ds, buffer, (1,), rows, cols)
data = buffer[1:length(cols), 1:length(rows)]
count += sum(data .> 0)
total += sum(data)
end
@test buffer isa SubArray
@test total / count 76.33891347095299
@test total / (AG.height(ds) * AG.width(ds)) 47.55674749653172
end
end
end

@testset "Complex IO" begin
a = rand(ComplexF32, 10, 10)

Expand Down

0 comments on commit 0f76a51

Please sign in to comment.