Given a GeoDataFrame where the
geometry column contains both Polygons and MultiPolygons.
How can I force the orientation (left-hand-rule or right-hand-rule) on these geometries?
The orient functionality is included within shapely as function in the
ops module from version
1.7a2 and onwards.
If you have this function you can do the following:
from shapely.ops import orient # version >=1.7a2 gdf.geometry = gdf.geometry.apply(orient, args=(-1,))
If you have an earlier version of shapely and cannot update to the latest version, try to emulate this new functionality with the following function:
from shapely.geometry.base import BaseMultipartGeometry from shapely.geometry.polygon import orient as orient_ from shapely.geometry import Polygon def orient(geom, sign=1.0): if isinstance(geom, BaseMultipartGeometry): return geom.__class__( list( map( lambda geom: orient(geom, sign), geom.geoms, ) ) ) if isinstance(geom, (Polygon,)): return orient_(geom, sign) return geom gdf.geometry = gdf.geometry.apply(orient, args=(-1,))