17

Dissolve is a common geoproccessing technique discussed as an sf approach here.

I'm trying to replicate dissolve as it functions in ArcGIS. Consider counties by two groups in ArcGIS.

The ArcGIS dissolve command yields two polygons, regardless of the fact that the eastern peninsula consists of additional separate polygons. Like so:

This is the functionality I'd like to replicate in sf, however I cannot as demonstrated 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
library(dplyr)#the summarize function is based on the one from dplyr (which may interfere with summarize from other libraries that may be loaded)
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))))

Notice the peninsula has multiple extraneous polygons.

How do I wind up with just two as in the ArcGIS case? Many thanks.

user3386170
  • 276
  • 4
  • 22
ReginaldMilton
  • 279
  • 1
  • 2
  • 8
  • is there anyway to only disolve boundaries if the polygons touch or overlap? I have over 36000 polygons in my shapefile, so trying to specify where to disolve on a case by case basis isn't an option. – tnt Jun 24 '22 at 18:32
  • 1
    `summarize` refers to the function from `dplyr` which conflicts with `summarize` from `plyr` – user3386170 Dec 12 '22 at 17:49

1 Answers1

6

I am not too familiar with how ArcGIS defines a polygon, but the simple feature access (an ISO standard) specification of a polygon is a single ring with zero or more inner rings denoting holes. This means that under that specification, if you have the main land + a couple of islands, you don't have a single polygon. To represent these as a single feature, the corresponding geometry type is multipolygon. Meaning your answer is in nc_dissolve: it has two features.

Edzer Pebesma
  • 3,814
  • 16
  • 26