0

I have a raster(plot 1) for which I have to create a buffer zone using a polygon (Plot 2). The raster cell size is 10m and the polygon width (d), as indicated is constant and is 15m, so the buffered raster will need to include either 1 more adjacent cell (one side) or 2 adjacent cells (both sides) and should assign the same value (X) as the original cell for the new cell(s) as indicated. How can I do this in R? I thought I can do this by using raster::buffer and raster::extend, but I realised it's not a straightforward task.

I have to do this for many cases and sometimes the width of the polygon is not constant, so a more general solution would be great.

enter image description here

enter image description here

4
  • What's the polygon's role here? Do you have the polygon in R? Can you use st_buffer to create a buffered version of the polygon and then rasterize that?
    – Spacedman
    Commented Jun 9, 2021 at 12:09
  • The buffer zone is defined by the polygon boundary - the polygon is shown in light purple colour. In this particular case, the polygon width is constant (d = 15 m), so the buffer zone is always 15m. Each cell value is different, so I don't think I can do this by rasterising the polygon and assigning one cell value for the entire raster if that's what you mean?
    – rm167
    Commented Jun 9, 2021 at 15:13
  • I still don't quite get it. Let's try simplifying. If your raster was blank except for one row of pixels with values from 1 to 50 across the centre of the raster. Suppose your pixels are 10m across. Suppose your polygon is 10m wide on the left, and goes to 30m in the middle, and back to 10m on the right. What do you want as output? Is it a raster shape that goes from 10m to 30m to 10m (ie the same as the polygon) but with values taken from the matching position on the raster along the polygon's length?
    – Spacedman
    Commented Jun 10, 2021 at 6:46
  • Please see the accepted answer. Thanks
    – rm167
    Commented Jun 10, 2021 at 7:39

1 Answer 1

1

Your question is not very clear and you do not provide example data. My understanding is that you want to assign values to raster cells that are covered by a polygon, but only values that are currently NA, and that they should be similar to the nearby raster cell values. Your image suggests that you rasterized a line and the whole thing might be easier if you backtracked a bit --- but you provide insufficient information to comment. If my understanding of your goal is right, you could do something like this:

Example data

library(raster)
r <- raster(resolution=5)
values(r) <- 1:ncell(r)
cds <- rbind(c(-160,-20), c(-140,55), c(10, 0), c(140,60))
line <- spLines(cds)
r <- mask(r, line)
poly <- buffer(line, 10)

Solution: assign similar values to neighboring cells. The size of the window depends on the polygon used, but 5x5 works for the example. Then use mask to remove cells that are outside the polygon

rr <- focal(r, matrix(1, 5, 5), fun=mean, na.only=TRUE, na.rm=TRUE)
x <- mask(rr, poly)

Instead of with raster you can also do this with terra (it is faster)

library(terra)
r <- rast(resolution=5)
values(r) <- 1:ncell(r)
cds <- rbind(c(-160,-20), c(-140,55), c(10, 0), c(140,60))
line <- vect(cds, "lines", crs=crs(r))
r <- mask(r, line)
poly <- buffer(line, 1000000)

rr <- focal(r, matrix(1, 5, 5), fun=mean, na.only=TRUE, na.rm=TRUE)
x <- mask(rr, poly)
5
  • focal is exactly what I wanted. Apologise if the question was not clear and for not providing data. Your assumption is right and this is what I wanted to do.
    – rm167
    Commented Jun 10, 2021 at 7:38
  • Great, glad to be of help Commented Jun 10, 2021 at 18:37
  • Apologies for not raising this point sooner - but I realised you used "mean" inside focal which essentially takes the average of all surrounding cells. What I actually want is to assign the exact values orthogonally (unless there is an overlap between two orthogonals in that case average is fine). I thought I could do this using buffer, but it doesn't work. Any help is appreciated. Thanks
    – rm167
    Commented Jun 25, 2021 at 15:05
  • I think that would be worth a separate question: "how to fill NA values orthogonally to a line?" Commented Jun 25, 2021 at 19:29
  • I just posted a new question. I used the data provided by you. I hope that's okay with you.
    – rm167
    Commented Jun 29, 2021 at 14:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.