Skip to content
Advertisement

Extract points/coordinates from a polygon in Shapely

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)

Advertisement

Answer

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()
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement