【发布时间】:2019-10-27 01:29:51
【问题描述】:
我有一条路径作为与多边形相交的一系列有序点。我想在第一个多边形交点之前提取线的一部分。
我试图通过计算与多边形的差异来分割路径,但线也在其自相交处被分割(参见示例)。我需要从一开始一直提取路径的完整部分,直到它首先与多边形相交(示例中为蓝色正方形)。
# A wonky line that intersects itself
l = sf::st_linestring(cbind(cos((0:100) * pi / 50), sin((0:100) * pi / 15 )))
# A polygon that intersects the line
p = sf::st_polygon(list(cbind(c(-.3, -.3, -.2, -.2, -.3), c(-.3, -.2, -.2, -.3, -.3))))
# Visualisation of the problem
plot(l)
plot(p, add = TRUE, col = "blue")
# Taking the first fragment of the difference does not work as the path intersects and divides itself
d = sf::st_difference(l, p)
plot(sf::st_linestring(d[[1]]), add = TRUE, col = "red")
在示例中,路径由所有交叉点(甚至在其自身)分割,因此路径的第一部分不会一直延伸到多边形。我怀疑 sf 包中有一个专门用于我目的的函数 - 但我还没有找到它。
【问题讨论】: