Skip to content

Instantly share code, notes, and snippets.

@arcaravaggi
Last active February 14, 2018 17:21
Show Gist options
  • Save arcaravaggi/bd59fa55dfcaf855c1f43fdab5655e28 to your computer and use it in GitHub Desktop.
Save arcaravaggi/bd59fa55dfcaf855c1f43fdab5655e28 to your computer and use it in GitHub Desktop.
Create species presence/absence raster
# Create species presence/absence raster
# mask.raster = raster of focal area
# species.data = presence points of focal species
# raster.label = label for raster (e.g. species name)
#
# example
# pa.ras <- presabRa(mask.raster = raster1, species.data = hares, raster.label = "Lepus sp.")
# See https://amywhiteheadresearch.wordpress.com/2016/01/25/extracting-raster-data-using-a-shapefile/#more-918 for more info]
presabRas <- function (mask.raster,species.data,raster.label="") {
require(raster)
# set the background cells in the raster to 0
mask.raster[!is.na(mask.raster)] <- 0
#set the cells that contain points to 1
speciesRaster <- rasterize(species.data,mask.raster,field=1)
speciesRaster <- merge(speciesRaster,mask.raster)
#label the raster
names(speciesRaster) <- raster.label
return(speciesRaster)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment