Create a raster with 1 (habitat) and 0 (non-habitat) for kernel estimation

51 Views Asked by At

I need to create a raster that I use as a grid for kernel estimation of seabird tracking data. The raster needs to hold values of "1" for habitat (points at sea), and "0" for non-habitat (points on land). This is because I do not want kernel density estimations to go across land.

How do I do this?

I have seen this thread # https://stat.ethz.ch/pipermail/r-sig-geo/2011-June/012093.htm but have not been able to apply this successfully.

Below is what I have so far:

# Make raster layer of study area
ras = raster(ext=extent(-70, -55, -60, -38), res=c(0.01,0.01)) 

#give all ponints a "1"
ras[] <- 1
#project the grid 
projection(ras) <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0" 

# load land
library(rworldmap)
worldMap <- getMap(resolution = "high")
projection(worldMap) <- CRS(proj4string(ras))

#crop and mask raster by land
r2 <- crop(ras, worldMap)
r3 <- mask(r2, worldMap, inverse = T)

# none of the following work
r3.sg <- as(r3, 'SpatialGridDataFrame')
fullgrid(r3) <- TRUE


Thanks for the assistance

1

There are 1 best solutions below

0
On

The mask just applies NA values where you want 1s. So you can do:

r3@data@values[is.na(r3@data@values)] <- 0
r3@data@values <- 1 - r3@data@values

This leaves a 0 over the land masses and a 1 over the sea, which we can show by plotting with 0 as green and 1 as blue:

plot(r3, col = c("#809020", "lightblue"))

enter image description here