Skip to content

Commit aaf2d23

Browse files
committed
clip range
1 parent d4fac9c commit aaf2d23

File tree

1 file changed

+12
-11
lines changed

1 file changed

+12
-11
lines changed

ext/ArchGDALExt/archgdaldataset.jl

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,15 @@ DiskArrays.haschunks(::GDALBand) = DiskArrays.Chunked()
2424
function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange...)
2525
AG.read(b.filename) do ds
2626
AG.getband(ds, b.band) do bh
27-
(xsize, ysize) = AG.width(bh), AG.height(bh)
27+
xsize, ysize = AG.width(bh), AG.height(bh)
2828
for chunk in eachchunk(b)
2929
# Compute overlap between the requested range and the current chunk range
30-
overlap = tuple((intersect(r[i], chunk[i]) for i in eachindex(r))...)
31-
# If there's any overlap, process the chunk
32-
if all(x -> !isempty(x), overlap)
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
30+
overlap = tuple(intersect(r[i], chunk[i]) for i in eachindex(r))
31+
overlap_clipped = (
32+
clip_overlap(overlap[1], xsize), # Clip x-range to raster width
33+
clip_overlap(overlap[2], ysize) # Clip y-range to raster height
34+
)
35+
if all(x -> !isempty(x), overlap_clipped)
3936
chunk_data = AG.read(bh, overlap_clipped...)
4037
aout_indices = tuple((overlap_clipped[i] .- first(r[i]) .+ 1 for i in eachindex(r))...)
4138
view(aout, aout_indices...) .= chunk_data
@@ -44,7 +41,11 @@ function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange..
4441
end
4542
end
4643
end
47-
44+
function clip_overlap(range, max_size)
45+
start = max(1, range.start)
46+
stop = min(max_size, range.stop)
47+
return start <= stop ? start : stop
48+
end
4849

4950
function DiskArrays.readblock!(b::GDALBand, aout, r::AbstractUnitRange...)
5051
aout2 = similar(aout)

0 commit comments

Comments
 (0)