<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>
<div>
<div>
<div>Hi,</div>
<div>I wanted to count the number of polygons (home ranges of my species) inside a grid in order to estimate the number of species in a given area. I followed the example on this website, but it doesn't work properly: <a href="http://gis.stackexchange.com/questions/116388/count-overlapping-polygons-in-single-shape-file-with-r">http://gis.stackexchange.com/questions/116388/count-overlapping-polygons-in-single-shape-file-with-r</a></div>
<div> </div>
<div>Here is what I did:</div>
<div> </div>
<div>#Data</div>
<div>You can download a part of my data here: http://www.linkfile.de/download-136449c43d7939ea2de29accbc9cc981.php</div>
<div> </div>
<div>#Calculation of the species home range</div>
<div> </div>
<div>Fishsplit<-split(Data, f=Data$Species ) #split data</div>
<div>HaeVit<-Fishsplit[["Haemulon vittata"]]</div>
<div>HaeVit2 <- SpatialPoints(HaeVit[,2:3])</div>
<div>HaeVitKernel<-kernelUD(HaeVit2, kern ="epa") #home range estimation</div>
<div>HomeRange1 <- getverticeshr(HaeVitKernel, 95) #create Polygon</div>
<div> </div>
<div>Question1: I repeated the calculation for each individual species. I have in total around 600 species, so it will take a long time if I do it like this. The function <code>kernelUD</code> returns a list of elements of class <code>estUD</code> when the object <code>xy</code> passed as argument contains the relocations of several animals (i.e., belong to the class <code>SpatialPointsDataFrame</code>). But somehow I am unable to convert my species data to a <code>SpatialPointsDataFrame </code>which allows me to calculate several home ranges.</div>
<div> </div>
<div>Question2: I would like to define a barrier (boundary of the Ocean) that cannot be crossed by the animals. I know that there is an argument named boundary, but I don’t know how I can used it in order to add the ocean borders of the Caribbean sea (shapefile) as boundary.</div>
<div> </div>
<div># Load shapefile of the Caribbean Sea (you are able to download it at http://www.naturalearthdata.com/downloads/10m-physical-vectors/10m-ocean/</div>
<div> </div>
<div>World <- readOGR(dsn = "Z:/…/Oceans", layer = "LME66") </div>
<div>Caribbean <- World[World@data$OBJECTID %in% c("25"),]</div>
<div> </div>
<div>#create a grid</div>
<div> </div>
<div>bb <- bbox(Caribbean)</div>
<div>cs <- c(1, 1)*0.5 # cell size</div>
<div>cc <- bb[, 1] + (cs/2) # cell offset</div>
<div>cd <- ceiling(diff(t(bb))/cs) # number of cells per direction</div>
<div>grd <- GridTopology(cellcentre.offset=cc, cellsize=cs, cells.dim=cd)</div>
<div>grd</div>
<div> </div>
<div># find the count of polygons below each grid cell</div>
<div> </div>
<div>sp_grd <- SpatialGrid(grd)</div>
<div>proj4string(sp_grd) <- CRS("+proj=cea +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0") </div>
<div> </div>
<div>Polygons<-Homerange1+ Homerange 2+ Homerange 3+ Homerange 4</div>
<div> </div>
<div>Question3:I don’t know how I should “combine” the polygons. This is not working since I can not count the polygons (see result). I don’t know if it is even necessary if I calculate the home range for several species at once.</div>
<div> </div>
<div>PolygonsSP<- as(Polygons, "SpatialPolygons")</div>
<div>crs(sp_grd)<-crs(PolygonsSP)</div>
<div>o <- over(sp_grd, CombinedPolyTestSP, returnList=TRUE)</div>
<div>ct <- sapply(o, length)</div>
<div>summary(ct)</div>
<div>SGDF <- SpatialGridDataFrame(sp_grd, data=data.frame(ct=ct))</div>
<div> </div>
<div># plot</div>
<div> </div>
<div>spplot (SGDF, "ct", colorkey=TRUE,col.regions=colorRampPalette(c("white", "red"), bias=2),</div>
<div> panel = function(...) {</div>
<div> panel.gridplot(..., border="black")</div>
<div> sp.polygons(Caribbean)</div>
<div> sp.points(Data[,2:3], cex=1,pch = 20, col="black")</div>
<div> })</div>
<div> </div>
<div>Result: the plot shows, that only 1 Polygon was counted in each grid cell. So the 4 home range polygons were counted as one.</div>
<div> </div>
<div>I hope you can help me to fix this problem,</div>
<div>Best regards</div>
</div>
</div>
<div> </div>
</div></div></body></html>