How to check geometry type (MultiPolygon or Polygon) with Geopandas ?

Published: October 02, 2023

Tags: Python; Geopandas;

DMCA.com Protection Status

Geopandas is a powerful Python library used for working with geospatial data. It allows users to read, write, and manipulate geographic data, making it an essential tool for anyone working with location-based information.

Checking Geometry Type

One of the important tasks when working with geospatial data is checking the geometry type of your data. This information is crucial for performing various operations, as different geometry types require different methods and tools.

In Geopandas, there are two main geometry types - MultiPolygon and Polygon. A MultiPolygon is made up of multiple polygons, while a Polygon represents a single area on a map.

To check Geometry Type a solution is to use geopandas.GeoSeries.geom_type

geom_type

Example

Let's consider for example the following dataframe (see How to retrieve country name for a given latitude and longitude using geopandas and naturalearthdata ?):

import geopandas as gpd
import pandas as pd

from shapely.geometry import Point

df = gpd.read_file('110m_cultural')

cols = ['NAME', 'geometry']

df[cols]

How to retrieve country name for a given latitude and longitude using geopandas and naturalearthdata ?
How to retrieve country name for a given latitude and longitude using geopandas and naturalearthdata ?

Note that

df['geometry'].geom_type

gives

0      MultiPolygon
1           Polygon
2           Polygon
3      MultiPolygon
4      MultiPolygon
           ...     
172         Polygon
173         Polygon
174         Polygon
175         Polygon
176         Polygon
Length: 177, dtype: object

and

df['geometry'].geom_type.value_counts()

gives

Polygon         148
MultiPolygon     29
dtype: int64

Perform iteration on each row:

for index, row in df.iterrows():
    if 'MultiPolygon' in row['geometry'].geom_type:
        print( 'MultiPolygon')
    if 'Polygon' in row['geometry'].geom_type:
        print( 'Polygon')

gives

MultiPolygon
Polygon
Polygon
Polygon
MultiPolygon
Polygon
MultiPolygon
.
.
.

References

Image

of