Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release files directly after reading. #160

Merged
merged 3 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ pre and postfixes to read NetCDF, HDF4 and HDF5.
"""
function read(fn::AbstractString; masked::Bool=true, band=nothing)
startswith(fn, "/vsi") || occursin(":", fn) || isfile(fn) || error("File not found.")
GeoArray(ArchGDAL.readraster(fn), masked, band)
ds = ArchGDAL.readraster(fn)
ga = GeoArray(ds, masked, band)
masked && ArchGDAL.destroy(ds)
return ga
end

function GeoArray(ds::ArchGDAL.Dataset)
dataset = ArchGDAL.RasterDataset(ds)
GeoArray(dataset)
ga = GeoArray(dataset)
ArchGDAL.destroy(ds)
return ga
end

function GeoArray(dataset::ArchGDAL.RasterDataset, masked=true, band=nothing)
Expand Down Expand Up @@ -122,6 +127,7 @@ function write(fn::AbstractString, ga::GeoArray; nodata::Union{Nothing,Number}=n
elseif cancopy
dataset = ArchGDAL.Dataset(ga::GeoArray, nodata, bandnames)
ArchGDAL.copy(dataset, filename=fn, driver=driver, options=stringlist(options))
ArchGDAL.destroy(dataset)
else
@error "Cannot create file with $shortname driver."
end
Expand Down
5 changes: 4 additions & 1 deletion test/test_io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,24 @@ end
fn = joinpath(tempdir(), "test_complex_$T.tif")
GeoArrays.write(fn, ga)
ga2 = GeoArrays.read(fn)
rm(fn)
@test ga2 == ga
end
end
@testset "Bandnames" begin
ga = GeoArray(rand(100, 200, 3))
ga.metadata = Dict("" => Dict("FOO" => "BAR"))
fn = GeoArrays.write(joinpath(tempdir(), "test_bandnames.tif"), ga; shortname="COG", nodata=1.0, options=Dict("compress" => "deflate"), bandnames=["a", "b", "c"])
fn = GeoArrays.write(joinpath(tempdir(), "test_bandnames.tif"), ga; nodata=1.0, options=Dict("compress" => "deflate"), bandnames=["a", "b", "c"])
GeoArrays.read(fn)
rm(fn)
end
@testset "Metadata" begin
ga = GeoArray(rand(100, 200, 3))
d = Dict("" => Dict("FOO" => "BAR"))
ga.metadata = d
fn = GeoArrays.write(joinpath(tempdir(), "test_metadata.tif"), ga)
ga2 = GeoArrays.read(fn)
rm(fn)
GeoArrays.metadata(ga2) == d
end
end
Loading