如何将带孔的 sf 多边形的坐标转换为 X 和 Y(无 L1、L2)?

How to convert coordinates of sf polygons with holes to X and Y (without L1, L2)?

我有带孔的 sf 多边形

polygon <- structure(list(n_pins = 0.125, geometry = structure(list(structure(list(
    structure(c(1639, 2243, 2243, 2243, 1735, 1735, 1236, 1236, 
    1232, 1232, 1639, 1639, 888, 888, 769, 517, 517, 640, 640, 
    517, 517, 801, 801, 888), .Dim = c(12L, 2L)), structure(c(2243, 
    1872, 1870, 2241, 2243, 769, 775, 639, 633, 769), .Dim = c(5L, 
    2L)), structure(c(1543, 1541, 1833, 1835, 1543, 780, 645, 
    641, 776, 780), .Dim = c(5L, 2L)), structure(c(1364, 1362, 
    1504, 1506, 1364, 783, 647, 645, 781, 783), .Dim = c(5L, 
    2L)), structure(c(1240, 1238, 1337, 1339, 1240, 784, 649, 
    648, 783, 784), .Dim = c(5L, 2L))), class = c("XY", "POLYGON", 
"sfg"))), class = c("sfc_POLYGON", "sfc"), precision = 0, bbox = structure(c(xmin = 1232, 
ymin = 517, xmax = 2243, ymax = 888), class = "bbox"), crs = structure(list(
    input = NA_character_, wkt = NA_character_), class = "crs"), n_empty = 0L)), row.names = "...16", class = c("sf", 
"data.frame"), sf_column = "geometry", agr = structure(c(n_pins = NA_integer_), .Label = c("constant", 
"aggregate", "identity"), class = "factor"))

> polygon
Simple feature collection with 1 feature and 1 field
Geometry type: POLYGON
Dimension:     XY
Bounding box:  xmin: 1232 ymin: 517 xmax: 2243 ymax: 888
CRS:           NA
      n_pins                       geometry
...16  0.125 POLYGON ((1639 888, 2243 88...

plot(polygon)

我需要获取多边形的坐标并将其转换为没有 L1 和 L2 的 X 和 Y,以便使用通常的方式正确绘制 plot(x, y, type = "l")

  > head(sf::st_coordinates(polygon), 15)
         X   Y L1 L2
 [1,] 1639 888  1  1
 [2,] 2243 888  1  1
 [3,] 2243 769  1  1
 [4,] 2243 517  1  1
 [5,] 1735 517  1  1
 [6,] 1735 640  1  1
 [7,] 1236 640  1  1
 [8,] 1236 517  1  1
 [9,] 1232 517  1  1
[10,] 1232 801  1  1
[11,] 1639 801  1  1
[12,] 1639 888  1  1
[13,] 2243 769  2  1
[14,] 1872 775  2  1
[15,] 1870 639  2  1

如何操作?

当我绘制此坐标(仅限 x 和 y)时,我得到了错误的图片(带有额外的线条)

我知道解决这个拓扑问题是不可能的,但我需要任何实用的想法来通过 x 和 y 绘制这个多边形。

如果我理解正确,请在下面找到一种使用 sfdplyr 库的可能解决方案。

解决方案基于创建一个包含 n 个表格的列表,其中包含图形的 n 个特征的 XY 坐标。

Reprex

  • 代码
library(sf)
library(dplyr)

tables_XY <- polygon %>% 
  st_coordinates() %>% # retrieves coordinates in a matrix
  as.data.frame %>%    # converts into dataframe
  split(.,.$L1) %>%    # creates a list with one coordinates table for each feature
  lapply(., `select`, c("X", "Y")) # keeps only X and Y columns for each table of the list

# To what it looks like:
tables_XY
#> $`1`
#>       X   Y
#> 1  1639 888
#> 2  2243 888
#> 3  2243 769
#> 4  2243 517
#> 5  1735 517
#> 6  1735 640
#> 7  1236 640
#> 8  1236 517
#> 9  1232 517
#> 10 1232 801
#> 11 1639 801
#> 12 1639 888
#> 
#> $`2`
#>       X   Y
#> 13 2243 769
#> 14 1872 775
#> 15 1870 639
#> 16 2241 633
#> 17 2243 769
#> 
#> $`3`
#>       X   Y
#> 18 1543 780
#> 19 1541 645
#> 20 1833 641
#> 21 1835 776
#> 22 1543 780
#> 
#> $`4`
#>       X   Y
#> 23 1364 783
#> 24 1362 647
#> 25 1504 645
#> 26 1506 781
#> 27 1364 783
#> 
#> $`5`
#>       X   Y
#> 28 1240 784
#> 29 1238 649
#> 30 1337 648
#> 31 1339 783
#> 32 1240 784
  • 可视化(即 XY 框架中的图表)
plot(tables_XY[[1]], type = "l")
lapply(tables_XY[2:length(tables_XY)], lines, type = "l")

reprex package (v2.0.1)

于 2021-12-22 创建