AMBarbosa / fuzzySim

Fuzzy Similarity in Species Distributions
http://fuzzysim.r-forge.r-project.org/
GNU General Public License v3.0
0 stars 0 forks source link

fuzzyOverlay for rasters #2

Open Nowosad opened 3 days ago

Nowosad commented 3 days ago

Hi @AMBarbosa -- thanks for adding the raster support for the package. I've tried it today, and all is looking good, except fuzzyOverlay() which gives a warning and returns a strange-looking raster:

# install.packages("fuzzySim")
library(fuzzySim)
library(terra)
clc2000_tartu = rast("https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2000_tartu.tif")
clc2018_tartu = rast("https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2018_tartu.tif")
clc2000_tartub = ifel(clc2000_tartu == 3, 1, 0)
clc2018_tartub = ifel(clc2018_tartu == 3, 1, 0)
plot(clc2000_tartub, main = "Tartu (2000)")

plot(clc2018_tartub, main = "Tartu (2018)")


# works
fuzSim(clc2000_tartub, clc2018_tartub, method = "Jaccard")
#> [1] 0.858819
simMat(c(clc2000_tartub, clc2018_tartub), method = "Jaccard")
#> Computing 1 pair-wise similarities...
#>   |                                                                              |                                                                      |   0%  |                                                                              |----------------------------------------------------------------------| 100%
#> 
#> Finished!
#> It took 0.1 secs.
#>          CLC_2000 CLC_2018
#> CLC_2000 1.000000 0.858819
#> CLC_2018 0.858819 1.000000
modOverlap(clc2000_tartub, clc2018_tartub)
#> $SchoenerD
#> [1] 0.9082571
#> 
#> $WarrenI
#> [1] 0.9241877
#> 
#> $HellingerDist
#> [1] 0.38939
fuzzyRangeChange(clc2000_tartub, clc2018_tartub)

#>                 Number Proportion
#> Gain              2134       0.06
#> Loss             -3401      -0.09
#> Stable positive  33670       0.91
#> Stable negative  87122       0.98
#> Balance          -1267      -0.03

# does not seem to work correctly
fo = fuzzyOverlay(c(clc2000_tartub, clc2018_tartub))
#> Warning in v[] <- value: number of items to replace is not a multiple of
#> replacement length
plot(fo)

AMBarbosa commented 2 days ago

Indeed, NAs were not being handled properly for raster inputs. This is because fuzzyOverlay was converting all inputs with as.data.frame (to accommodate also matrices, tibbles, sf objects, etc.), but for SpatRaster as.data.frame (by default) removes the NAs. So, the result had fewer values than input pixels (hence that warning message), and the output pixel values were all messed up.

This is fixed now in the development version on R-Forge. You can run install.packages("fuzzySim", repos="http://R-Forge.R-project.org") and try again.

Many thanks for reporting!

Nowosad commented 10 minutes ago
# pak::pak("ambarbosa/fuzzySim")
library(fuzzySim)
library(terra)
#> terra 1.7.83
clc2000_tartu = rast("https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2000_tartu.tif")
clc2018_tartu = rast("https://github.com/Nowosad/comparing-spatial-patterns-2024/raw/refs/heads/main/data/clc2018_tartu.tif")
clc2000_tartub = ifel(clc2000_tartu == 3, 1, 0)
clc2018_tartub = ifel(clc2018_tartu == 3, 1, 0)
plot(clc2000_tartub, main = "Tartu (2000)")

plot(clc2018_tartub, main = "Tartu (2018)")

fo = fuzzyOverlay(c(clc2000_tartub, clc2018_tartub))
plot(fo)

plot(rast(matrix(fo, ncol = 400)))

Hi @AMBarbosa -- thanks for working on this. I rerun the code with the new package version and now:

  1. The output is a numeric vector, not a raster as before (is this expected?)
  2. When I converted the output to a raster, it seems to be slightly rotated (or maybe this is just my perception)