Extract points/coordinates from a polygon in Shapely

polygon, python, shapely

Solution

The trick is to use a combination of the `Polygon` class methods:

from shapely.geometry import Polygon

# Create polygon from lists of points
x = [0.0, 0.0, 1.0, 1.0, 0.0]
y = [0.0, 1.0, 1.0, 0.0, 0.0]

poly = Polygon(zip(x,y))

# Extract the point values that define the perimeter of the polygon
xx, yy = poly.exterior.coords.xy

# Note above return values are of type `array.array` 
assert x == xx.tolist()
assert y == yy.tolist()

If you would like them as coordinate pairs

assert tuple(poly.exterior.coords) == tuple(zip(x,y))

or as a `numpy` array

assert np.array_equal(np.array(poly.exterior.coords), np.asarray(tuple(zip(x,y))))

Problem

How do you get/extract the points that define a `shapely` polygon? Thanks! Example of a shapely polygon ``` from shapely.geometry import Polygon # Create polygon from lists of points x = [list of x vals] y = [list of y vals] polygon = Polygon(x,y) ```

Original source

Related problems