Reputation: 138
I need to remove duplicate polygons from a SpatialPolygonsDataFrame in R. There is a method for points but not for polygons.
I need to do that for creating a tool to similar reshape() for spatial data.
Upvotes: 1
Views: 4165
Reputation: 47146
The answer would depend a bit on what you want to do with the attributes.
Example data:
library(raster)
p <- shapefile(system.file("external/lux.shp", package="raster"))
p <- p[1:2, ]
pp <- bind(p,p)
data.frame(pp)
# ID_1 NAME_1 ID_2 NAME_2 AREA
#1 1 Diekirch 1 Clervaux 312
#2 1 Diekirch 2 Diekirch 218
#3 1 Diekirch 1 Clervaux 312
#4 1 Diekirch 2 Diekirch 218
If you want to combine polygons that have the same attributes (but perhaps different geometries), you can use aggregate:
a <- aggregate(pp, names(pp))
data.frame(a)
# ID_1 NAME_1 ID_2 NAME_2 AREA
#1 1 Diekirch 1 Clervaux 312
#2 1 Diekirch 2 Diekirch 218
If you know they are duplicates in geometry and attributes, you can use @DJack's solution:
b <- pp[!duplicated(data.frame(pp)),]
If it is just the areas you care about, you could do
d <- union(pp)
And you could reconstitute the attributes with the info in
data.frame(d)
# ID.1 ID.2 ID.3 ID.4 count
#1 1 0 1 0 2
#2 0 1 0 1 2
But in that case, it would be simpler to do:
g <- geom(pp)
x <- split(g[,c(2, 4:6)], g[,1])
dd <- duplicated(x)
dd
[1] FALSE FALSE TRUE TRUE
Followed by
z <- pp[!dd, ]
Package terra
has a unique
method for SpatVector
objects:
library(terra)
v <- vect(pp)
u <- unique(pp)
u
# class : SpatVector
# geometry : polygons
# dimensions : 2, 5 (geometries, attributes)
# extent : 5.826232, 6.315773, 49.78479, 50.18162 (xmin, xmax, ymin, ymax)
# coord. ref. : +proj=longlat +datum=WGS84 +no_defs
# names : ID_1 NAME_1 ID_2 NAME_2 AREA
# type : <num> <chr> <num> <chr> <num>
# values : 1 Diekirch 1 Diekirch 312
# 1 Diekirch 2 Diekirch 218
Upvotes: 3
Reputation: 4121
Try using rgeos::gEquals(x, byid = TRUE)
with x
the object with the polygons.
Upvotes: 1
Reputation: 4940
If by duplicated polygons, you mean polygons with identical data, you can use this code:
shp <- shapefile("shape.shp")
shp <- shp[!duplicated(shp@data),]
EDIT
If by duplicated polygons, you mean polygons with identical coordinates (@coords
slot), you can use this code:
shp <- shapefile("shape.shp")
coord_poly <- lapply(shp@polygons, function(x){lapply(x@Polygons, function(x){coordinates(x)})})
shp <- shp[!duplicated(coord_poly),]
Upvotes: 5