简体   繁体   English

Python - 获取线的周围区域(坐标)

[英]Python - get surrounding area of line (coordinates)

I have my coords saved in numpy arrays x and y. 我把我的坐标保存在numpy数组x和y中。 Now all I want is to get a polygon (respectively arrays of points) that defines the surrounding area with a given width parameter. 现在我想要的是获得一个多边形(分别是点阵列),用于定义具有给定宽度参数的周围区域。

The problem I have is that I need to have a polygon without(!) intersections. 我遇到的问题是我需要一个没有(!)交叉点的多边形。 But this does occur, when there is a narrow curve. 但是,当曲线很窄时,确实会发生这种情况。 For my application it would be best to identify these points and omit them. 对于我的应用程序,最好确定这些点并省略它们。 Is there a way to easily find these points? 有没有办法轻松找到这些点?

So far I have: 到目前为止,我有:

# 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

Now "points" does contain the polygon surrounding the coord.-line that has the desired distance to the coord.-line. 现在,“点”确实包含围绕coord.-line的多边形,它与coord.-line有所需的距离。 However there can still be some intersections in the polygon. 但是,多边形中仍可能存在一些交叉点。 I tried to get rid of them by interpolate (eg with scipy.interpolate.spline). 我试图通过插值去掉它们(例如用scipy.interpolate.spline)。 But I couldn't manage it to work properly. 但我无法管理它才能正常工作。

Can anyone please help =) ? 任何人都可以请帮助=)?

Shapely did work: 身材匀称确实有效:

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

Thanks for the hint;) 谢谢你的暗示;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM