如何在执行 st_union() 操作后将多多边形几何分离为多个多边形对象?
How to separate a multipolygon geometry into several polygons objects after performing a st_union() operation?
我有一组多边形,其中一些相交 and/or 接触(公共边界)。我正在使用 R 的 sf
包对多边形执行操作。到目前为止,我的方法是使用 sf::st_union()
连接相邻和相交的多边形,但它也将所有多边形组合成一个 MULTIPOLYGON
几何体。我想将每个多边形分隔为 sf(data.frame) class,其中每个多边形对象在 data.frame
中显示为一行
我在下面展示一个例子。我首先创建一个示例数据集:
# 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
包执行此操作?
感谢 Edzer 提供答案并为我们提供了很棒的 sf
软件包!
如 Edzer 所述,执行 st_cast
将 MULTIPOLYGON
对象转换为多个 POLYGON
对象:
df_union_cast <- st_cast(df_union, "POLYGON")
df_union_cast
# Geometry set for 3 features
# geometry type: POLYGON
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
# epsg (SRID): NA
# proj4string: NA
# POLYGON((3 3, 4 3, 4 2, 3 3))
# POLYGON((4 0, 3 0, 3 1, 4 1, 5 1, 5 0, 4 0))
# POLYGON((0 0, 1 4, 2 4, 3 2, 1 0, 0 0))
我有一组多边形,其中一些相交 and/or 接触(公共边界)。我正在使用 R 的 sf
包对多边形执行操作。到目前为止,我的方法是使用 sf::st_union()
连接相邻和相交的多边形,但它也将所有多边形组合成一个 MULTIPOLYGON
几何体。我想将每个多边形分隔为 sf(data.frame) class,其中每个多边形对象在 data.frame
我在下面展示一个例子。我首先创建一个示例数据集:
# 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
包执行此操作?
感谢 Edzer 提供答案并为我们提供了很棒的 sf
软件包!
如 Edzer 所述,执行 st_cast
将 MULTIPOLYGON
对象转换为多个 POLYGON
对象:
df_union_cast <- st_cast(df_union, "POLYGON")
df_union_cast
# Geometry set for 3 features
# geometry type: POLYGON
# dimension: XY
# bbox: xmin: 0 ymin: 0 xmax: 5 ymax: 4
# epsg (SRID): NA
# proj4string: NA
# POLYGON((3 3, 4 3, 4 2, 3 3))
# POLYGON((4 0, 3 0, 3 1, 4 1, 5 1, 5 0, 4 0))
# POLYGON((0 0, 1 4, 2 4, 3 2, 1 0, 0 0))