【问题标题】:Python - get surrounding area of line (coordinates)Python - 获取线的周围区域(坐标)
【发布时间】:2015-09-26 05:47:33
【问题描述】:

我的坐标保存在 numpy 数组 x 和 y 中。 现在我想要的是获得一个多边形(分别是点数组),它用给定的宽度参数定义周围区域。

我遇到的问题是我需要一个没有(!)交叉点的多边形。但这确实会发生,当有一个狭窄的曲线时。对于我的应用程序,最好识别这些点并省略它们。有没有办法轻松找到这些点?

到目前为止我有:

# x contains x coords
# y contains y coords
# angle contains the current moving direction (radian)
import numpy as np

phi = np.pi/2 + angle
x_left = x + dist*np.cos( phi )
y_left = y + dist*np.sin( phi )

x_right = x - dist*np.cos( phi )
y_right = y - dist*np.sin( phi )

x_total = hstack((x_left, x_right[::-1]))
y_total = hstack((y_left, y_right[::-1]))        

##----- Omit Points with minimal dist < threshold to ANY point on traj
x_res = []
y_res = []
for idx in range(len(x_total)):
    m = np.min( np.sqrt( (x-x_total[idx])**2 + (y-y_total[idx])**2) )
    if m > dist-epsilon:
        x_res.append( x_total[idx] )
        y_res.append( y_total[idx] )    
points = np.vstack( (x_res, y_res) ).T

现在“点”确实包含围绕坐标线的多边形,该多边形与坐标线具有所需的距离。但是,多边形中仍然可能存在一些交叉点。我试图通过插值(例如使用 scipy.interpolate.spline)来摆脱它们。但我无法让它正常工作。

谁能帮忙=)?

【问题讨论】:

  • 身材匀称的看看。它应该做你想做的事。
  • 我试了一下身材匀称。但大同小异:“同样,Shapely 不会阻止无效特征的创建,但在操作时会引发异常。”请参阅toblerity.org/shapely/manual.html#polygons 我找不到将这个无效多边形转换为有效多边形的方法
  • 你在那个库中有任何“偏移”功能吗?您可以将整条线向两侧偏移,然后将它们的点放在一个数组中。

标签: python numpy geometry polygon computational-geometry


【解决方案1】:

Shapely 确实有效:

import shapely.geometry as shgeo
line = vstack( (x,y) ).T
line = shgeo.LineString( line )
surrounding_polygon = line.buffer( 10,cap_style=3 ) # 10=Dist

感谢您的提示;)

【讨论】:

  • 您可以考虑将自己的答案标记为“已回答”,以便其他看到此内容的人也知道答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多