I have a shapefile of range polygons. Each colored set of polygons represents a different group of species/genera/whatever.
I next overlay a 5x5 grid onto this map. And then use join attributes by location to find the number of grids that intersect each polygon
The final output gives me a column in the attribute table COUNT that indicates the number of grids intersected by the polygons.
EDIT: In other words, it gives me the site occupancy of each species.
################## NEXT PHASEI want to duplicate this same process entirely within R. First I load the same shapefile of range polygons into R using the rgdal package.
RangeShape<-readOGR(".","overflowExample07162015")
I then generate an analogous 5x5 grid in R using the following function and the raster package.
# Generate lat/lng grid for use with mammals dataset
generateGrid<-function(XMin=-180,XMax=180,YMin=-90,YMax=90,BinSize=5) {
LatitudeRows<-length(seq(YMin,YMax,BinSize))
LongitudeColumns<-length(seq(XMin,XMax,BinSize))
Grid<-raster(extent(matrix(c(XMin,YMin,XMax,YMax),nrow=2)),nrow=LatitudeRows,ncol=LongitudeColumns,crs="+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0")
Grid[]<-1:ncell(Grid)
Grid<-as(Grid,"SpatialPixelsDataFrame")
return(Grid)
}
# Create a GridShape object
GridShape<-generateGrid(XMin=-180,XMax=180,YMin=-90,YMax=90,BinSize=5)
Next I attempt to overlay them using the over() function in the sp package with the following function.
# Find Occupancy of taxa using shapefile
shapeOccupancy<-function(RangeShape,GridShape) {
Occupancies<-vector("numeric",length=nrow(RangeShape))
for (i in 1:nrow(RangeShape)) {
Intersect<-over(GridShape,RangeShape[i,])
Present<-apply(is.na(Intersect),1,all) # identify grids that do not intersect range polygons
Occupancies[i]<-nrow(Intersect[Present!=TRUE,])
}
return(Occupancies)
}
Occupancy<-shapeOccupancy(RangeShape,GridShape)
################## The problem
The counts given by my R process are not even close to the counts given by the QGIS process. I could understand slight differences, but these are quite large.
# Sample of differences
RResults QGISResult
Abrothrix 7 23
Alouatta 31 69
Aplodontia 1 6
Artibeus 47 88
Ateles 24 48
Avahi 1 7
Balantiopteryx 3 17
My best guess currently is that this is a result of the fact that some of the range polygons are discontinuous and R has a difficult time handling that, but I'm not sure. Based on a few manual spotchecks (i.e., manually counting grid and polygon intersections on the map), the QGIS results seem to be accurate and it is the R results that are wrong.
EDIT: A subset of the range polygons is included at the following github repository. https://github.com/aazaff/StackOverflowExamples/