I have a Formal class SpatiaPolygonsDataFrame that has two polygons inside it, meaning that when I write length(x@polygons)
it gives me answer = 2.
How can I merge these two polygons inside the same Formal class SpatiaPolygonsDataFrame?
You can use aggregate()
from sp
package. dissolve=TRUE
argument will merge your polygons:
> shape
class : SpatialPolygonsDataFrame
features : 2
extent : -7.488767, -7.48873, -4.345411e-06, 1.463535e-05 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0
variables : 1
names : id
min values : 1
max values : 2
> length(shape@polygons)
[1] 2
> shape2 <- aggregate(shape,dissolve=T)
> length(shape2@polygons)
[1] 1
> plot(shape)