Search code examples
pythonpolygongeopandasmultipolygons

Rotate a multipolygon without changing the inner spatial relation


I have a multipolygon shapefile that i want to rotate. I can do the rotation but the problem is that the roatation changes the inner vertices. This creates overlap of polygon which i dont want.

This is what i have tried.

import geopandas as gpd
input_poly_path = "poly3.shp"
gdf = gpd.read_file(input_poly_path)
explode = gdf.explode(ignore_index=True)
ex = explode.rotate(-90, origin="centroid")
g = gpd.GeoDataFrame(columns=['geometry'], geometry='geometry')
g["geometry"] = ex

Original polygon Rotated polygon

https://drive.google.com/drive/folders/1HJpnNL-iXU_rReQzVcDGuyWZ8IjciKP8?usp=drive_link Link to the polygon


Solution

  • IIUC, you need to pass the centroid of the unary_union as the origin of the rotation :

    out = gpd.GeoDataFrame(
        geometry=gdf.rotate(
            angle=-90, origin=list(gdf.unary_union.centroid.coords)[0]
        )
    )
    

    NB: There is no need to explode the geometry, because you do not have MultiPolygons.

    enter image description here

    Used input (gdf) :

    import geopandas as gpd
    
    # download from Google Drive
    input_poly_path = (
        "C:/Users/Timeless/Downloads/"
        "share-20240330T125957Z-001.zip!share"
    )
    
    gdf = gpd.read_file(input_poly_path, engine="pyogrio")