Python - 获取线的周边区域(坐标)
Python - get surrounding area of line (coordinates)
我的坐标保存在 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
现在 "points" 确实包含围绕坐标线的多边形,该多边形与坐标线具有所需的距离。但是,多边形中仍可能存在一些交叉点。我试图通过插值来摆脱它们(例如 scipy.interpolate.spline)。但是我无法让它正常工作。
谁能帮忙=)?
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
感谢提示;)
我的坐标保存在 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
现在 "points" 确实包含围绕坐标线的多边形,该多边形与坐标线具有所需的距离。但是,多边形中仍可能存在一些交叉点。我试图通过插值来摆脱它们(例如 scipy.interpolate.spline)。但是我无法让它正常工作。
谁能帮忙=)?
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
感谢提示;)