Not sure if one line method exists, but the following ways could work. (Solutions are for the first feature's geometry, and they are just for Polygon
, not for MultiPolygon
)
Solution 1: boundary
property of a polygon returns exterior and all interiors of the polygon.
import numpy as np
import geopandas as gpd
df = gpd.read_file('/home/bera/geodata/Rectangle_with_hole.shp')
g = [i for i in df.geometry]
all_coords = []
for b in g[0].boundary: # for first feature/row
coords = np.dstack(b.coords.xy).tolist()
all_coords.append(*coords)
all_coords

Result:
[[[0.0, 0.0], #1 #exterior
[0.0, 4.0], #2
[7.0, 4.0], #3
[7.0, 0.0], #4
[0.0, 0.0]], #1
[[1.0, 1.0], #5 #interior1
[3.0, 1.0], #6
[3.0, 3.0], #7
[1.0, 3.0], #8
[1.0, 1.0]], #5
[[4.0, 3.0], #9 #interior2
[4.0, 1.0], #10
[6.0, 1.0], #11
[6.0, 3.0], #12
[4.0, 3.0]]] #9
Solution 2: polygon.interiors
returns InteriorRingSequence
object which consists of LinearRing
objects.
import numpy as np
import geopandas as gpd
df = gpd.read_file('/home/bera/geodata/Rectangle_with_hole.shp')
g = [i for i in df.geometry]
x,y = g[0].exterior.coords.xy
all_coords = np.dstack((x,y)) ####
for interior in g[0].interiors: # for first feature/row
x, y = interior.coords.xy
coords = np.dstack((x,y))
all_coords = np.append(all_coords, coords, axis=0)
all_coords # or all_coords.tolist()
Result:
array([[[0., 0.], #1 #exterior
[0., 4.], #2
[7., 4.], #3
[7., 0.], #4
[0., 0.]], #1
[[1., 1.], #5 #interior1
[3., 1.], #6
[3., 3.], #7
[1., 3.], #8
[1., 1.]], #5
[[4., 3.], #9 #interior2
[4., 1.], #10
[6., 1.], #11
[6., 3.], #12
[4., 3.]]])#9
Solution 3: shapely.geometry.mapping
function returns the GeoJSON-like mapping of a geometric object.
import geopandas as gpd
from shapely.geometry import mapping
df = gpd.read_file('/home/bera/geodata/Rectangle_with_hole.shp')
g = [i for i in df.geometry]
geojson_ob = mapping(g[0]) # for first feature/row
all_coords = geojson_ob["coordinates"]
all_coords
Result:
(((0.0, 0.0), (0.0, 4.0), (7.0, 4.0), (7.0, 0.0), (0.0, 0.0)), #exterior
((1.0, 1.0), (3.0, 1.0), (3.0, 3.0), (1.0, 3.0), (1.0, 1.0)), #interior1
((4.0, 3.0), (4.0, 1.0), (6.0, 1.0), (6.0, 3.0), (4.0, 3.0))) #interior2