How do you get/extract the points that define a shapely
polygon?
Thanks!
Example of a shapely polygon
JavaScript
x
8
1
from shapely.geometry import Polygon
2
3
# Create polygon from lists of points
4
x = [list of x vals]
5
y = [list of y vals]
6
7
polygon = Polygon(x,y)
8
Advertisement
Answer
The trick is to use a combination of the Polygon
class methods:
JavaScript
1
15
15
1
from shapely.geometry import Polygon
2
3
# Create polygon from lists of points
4
x = [0.0, 0.0, 1.0, 1.0, 0.0]
5
y = [0.0, 1.0, 1.0, 0.0, 0.0]
6
7
poly = Polygon(zip(x,y))
8
9
# Extract the point values that define the perimeter of the polygon
10
xx, yy = poly.exterior.coords.xy
11
12
# Note above return values are of type `array.array`
13
assert x == xx.tolist()
14
assert y == yy.tolist()
15