1
我有一组多边形,其中一些相交和/或触摸(共同边框)。我正在使用R的sf
包对多边形执行操作。到目前为止,我的方法是使用sf::st_union()
,它可以根据需要连接相邻和相交的多边形,但它也会将所有多边形合并到一个MULTIPOLYGON
几何中。我想将每个多边形分隔为一个sf(data.frame)类,其中每个多边形对象都显示为一个行,在data.frame
如何在执行st_union()操作后将多面几何体分离为多个多边形对象?
下面显示一个示例。我首先创建一个示例数据集:
# Creating four example polygons, of which two (two squares) are neighbors:
p1 <- rbind(c(0,0), c(1,0), c(3,2), c(2,4), c(1,4), c(0,0))
pol1 <-st_polygon(list(p1))
p2 <- rbind(c(3,0), c(4,0), c(4,1), c(3,1), c(3,0))
pol2 <-st_polygon(list(p2))
p3 <- rbind(c(4,0), c(4,1), c(5,1), c(5,0),c(4,0))
pol3 <-st_polygon(list(p3))
p4 <- rbind(c(3,3), c(4,2), c(4,3), c(3,3))
pol4 <-st_polygon(list(p4))
d = data.frame(some_attribute = 1:4)
d$geometry = st_sfc(pol1,pol2,pol3,pol4)
df = st_as_sf(d)
class(df)
#[1] "sf" "data.frame"
df
# Simple feature collection with 4 features and 1 field
# geometry type: POLYGON
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
# epsg (SRID): NA
# proj4string: NA
# some_attribute geometry
# 1 1 POLYGON((0 0, 1 0, 3 2, 2 4...
# 2 2 POLYGON((3 0, 4 0, 4 1, 3 1...
# 3 3 POLYGON((4 0, 4 1, 5 1, 5 0...
# 4 4 POLYGON((3 3, 4 2, 4 3, 3 3))
plot(df)
给出:
我然后执行st_union()
操作以相交或触摸(两个平方以上)所有多边形的几何形状组合成一个:
df_union <- df %>% st_union()
df_union
# Geometry set for 1 feature
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
# epsg (SRID): NA
# proj4string: NA
# MULTIPOLYGON(((3 3, 4 3, 4 2, 3 3)), ((4 0, 3 0...
plot(df_union)
结果:
如图所示的df_union
上述结果是一个MULTIPOLYGON
几何只有一行。我想执行每个多边形成几何分离,如上面的图中,但造成几个多边形对象的操作,这东西相当于:
# Simple feature collection with 4 features and 1 field
# geometry type: MULTIPOLYGON
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
# epsg (SRID): NA
# proj4string: NA
# some_attribute geometry
# 1 1 POLYGON((0 0, 1 0, 3 2, 2 4...
# 2 2 POLYGON((3 0, 4 0, 5 1, 5 0...
# 3 3 POLYGON((3 3, 4 2, 4 3, 3 3))
此使用sf
包我该怎么办?
你只想工会那些相互接触的多边形? –
是,对于彼此接触和/或相交的联合多边形,将每个新多边形作为“sf”“data.frame”类对象中的一行返回。 – trevi
然后'st_cast'将'MULTIPOLYGON'改为'POLYGON' –