Skip to content

Commit 827e37c

Browse files
committed
minimal working gdal writing
1 parent 20d594b commit 827e37c

File tree

1 file changed

+34
-15
lines changed

1 file changed

+34
-15
lines changed

ext/ArchGDALExt/archgdaldataset.jl

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ DiskArrays.haschunks(::GDALBand) = DiskArrays.Chunked()
1717
function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::AbstractUnitRange...)
1818
AG.read(b.filename) do ds
1919
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!
20+
DiskArrays.readblock!(bh, aout, r...) # ? what to do if size(aout) < r ranges ?, i.e. chunk reads! is a DiskArrays issue!
2121
end
2222
end
2323
end
@@ -27,7 +27,7 @@ function DiskArrays.readblock!(b::GDALBand, aout::Matrix, r::Tuple{AbstractUnitR
2727
end
2828

2929
function DiskArrays.writeblock!(b::GDALBand, ain, r::AbstractUnitRange...)
30-
AG.read(b.filename, flags=AG.OF_Update) do ds
30+
AG.read(b.filename, flags=AG.OF_UPDATE) do ds
3131
AG.getband(ds, b.band) do bh
3232
DiskArrays.writeblock!(bh, ain, r...)
3333
end
@@ -42,12 +42,12 @@ end
4242
struct GDALDataset
4343
filename::String
4444
bandsize::Tuple{Int,Int}
45-
projection::String
45+
projection::Union{String, AG.AbstractSpatialRef}
4646
trans::Vector{Float64}
4747
bands::OrderedDict{String}
4848
end
4949

50-
function GDALDataset(filename;mode="r")
50+
function GDALDataset(filename; mode="r")
5151
AG.read(filename) do r
5252
nb = AG.nraster(r)
5353
allbands = map(1:nb) do iband
@@ -121,17 +121,19 @@ function totransform(x, y)
121121
end
122122
totransform(x::AbstractRange, y::AbstractRange) =
123123
Float64[first(x), step(x), 0.0, first(y), 0.0, step(y)]
124+
124125
getproj(userproj::String, attrs) = AG.importPROJ4(userproj)
125126
getproj(userproj::AG.AbstractSpatialRef, attrs) = userproj
127+
126128
function getproj(::Nothing, attrs)
127-
if haskey(attrs, "projection_PROJ4")
129+
if haskey(attrs, "projection")
130+
return AG.importWKT(attrs["projection"])
131+
elseif haskey(attrs, "projection_PROJ4")
128132
return AG.importPROJ4(attrs["projection_PROJ4"])
129133
elseif haskey(attrs, "projection_WKT")
130134
return AG.importWKT(attrs["projection_WKT"])
131135
else
132-
error(
133-
"Could not determine output projection from attributes, please specify userproj",
134-
)
136+
error("Could not determine output projection from attributes, please specify userproj")
135137
end
136138
end
137139

@@ -150,16 +152,22 @@ function YAB.create_dataset(
150152
userproj = nothing,
151153
kwargs...,
152154
)
155+
# ? flip dimnames and dimvals
156+
dimnames = reverse(dimnames)
157+
dimvals = reverse(dimvals)
158+
153159
@assert length(dimnames) == 2
160+
merged_varattrs = merge(varattrs...)
161+
154162
proj, trans = if islon(dimnames[1]) && islat(dimnames[2])
155163
#Lets set the crs to EPSG:4326
156164
proj = AG.importEPSG(4326)
157165
trans = totransform(dimvals[1], dimvals[2])
158166
proj, trans
159-
# ? debug here, indices 1 and 2, the order.
160167
elseif isx(dimnames[1]) && isy(dimnames[2])
161-
#Try to find out srs
162-
proj = getproj(userproj, merge(gatts,varattrs...))
168+
# Try to find out crs
169+
all_attrs = merge(gatts, merged_varattrs)
170+
proj = getproj(userproj, all_attrs)
163171
trans = totransform(dimvals[1], dimvals[2])
164172
proj, trans
165173
else
@@ -168,7 +176,13 @@ function YAB.create_dataset(
168176
cs = first(varchunks)
169177
@assert all(isequal(varchunks[1]), varchunks)
170178

171-
driver = AG.getdriver(AG.extensiondriver(outpath))
179+
# driver = AG.getdriver(AG.extensiondriver(outpath)) # ? it looks like this driver (for .tif) is not working
180+
181+
if !endswith(lowercase(outpath), ".tif") && !endswith(lowercase(outpath), ".tiff")
182+
outpath = outpath * ".tif"
183+
end
184+
# Use this:
185+
driver = AG.getdriver("GTiff")
172186

173187
nbands = length(varnames)
174188
dtype = promote_type(vartypes...)
@@ -180,16 +194,21 @@ function YAB.create_dataset(
180194
height = length(dimvals[2]),
181195
nbands = nbands,
182196
dtype = dtype,
183-
options = ["BLOCKXSIZE=$(cs[1])", "BLOCKYSIZE=$(cs[2])"]
197+
options = [
198+
"BLOCKXSIZE=$(cs[1])",
199+
"BLOCKYSIZE=$(cs[2])",
200+
"TILED=YES",
201+
"COMPRESS=LZW"
202+
]
184203
) do ds
185204
AG.setgeotransform!(ds, trans)
186205
bands = map(1:length(varnames)) do i
187206
b = AG.getband(ds, i)
188207
icol = findfirst(isequal(varnames[i]), colornames)
189208
if isnothing(icol)
190-
AG.setcolorinterp!(b, AG.GDAL.GDALColorInterp(0))
209+
AG.setcolorinterp!(b, AG.GDALColorInterp(0))
191210
else
192-
AG.setcolorinterp!(b, AG.GDAL.GDALColorInterp(icol - 1))
211+
AG.setcolorinterp!(b, AG.GDALColorInterp(icol - 1))
193212
end
194213
AG.GDAL.gdalsetdescription(b.ptr, varnames[i])
195214
atts = varattrs[i]

0 commit comments

Comments
 (0)