Skip to content

Commit 20d594b

Browse files
committed
adds method
1 parent aaf2d23 commit 20d594b

File tree

2 files changed

+23
-54
lines changed

2 files changed

+23
-54
lines changed

ext/ArchGDALExt/ArchGDALExt.jl

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ module ArchGDALExt
4040
error("RasterDataset only has 3 dimensions")
4141
end
4242
end
43+
4344
iscontdim(a::RasterDataset, i) = i < 3 ? true : nraster(a)<8
4445
function getattributes(a::RasterDataset)
4546
globatts = Dict{String,Any}(
@@ -50,7 +51,7 @@ module ArchGDALExt
5051
allbands = mergewith(bands...) do a1,a2
5152
isequal(a1,a2) ? a1 : missing
5253
end
53-
merge(globatts, allbands)
54+
return merge(globatts, allbands)
5455
end
5556

5657

@@ -75,9 +76,9 @@ module ArchGDALExt
7576
end
7677
iscontdim(a::AbstractRasterBand, i) = true
7778
function getattributes(a::AbstractRasterBand)
78-
atts = getattributes(AG.RasterDataset(AG.getdataset(a)))
79-
bandatts = getbandattributes(a)
80-
merge(atts, bandatts)
79+
atts = getattributes(AG.RasterDataset(AG.getdataset(a)))
80+
bandatts = getbandattributes(a)
81+
return merge(atts, bandatts)
8182
end
8283

8384
function insertattifnot!(attrs, val, name, condition)
@@ -86,13 +87,13 @@ module ArchGDALExt
8687
end
8788
end
8889
function getbandattributes(a::AbstractRasterBand)
89-
atts = Dict{String,Any}()
90-
catdict = Dict((i-1)=>v for (i,v) in enumerate(AG.getcategorynames(a)))
91-
insertattifnot!(atts, AG.getnodatavalue(a), "missing_value", isnothing)
92-
insertattifnot!(atts, catdict, "labels", isempty)
93-
insertattifnot!(atts, AG.getunittype(a), "units", isempty)
94-
insertattifnot!(atts, AG.getoffset(a), "add_offset", iszero)
95-
insertattifnot!(atts, AG.getscale(a), "scale_factor", x->isequal(x, one(x)))
96-
atts
90+
atts = Dict{String,Any}()
91+
catdict = Dict((i-1)=>v for (i,v) in enumerate(AG.getcategorynames(a)))
92+
insertattifnot!(atts, AG.getnodatavalue(a), "missing_value", isnothing)
93+
insertattifnot!(atts, catdict, "labels", isempty)
94+
insertattifnot!(atts, AG.getunittype(a), "units", isempty)
95+
insertattifnot!(atts, AG.getoffset(a), "add_offset", iszero)
96+
insertattifnot!(atts, AG.getscale(a), "scale_factor", x->isequal(x, one(x)))
97+
return atts
9798
end
9899
end

ext/ArchGDALExt/archgdaldataset.jl

Lines changed: 10 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,15 @@ Base.size(b::GDALBand) = b.size
1414
DiskArrays.eachchunk(b::GDALBand) = b.cs
1515
DiskArrays.haschunks(::GDALBand) = DiskArrays.Chunked()
1616

17-
# function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange...)
18-
# AG.read(b.filename) do ds
19-
# AG.getband(ds, b.band) do bh
20-
# DiskArrays.readblock!(bh, aout, r...)
21-
# end
22-
# end
23-
# end
2417
function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange...)
25-
AG.read(b.filename) do ds
26-
AG.getband(ds, b.band) do bh
27-
xsize, ysize = AG.width(bh), AG.height(bh)
28-
for chunk in eachchunk(b)
29-
# 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-
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)
36-
chunk_data = AG.read(bh, overlap_clipped...)
37-
aout_indices = tuple((overlap_clipped[i] .- first(r[i]) .+ 1 for i in eachindex(r))...)
38-
view(aout, aout_indices...) .= chunk_data
39-
end
40-
end
18+
AG.read(b.filename) do ds
19+
AG.getband(ds, b.band) do bh
20+
DiskArrays.readblock!(bh, aout, r...) # ? what to do if size(aout) < r ranges ?, i.e. chunk reads!
4121
end
4222
end
43-
end
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
23+
end
4924

50-
function DiskArrays.readblock!(b::GDALBand, aout, r::AbstractUnitRange...)
51-
aout2 = similar(aout)
52-
DiskArrays.readblock!(b, aout2, r)
53-
aout .= aout2
54-
end
55-
function DiskArrays.readblock!(b::GDALBand, aout, r::Tuple{AbstractUnitRange, AbstractUnitRange})
25+
function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::Tuple{AbstractUnitRange, AbstractUnitRange})
5626
DiskArrays.readblock!(b, aout, r...)
5727
end
5828

@@ -63,14 +33,12 @@ function DiskArrays.writeblock!(b::GDALBand, ain, r::AbstractUnitRange...)
6333
end
6434
end
6535
end
66-
67-
function DiskArrays.writeblock!(b::GDALBand, ain, r::Tuple{AbstractUnitRange, AbstractUnitRange})
68-
AG.read(b.filename, flags=AG.OF_Update) do ds
69-
AG.getband(ds, b.band) do bh
70-
DiskArrays.writeblock!(bh, ain, r...)
71-
end
72-
end
36+
function DiskArrays.readblock!(b::GDALBand, aout, r::AbstractUnitRange...)
37+
aout2 = similar(aout)
38+
DiskArrays.readblock!(b, aout2, r)
39+
aout .= aout2
7340
end
41+
7442
struct GDALDataset
7543
filename::String
7644
bandsize::Tuple{Int,Int}

0 commit comments

Comments
 (0)