Repository navigation
Creating epmGrids from sp objects
If you are not yet familiar with the sf package, the epm package can also handle spatial objects of class SpatialPolygons from the sp package. The following code does the same as shown in the main demo but without using sf.
library(sp)
library(rgdal)
library(rgeos)
# filename and location of IUCN mammals shapefile
IUCNfile <- "MAMMALS_TERRESTRIAL_ONLY/MAMMALS_TERRESTRIAL_ONLY.shp"
mammals <- readOGR(IUCNfile)
Some class conversion to avoid issues from 'factor'
mammals@data$binomial <- as.character(mammals@data$binomial)
Here we identify all squirrel species
allsp <- unique(mammals@data$binomial)
squirrelSp <- grep("Spermophilus|Citellus|Tamias|Sciurus|Glaucomys|Marmota|Cynomys\\s", allsp, value = TRUE, ignore.case = TRUE)
head(squirrelSp)
The mammal ranges are currently all combined into a multipolygon object. We will now extract the squirrel species and store them as a list of separate species range polygons.
spList <- vector('list', length(squirrelSp))
names(spList) <- squirrelSp
for (i in 1:length(squirrelSp)) {
ind <- which(mammals$binomial == squirrelSp[i])
spList[[i]] <- mammals[ind,]
}
These range polygons are unprojected, in longitude/latitude. We would like to work with equal area grid cells, so we will transform these range polygons to an equal area projection. The North America Albers Equal Area projection works well for North America, so we will use the following coordinate system definition:
EAproj <- '+proj=aea +lat_1=20 +lat_2=60 +lat_0=40 +lon_0=-96 +x_0=0 +y_0=0 +ellps=GRS80 +datum=NAD83 +units=m +no_defs'
And we will now transform our range polygons.
spListEA <- lapply(spList, function(x) spTransform(x, CRS(EAproj)))
We will also make some changes to some of the taxonomy so that it matches with taxonomic and phylogenetic data later on.
names(spList) <- gsub('Neotamias', 'Tamias', names(spList))
names(spListEA) <- gsub('Neotamias', 'Tamias', names(spListEA))