PredictiveEcology / SpaDES

R package for developing and running Spatial Discrete Event Simulation models
https://spades.predictiveecology.org
Other
55 stars 21 forks source link

use case for alternate returns from adj() and friends #352

Closed SteveCumming closed 6 years ago

SteveCumming commented 6 years ago

So there are circumstances, as in implementing spatial dependencies in State and Transition Models, where one wants to know the values in the cells adjacent to a focal cell, not the indices. When this is needed for every cell in a large raster, it might be more efficient to have adj() return the array or dt of values, rather that to return the indices, and then to have the user extract the values.

Parenthetically, is there no analogue of calc that works on indices rather than values?

eliotmcintire commented 6 years ago

Try raster function focal, which does this.

I do not know of a calc that works on indices.

SteveCumming commented 6 years ago

nope. focal does not meet the use case, because a vector-valued function is required.

eliotmcintire commented 6 years ago

I am not sure what you mean? Are you are referring to the argument, fun? i.e., do you want "all the values in the adjacent cells" rather than "some summary of all the values"?

Incidentally, so we are clear, is this a reproducible example for what you are trying to do? But you just want more efficient?

aa <- adj(rast, 1:ncell(rast));
rast[][aa[,"to"]]
SteveCumming commented 6 years ago

yes, I believe your reproducible example is what I meant. Maybe it's not what I need though. The raster is ratified. I need to count the frequency of each factor level in the neighbourhood of each cell, with the 0s in the right places. I'm having a little trouble, but it's not really a SpaDES problem.

eliotmcintire commented 6 years ago
rast <- raster::raster(extent(0,10,0,10), vals = sample(1:5, size = 100, replace = TRUE), res = 1);
aa <- adj(rast, 1:ncell(rast))
bb <- data.table::data.table(aa, value=rast[][aa[,"to"]], key = "from");
cc <- bb[,.N,by="from,value"]

cc gives you the frequency of each factor level, from each "from" cell