Skip to content

Commit

Permalink
Release files directly after reading. (#160)
Browse files Browse the repository at this point in the history
* Release files directly after reading.

* More destroys.

* Don't destroy COG.
  • Loading branch information
evetion authored May 29, 2024
1 parent 73055bd commit 6c9cfba
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
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

0 comments on commit 6c9cfba

Please sign in to comment.