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

Commit

Permalink
🐛 fix geotiff modifying source layer, closes #104
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrieldansereau committed Jun 2, 2021
1 parent 1624f1a commit 78a5284
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
9 changes: 2 additions & 7 deletions src/datasets/geotiff.jl
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +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 = layer.grid
replace!(array, nothing => NaN)
array = convert(Matrix{T}, array)
dtype = eltype(array)
array_t = reverse(permutedims(array, [2, 1]); dims=2)
array_t = _prepare_layer_for_burnin(layer)
width, height = size(array_t)

# Geotransform
Expand Down Expand Up @@ -144,8 +140,7 @@ end

function _prepare_layer_for_burnin(layer::T) where {T <: SimpleSDMLayer}
@assert eltype(layer) <: Number
array = layer.grid
replace!(array, nothing => NaN)
array = replace(layer.grid, nothing => NaN)
array = convert(Matrix{eltype(layer)}, array)
dtype = eltype(array)
array_t = reverse(permutedims(array, [2, 1]); dims=2)
Expand Down
7 changes: 6 additions & 1 deletion test/subsetting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,16 @@ temp = SimpleSDMPredictor(WorldClim, BioClim, 1)
coords = (left = -145.0, right = -50.0, bottom = 20.0, top = 75.0)
l1 = temp[coords]
l2 = SimpleSDMPredictor(WorldClim, BioClim, 1; coords...)
tempfile = tempname()
geotiff(tempfile, l2)
l3 = replace(geotiff(SimpleSDMPredictor, tempfile), NaN => nothing)

@test size(l1) == size(l2)
@test size(l1) == size(l3)
@test l1.grid == l2.grid
@test l1.grid == l3.grid

for l in (l1, l2)
for l in (l1, l2, l3)
@test size(l) == (330, 570)

@test l.left == coords.left
Expand Down

0 comments on commit 78a5284

Please sign in to comment.