Skip to content

Commit d4fac9c

Browse files
committed
do clipping
1 parent 1d350b1 commit d4fac9c

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

ext/ArchGDALExt/archgdaldataset.jl

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,30 @@ DiskArrays.haschunks(::GDALBand) = DiskArrays.Chunked()
2222
# end
2323
# end
2424
function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange...)
25-
# Open the dataset before reading
2625
AG.read(b.filename) do ds
27-
# Get the correct band from the dataset
2826
AG.getband(ds, b.band) do bh
27+
(xsize, ysize) = AG.width(bh), AG.height(bh)
2928
for chunk in eachchunk(b)
29+
# Compute overlap between the requested range and the current chunk range
3030
overlap = tuple((intersect(r[i], chunk[i]) for i in eachindex(r))...)
31+
# If there's any overlap, process the chunk
3132
if all(x -> !isempty(x), overlap)
32-
# Read the chunk data from the band `bh` for the overlapping ranges
33-
chunk_data = AG.read(bh, overlap...)
34-
aout_indices = tuple((overlap[i] .- first(r[i]) .+ 1 for i in eachindex(r))...)
33+
# Clip the overlap to the valid raster bounds
34+
overlap_clipped = (
35+
max(1, min(xsize, overlap[1])), # Clip x-range to raster width
36+
max(1, min(ysize, overlap[2])) # Clip y-range to raster height
37+
)
38+
# Read the chunk data from the band `bh` based on the clipped overlap
39+
chunk_data = AG.read(bh, overlap_clipped...)
40+
aout_indices = tuple((overlap_clipped[i] .- first(r[i]) .+ 1 for i in eachindex(r))...)
3541
view(aout, aout_indices...) .= chunk_data
3642
end
3743
end
3844
end
3945
end
4046
end
4147

48+
4249
function DiskArrays.readblock!(b::GDALBand, aout, r::AbstractUnitRange...)
4350
aout2 = similar(aout)
4451
DiskArrays.readblock!(b, aout2, r)

0 commit comments

Comments
 (0)