2

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?

1 Answer 1

2

Try this piece of code:

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])

for i, interior in enumerate(poly.interiors):
    for pair in list(interior.coords):
        print(i, interior, pair)

that results in:

0 LINEARRING (4 7, 4 5, 6 5, 6 7, 4 7) (4.0, 7.0)
0 LINEARRING (4 7, 4 5, 6 5, 6 7, 4 7) (4.0, 5.0)
0 LINEARRING (4 7, 4 5, 6 5, 6 7, 4 7) (6.0, 5.0)
0 LINEARRING (4 7, 4 5, 6 5, 6 7, 4 7) (6.0, 7.0)
0 LINEARRING (4 7, 4 5, 6 5, 6 7, 4 7) (4.0, 7.0)
1 LINEARRING (3 5, 3 4, 4 4, 4 5, 3 5) (3.0, 5.0)
1 LINEARRING (3 5, 3 4, 4 4, 4 5, 3 5) (3.0, 4.0)
1 LINEARRING (3 5, 3 4, 4 4, 4 5, 3 5) (4.0, 4.0)
1 LINEARRING (3 5, 3 4, 4 4, 4 5, 3 5) (4.0, 5.0)
1 LINEARRING (3 5, 3 4, 4 4, 4 5, 3 5) (3.0, 5.0)
2
  • Thank you @Taras - I want to iterate one interior and each coordinate pair one-after-the-other; then the next interior and each coordinate pair one-after-the-other.
    – arkriger
    Jun 9 at 12:36
  • Thank you @Taras.
    – arkriger
    Jun 9 at 13:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.