Home > Back-end >  How to dissolve separated polygons into a large one?
How to dissolve separated polygons into a large one?

Time:03-20

I have a shape file that I can read like this in R:

    library(rgdal)
    shape <- readOGR(dsn = "~/path", layer = "a")

I am interested in the whole region that cover all polygons (black curve here). How to dissolve all polygons even those separated into one polygon like this?

I am open to solutions from R or Qgis

CodePudding user response:

Using R & the sf package you can make a convex hull of the unioned (if necessary) shapefile. Since you haven't included data, I've used the nc data included with the sf package to illustrate the method.

library(dplyr)
library(sf)
library(ggplot2)

# setting up sample data,
# you'll need to use st_read() to read your shapefile, not readOGR()
nc <-  st_read(system.file("shape/nc.shp", package="sf"))
#> Reading layer `nc' from data source 
#>   `.../sf/shape/nc.shp' 
#>   using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> Geometry type: MULTIPOLYGON
#> Dimension:     XY
#> Bounding box:  xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> Geodetic CRS:  NAD27


nc <- nc[c(1:30, 85:81),] #Use some non-contiguous counties

# make a convex hull of the unioned geometries
nc_hull <- st_convex_hull(st_union(nc))

ggplot()   
  geom_sf(data = nc, fill = NA, color = 'red')  
  geom_sf(data = nc_hull, fill = NA, color = 'black')

Created on 2022-03-18 by the reprex package (v2.0.1)

  • Related