8

Is there any method to count number of vertices of polygon (or multipolygon etc.) that is in 'geometry' column in datatable, but using GeoPandas?

Example:

Let's say I have polygon

POLYGON ((552971.4480249976 155085.9494381994, 552975.0980249938 155093.2494382081, 552980.7980249878 155104.5994382233, 552981.8480249867 155106.6494382256, 552986.8670249796 155118.8284382395, 552993.9820249865 155114.5394382458, 552992.3430249887 155110.6504382399, 552989.1980249948 155100.8494382298, 552983.1480250009 155089.1494382163, 552982.6980250012 155088.5494382135, 552981.3860250067 155080.7984382049, 552971.4480249976 155085.9494381994))

After loading shapefile and iterating over lines of data using iterrows() method, how to count that this polygons is made from e.g. 41 points, another on 30 etc. Maybe there is some function like .length or .area?

2

3 Answers 3

12

goepandas uses shapely package for geometry. As far as I know, shapely has no any function that shows number of vertices like geometry.vertices or len(geometry) etc. But you can implicitly get the number of vertices of any geometry.

import geopandas as gp
df = gp.read_file("shapefile_path")

for i, row in df.iterrows():
    # It's better to check if multigeometry
    multi = row.geometry.type.startswith("Multi")

    if multi:
        n = 0
        # iterate over all parts of multigeometry
        for part in row.geometry:
            n += len(part.exterior.coords)
    else: # if single geometry like point, linestring or polygon
        n = len(row.geometry.exterior.coords)

    print(n)

If you want to add number of vertices as a column into df, try in that way:

import geopandas as gp
df = gp.read_file("shapefile_path")

n_vertices=[] ###

for i, row in df.iterrows():
    # It's better to check if multigeometry
    multi = row.geometry.type.startswith("Multi")

    if multi:
        n = 0
        # iterate over all parts of multigeometry
        for part in row.geometry:
            n += len(part.exterior.coords)
    else:
        n = len(row.geometry.exterior.coords)
    n_vertices.append(n) ###


df["n_vertices"] = n_vertices ###
df.to_file("new_shapefile_path") ###

# EXAMPLE
#                                           geometry        n_vertices
#  0    POLYGON ((3241688.46003131 4980597.749317063, ...   6
#  1    POLYGON ((3241575.693387136 4980461.03521121, ...   5

EDIT: This method just counting vertices of exterior ring(s), it doesn't take account of holes (interior ring(s)).

6
  • Thanks! This method is excellent, especially the part when you are checking for multipolygons, cause (i've checked that) it may cause problems. It works quite fast as well. [SOLVED]
    – Vilq
    Commented Jul 16, 2019 at 12:04
  • Note that this method fails to account for any interior rings.
    – RutgerH
    Commented Jul 16, 2019 at 12:53
  • @RutgerH Yes, just counting vertices of exterior ring, not interior's. Commented Jul 16, 2019 at 12:58
  • 1
    I changed for part in row.geometry: to for part in row.geometry.geoms: and this worked with geopandas '0.13.0' Commented Jun 7, 2023 at 19:34
  • @Cullen.Molitor I didn't test but afaik geoms works if geometry type is multipolygon. Commented Jun 8, 2023 at 7:07
8

I add an example, based on my comment using this reference

I don't know if you have a direct method but you can get what you want using a loop.(I'm using your polygon)

import geopandas as gpd
gdf = gpd.read_file(r"C:\Users\fran\Desktop\test.shp")

nodes=[]

for index, row in gdf.iterrows():
     for pt in list(row['geometry'].exterior.coords): 
        nodes.append(pt)

len(nodes)

And if we check the result with QGIS, we see that it is the same.

enter image description here

1
  • I did the same (mean checking in QGIS) and it works very accurate.
    – Vilq
    Commented Jul 16, 2019 at 12:07
1

Since geopandas v1 there is the count_coordinates() method: docs

from shapely.geometry import Polygon, LineString, Point

s = geopandas.GeoSeries(
    [
        LineString([(0, 0), (1, 1), (1, -1), (0, 1)]),
        LineString([(0, 0), (1, 1), (1, -1)]),
        Point(0, 0),
        Polygon([(10, 10), (10, 20), (20, 20), (20, 10), (10, 10)]),
        None
    ]
)
s.count_coordinates()

will result in:

0    4
1    3
2    1
3    5
4    0
dtype: int32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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