spatial-ews / spatialwarnings

An R package to compute spatial early-warning signals of ecosystem degradation
Other
14 stars 4 forks source link

calculate spatial warnings on raster dataset #82

Closed SophieSt closed 4 years ago

SophieSt commented 6 years ago

Hi!

Nice that you have just updated the package :-) Have you thought about raster datasets as input data type to the spatial warnings function? When working with remote sensing data, for instance, this would be super handy!

Cheers, Sophie

alexgenin commented 6 years ago

Hello, thanks for your interest in our work !

Yes, it would be really nice to have functions working directly on raster objects, it's actually a natural application of the package. It would require a bit of work to read raster attributes (resolution, etc.) and adapt the function outputs to them (e.g. having spectra displayed with proper units on the x-axis), but it's definitely doable.

Right now, most of the functions in spatialwarnings can be applied to raster objects by explicitly converting individual raster bands to matrices (using as.matrix), e.g:


library(raster)
#> Loading required package: sp
library(spatialwarnings)
#> This is spatialwarnings 1.2
#> Use options(mc.cores = <n>) to set up multi-core processing (unix only)

# It makes no sense computing indicators on those images, they just are
# example geotiffs
example_files <- c("https://download.osgeo.org/geotiff/samples/spot/chicago/SP27GTIF.TIF", 
  "https://download.osgeo.org/geotiff/samples/spot/chicago/UTM2GTIF.TIF")

list_of_raster_objects <- lapply(example_files, function(f) {
  tmpfile <- tempfile()
  download.file(f, destfile = tmpfile)
  raster(tmpfile)
})
list_of_matrices_objects <- lapply(list_of_raster_objects, as.matrix)

generic_sews(list_of_matrices_objects)
#> Warning in FUN(X[[i]], ...): Input matrix has continous values but will be
#> coarse-grained anyway. Set subsize=1 to disable coarse graining.

#> Warning in FUN(X[[i]], ...): Input matrix has continous values but will be
#> coarse-grained anyway. Set subsize=1 to disable coarse graining.
#> Generic Spatial Early-Warnings
#> 
#>  2 matrices (size: 929x699)
#> 
#>  Mat. # Mean Moran's I Skewness Variance
#>       1  115      0.64     0.68     1177
#>       2  115      0.63     0.68     1175
#> 
#> Use as.data.frame() to retrieve values in a convenient form

Of course, for now it means that indicators can only be applied on one band at a time (i.e. on RasterLayer objects or on one band of a Raster{Brick/Stack} object), and raster attributes are not used (extent/resolution, etc).

Cheers

alexgenin commented 6 years ago

This is now also answered in the FAQ

alexgenin commented 4 years ago

Good news! Supporting raster objects is now being implemented. More specifically, spatialwarnings will support RasterLayer objects transparently. Supporting multi-band objects would be require a lot more code and might come at a later stage.

In the development version, you can write the following code:

ex <- system.file("external/rlogo.grd", package = "raster")
ra <- raster::raster(ex)
display_matrix(ra)
generic_sews(ra)
slarge commented 4 years ago

For some reason, I am getting an error when I run your raster code:

devtools::install_github("spatial-ews/spatialwarnings")

library(spatialwarnings) ex <- system.file("external/rlogo.grd", package = "raster") ra <- raster::raster(ex) display_matrix(ra) generic_sews(ra)

Error in UseMethod("convert_to_matrix") : no applicable method for 'convert_to_matrix' applied to an object of class "c('RasterLayer', 'Raster', 'BasicRaster')"

devtools::session_info()

alexgenin commented 4 years ago

Hi,

Yes this message is outdated now, support for raster objects has moved to a companion package spatialwarningsGis, see also this entry in the doc. This has been done to reduce the amount of dependencies base spatialwarnings requires and simplify its installation.

Adapting your code to install and load spatialwarningsGis works for me::

devtools::install_github("spatial-ews/spatialwarnings")
devtools::install_github("spatial-ews/spatialwarningsGis")

library(spatialwarnings)
library(spatialwarningsGis)

ex <- system.file("external/rlogo.grd", package = "raster")
ra <- raster::raster(ex)
display_matrix(ra)
generic_sews(ra)

Let me know if that doesn't solve it for you !