wet<-readOGR(dsn=".",layer="shp") #wetlands
r.raster <- raster()
extent(r.raster) <- extent(wet)
res(r.raster) <- 100 
wet.r <- rasterize(wet,r.raster) #rasterize
b500<-buffer(wet.r,width=500) #buffer of the wetlands 500m
b1000<-buffer(wet.r,width=1000) #buffer of the wetlands 1000m
sum<-b1000 | b500 #aggregate them with "OR"
plot(sum)  #plot them

I have to change the values of the cells of these rasters, b500 and b1000. How to do that? Furthermore, how do i check the current values of a raster?

  • You need to start including a sample data set and description with your scripts to make the examples reproducible. R has many sample datasets to choose from, or you can simply create your own. For example, readers have no idea what what your b500 and b1000 variables are doing because there is no sample data to test on. – Aaron Jul 15 '15 at 16:03
  • how can i include it ?Can you also tell me a dataset (spatial)? – gsa Jul 15 '15 at 16:04
  • See the "rasterize polygon" example in ??rasterize from the raster package – Aaron Jul 15 '15 at 16:26
  • 2
    It would help considerably, with many of your questions, if you would read the R help. There is a reclassify function in the raster package that answers your question. Besides consistently not providing reproducible examples, your question(s) also tend to have a considerable amount of superfluous information making it difficult to figure out exactly what you are after. Please research your options and think through you question carefully. Commonly using examples in the help or vignettes for a package you can pull together an example that is reproducible. – Jeffrey Evans Jul 15 '15 at 17:06
  • Thanks, i will try to follow that for the future questions.About reclassify now i already read the r help but it wan't helpful for me that's why i give away my script, in order to help you provide some info based on my problem. – gsa Jul 15 '15 at 17:10

If I understand your question you would like specific values assigned to each resulting raster buffer. This can be done by simply assigning values via a index bracket assignment.

First, create a raster with a polygon to buffer.

p <- spPolygons(rbind(c(-180,-20), c(-140,55), c(10, 0), c(-140,-60), c(-180,-20)))
r <- raster(ncol=100, nrow=100)
r <- rasterize(p, r, fun=sum)

Then we can create buffers and assign values using a bracket assignment "[]". The "!is.na" within the bracket is a conditional statment that only evaluates values in the raster that are not NA. You can then just print the raster object to get some general information or use summary to display summary statistics on the raster values.

b1 <- buffer(r, width = 1000000)
  b1[!is.na(b1)] <- 100

b2 <- buffer(r, width = 2000000)
  b2[!is.na(b2)] <- 200

Here are the plotted results.

plot(b2, col="black", xlim=c(-200,100), ylim=c(-95,95), legend = FALSE)
  plot(b1, col = "green", add = TRUE, legend = FALSE)
  plot(r, col = "red", add = TRUE, legend = FALSE)
  legend("topright", legend=c("100","200","org"), 
         fill=c("green","black","red") )  

The trick to combining the buffers is to turn the background (NA's) to zero, so an overlay will not just return a single buffer, and then set the zero background back to NA. You can then use the "mask" function in raster to clip to additional features (eg., coastline).

b1[is.na(b1)] <- 0
b2[is.na(b2)] <- 0
r.sum <- overlay(b2,b1,fun=sum)
r.sum[r.sum == 0] <- NA 
plot(r.sum, xlim=c(-200,100), ylim=c(-95,95), legend = FALSE)
  • it says: Error in eval(expr, envir, enclos) : could not find function "spPolygons" – gsa Jul 15 '15 at 18:00
  • 1
    Do you have raster loaded? If so, it is likely that spPolygons is a new function and you need to update your version of raster. There are some complications with R 3.2.0 and I would recommend updating to 3.2.1 and then updating all of your packages. There are syntax changes in some of the internal calls and updating everything will save you a headache. I just spent an entire day trying to figure out why one of my packages was failing the CRAN CMD check just to find out that it was a change in R and updated packages were conflicting with previous versions of R. – Jeffrey Evans Jul 15 '15 at 18:11
  • how to combine those two buffers and give them values and use mask according to the coastline? – gsa Jul 16 '15 at 15:19
  • 2
    @gsa, To avoid the pain of another question, I went ahead and modified my answer to include combining the buffers. In the future please limit your post to one question. Here you have: 1) how do I assign raster values, 2) how do I check values in a raster and now, 3) how to I combine rasters and finally 4) how do I mask a raster. – Jeffrey Evans Jul 16 '15 at 15:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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