sf_dissolve: Dissolve polygons

View source: R/sf_dissolve.R

sf_dissolveR Documentation

Dissolve polygons

Description

Dissolve polygon feature calss

Usage

sf_dissolve(x, y = NULL, overlaps = FALSE)

Arguments

x

An sf POLYGON or MULTIPOLYGON object

y

An attribute in x to dissolve by, default is NULL

overlaps

(FALSE/TRUE) Dissolve overlapping polygons, negates using attribute

Details

If a dissolve attribute is defined, the result will be a MULTIPOLYGON with the grouping attribute column. If y=NULL all polygons will be dissolved into a single attribute, unless there is spatial discontinuity (eg., gaps) in the data. The intent of overlaps=TRUE is to provide functionality for dissolving overlapping polygons and should only be used in this specialized case.

Value

A dissolved POLYGON or MULTIPOLYGON object

Author(s)

Jeffrey S. Evans <jeffrey_evans@tnc.org>

Examples

library(sf)
nc <- st_read(system.file("shape/nc.shp", package="sf"))
  nc$group <- ifelse(nc$CNTY_ <= 1902, 1, 
                ifelse(nc$CNTY_ > 1902 & nc$CNTY_ <= 1982, 2, 
				     ifelse(nc$CNTY_ > 1982, 3, NA))) 

# Dissolve by group attribute
d <- sf_dissolve(nc, "group")
  plot(st_geometry(nc), border="grey")
    plot(st_geometry(d), border="red", col=NA, 
         lwd=2, add=TRUE) 

# Dissolve all polygons
d <- sf_dissolve(nc)
  plot(st_geometry(nc), border="grey")
    plot(st_geometry(d), border="red", col=NA, 
         lwd=2, add=TRUE)

# Dissolve overlapping polygons
sq <- function(pt, sz = 1) st_polygon(list(rbind(c(pt - sz), 
     c(pt[1] + sz, pt[2] - sz), c(pt + sz), c(pt[1] - sz, pt[2] + sz), 
	 c(pt - sz))))
pol <- st_sf(box = 1:6, st_sfc(sq(c(4.2,4.2)), sq(c(0,0)), sq(c(1, -0.8)), 
          sq(c(0.5, 1.7)), sq(c(3,3)), sq(c(-3, -3))))
 st_geometry(pol) <- "geometry" 		  

plot(pol)

d <- sf_dissolve(pol, overlaps=TRUE)
  plot(d["diss"])


spatialEco documentation built on Nov. 18, 2023, 1:13 a.m.