【问题标题】:Identify separate spatial lines on a map在地图上识别单独的空间线
【发布时间】:2020-04-15 05:27:59
【问题描述】:

我有一张多条河流的地图,我想向几何数据框添加一个 ID 列,添加一个定义每条河流的分组变量。我只有线几何信息,没有其他元数据,这可能吗?

一个例子:

dat <- structure(list(geometry = structure(list(structure(c(169.023627307075, 
  169.02315299228, -45.3068517761089, -45.3081870363656), .Dim = c(2L, 
    2L), class = c("XY", "LINESTRING", "sfg")), structure(c(169.01854883529, 
      169.018930977689, 169.02315299228, -45.3083004691879, -45.3083134946477, 
      -45.3081870363656), .Dim = 3:2, class = c("XY", "LINESTRING", 
        "sfg")), structure(c(169.02315299228, 169.0330663306, -45.3081870363656, 
          -45.3144702778175), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
            "sfg")), structure(c(169.015997396195, 169.022945130719, -45.3119974282578, 
              -45.3168289670259), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
                "sfg")), structure(c(169.022945130719, 169.032555385154, -45.3168289670259, 
                  -45.3163448854193), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
                    "sfg")), structure(c(169.01868555271, 169.022945130719, -45.3174947235968, 
                      -45.3168289670259), .Dim = c(2L, 2L), class = c("XY", "LINESTRING", 
                        "sfg"))), n_empty = 0L, crs = structure(list(input = "4326", 
                          wkt = "GEOGCS[\"GCS_WGS_1984\",\n    DATUM[\"WGS_1984\",\n        SPHEROID[\"WGS_84\",6378137,298.257223563]],\n    PRIMEM[\"Greenwich\",0],\n    UNIT[\"Degree\",0.017453292519943295],\n    AUTHORITY[\"EPSG\",\"4326\"]]"), class = "crs"), class = c("sfc_LINESTRING", 
                            "sfc"), precision = 0, bbox = structure(c(xmin = 169.015997396195, 
                              ymin = -45.3174947235968, xmax = 169.0330663306, ymax = -45.3068517761089
                            ), class = "bbox"))), row.names = c(NA, 6L), class = c("sf", 
                              "data.frame"), sf_column = "geometry", agr = structure(integer(0), .Label = c("constant", 
                                "aggregate", "identity"), class = "factor"))

library(ggplot2)
library(sf)

ggplot() + 
  geom_sf(data = dat, size = 1) + 
  coord_sf()

在这个简单的示例中,有两条“河流”,但我无法在数据框中识别它们。我想要这样的专栏:

dat$ID <- c("A", "A", "A", "B", "B", "B")

我能想到的唯一方法是比较线端的坐标值,但这似乎不是很有效。非常感谢任何帮助。

【问题讨论】:

    标签: r spatial sf


    【解决方案1】:

    可以使用igraph 根据交集计算集群 ID,如下所示:

    library(sf)
    library(igraph)
    
    dat <- structure(...)
    
    # Detect clusters
    m = st_intersects(dat, sparse = FALSE)
    g = graph_from_adjacency_matrix(m)
    dat$id = clusters(g)$membership
    dat
    
    ## Simple feature collection with 6 features and 1 field
    ## geometry type:  LINESTRING
    ## dimension:      XY
    ## bbox:           xmin: 169.016 ymin: -45.31749 xmax: 169.0331 ymax: -45.30685
    ## geographic CRS: WGS 84
    ##                         geometry id
    ## 1 LINESTRING (169.0236 -45.30...  1
    ## 2 LINESTRING (169.0185 -45.30...  1
    ## 3 LINESTRING (169.0232 -45.30...  1
    ## 4 LINESTRING (169.016 -45.312...  2
    ## 5 LINESTRING (169.0229 -45.31...  2
    ## 6 LINESTRING (169.0187 -45.31...  2
    
    # Plot
    plot(st_geometry(dat))
    text(st_coordinates(st_centroid(dat)), as.character(dat$id))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-06-27
      • 1970-01-01
      • 2021-07-06
      • 2013-07-10
      • 2021-04-19
      • 1970-01-01
      • 2016-07-25
      相关资源
      最近更新 更多