【发布时间】: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对象中创建新行)。我用谷歌搜索了一下,发现了一个不同的解决方案来解决我的问题。如果有人提出替代解决方案,我会暂时搁置这个问题。