exterior = [(2, 8), (7, 8), (7, 3), (2, 3), (2, 8)]
interior = [(4, 7), (4, 5), (6, 5), (6, 7), (4, 7)]
poly = Polygon(exterior, holes=[interior])
# numbers of holes
len(poly.interiors)
1
# therefore
print(list(poly.interiors[0].coords))
[(4.0, 7.0), (4.0, 5.0), (6.0, 5.0), (6.0, 7.0), (4.0, 7.0)]
#
print(poly.interiors[0].coords[:])
[(4.0, 7.0), (4.0, 5.0), (6.0, 5.0), (6.0, 7.0), (4.0, 7.0)]
And
for coord in poly.interiors[0].coords[:]:
print(coord)
(4.0, 7.0)
(4.0, 5.0)
(6.0, 5.0)
(6.0, 7.0)
(4.0, 7.0)
# or
# first coordinate
poly.interiors[0].coords[:][0]
(4.0, 7.0)
# second coordinate
poly.interiors[0].coords[:][1]
(4.0, 5.0)
....
type(poly.interiors[0])
<class 'shapely.geometry.polygon.LinearRing'>
New
There is a problem with your solution: oring is a simple 1D list therefore you can iterate iterate over a list by one value
for i in (1,2):
print(i,)
1,2
But not by more:
for x,y in (1,2):
print(x,y)
TypeError: 'int' object is not iterable
The solution is (destructuring the list as variables)
x,y = (1,2) # len = 2
Therefore, with geometry.interior as a list of Polygons
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])
To extract individual x,y
# first coordinate of the first interior polygon
x,y = poly.interiors[0].coords[:][0]
print(x,y)
x: 4.0 y: 7.0
# third coordinate of the second interior polygon
x,y = poly.interiors[1].coords[:][2]
print("x:",x,"y:",y)
x: 4.0 y: 4.0
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
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)
for u in i: print(u) break---followingfor i in interior.coords:not produce one (x, y) pair? The print statement is the entire LINEARRING. How do I extract each coordinte pair from a LINEARRING? – arkriger Apr 18 '22 at 07:19