r - 使用带有 R 的 sf 包从线、圆和弧创建多边形
问题描述
所以我需要使用sf
包用 R 制作一个多边形,我所拥有的只是一个相对简单的数据框,我可以在其中看到构成线、弧的点(多边形边缘的某些部分是弧而不是直线),然后是圆圈(这些在多边形内部并且是表面上的孔)。
多边形边缘是线或弧,它们应该相互连接,这样我就不必使用st_convex_hull
或类似的东西。完整的圆圈在多边形内部,最后将是多边形中的孔。
我是新手,sf
但我已经想出了如何处理线条和圆圈,尽管可能有更好的方法。
我创建了具有 3 个元素的虚拟数据,每个元素一个,以及我如何构建不同的几何图形。我实际上被弧线部分困住了。我很有信心这应该很容易,也许使用circularstring
type 但不确定这是否是最好的方法。显然,这些虚拟数据不会有最终的多边形,但希望它能传达这个想法。
这是虚拟数据和我的代码。
library(tidyverse)
#> Warning: package 'tidyverse' was built under R version 4.0.3
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
(sample <- data.frame(number = 1,
name = c("Arc", "Circle", "Line"),
area = c(0.46, 330, NA),
start_angle = c(134, NA, NA),
angle_total = c(17, NA, NA),
center_x = c(974, 377, NA),
center_y = c(7299, 7250, NA),
center_z = c(0, 0, NA),
length = c(4.27, NA, 15),
radius = c(14, 10.2, NA),
angle = c(NA, NA, 270),
delta_x = c(NA, NA, 0),
delta_y = c(NA, NA, -15),
delta_z = c(NA, NA, 0),
start_x = c(NA, NA, 18.2),
start_y = c(NA, NA, 7000),
start_z = c(NA, NA, 0),
end_x = c(NA, NA, 18.2),
end_y = c(NA, NA, 146),
end_z = c(NA, NA, 0)) %>%
as_tibble())
#> # A tibble: 3 x 20
#> number name area start_angle angle_total center_x center_y center_z length
#> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
#> 1 1 Arc 0.46 134 17 974 7299 0 4.27
#> 2 1 Circ~ 330 NA NA 377 7250 0 NA
#> 3 1 Line NA NA NA NA NA NA 15
#> # ... with 11 more variables: radius <dbl>, angle <dbl>, delta_x <dbl>,
#> # delta_y <dbl>, delta_z <dbl>, start_x <dbl>, start_y <dbl>, start_z <dbl>,
#> # end_x <dbl>, end_y <dbl>, end_z <dbl>
line_start <- sample %>%
filter(name == "Line") %>%
select(start_x, start_y) %>%
rename(X = start_x, Y = start_y) %>%
mutate(ID = 1:nrow(.))
line_end <- sample %>%
filter(name == "Line") %>%
select(end_x, end_y) %>%
rename(X = end_x, Y = end_y) %>%
mutate(ID = 1:nrow(.))
(lines <- line_start %>%
bind_rows(line_end) %>%
st_as_sf(coords = c("X", "Y")) %>%
group_by(ID) %>%
summarise(do_union = FALSE) %>%
st_cast("LINESTRING"))
#> `summarise()` ungrouping output (override with `.groups` argument)
#> Simple feature collection with 1 feature and 1 field
#> geometry type: LINESTRING
#> dimension: XY
#> bbox: xmin: 18.2 ymin: 146 xmax: 18.2 ymax: 7000
#> CRS: NA
#> # A tibble: 1 x 2
#> ID geometry
#> <int> <LINESTRING>
#> 1 1 (18.2 7000, 18.2 146)
# ggplot() +
# geom_sf(data = lines) +
# coord_sf(xlim = c(10, 20))
circle_centers <- sample %>%
filter(name == "Circle") %>%
select(center_x, center_y) %>%
rename(X = center_x, Y = center_y) %>%
mutate(ID = 1:nrow(.))
circle_radii <- sample %>%
filter(name == "Circle") %>%
select(radius) %>%
rename(R = radius) %>%
mutate(ID = 1:nrow(.))
(circle <- circle_centers %>%
st_as_sf(coords = c("X", "Y")) %>%
st_buffer(circle_radii$R))
#> Simple feature collection with 1 feature and 1 field
#> geometry type: POLYGON
#> dimension: XY
#> bbox: xmin: 366.8 ymin: 7239.8 xmax: 387.2 ymax: 7260.2
#> CRS: NA
#> # A tibble: 1 x 2
#> ID geometry
#> * <int> <POLYGON>
#> 1 1 ((387.2 7250, 387.186 7249.466, 387.1441 7248.934, 387.0744 7248.404, 3~
# ggplot() +
# geom_sf(data = circle)
# ggplot() +
# geom_sf(data = rbind(lines, circle)) +
# coord_sf(xlim = c(-500, 1000))
由reprex 包(v0.3.0)于 2020-12-03 创建
唯一缺少的步骤是将所有这些几何图形组合在一起,以使最终的多边形内部带有孔,但是一旦我拥有了所有分离的几何图形,这应该很容易。
也可能有更好的方法来处理线条和圆圈部分。同样,我才刚刚开始,sf
所以如果您知道一种方法,请随时教我更有效的方法。
提前感谢您的帮助!
解决方案
推荐阅读
- docker - 不使用默认端口 80 时,没有尾部斜杠的 nginx 重定向在 docker 中不起作用
- github - 在 Github 中,如何查看仅显示哪些文件已更改的提交?
- django - Django 在代理通行证上找不到静态文件
- linux - 使用:“pip install”从源代码构建。不适用于 sudo
- reactjs - 从 React 的 Context API 组件重定向
- python - 在 Pandas 中的 groupby 函数之后,如何获得包含行数的第三列?
- python-3.x - 在 discord.py 中添加对 ctx.send 消息的反应
- javascript - 仅在 setState 完成后返回调用者函数
- android-studio - 在 MPAndroid 折线图中显示数据
- python - WARNING: WARNING:tensorflow:Model 是用形状 (None, 150) 构造的,但它是在形状不兼容的输入上调用的 (None, 1)