PoisotLab / SimpleSDMLayers.jl

Simple layers for species distribution modeling and bioclimatic data
https://docs.ecojulia.org/SimpleSDMLayers.jl/stable/
MIT License
19 stars 2 forks source link

Add a new method to `similar` #47

Closed tpoisot closed 3 years ago

tpoisot commented 3 years ago

Something to create a similar layer but with a different type (e.g. same as a worldclime layer but with Bool values)

similar(layer::ST, T) where {ST <: SimpleSDMLayer}

or something to that effect.

tpoisot commented 3 years ago

Something like this works:

function Base.similar(layer::LT, ::Type{T}) where {LT <: SimpleSDMLayer, T<:Any}
    newgrid = fill(nothing, size(layer))
    newgrid = convert(Matrix{Union{T,Nothing}}, newgrid)
    for i in eachindex(layer)
        if !isnothing(layer[i])
            newgrid[i] = zero(T)
        end
    end
    return SimpleSDMPredictor(newgrid, layer.left, layer.right, layer.bottom, layer.top)
end