This question is related to Access each (x, y) within Multipart gdf.geometry.interiors.
With a MultiPart geometry.interiors:
from shapely.geometry import Polygon
exterior = [(2, 8), (7, 8), (7, 3), (2, 3), (2, 8)]
interior = [(4, 7), (4, 5), (6, 5), (6, 7), (4, 7)]
interior2 = [(3, 5), (3, 4), (4, 4), (4, 5), (3, 5)]
poly = Polygon(exterior, holes=[interior,interior2])
You could access all interior coordinates with either:
# all coordinates
for i, interior in enumerate(poly.interiors):
print("interior: ",i)
for oring in list(interior.coords):
x,y = oring # oring is a list of floats
print("oring:",oring,"x:",x,"y: ",y)
interior: 0
oring:(4.0, 7.0) x: 4.0 y: 7.0
oring:(4.0, 5.0) x: 4.0 y: 5.0
oring:(6.0, 5.0) x: 6.0 y: 5.0
oring:(6.0, 7.0) x: 6.0 y: 7.0
oring:(4.0, 7.0) x: 4.0 y: 7.0
interior: 1
oring:(3.0, 5.0) x: 3.0 y: 5.0
oring:(3.0, 4.0) x: 3.0 y: 4.0
oring:(4.0, 4.0) x: 4.0 y: 4.0
oring: 4.0, 5.0) x: 4.0 y: 5.0
oring:(3.0, 5.0) x: 3.0 y: 5.0
To extract lists of x and y
or
for i, interior in enumerate(poly.interiors):
ppx, ppy = zip(*interior.coords)
print("interior:",i,"ppx:",ppx,"ppy:",ppy)
interior: 0 ppx: (4.0, 4.0, 6.0, 6.0, 4.0) ppy: (7.0, 5.0, 5.0, 7.0, 7.0)
interior: 1 ppx: (3.0, 3.0, 4.0, 4.0, 3.0) ppy: (5.0, 4.0, 4.0, 5.0, 5.0)
I want to iterate each interior, extract one coordinate pair and perform some function so:
for i, interior in enumerate(poly.interiors):
ppx, ppy = zip(*interior.coords)
for co in zip(ppx, ppy):
print(co)
gives:
(4, 7)
(3, 5)
I only want (4, 7). How do I iterate each interior and each coordinate pair one-after-the-other?