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.
Table of contents
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 gpdimport pandas as pdfrom shapely.geometry import Pointdf = gpd.read_file('110m_cultural')cols = ['NAME', 'geometry']df[cols]

Note that
df['geometry'].geom_type
gives
0 MultiPolygon1 Polygon2 Polygon3 MultiPolygon4 MultiPolygon...172 Polygon173 Polygon174 Polygon175 Polygon176 PolygonLength: 177, dtype: object
and
df['geometry'].geom_type.value_counts()
gives
Polygon 148MultiPolygon 29dtype: 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
MultiPolygonPolygonPolygonPolygonMultiPolygonPolygonMultiPolygon...
References
| Links | Site |
|---|---|
| Multi-part geometries will no longer be “sequences” (length, iterable, indexable) | shapely.readthedocs.io |
| geopandas.GeoSeries.geom_type | geopandas.org |
