Skip to content

Commit

Permalink
bug(sdt): writing a raster works without tweaks now
Browse files Browse the repository at this point in the history
* bug(sdt): write with GTiff format

Closes #190

* test(sdt): add a test for write/read for geotiff

* test(sdt): update the tests for io

* semver(sdt): v0.0.7

* feat(sdt): remove prefixed file when writing layers
  • Loading branch information
tpoisot authored May 13, 2023
1 parent 54807d5 commit 385831f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "SpeciesDistributionToolkit"
uuid = "72b53823-5c0b-4575-ad0e-8e97227ad13b"
authors = ["Timothée Poisot <timothee.poisot@umontreal.ca>"]
version = "0.0.6"
version = "0.0.7"

[deps]
ArchGDAL = "c9ce4bd3-c3d5-55b8-8973-c0e20141b8c3"
Expand Down
21 changes: 11 additions & 10 deletions src/io/geotiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ function _read_geotiff(
driver::String = "GTiff",
compress::String = "LZW",
) where {LT <: SimpleSDMLayer}
@assert driver keys(ArchGDAL.listdrivers()) ||
throw(ArgumentError("Not a valid driver."))

@assert driver keys(ArchGDAL.listdrivers()) || throw(ArgumentError("Not a valid driver."))

try
ArchGDAL.read(file) do stuff
wkt = ArchGDAL.importPROJ4(ArchGDAL.getproj(stuff))
Expand Down Expand Up @@ -143,11 +143,11 @@ function _write_geotiff(
file::AbstractString,
layer::SimpleSDMPredictor{T};
nodata::T = convert(T, -9999),
driver::String = "COG",
compress="LZW"
driver::String = "GTiff",
compress = "LZW",
) where {T <: Number}

@assert driver keys(ArchGDAL.listdrivers()) || throw(ArgumentError("Not a valid driver."))
@assert driver keys(ArchGDAL.listdrivers()) ||
throw(ArgumentError("Not a valid driver."))
#@assert compress ∈ keys(ArchGDAL.listcompress()) || throw(ArgumentError("Not a valid compression."))

array_t = _prepare_layer_for_burnin(layer, nodata)
Expand Down Expand Up @@ -187,6 +187,7 @@ function _write_geotiff(
options = ["COMPRESS=$compress"],
)
end
isfile(prefix) && rm(prefix)
return file
end

Expand Down Expand Up @@ -214,11 +215,10 @@ function _write_geotiff(
layers::Vector{SimpleSDMPredictor{T}};
nodata::T = convert(T, -9999),
driver::String = "GTiff",
compress::String = "LZW"
compress::String = "LZW",
) where {T <: Number}


@assert driver keys(ArchGDAL.listdrivers()) || throw(ArgumentError("Not a valid driver."))
@assert driver keys(ArchGDAL.listdrivers()) ||
throw(ArgumentError("Not a valid driver."))
# to be uncommented once ths getcompressions fcn exists
#@assert compress ∈ keys(ArchGDAL.listcompress()) || throw(ArgumentError("Not a valid compression."))

Expand Down Expand Up @@ -262,6 +262,7 @@ function _write_geotiff(
options = ["COMPRESS=$compress"],
)
end
isfile(prefix) && rm(prefix)
return file
end

Expand Down
12 changes: 11 additions & 1 deletion test/01_integration_read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,14 @@ layer2 = SpeciesDistributionToolkit._read_geotiff(f, SimpleSDMPredictor)
@test typeof(layer2) <: SimpleSDMPredictor
@test all(values(layer) .== values(layer2))

end
# Write the data
f = tempname() * ".tiff" # This is important because the save function checks extensions
SpeciesDistributionToolkit.save(f, [layer]; nodata = typemax(D))
@test isfile(f)

# Read the data
layer2 = SimpleSDMPredictor(f)
@test typeof(layer2) <: SimpleSDMPredictor
@test all(values(layer) .== values(layer2))

end

2 comments on commit 385831f

@tpoisot
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/83531

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.0.7 -m "<description of version>" 385831f8b4c2c4fa4e4272874a95c664826a9562
git push origin v0.0.7

Please sign in to comment.