Created
June 26, 2013 19:36
-
-
Save mkoo/5870818 to your computer and use it in GitHub Desktop.
R code snippets of useful, slightly generic scripts used in the VertNet Biodiversity Informatics Training Workshop
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################# | |
###VertNet Biodiversity Informatics Training Workshop | |
###June 2013 | |
# | |
# Example of clipping rasters with a ForLoop | |
# | |
# MSK, 20130625 | |
################################################# | |
getwd() | |
setwd() | |
install.packages(c('dismo','raster','sp','maptools')); #if you have not already installed these packages | |
library(dismo) | |
library(raster) | |
library(sp) | |
library(maptools) | |
library(rgdal) | |
bc <- getData('worldclim', var = 'bio', res = 10) | |
#Or: | |
# set the working directory to the path that contains your .bil files | |
setwd() | |
# create a list of .bil files that exist in the wd | |
files <- list.files(pattern='\\.bil$') | |
# combine all list elements into a stack | |
s <- stack(files) | |
#read in a shape poly | |
# are your shapefiles in another directory than your grids? you can setwd() again or put in the full path | |
#How would you see other arguments for readShapePoly()? | |
Africa<-readShapePoly(file.choose()) | |
plot(Africa, add=T) | |
# crop to your study extent | |
s.crop <- crop(s, Africa) | |
# plot(s.crop[[1]]) | |
# looping through list | |
bioNum<-c(1:19) | |
for (i in 1:length(files)){ | |
writeRaster(s.crop, filename="bio.asc", bylayer=TRUE, overwrite=TRUE) | |
} | |
#Simple generic example, with lots of room for customization |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment