What I'd like to do is basically create a dummy variable called CBSA
with a 1 for CBSA counties (red in the map) and a 0 for those not.
I'm no spatial expert, but maybe you tweak something based on this:
library(rgeos)
# shrink the cbsa polygons a bit to play it safe:
cbsa_b <- gBuffer(cbsa, byid=TRUE, width=-0.05)
# get intersection matrix:
m <- gIntersects(county, cbsa_b, byid=TRUE)
# which county polygons intersect any cbsa?
in_cbsa <- colSums(m)>0
in_cbsa
is the dummy variable CBSA with TRUE
(or 1
if you make it in_cbsa+0L
) for CBSA counties:
head(in_cbsa)
# 0 1 2 3 4 5
# FALSE FALSE FALSE TRUE TRUE TRUE
sum(in_cbsa)
# [1] 1868
# plot to verify:
library(ggplot2)
county_f <- fortify(county)
cbsa_f <- fortify(cbsa)
ggplot() +
geom_polygon(aes(long,lat,group=group), county_f, fill=NA, color="black") +
coord_quickmap(xlim=c(-128,-75), ylim=c(25,55)) +
geom_polygon(aes(long,lat,group=group), cbsa_f, fill="red", color=NA) +
geom_polygon(aes(long,lat,group=group), county_f[county_f$id %in% names(which(!in_cbsa)), ], fill="blue", color=NA, alpha = .3)
Best Answer
To remove duplicates:
You can use the Delete duplicate geometries tool by accessing it via the Processing Toolbox:
Another option is to use the v.clean tool from GRASS and select the rmdupl option:
To remove overlaps:
You can use the Dissolve tool, provided there are common attributes between the original polygon and the overlapping polygon:
As always, you can manually remove them if there are only a handful. You can do this via the Attribute Table, find your overlapping polygons (useful in conjunction with a topology checker to highlight the overlaps) and select the option to delete features.