【问题标题】:st_intersection LINESTRING with borders of POLYGONs while preserving the order of points in r / sfst_intersection LINESTRING 与 POLYGONs 的边界,同时保留 r / sf 中点的顺序
【发布时间】:2019-07-25 11:29:20
【问题描述】:

我需要将LINESTRING 与边界POLYGON 特征相交,同时保留生成的POINT 特征的顺序。背景是我需要弄清楚进入/离开特定国家的过境车,但过境的顺序很重要。

我已经实现了以下方法:

# setup test data
  poly <- 
    list(matrix(c(0,0,10,0,10,10,0,10,0,0),ncol=2, byrow=TRUE)) %>% 
    sf::st_polygon() %>% 
    sf::st_sfc() %>% 
    sf::st_sf()

  line1 <- matrix(c(-1, 10, 5, -1),ncol=2, byrow=TRUE) %>% 
    sf::st_linestring() %>% 
    sf::st_sfc() %>% 
    sf::st_sf()

  # reverse of line 1
  line2 <- matrix(c(5, -1, -1, 10), ncol=2, byrow=TRUE) %>% 
    sf::st_linestring() %>% 
    sf::st_sfc() %>% 
    sf::st_sf()

  # preview
  leaflet::leaflet() %>% 
    leaflet::addPolygons(data = poly) %>% 
    leaflet::addPolylines(data = line1) %>%
    leaflet::addPolylines(data = line2) %>% 
    leaflet::addTiles()


# do the intersection
  # cast to multilinestring because I just need the border crossing points
  ml <- sf::st_cast(poly, "MULTILINESTRING") 
  sf::st_intersection(ml, line1)
  sf::st_intersection(ml, line2)

但是,这种方法会丢失过境点的顺序。有人有更好的主意吗?

【问题讨论】:

  • 我只想添加一个指示线串方向的列。然后您可以相应地订购您的积分。
  • 它只是一个单一的线串,可以与国家边界有多个交叉点,所以这不起作用(如果我正确理解你的评论)
  • 我会分割线串,添加指示方向的列,空间连接到交叉点,并按方向指示对点进行排序。
  • 感谢您的提示。我玩过st_segmentize(),但它只会向线串添加额外的顶点(没有在sf 对象中创建新行)。我用谷歌搜索了一下,发现了一个不同的解决方案来解决我的问题。如果有人提出替代解决方案,我会暂时搁置这个问题。

标签: r gis spatial sf


【解决方案1】:

我找到了一个涉及rgeos::gProject() 的答案。这个函数计算直线上的点之间的距离,我可以用它来为我的点推导排序顺序:

# do the intersection
ml <- sf::st_cast(poly, "MULTILINESTRING") 

points1 <- sf::st_intersection(ml, line1) %>% 
  sf::st_cast("POINT")

points2 <- sf::st_intersection(ml, line2) %>% 
  sf::st_cast("POINT")

# Calculate sort order for the points
points1$order <- rgeos::gProject(sf::as_Spatial(line1), sf::as_Spatial(points1))
points2$order <- rgeos::gProject(sf::as_Spatial(line2), sf::as_Spatial(points2))

【讨论】:

    猜你喜欢
    • 2020-12-29
    • 2020-06-22
    • 2019-08-02
    • 2017-06-03
    • 1970-01-01
    • 1970-01-01
    • 2019-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多