Closed SophieSt closed 4 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
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)
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()
Session info ------------------------------------------------------------------------------------------------------------------------------
setting value
version R version 3.6.3 (2020-02-29)
os Windows 10 x64
system x86_64, mingw32
ui RStudio
language (EN)
collate English_United States.1252
ctype English_United States.1252
tz America/New_York
date 2020-06-24
Packages ----------------------------------------------------------------------------------------------------------------------------------
! package version date lib source
abind 1.4-5 2016-07-21 [1] CRAN (R 3.6.0)
adehabitatMA 0.3.13 2019-04-07 [1] CRAN (R 3.6.1)
aqp 1.18.1 2019-12-06 [1] CRAN (R 3.6.1)
assertthat 0.2.1 2019-03-21 [1] CRAN (R 3.6.1)
backports 1.1.5 2019-10-02 [1] CRAN (R 3.6.1)
bit 1.1-14 2018-05-29 [1] CRAN (R 3.6.0)
bit64 0.9-7 2017-05-08 [1] CRAN (R 3.6.0)
blob 1.2.0 2019-07-09 [1] CRAN (R 3.6.1)
boot 1.3-24 2019-12-20 [2] CRAN (R 3.6.3)
callr 3.4.3 2020-03-28 [1] CRAN (R 3.6.3)
class 7.3-15 2019-01-01 [2] CRAN (R 3.6.3)
classInt 0.4-2 2019-10-17 [1] CRAN (R 3.6.1)
cli 2.0.2 2020-02-28 [1] CRAN (R 3.6.3)
cluster 2.1.0 2019-06-19 [2] CRAN (R 3.6.3)
codetools 0.2-16 2018-12-24 [2] CRAN (R 3.6.3)
colorRamps 2.3 2012-10-29 [1] CRAN (R 3.6.0)
colorspace 1.4-1 2019-03-18 [1] CRAN (R 3.6.1)
crayon 1.3.4 2017-09-16 [1] CRAN (R 3.6.1)
curl 4.3 2019-12-02 [1] CRAN (R 3.6.3)
DBI 1.1.0 2019-12-15 [1] CRAN (R 3.6.1)
desc 1.2.0 2018-05-01 [1] CRAN (R 3.6.1)
devtools 2.2.2.9000 2020-03-30 [1] Github (r-lib/devtools@f77c14e)
digest 0.6.25 2020-02-23 [1] CRAN (R 3.6.3)
dismo 1.1-4 2017-01-09 [1] CRAN (R 3.6.1)
dplyr 1.0.0 2020-05-29 [1] CRAN (R 3.6.3)
e1071 1.7-3 2019-11-26 [1] CRAN (R 3.6.1)
ecodata 0.1.0 2020-06-17 [1] Github (NOAA-EDAB/ecodata@48a87c5)
ellipsis 0.3.0 2019-09-20 [1] CRAN (R 3.6.3)
fansi 0.4.1 2020-01-08 [1] CRAN (R 3.6.3)
FNN 1.1.3 2019-02-15 [1] CRAN (R 3.6.1)
foreign 0.8-75 2020-01-20 [2] CRAN (R 3.6.3)
fs 1.3.2 2020-03-05 [1] CRAN (R 3.6.3)
future 1.17.0 2020-04-18 [1] CRAN (R 3.6.3)
future.apply 1.5.0 2020-04-17 [1] CRAN (R 3.6.3)
generics 0.0.2 2018-11-29 [1] CRAN (R 3.6.1)
ggplot2 3.2.1 2019-08-10 [1] CRAN (R 3.6.1)
globals 0.12.5 2019-12-07 [1] CRAN (R 3.6.1)
glue 1.3.2 2020-03-12 [1] CRAN (R 3.6.3)
gstat 2.0-3 2019-09-26 [1] CRAN (R 3.6.1)
gtable 0.3.0 2019-03-25 [1] CRAN (R 3.6.1)
here 0.1 2017-05-28 [1] CRAN (R 3.6.1)
intervals 0.15.1 2015-08-27 [1] CRAN (R 3.6.0)
KernSmooth 2.23-16 2019-10-15 [2] CRAN (R 3.6.3)
labeling 0.3 2014-08-23 [1] CRAN (R 3.6.0)
lattice 0.20-38 2018-11-04 [2] CRAN (R 3.6.3)
lazyeval 0.2.2 2019-03-15 [1] CRAN (R 3.6.1)
lifecycle 0.2.0 2020-03-06 [1] CRAN (R 3.6.3)
listenv 0.8.0 2019-12-05 [1] CRAN (R 3.6.3)
lwgeom 0.1-7 2019-05-06 [1] CRAN (R 3.6.1)
magrittr 1.5 2014-11-22 [1] CRAN (R 3.6.1)
marmap 1.0.3 2019-07-03 [1] CRAN (R 3.6.1)
MASS 7.3-51.5 2019-12-20 [2] CRAN (R 3.6.3)
Matrix 1.2-18 2019-11-27 [2] CRAN (R 3.6.3)
memoise 1.1.0 2017-04-21 [1] CRAN (R 3.6.1)
moments 0.14 2015-01-05 [1] CRAN (R 3.6.0)
munsell 0.5.0 2018-06-12 [1] CRAN (R 3.6.1)
ncdf4 1.17 2019-10-23 [1] CRAN (R 3.6.1)
R neusvast 0.0.0.9000
pillar 1.4.3 2019-12-20 [1] CRAN (R 3.6.3)
pixmap 0.4-11 2011-07-19 [1] CRAN (R 3.6.0)
pkgbuild 1.0.6 2019-10-09 [1] CRAN (R 3.6.3)
pkgconfig 2.0.2 2018-08-16 [1] CRAN (R 3.6.1)
pkgload 1.0.2 2018-10-29 [1] CRAN (R 3.6.1)
plotKML 0.6-0 2019-11-12 [1] CRAN (R 3.6.1)
plotrix 3.7-7 2019-12-05 [1] CRAN (R 3.6.1)
plyr 1.8.4 2016-06-08 [1] CRAN (R 3.6.1)
prettyunits 1.1.1 2020-01-24 [1] CRAN (R 3.6.3)
processx 3.4.2 2020-02-09 [1] CRAN (R 3.6.3)
ps 1.3.2 2020-02-13 [1] CRAN (R 3.6.3)
purrr 0.3.3 2019-10-18 [1] CRAN (R 3.6.1)
R6 2.4.1 2019-11-12 [1] CRAN (R 3.6.3)
raster
RColorBrewer 1.1-2 2014-12-07 [1] CRAN (R 3.6.0)
Rcpp 1.0.4.6 2020-04-09 [1] CRAN (R 3.6.3)
remotes 2.1.1 2020-02-15 [1] CRAN (R 3.6.3)
reshape 0.8.8 2018-10-23 [1] CRAN (R 3.6.1)
reshape2 1.4.3 2017-12-11 [1] CRAN (R 3.6.1)
rgdal 1.4-8 2019-11-27 [1] CRAN (R 3.6.1)
rlang 0.4.6 2020-05-02 [1] CRAN (R 3.6.3)
rprojroot 1.3-2 2018-01-03 [1] CRAN (R 3.6.1)
RSAGA 1.3.0 2018-11-12 [1] CRAN (R 3.6.1)
RSQLite 2.1.5 2019-12-18 [1] CRAN (R 3.6.1)
rstudioapi 0.11 2020-02-07 [1] CRAN (R 3.6.3)
scales 1.0.0 2018-08-09 [1] CRAN (R 3.6.1)
sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 3.6.1)
sf 0.9-4 2020-06-13 [1] CRAN (R 3.6.3)
shape 1.4.4 2018-02-07 [1] CRAN (R 3.6.0)
shapefiles 0.7 2013-01-26 [1] CRAN (R 3.6.1)
sp 1.3-2 2019-11-07 [1] CRAN (R 3.6.1)
spacetime 1.2-2 2018-07-17 [1] CRAN (R 3.6.1)
spatialwarnings 2.99.99 2020-06-24 [1] Github (spatial-ews/spatialwarnings@aed7d03)
stars 0.4-1 2020-04-07 [1] CRAN (R 3.6.3)
stringi 1.4.6 2020-02-17 [1] CRAN (R 3.6.2)
stringr 1.4.0 2019-02-10 [1] CRAN (R 3.6.1)
testthat 2.3.2 2020-03-02 [1] CRAN (R 3.6.3)
tibble 3.0.0 2020-03-30 [1] CRAN (R 3.6.3)
tidyr 1.0.0 2019-09-11 [1] CRAN (R 3.6.1)
tidyselect 1.1.0 2020-05-11 [1] CRAN (R 3.6.3)
TMB 1.7.16 2020-01-15 [1] CRAN (R 3.6.3)
units 0.6-5 2019-10-08 [1] CRAN (R 3.6.1)
usethis 1.6.1 2020-04-29 [1] CRAN (R 3.6.3)
utf8 1.1.4 2018-05-24 [1] CRAN (R 3.6.1)
vctrs 0.3.1 2020-06-05 [1] CRAN (R 3.6.3)
viridisLite 0.3.0 2018-02-01 [1] CRAN (R 3.6.1)
withr 2.1.2 2018-03-15 [1] CRAN (R 3.6.1)
XML 3.98-1.20 2019-06-06 [1] CRAN (R 3.6.0)
xts 0.11-2 2018-11-05 [1] CRAN (R 3.6.1)
zoo 1.8-6 2019-05-28 [1] CRAN (R 3.6.1)
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 !
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