Yes, using a lapply
, e.g.:
x = c(0,1,1,0,0)
y = c(0,0,1,1,0)
bbox_matrix_sp = cbind(rep(x,13),rep(y,13))
require(sp)
sp_re_alle = SpatialPolygons(lapply(1:13, function(x) Polygons(list(Polygon(bbox_matrix_sp[((x-1)*5+1):(x*5),])), paste0("reh",x))))
in case you are converting grids into polygons, there are direct conversion methods in sp, as in
demo(meuse, ask = FALSE)
m.p = as(meuse.grid, "SpatialPolygonsDataFrame")
Thanks for the clear question and reproducible example.
Your understanding is correct, and this boils down to a bug in rgeos::over, which was fixed a month ago but has not made it into a CRAN release yet. The following is a work-around if you're only interested in the number of intersections:
world.map$val = sapply(over(geometry(world.map), pointbuff.spdf, returnList = TRUE), NROW)
I'm using NROW
here instead of length
so that it works with the wrong rgeos (0.3-8, from CRAN) as well as the corrected (0.3-10, from r-forge). The earlier suggestion of using
a = aggregate(pointbuff.spdf, world.map, sum)
also counts the number of intersections, but only with the fixed rgeos version installed. Its advantage, besides a more intuitive name, is that it directly returns a Spatial
object, with the geometry of world.map
.
To get rgeos 0.3-8 working, add
setMethod("over",
signature(x = "SpatialPolygons", y = "SpatialPolygonsDataFrame"),
rgeos:::overGeomGeomDF)
to your script, before you use over
.
Best Answer
To create an empty SpatialPolygons object use
SpatialPolygons(list())
credit to jebyrnes, but I thought it should be posted as an answer not a comment.