From 3cfcd27b8bf5b7385c4ba0a4b934a34c91e9627f Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Sun, 26 May 2024 10:35:37 +0200 Subject: [PATCH 1/3] Release files directly after reading. --- src/io.jl | 5 ++++- test/test_io.jl | 3 +++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/io.jl b/src/io.jl index a01d77a..ad2dbfc 100644 --- a/src/io.jl +++ b/src/io.jl @@ -14,7 +14,10 @@ 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) diff --git a/test/test_io.jl b/test/test_io.jl index 068d451..6172a88 100644 --- a/test/test_io.jl +++ b/test/test_io.jl @@ -105,6 +105,7 @@ end fn = joinpath(tempdir(), "test_complex_$T.tif") GeoArrays.write(fn, ga) ga2 = GeoArrays.read(fn) + rm(fn) @test ga2 == ga end end @@ -113,6 +114,7 @@ end 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"]) GeoArrays.read(fn) + rm(fn) end @testset "Metadata" begin ga = GeoArray(rand(100, 200, 3)) @@ -120,6 +122,7 @@ end ga.metadata = d fn = GeoArrays.write(joinpath(tempdir(), "test_metadata.tif"), ga) ga2 = GeoArrays.read(fn) + rm(fn) GeoArrays.metadata(ga2) == d end end From 5299619490599d00da8ac82c63867a51bbc43a01 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Tue, 28 May 2024 11:14:25 +0200 Subject: [PATCH 2/3] More destroys. --- src/io.jl | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/io.jl b/src/io.jl index ad2dbfc..db42a08 100644 --- a/src/io.jl +++ b/src/io.jl @@ -22,7 +22,9 @@ 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) @@ -125,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 From 0ff4261bc5b35b372fc33a7466c570bbecae23f6 Mon Sep 17 00:00:00 2001 From: Maarten Pronk Date: Tue, 28 May 2024 11:27:57 +0200 Subject: [PATCH 3/3] Don't destroy COG. --- test/test_io.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_io.jl b/test/test_io.jl index 6172a88..1cc09d0 100644 --- a/test/test_io.jl +++ b/test/test_io.jl @@ -112,7 +112,7 @@ 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