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

Hcat/vcat return inversed bounding coordinates #102

Closed gabrieldansereau closed 3 years ago

gabrieldansereau commented 3 years ago

hcat and vcat return inversed bottom & top bounding coordinates. Because of this, the results of latitudes (and other functions which depend on it) may be wrong. Somehow, this created a circular error and the tests still passed, but it is clearly not the intended behaviour.

Example from the tests:

julia> l1 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=0.0, right=10.0, bottom=0.0, top=10.0);
julia> l2 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=0.0, right=10.0, bottom=10.0, top=20.0);
julia> l3 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=10.0, right=20.0, bottom=0.0, top=10.0);

julia> vl1 = vcat(l1, l2) # latitudes span makes no sense
SDM predictor → 122×61 grid with 5573 Float32-valued cells
  Latitudes     (9.834016393442623, 9.99931693989071)
  Longitudes    (-0.08333333333333333, 9.916666666666666)

julia> ml1 = hcat(l1, l3) # things look fine here, but are they?
SDM predictor → 61×122 grid with 5573 Float32-valued cells
  Latitudes     (-0.08333333333333333, 9.916666666666666)
  Longitudes    (-0.08401639344262293, 19.917349726775956)

julia> latitudes(vl1) # span really makes no sense
122-element LinRange{Float64}:
 9.99932,9.99795,9.99658,9.99522,9.99385,9.99249,…,9.83948,9.83811,9.83675,9.83538,9.83402

julia> vl1.bottom < vl1.top # false
false

julia> latitudes(ml1) # latitudes are reversed??
61-element LinRange{Float64}:
 9.91667,9.75,9.58333,9.41667,9.25,9.08333,8.91667,…,0.583333,0.416667,0.25,0.0833333,-0.0833333

julia> ml1.bottom < ml1.top # false
false
gabrieldansereau commented 3 years ago

While at it, I added an ArgumentError in 1624f1a to prevent using hcat and vcat on non-contiguous layers.

Previously, this worked:

# Base layer
l1 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=0.0, right=10.0, bottom=0.0, top=10.0)
# Non contiguous layers
l2 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=0.0, right=10.0, bottom=20.0, top=30.0)
l3 = SimpleSDMPredictor(WorldClim, BioClim, 1; left=20.0, right=30.0, bottom=0.0, top=10.0)

vcat(l2, l1) # works
hcat(l1, l3) # works

Now it will throw:

julia> vcat(l2, l1)
ERROR: LoadError: ArgumentError: The two layers passed to vcat must have contiguous bottom and top coordinates

julia> hcat(l1, l3)
ERROR: LoadError: ArgumentError: The two layers passed to hcat must have contiguous left and right coordinates