简体   繁体   中英

How to convert the following coordinates to 'shapely' polygon?

I have the coordinates from a list in the following format :

[572.71063 453.9848  622.2049  472.86023]

where the four numbers correspond to X1, Y1, X2, Y2 coordinates of a rectangle. (X1,Y1 top left corner, X2,Y2 bottom right corner).

I want to convert the list item to a 'shapely' Polygon. The list item is not in the right format, in this case, Polygon to work. So I used the following function

brd = Polygon(map(np.squeeze, bb))

It does not work. I think the issue with rectangle coordinates is actually not in contours format. I think the contour is supposed to be close.

What is the best way I could convert the rectangle coordinates in the list to shapely polygon?

If I understand problem you need simply this:

rect = [572.71063, 453.9848, 622.2049, 472.86023]

X1, Y1, X2, Y2 = rect

polygon = [(X1, Y1), (X2, Y1), (X2, Y2), (X1, Y2)]

EDIT:

Minimale working code:

from shapely.geometry import Polygon
import matplotlib.pyplot as plt

rect = [572.71063, 453.9848, 622.2049, 472.86023]

X1, Y1, X2, Y2 = rect

polygon = [(X1, Y1), (X2, Y1), (X2, Y2), (X1, Y2)]

p = Polygon(polygon)

x, y = p.exterior.xy

plt.plot(x, y)
plt.show()

Result:

在此处输入图像描述

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