List all polygon vertices coordinates using geopandas

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 geopandas as gpd
import numpy as np

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

enter image description here

Result:

[[[0.0, 0.0],  #1
  [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
  [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
  [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 geopandas as gpd
import numpy as np

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
        [0., 4.],  #2
        [7., 4.],  #3
        [7., 0.],  #4
        [0., 0.]], #1

       [[1., 1.],  #5
        [3., 1.],  #6
        [3., 3.],  #7
        [1., 3.],  #8
        [1., 1.]], #5                     

       [[4., 3.],  #9
        [4., 1.],  #10
        [6., 1.],  #11
        [6., 3.],  #12
        [4., 3.]]])#9

Solution 3: shapely.geometry.mapping function.

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]
all_coords = mapping(g[0])["coordinates"] # for first feature/row
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

One way might be to convert to json then read back to dictionary:

import geopandas as gpd
import numpy as np
import json

df = gpd.read_file('/home/bera/geodata/Rectangle_with_hole.shp')
g = json.loads(df.to_json())

coords = np.array(g['features'][0]['geometry']['coordinates'])

Tags:

Geopandas