Skip to content
This repository has been archived by the owner on Dec 11, 2022. It is now read-only.

Commit

Permalink
🐛 fix geotiff writing with custom nodata field, closes #108
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldansereau committed Jun 4, 2021
1 parent 837dd07 commit d56f46b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
10 changes: 5 additions & 5 deletions src/datasets/geotiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ Write a single `layer` to a `file`, where the `nodata` field is set to an
arbitrary value.
"""
function geotiff(file::AbstractString, layer::SimpleSDMPredictor{T}; nodata::T=convert(T, -9999)) where {T <: Number}
array_t = _prepare_layer_for_burnin(layer)
array_t = _prepare_layer_for_burnin(layer, nodata)
width, height = size(array_t)

# Geotransform
Expand Down Expand Up @@ -138,9 +138,9 @@ function geotiff(file::AbstractString, layer::SimpleSDMPredictor{T}; nodata::T=c
return file
end

function _prepare_layer_for_burnin(layer::T) where {T <: SimpleSDMLayer}
function _prepare_layer_for_burnin(layer::SimpleSDMPredictor{T}, nodata::T) where {T <: Number}
@assert eltype(layer) <: Number
array = replace(layer.grid, nothing => NaN)
array = replace(layer.grid, nothing => nodata)
array = convert(Matrix{eltype(layer)}, array)
dtype = eltype(array)
array_t = reverse(permutedims(array, [2, 1]); dims=2)
Expand All @@ -156,7 +156,7 @@ Stores a series of `layers` in a `file`, where every layer in a band. See
function geotiff(file::AbstractString, layers::Vector{SimpleSDMPredictor{T}}; nodata::T=convert(T, -9999)) where {T <: Number}
bands = 1:length(layers)
_layers_are_compatible(layers)
width, height = size(_prepare_layer_for_burnin(layers[1]))
width, height = size(_prepare_layer_for_burnin(layers[1], nodata))

# Geotransform
gt = zeros(Float64, 6)
Expand All @@ -179,7 +179,7 @@ function geotiff(file::AbstractString, layers::Vector{SimpleSDMPredictor{T}}; no
band = ArchGDAL.getband(dataset, i)

# Write data to band
ArchGDAL.write!(band, _prepare_layer_for_burnin(layers[i]))
ArchGDAL.write!(band, _prepare_layer_for_burnin(layers[i], nodata))

# Write nodata and projection info
ArchGDAL.setnodatavalue!(band, nodata)
Expand Down
20 changes: 15 additions & 5 deletions test/dataread.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,21 @@ using SimpleSDMLayers
using Test

l = SimpleSDMPredictor(WorldClim, BioClim, 1)
f = tempname()
geotiff(f, l)
mp = geotiff(SimpleSDMResponse, f)

@test typeof(mp) <: SimpleSDMResponse
@test size(mp) == size(l)
f1 = tempname()
geotiff(f1, l)
mp1 = geotiff(SimpleSDMResponse, f1)

@test typeof(mp1) <: SimpleSDMResponse
@test size(mp1) == size(l)
@test mp1 == l

f2 = tempname()
geotiff(f2, l; nodata=-3.4f38)
mp2 = geotiff(SimpleSDMPredictor, f2)

@test typeof(mp2) <: SimpleSDMResponse
@test size(mp2) == size(l)
@test mp2 == l

end
2 changes: 1 addition & 1 deletion test/subsetting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ l1 = temp[coords]
l2 = SimpleSDMPredictor(WorldClim, BioClim, 1; coords...)
tempfile = tempname()
geotiff(tempfile, l2)
l3 = replace(geotiff(SimpleSDMPredictor, tempfile), NaN => nothing)
l3 = geotiff(SimpleSDMPredictor, tempfile)

@test size(l1) == size(l2)
@test size(l1) == size(l3)
Expand Down

0 comments on commit d56f46b

Please sign in to comment.