【问题标题】:Finding Intersections Region Based Trajectories vs. Line Trajectories查找交叉点基于区域的轨迹与线轨迹
【发布时间】:2018-03-05 22:27:28
【问题描述】:

我有两条轨迹(即两个点列表),我正在尝试找到这两条轨迹的交点。但是,如果我将这些轨迹表示为线,我可能会错过现实世界的交叉点(只是错过)。

我想做的是将线表示为在点周围具有一定宽度的多边形,然后找到两个多边形彼此相交的位置。

我正在使用 python 空间库,但我想知道以前是否有人这样做过。这是一张不相交的线段的图片,因为它们只是彼此错过。下面是代表两个物体轨迹的示例数据代码。

object_trajectory=np.array([[-3370.00427248,  3701.46800775],
   [-3363.69164715,  3702.21408203],
   [-3356.31277271,  3703.06477984],
   [-3347.25951787,  3704.10740164],
   [-3336.739511  ,  3705.3958357 ],
   [-3326.29355823,  3706.78035903],
   [-3313.4987339 ,  3708.2076586 ],
   [-3299.53433345,  3709.72507366],
   [-3283.15486406,  3711.47077376],
   [-3269.23487255,  3713.05635557]])
target_trajectory=np.array([[-3384.99966703,  3696.41922372],
   [-3382.43687562,  3696.6739521 ],
   [-3378.22995178,  3697.08802862],
   [-3371.98983789,  3697.71490469],
   [-3363.5900481 ,  3698.62666805],
   [-3354.28520354,  3699.67613798],
   [-3342.18581931,  3701.04853915],
   [-3328.51519511,  3702.57528111],
   [-3312.09691577,  3704.41961271],
   [-3297.85543763,  3706.00878621]])
plt.plot(object_trajectory[:,0],object_trajectory[:,1],'b',color='b')
plt.plot(vehicle_trajectory[:,0],vehicle_trajectory[:,1],'b',color='r')

【问题讨论】:

  • “将线表示为多边形”是什么意思?多边形由线组成。你的意思是你想找到点或点之间的线在另一个轨迹的某个阈值内的位置吗?
  • 感谢@JeremyMcGibbon 的澄清。基本上我想在每一行周围都有一个信封。信封将被表示为一个多边形。然后我可以确定两个信封相互交叉的位置,然后对这些交叉点进行更多分析。考虑绘图图像。在这种情况下,两条线都不相交,但在实际情况下,它确实是因为它不是一条线并且有宽度。这有帮助吗?
  • 点的采样率如何?仅检查点附近的重叠是否足够,或者您是否还需要查看这些点之间的轨迹?你的轨迹是多少个点,作为一个数量级?
  • 听起来你想缓冲这些线,然后计算交点。大概可以用shapely:toblerity.org/shapely/manual.html
  • 排序 - 你在你的线周围创建一个缓冲区 polygon (如香肠),缓冲区宽度是你的公差。如果您可以编辑您的 Q 以包含一些代码来生成示例数据,我可能会有机会处理它。

标签: python polygon geospatial spatial


【解决方案1】:

假设您有两行由 numpy 数组 x1y1x2y2 定义。

import numpy as np

您可以创建一个数组distances[i, j],其中包含第一行中的ith 点和第二行中的jth 点之间的距离。

distances = ((x1[:, None] - x2[None, :])**2 + (y1[:, None] - y2[None, :])**2)**0.5

然后您可以找到distances 小于您要为交集定义的某个阈值的索引。如果您认为线条有一定的粗细,则阈值将是该粗细的一半。

threshold = 0.1
intersections = np.argwhere(distances < threshold)

intersections 现在是一个 N×2 数组,包含所有被认为是“相交”的点对([i, 0] 是第一行的索引,[i, 1] 是第二行的索引) .如果您想从每条相交的线中获取所有索引的集合,您可以使用类似

first_intersection_indices = np.asarray(sorted(set(intersections[:, 0])))
second_intersection_indices = np.asarray(sorted(set(intersections[:, 1])))

从这里,您还可以通过只取每个列表中任何连续值的中心值来确定有多少个交叉点。

L1 = []
current_intersection = []
for i in range(first_intersection_indices.shape[0]):
    if len(current_intersection) == 0:
        current_intersection.append(first_intersection_indices[i])
    elif first_intersection_indices[i] == current_intersection[-1]:
        current_intersection.append(first_intersection_indices[i])
    else:
        L1.append(int(np.median(current_intersection)))
        current_intersection = [first_intersection_indices[i]]
print(len(L1))

您可以使用这些来打印每个交叉点的坐标。

for i in L1:
    print(x1[i], y1[i])

【讨论】:

  • 谢谢!寻找接近点的绝佳替代方法,作为两个多边形的交点的近似值。一个问题 - 如果点的采样有点“不走运”(因为不考虑点之间的插值),这种方法可能不会找到交叉点,而实际上会有交叉点。这准确吗?
【解决方案2】:

事实证明,shapely 包已经有很多方便的功能,让我在这方面走得很远。

from shapely.geometry import Point, LineString, MultiPoint
# I assume that self.line is of type LineString (i.e. a line trajectory)
region_polygon = self.line.buffer(self.lane_width)
# line.buffer essentially generates a nice interpolated bounding polygon around the trajectory.
# Now we can identify all the other points in the other trajectory that intersects with the region_polygon that we just generated. You can also use .intersection if you want to simply generate two polygon trajectories and find the intersecting polygon as well.
is_in_region = [region_polygon.intersects(point) for point in points]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-22
    相关资源
    最近更新 更多