You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在R中创建sfn_network:合并关联LINESTRING为单一线路

R中合并分组首尾相连LINESTRING并构建简化空间网络

需求背景

我正在用R构建空间网络,现有一批首尾相接的LINESTRING,每个代表一段铁路线路。我的目标是按分组变量GROUP,把同组内相连的LINESTRING合并成从起点到终点的单一LINESTRING;后续用sfn_network()构建网络时,只保留每组的起点和终点作为节点,不纳入每个单独LINESTRING的中间节点。

数据可视化说明

  • 第一张图:不同颜色区分每个独立的LINESTRING,能看到它们彼此首尾相连;
  • 第二张图:展示GROUP分组,标识哪些LINESTRING属于同一相连组。

已尝试的代码

rails1 <- rails %>% group_by(GROUP) %>% 
  summarise(do_union = TRUE) %>% 
  ungroup() %>% 
  st_cast("MULTILINESTRING") %>% 
  st_cast("LINESTRING")

数据结构示例

structure(list(id = c(1, 1, 1, 1, 1, 6, 7), ID_1 = c(1, 2, 3, 
4, 5, 6, 7), GROUP = c(1, 1, 1, 2, 2, 3, 3), geometry = structure(list(
    structure(c(9.49750923562068, 9.25766974945376, 51.3143637469628, 
    51.1041822177009), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(9.25766974945376, 8.943977955227, 51.1041822177009, 
    51.0783319880388), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(8.943977955227, 8.773918358099, 51.0783319880388, 
    51.1867877726016), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(8.773918358099, 8.57243470497997, 51.1867877726016, 
    51.1786769084193), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(8.57243470497997, 8.56919987568677, 
    51.1786769084193, 51.144914596165), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(9.25766974945376, 9.36450576334242, 
    51.1041822177009, 50.9477904259333), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg")), structure(c(9.36450576334242, 9.21755208973725, 
    50.9477904259333, 50.9058462470297), dim = c(2L, 2L), class = c("XY", "LINESTRING", 
    "sfg"))), n_empty = 0L, crs = structure(list(
    input = "WGS 84", wkt = "GEOGCRS[\"WGS 84\",
    DATUM[\"World Geodetic System 1984\",
        ELLIPSOID[\"WGS 84\",6378137,298.257223563,
            LENGTHUNIT[\"metre\",1]]],
    PRIMEM[\"Greenwich\",0,
        ANGLEUNIT[\"degree\",0.0174532925199433]],
    CS[ellipsoidal,2],
        AXIS[\"latitude\",north,
            ORDER[1],
            ANGLEUNIT[\"degree\",0.0174532925199433]],
        AXIS[\"longitude\",east,
            ORDER[2],
            ANGLEUNIT[\"degree\",0.0174532925199433]],
    ID[\"EPSG\",4326]]"), class = "crs"), class = c("sfc_LINESTRING", 
"sfc"), precision = 0, bbox = structure(c(xmin = 8.56919987568677, 
ymin = 50.9058462470297, xmax = 9.49750923562068, ymax = 51.3143637469628
), class = "bbox"))), row.names = c(NA, -7L), class = c("sf", 
"data.frame"), sf_column = "geometry", agr = structure(c(id = NA_integer_, 
ID_1 = NA_integer_, GROUP = NA_integer_), class = "factor", levels = c("constant", 
"aggregate", "identity")))

内容的提问来源于stack exchange,提问作者Sulz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.19 15:05:55