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

Fix cropping when omitting a limit #18

Closed gabrieldansereau closed 4 years ago

gabrieldansereau commented 4 years ago

Cropping a layer while omitting to specify one of the limits did not work properly. For example:

julia> temp = worldclim(1);
julia> temp[left = -145.0, right = -50.0, top = 75.0, bottom = 20.0];
julia> temp[left = -145.0, right = -50.0, top = 75.0];
ERROR: MethodError: no method matching #getindex#2(::Float64, ::Float64, ::Float64, ::Nothing, ::typeof(getindex), ::SimpleSDMPredictor{Float64})
Closest candidates are:
  #getindex#2(::K, ::K, ::K, ::K, ::typeof(getindex), ::T) where {T<:SimpleSDMLayer, K<:Union{Nothing, AbstractFloat}} at /home/gdansereau/github/SimpleSDMLayers.jl/src/lib/overloads.jl:166
Stacktrace:
 [1] top-level scope at none:0

I fixed it with two tweaks to the getindex overload (the match_longitude calls and the types).

While at it, I also added an overload to crop based on a NamedTuple. I think it's useful as it allows to crop with a variable, which can be reused easily.

coords = (left = 145.0, right = -50.0, top = 75.0, bottom = 20.0)
temp[coords]

I've added tests for both of these.

tpoisot commented 4 years ago

I think this should simplify the declaration substantially

gabrieldansereau commented 4 years ago

Yes, that's great!