Dissolve polygons with sf package

Dissolve a general geoproccessing technique is discussed as an NF approach here .

I am trying to replicate dissolution as it works in ArcGIS. Consider counties in two groups in ArcGIS.

The ArcGIS dissolve command produces two polygons, regardless of whether the eastern peninsula is made up of additional individual polygons. Like this:

This is the functionality I would like to reproduce in sf, however I am unable as shown below.

nc <- st_read(system.file("shape/nc.shp", package="sf"))

#create two homogenous spatial groups
nc$group <- ifelse(nc$CNTY_ <= 1980,1,2)

#plot
ggplot() + geom_sf(data=nc, aes(fill = factor(group)))  

#dissolve
nc_dissolve <- nc %>% group_by(group) %>% summarize() 

#plot dissolved
ggplot() + geom_sf(data=nc_dissolve, aes(fill = factor(group)))

#Cartographically, it looks like we have two polygons, but there are 
#actually several more wrapped up as MULTIPOLYGONS. We can plot these.
t <- nc_dissolve %>% st_cast() %>% st_cast("POLYGON")
ggplot() + geom_sf(data=t, aes(fill=factor(row.names(t))))

      

Note that there are several extraneous polygons on the peninsula.

How do I get just two, like with ArcGIS? Many thanks.

+7


source to share


1 answer


I'm not very familiar with how ArcGIS defines a polygon, but the ISO standard for a polygon is a single ring with zero or more inner rings representing holes. This means that according to this specification, if you have a main land + a pair of islands, you do not have a single polygon. To represent them as a single feature, the corresponding geometry type is a polygon. This means that your answer is in nc_dissolve

: it has two functions.



+4


source







All Articles