Home > Back-end >  How to convert the following coordinates to 'shapely' polygon?
How to convert the following coordinates to 'shapely' polygon?

Time:05-20

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?

CodePudding user response:

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:

enter image description here

  • Related