简体   繁体   中英

Python - get surrounding area of line (coordinates)

I have my coords saved in numpy arrays x and 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. However there can still be some intersections in the polygon. I tried to get rid of them by interpolate (eg with 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;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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