# Creating a buffer for a line raster using a polygon boundary

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.  • 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? Jun 9 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?
– MaMu
Jun 9 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? Jun 10 at 6:46
– MaMu
Jun 10 at 7:39

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)
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)
``````

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))
poly <- buffer(line, 1000000)

rr <- focal(r, matrix(1, 5, 5), fun=mean, na.only=TRUE, na.rm=TRUE)
• `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.