3
votes

Consider the following data:

library(sp)  
library(raster)

# create raster
r <- matrix(c(1.8, 1.2, 1.8, 1.2, 2.5, 2.7, 8.5, 7, 2), 3,3)

r <- raster(r)
extent(r) <- c(45,46,54,55)
projection(r) <- "+proj=utm +zone=33 +ellps=GRS80 +units=m +no_defs"

# create points
coords <- data.frame(x = c(45.6, 45.2),
                     y = c(54.8, 54.2))

data <- data.frame(a = c(20,22), b = c(1.5, 2.5))
p <- SpatialPointsDataFrame(coords = coords,
                           data = data, 
                           proj4string = crs(r))

plot(r)
plot(p, add=TRUE)

I have 2 points which are covering 2 raster cells. I want to replace these raster cell values by the values of a of the SpatialPointsDataFrame p. Therefore I transformed the SpatialPointsDataFrame into a raster:

p_ras <- rasterize(x = p, y = r, field = "a")

How can I update the values of r using those of p_ras, where p_ras has non-empty cell values and assign the values by location to r?

3

3 Answers

5
votes

Your example data

library(raster)
r <- raster(ncol=3, nrow=3, ext=extent(c(45,46,54,55)), crs = "+proj=utm +zone=33 +ellps=GRS80 +units=m")
values(r) <- c(1.8, 1.2, 1.8, 1.2, 2.5, 2.7, 8.5, 7, 2)
coords <- data.frame(x = c(45.6, 45.2), y = c(54.8, 54.2))
data <- data.frame(a = c(20,22), b = c(1.5, 2.5))
p <- SpatialPointsDataFrame(coords = coords, data = data, proj4string = crs(r))

You were on the right track with rasterize. Just add argument update=TRUE

x <- rasterize(p, r, field="a", update=TRUE)

That is equivalent to

p_ras <- rasterize(p, r, field = "a")
p_ras <- cover(p_ras, r)

And that should be clearer and more efficient than overlay. The approach with xyFromCell is risky for large objects as it may force all values into memory.

3
votes

Here's a solution that uses cellXY

############# START ORIGINAL CODE ############# 
library(sp)  
library(raster)
#> Warning: package 'raster' was built under R version 3.6.1

# create raster
r <- matrix(c(1.8, 1.2, 1.8, 1.2, 2.5, 2.7, 8.5, 7, 2), 3,3)

r <- raster(r)
extent(r) <- c(45,46,54,55)
projection(r) <- "+proj=utm +zone=33 +ellps=GRS80 +units=m +no_defs"

# create points
coords <- data.frame(x = c(45.6, 45.2),
                     y = c(54.8, 54.2))

data <- data.frame(a = c(1,2), b = c(1.5, 2.5))
p <- SpatialPointsDataFrame(coords = coords,
                            data = data, 
                            proj4string = crs(r))

############# END ORIGINAL CODE ############# 

What I do is look up the cells that correspond to the coordinates with cellXY and then replace the values with p$a values.

# Save original raster for comparison
r_before <- r

# Update cells
r[cellFromXY(r, p@coords)] <- p$a

# Plot difference
plot(r - r_before)
plot(p, add=TRUE)

Created on 2019-08-15 by the reprex package (v0.3.0)

The plot shows the difference between the original raster and the new updated raster. Only the relevant cells have been updated.

1
votes

I found the answer myself by working through the answer of this post: https://gis.stackexchange.com/questions/95481/in-r-set-na-cells-in-one-raster-where-another-raster-has-values

My solution was to use overlay from the raster package.

r_mod <- overlay(r, p_ras, fun = function(x, y){

  x[!is.na(y[])] <- y[!is.na(y[])]
  return(x)

})