Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to subset a raster based on grid cell values

Tags:

r

cell

raster

My following question builds on the solution proposed by @jbaums on this post: Global Raster of geographic distances

For the purpose of reproducing the example, I have a raster dataset of distances to the nearest coastline:

library(rasterVis); library(raster); library(maptools)
data(wrld_simpl)

# Create a raster template for rasterizing the polys. 
r <- raster(xmn=-180, xmx=180, ymn=-90, ymx=90, res=1)
# Rasterize and set land pixels to NA
r2 <- rasterize(wrld_simpl, r, 1)
r3 <- mask(is.na(r2), r2, maskvalue=1, updatevalue=NA) 
# Calculate distance to nearest non-NA pixel
d <- distance(r3) # if claculating distances on land instead of ocean: d <- distance(r3)
# Optionally set non-land pixels to NA (otherwise values are "distance to non-land")
d <- d*r2 
levelplot(d/1000, margin=FALSE, at=seq(0, maxValue(d)/1000, length=100),colorkey=list(height=0.6), main='Distance to coast (km)') 

The data looks like this: output plot

From here, I need to subset the distance raster (d), or create a new raster, that only contains cells for which the distance to coastline is less than 200 km. I have tried using getValues() to identify the cells for which the value <= 200 (as show below), but so far without success. Can anyone help? Am I on the right track?

#vector of desired cell numbers
my.pts <- which(getValues(d) <= 200)
# create raster the same size as d filled with NAs
bar <- raster(ncols=ncol(d), nrows=nrow(d), res=res(d))
bar[] <- NA
# replace the values with those in d
bar[my.pts] <- d[my.pts]
like image 711
fabfab Avatar asked Aug 01 '17 02:08

fabfab


People also ask

What does raster () do in R?

We can use the raster() function to import one single band from a single or multi-band raster. We can view the number of bands in a raster using the nlayers() function. However, raster data can also be multi-band, meaning that one raster file contains data for more than one variable or time period for each cell.

How do I read raster data in R?

Raster files are most easily read in to R with the raster() function from the raster package. You simply pass in the filename (including the extension) of the raster as the first argument, x .

What is a raster object in R?

An object of class "raster" is a matrix of colour values as given by rgb representing a bitmap image. It is not expected that the user will need to call these functions directly; functions to render bitmap images in graphics packages will make use of the as.


1 Answers

I think this is what you are looking for, you can treat a raster like a matrix here right after you d <- d*r2 line:

d[d>=200000]<-NA
levelplot(d/1000, margin=FALSE, at=seq(0, maxValue(d)/1000, length=100),colorkey=list(height=0.6), main='Distance to coast (km)') 

(in case you forgot: the unit is in meters so the threshold should be 200000, not 200)

like image 128
Julian Wittische Avatar answered Nov 11 '22 05:11

Julian Wittische