将特定长度的垂直折线添加到 R 中的连续多边形

Add vertical polylines of specific length to contiguous polygons in R

我正在尝试以编程方式将特定长度的多条垂直多段线添加到 R 中的连续多边形。多段线的数量和长度应由用户指定,范围可以是 1 到 8 条多段线和 5000 到每个连续多边形 10000 英尺长。我怎样才能在 R 中实现这一点?

我可以通过对几个多边形使用 mapedit 程序包手动执行此操作,但我想为数千个连续的多边形自动执行此过程。

# Load required libraries
library(mapedit)
library(mapview)
library(dplyr)
library(sp)

# Sample polygons and polylines
geometry = structure(list(structure(list(structure(c(8.769563, 8.769563, 
                                                     8.770507, 8.770507, 8.769563, 50.815273, 50.815714, 50.815714, 
                                                     50.815273, 50.815273), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", 
                                                                                                          "sfg")), structure(list(structure(c(8.769568, 8.769568, 8.770507, 
                                                                                                                                              8.770507, 8.769568, 50.814852, 50.81527, 50.81527, 50.814852, 
                                                                                                                                              50.814852), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                              )), structure(list(structure(c(8.769568, 8.769568, 8.770502, 
                                                                                                                                                                             8.770502, 8.769568, 50.814412, 50.814849, 50.814849, 50.814412, 
                                                                                                                                                                             50.814412), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                             )), structure(list(structure(c(8.769568, 8.769568, 8.770502, 
                                                                                                                                                                                                            8.770502, 8.769568, 50.814005, 50.814408, 50.814408, 50.814005, 
                                                                                                                                                                                                            50.814005), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                                                            )), structure(list(structure(c(8.770502, 8.770502, 8.771301, 
                                                                                                                                                                                                                                           8.771301, 8.770502, 50.815273, 50.815717, 50.815717, 50.815273, 
                                                                                                                                                                                                                                           50.815273), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                                                                                           )), structure(list(structure(c(8.770518, 8.770518, 8.771301, 
                                                                                                                                                                                                                                                                          8.771301, 8.770518, 50.814852, 50.81527, 50.81527, 50.814852, 
                                                                                                                                                                                                                                                                          50.814852), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                                                                                                                          )), structure(list(structure(c(8.770507, 8.770507, 8.771301, 
                                                                                                                                                                                                                                                                                                         8.771301, 8.770507, 50.814408, 50.814849, 50.814849, 50.814408, 
                                                                                                                                                                                                                                                                                                         50.814408), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                                                                                                                                                         )), structure(list(structure(c(8.770507, 8.770507, 8.771296, 
                                                                                                                                                                                                                                                                                                                                        8.771296, 8.770507, 50.814005, 50.814405, 50.814405, 50.814005, 
                                                                                                                                                                                                                                                                                                                                        50.814005), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", "sfg"
                                                                                                                                                                                                                                                                                                                                        )), structure(c(8.769794, 8.769783, 50.814785, 50.814076), .Dim = c(2L, 
                                                                                                                                                                                                                                                                                                                                                                                                            2L), class = c("XY", "LINESTRING", "sfg")), structure(c(8.770051, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                    8.770035, 50.814785, 50.814069), .Dim = c(2L, 2L), class = c("XY", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 "LINESTRING", "sfg")), structure(c(8.770271, 8.77026, 50.814781, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    50.814076), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", "sfg"
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    ))), class = c("sfc_GEOMETRY", "sfc"), precision = 0, bbox = structure(c(xmin = 8.769563, 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             ymin = 50.814005, xmax = 8.771301, ymax = 50.815717), class = "bbox"), crs = structure(list(
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               epsg = 4326L, proj4string = "+proj=longlat +datum=WGS84 +no_defs"), class = "crs"), classes = c("POLYGON", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "POLYGON", "POLYGON", "POLYGON", "POLYGON", "POLYGON", "POLYGON", 
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               "POLYGON", "LINESTRING", "LINESTRING", "LINESTRING"), n_empty = 0L)

# Visualize geometry
mapview(geometry)

我试图通过多边形内部的 spsample 函数创建定期采样点并用线连接它们,但没有成功。感谢我能得到的任何帮助。

这是在给定多边形的情况下创建线的一种方法。这可能不是您想要的,因为您的要求非常具体,但希望代码足够通用,您可以对其进行调整。

library(sf)
library(purrr)
polygon <- st_polygon(list(matrix(c(1,1,2,2,1,1,0,0,1,1), ncol = 2)))

# use polygon bounding box to o compute line parameters
bb <- st_bbox(polygon)
number_of_lines <- 5
line_length <- (bb[["ymax"]] - bb[["ymin"]]) / 1.2
y_offset <- bb[["ymin"]] + (bb[["ymax"]] - bb[["ymin"]] - line_length) / 2

# compute coordinates
xs <- seq(bb[["xmin"]], bb[["xmax"]], length.out = number_of_lines)
ys <- bb[["ymin"]] + line_length
# create a linestring
lines <- purrr::map2(xs, ys, ~st_linestring(matrix(c(.x, .x, .y,y_offset), ncol = 2))) %>% st_sfc(crs = st_crs(polygon)) 

# view
plot(polygon)
plot(lines, col = 2, add = TRUE)