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

时间:2019-05-25 14:36:32

标签: r geospatial polygon spatial polyline

我正在尝试以语法方式向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)

enter image description here

我尝试通过多边形内的spsample函数创建定期采样的点,并通过线将它们连接起来,但是没有成功。感谢您能获得的任何帮助。

1 个答案:

答案 0 :(得分:0)

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

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)

lines in polygon