【发布时间】:2021-11-12 09:24:17
【问题描述】:
我在地图上有一组空间坐标,以及穿过它们的多线串。我需要弄清楚线在每种颜色的多边形内的长度。
注意事项:
- 我的最小reprex 不是很好,我的实际数据是一个data.frame,其中有一个颜色列(它是真实数据中的组)、一些识别列和一个多面几何列。我真的不知道如何制作它,所以我的 reprex 只有 3 个单独的多边形。
- 有时两种颜色的多边形会重叠,在这种情况下,我希望区分线与重叠形状相交的长度与线仅与一种形状相交的长度。
最小的代表:
poly1 <-
# create list of matrices and the first point same as last point
list(
matrix(
c(0, 0,
4, 0,
5, 1,
4, 2,
3, 2,
1, 1,
0, 0),
ncol=2, byrow=T
)
)
poly1 <- sf::st_polygon(poly1)
poly2 <-
# create list of matrices and the first point same as last point
list(
matrix(
c(4, 1,
7, 0,
7, 1,
6, 3,
3, 2,
2, 1,
4, 1),
ncol=2, byrow=T
)
)
poly2 <- sf::st_polygon(poly2)
poly3 <-
# create list of matrices and the first point same as last point
list(
matrix(
c(7, 1,
10, 1,
12, 2,
11, 4,
8, 3,
7, 2,
7, 1),
ncol=2, byrow=T
)
)
poly3 <- sf::st_polygon(poly3)
line <-
# create list of matrices and the first point same as last point
list(
matrix(
c(0, 1,
2, 0,
4, 1,
6, 3,
8, 2,
10, 1,
12, 1),
ncol=2, byrow=T
)
)
line <-
sf::st_multilinestring(line)
ggplot() +
geom_sf(data = poly1, fill = "green",alpha=.5) +
geom_sf(data = poly2, fill = "blue",alpha=.5) +
geom_sf(data = poly3, fill = "green", alpha=.5)+
geom_sf(data = line, color="black", size=2) +
ggthemes::theme_map()
想要的输出:
- st 多边形的可视化表示仅裁剪到直线(我使用的是 st_intersection()),如下所示:
poly1_cropped <- st_intersection( line, poly1)
poly2_cropped <- st_intersection( line, poly2)
poly3_cropped <- st_intersection( line, poly3)
ggplot() +
geom_sf(data = poly1, fill = "green",alpha=.5) +
geom_sf(data = poly2, fill = "blue",alpha=.5) +
geom_sf(data = poly3, fill = "green", alpha=.5)+
geom_sf(data = line, color="black", size=2) +
ggthemes::theme_map() +
geom_sf(data = poly1_cropped, color="green", size=2) +
geom_sf(data = poly2_cropped, color="blue", size=2) +
geom_sf(data = poly3_cropped, color="green", size=2)
然后是量化线何时穿过每个形状的数据框,例如:
shape | color | shapes_overlapping | length
poly1 green 0 3
poly1 green 1 .5
poly2 blue 1 .5
poly2 blue 0 2
poly3 green 0 2.5
【问题讨论】: