11module ArchGDALExt
2- import ArchGDAL: RasterDataset, AbstractRasterBand,
3- getgeotransform, width, height, getname, getcolorinterp,
4- getband, nraster, getdataset
5- using ArchGDAL: ArchGDAL as AG
6- import YAXArrayBase: dimname, dimnames, dimvals, iscontdim, getattributes, getdata, yaxcreate
2+ import ArchGDAL: RasterDataset, AbstractRasterBand, getgeotransform, width, height
3+ import ArchGDAL: getname, getcolorinterp, getband, nraster, getdataset
4+ using ArchGDAL: ArchGDAL as AG
75
8- # include("archgdaldataset.jl")
6+ import ArchGDAL. DiskArrays: GridChunks, eachchunk
7+ import ArchGDAL. DiskArrays
8+
9+ import YAXArrayBase: dimname, dimnames, dimvals, iscontdim, getattributes, getdata, yaxcreate
10+ import YAXArrayBase: YAXArrayBase as YAB
11+ using DataStructures: OrderedDict
912
10- function dimname (a:: RasterDataset , i)
11- if i == 1
12- return :Y
13- elseif i == 2
14- return :X
15- elseif i == 3
16- return :Band
17- else
18- error (" RasterDataset only has 3 dimensions" )
13+ include (" archgdaldataset.jl" )
14+
15+ function dimname (a:: RasterDataset , i)
16+ if i == 1
17+ return :Y
18+ elseif i == 2
19+ return :X
20+ elseif i == 3
21+ return :Band
22+ else
23+ error (" RasterDataset only has 3 dimensions" )
24+ end
1925 end
20- end
21- function dimvals (a:: RasterDataset , i)
22- if i == 1
23- geo= getgeotransform (a)
24- latr = range (geo[1 ],length= width (a), step= geo[2 ])
25- elseif i == 2
26- geo= getgeotransform (a)
27- range (geo[4 ],length= height (a), step= geo[6 ])
28- elseif i == 3
29- colnames = map (ib -> getname (getcolorinterp (getband (a,ib))),1 : nraster (a))
30- if ! allunique (colnames)
31- colnames = string .(" Band_" ,1 : nraster (a))
26+ function dimvals (a:: RasterDataset , i)
27+ if i == 1
28+ geo= getgeotransform (a)
29+ latr = range (geo[1 ],length= width (a), step= geo[2 ])
30+ elseif i == 2
31+ geo= getgeotransform (a)
32+ range (geo[4 ],length= height (a), step= geo[6 ])
33+ elseif i == 3
34+ colnames = map (ib -> getname (getcolorinterp (getband (a,ib))),1 : nraster (a))
35+ if ! allunique (colnames)
36+ colnames = string .(" Band_" ,1 : nraster (a))
37+ end
38+ colnames
39+ else
40+ error (" RasterDataset only has 3 dimensions" )
3241 end
33- colnames
34- else
35- error (" RasterDataset only has 3 dimensions" )
3642 end
37- end
38- iscontdim (a:: RasterDataset , i) = i < 3 ? true : nraster (a)< 8
39- function getattributes (a:: RasterDataset )
40- globatts = Dict {String,Any} (
41- " projection_PROJ4" => AG. toPROJ4 (AG. newspatialref (AG. getproj (a))),
42- " projection_WKT" => AG. toWKT (AG. newspatialref (AG. getproj (a))),
43- )
44- bands = (getbandattributes (AG. getband (a, i)) for i in 1 : size (a, 3 ))
45- allbands = mergewith (bands... ) do a1,a2
46- isequal (a1,a2) ? a1 : missing
43+ iscontdim (a:: RasterDataset , i) = i < 3 ? true : nraster (a)< 8
44+ function getattributes (a:: RasterDataset )
45+ globatts = Dict {String,Any} (
46+ " projection_PROJ4" => AG. toPROJ4 (AG. newspatialref (AG. getproj (a))),
47+ " projection_WKT" => AG. toWKT (AG. newspatialref (AG. getproj (a))),
48+ )
49+ bands = (getbandattributes (AG. getband (a, i)) for i in 1 : size (a, 3 ))
50+ allbands = mergewith (bands... ) do a1,a2
51+ isequal (a1,a2) ? a1 : missing
52+ end
53+ merge (globatts, allbands)
4754 end
48- merge (globatts, allbands)
49- end
5055
5156
52- function dimname (:: AbstractRasterBand , i)
53- if i == 1
54- return :Y
55- elseif i == 2
56- return :X
57- else
58- error (" RasterDataset only has 3 dimensions" )
57+ function dimname (:: AbstractRasterBand , i)
58+ if i == 1
59+ return :Y
60+ elseif i == 2
61+ return :X
62+ else
63+ error (" RasterDataset only has 3 dimensions" )
64+ end
5965 end
60- end
61- function dimvals (b:: AbstractRasterBand , i)
62- geo = getgeotransform (getdataset (b))
63- if i == 1
64- range (geo[1 ],length= width (b), step= geo[2 ])
65- elseif i == 2
66- range (geo[4 ],length= height (b), step= geo[6 ])
67- else
68- error (" RasterDataset only has 3 dimensions" )
66+ function dimvals (b:: AbstractRasterBand , i)
67+ geo = getgeotransform (getdataset (b))
68+ if i == 1
69+ range (geo[1 ],length= width (b), step= geo[2 ])
70+ elseif i == 2
71+ range (geo[4 ],length= height (b), step= geo[6 ])
72+ else
73+ error (" RasterDataset only has 3 dimensions" )
74+ end
75+ end
76+ iscontdim (a:: AbstractRasterBand , i) = true
77+ function getattributes (a:: AbstractRasterBand )
78+ atts = getattributes (AG. RasterDataset (AG. getdataset (a)))
79+ bandatts = getbandattributes (a)
80+ merge (atts, bandatts)
6981 end
70- end
71- iscontdim (a:: AbstractRasterBand , i) = true
72- function getattributes (a:: AbstractRasterBand )
73- atts = getattributes (AG. RasterDataset (AG. getdataset (a)))
74- bandatts = getbandattributes (a)
75- merge (atts, bandatts)
76- end
7782
78- function insertattifnot! (attrs, val, name, condition)
79- if ! condition (val)
80- attrs[name] = val
83+ function insertattifnot! (attrs, val, name, condition)
84+ if ! condition (val)
85+ attrs[name] = val
86+ end
87+ end
88+ 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
8197 end
82- end
83- function getbandattributes (a:: AbstractRasterBand )
84- atts = Dict {String,Any} ()
85- catdict = Dict ((i- 1 )=> v for (i,v) in enumerate (AG. getcategorynames (a)))
86- insertattifnot! (atts, AG. getnodatavalue (a), " missing_value" , isnothing)
87- insertattifnot! (atts, catdict, " labels" , isempty)
88- insertattifnot! (atts, AG. getunittype (a), " units" , isempty)
89- insertattifnot! (atts, AG. getoffset (a), " add_offset" , iszero)
90- insertattifnot! (atts, AG. getscale (a), " scale_factor" , x-> isequal (x, one (x)))
91- atts
92- end
9398end
0 commit comments