-1

I have a raster file containing temperature values and a list of coordinate points as centers of my desired buffers, colored in red like in the figure:

temperatures field and centroids colored in red

How can I extract buffers around the centroids containing all the neighbor cells matching the condition Tmax= Tcentroid+6ºC? Also I would like to overlap buffers and in case that they intersect, merge them into a single one.

ADM
  • 13
  • 4
  • Do you want to get the values within a particular buffer that match the condition (get all values in buffer, remove those that do not match and remove spatial duplicates); or should the condition determine the buffer size (is the condition the buffer; which would seem a bit more complicated)? Also, please set up an reproducible example. – Robert Hijmans Jun 22 '16 at 17:43
  • I want the values within the buffer that match the condition. In the image attached I try to exemplify a case where the cells within the buffer are those smaller or equal to 6. [1]: http://i.stack.imgur.com/71PAs.png – ADM Jun 23 '16 at 07:38

1 Answers1

0

Here is an approach to get that. There is probably a more efficient way to do this via functions in the gdistance package

Example data:

library(raster)
r <- raster(ncol=36, nrow=18)
r[] <- 1:ncell(r)
xy <- cbind(-55, seq(-25, 20, by=20))

Algorithm (note that I am using the default "rook" adjacency rule):

start <- cellFromXY(r, xy)
svalues <- extract(r, xy)

result <- list()
for (i in 1:nrow(xy)) {
    value <- svalues[i]
    cells <- start[i]
    allcells <- cells
    while(TRUE) {
        adj <- adjacent(r, cells, pairs=FALSE)
        asel <- which(abs(r[adj] - value) < 5)
        if (length(asel) == 0) break
        cells <- adj[asel]
        cells <- cells[!cells %in% allcells]
        allcells <- c(allcells, cells)
    }
    result[[i]] <- allcells
}

Inspect results:

p <- xyFromCell(r, unlist(result))   
plot(r)
points(xy, pch=20)
points(p, pch='+')
Robert Hijmans
  • 24,233
  • 3
  • 36
  • 43
  • Thank you for your answer, but I have realised that I didn't explain correctly what I need. As far as I understand from your answer, the buffer encompasses all the raster values and, from the center points, it looks for values within it matching the condition. What I really need is to start looking from the center those values that are adjacent and that match the condition. Once it is found the first value not matching the condition, I want to stop looking for adjacent in that direction. This would imply that the condition determines the buffer size. – ADM Jun 27 '16 at 09:13
  • I changed my answer to reflect that – Robert Hijmans Jun 28 '16 at 16:44