Skip to content

Commit 1be89d6

Browse files
committed
Fix attributes for bands
1 parent 8c37aa6 commit 1be89d6

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

src/axisarrays/archgdal.jl

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ function dimvals(a::RasterDataset, i)
3030
end
3131
end
3232
iscontdim(a::RasterDataset, i) = i < 3 ? true : nraster(a)<8
33-
getattributes(a::RasterDataset) =
34-
Dict{String,Any}("projection"=>ArchGDAL.toPROJ4(ArchGDAL.newspatialref(ArchGDAL.getproj(a))))
33+
function getattributes(a::RasterDataset)
34+
globatts = Dict{String,Any}("projection"=>ArchGDAL.toPROJ4(ArchGDAL.newspatialref(ArchGDAL.getproj(a))))
35+
bands = (getbandattributes(ArchGDAL.getband(a, i)) for i in 1:size(a, 3))
36+
allbands = mergewith(bands...) do a1,a2
37+
isequal(a1,a2) ? a1 : missing
38+
end
39+
merge(globatts, allbands)
40+
end
3541

3642

3743
function dimname(::AbstractRasterBand, i)
@@ -54,5 +60,26 @@ function dimvals(b::AbstractRasterBand, i)
5460
end
5561
end
5662
iscontdim(a::AbstractRasterBand, i) = true
57-
getattributes(a::AbstractRasterBand) =
58-
getattributes(ArchGDAL.RasterDataset(ArchGDAL.getdataset(a)))
63+
function getattributes(a::AbstractRasterBand)
64+
atts = getattributes(ArchGDAL.RasterDataset(ArchGDAL.getdataset(a)))
65+
bandatts = getbandattributes(a)
66+
merge(atts, bandatts)
67+
end
68+
69+
function getbandattributes(a::AbstractRasterBand)
70+
atts = Dict{String,Any}()
71+
offs = ArchGDAL.getoffset(a)
72+
sf = ArchGDAL.getscale(a)
73+
missval = ArchGDAL.getnodatavalue(a)
74+
if !iszero(offs)
75+
atts["add_offset"] = offs
76+
end
77+
if sf != one(sf)
78+
atts["scale_factor"] = sf
79+
end
80+
T = eltype(a)
81+
if missval !== nothing
82+
atts["missing_value"] = missval
83+
end
84+
atts
85+
end

0 commit comments

Comments
 (0)