sf_dissolve | R Documentation |
Dissolve polygon feature calss
sf_dissolve(x, y = NULL, overlaps = FALSE)
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 |
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.
A dissolved POLYGON or MULTIPOLYGON object
Jeffrey S. Evans <jeffrey_evans@tnc.org>
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"])
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.