The matrix ids returned from gIntersect should correspond to the rownames in each source sp object. You should be able to just index the rownames position in order to subset the data.
r <- c(1,5,3,9,10)
sp.polys <- sp.polys[r,]
There are several ways to get at all of the coordinates for a SpatialPolygonsDataFrame
or a SpatialLinesDataFrame
.
Each of these methods records the object and the part within the object from the source, albeit with subtle differences and different names.
Say p
is a SpatialPolygonsDataFrame
.
#1. coerce to points, then to data frame
# names are Lines.NR Lines.ID Line.NR coords.x1 coords.x2
## start here for polys
lin <- as(p, "SpatialLinesDataFrame")
## start here for lines
pts <- as.data.frame(as(lin, "SpatialPointsDataFrame"))
#2. convert to matrix
# names are object part cump hole x y
mpts <- raster::geom(p)
#3. convert to data frame
# names are long lat order hole piece id group
dpts <- ggplot2::fortify(p)
#4. convert to data frame
## names are object_ branch_ island_ order_ x_ y_
spts <- spbabel::sptable(p)
Each of these outputs can be used interchangeably as long as you deal with the different name conventions, and for raster::geom
remember that it's a matrix. (Actually the coercion one doesn't record the hole/island status so that is less useful depending on what's needed).
You can round trip the last one by
p2 <- spbabel::sp(spts, attr_tab = as.data.frame(p))
(There's an added bonus with spbabel
in that you get a 'raster-like' print for the object, but with the data in 'tibble' print form. )
Best Answer
You can use
aggregate()
fromsp
package.dissolve=TRUE
argument will merge your polygons: