Search code examples
pythonpython-3.xshapefilegeopandas

How to write a list of Polygons to a .shp file in Python


I have outmulti being a list of 389 Polygon & Multipolygon objects in Python. I want to write outmulti to an shp file and then be able to actually see all the Polygons and Multipolygons in the space.

I am stuck here.. How can I achieve that?

Thanks in advance


Solution

  • Convert your list to geopandas.GeoDataFrame and save that. If you know the projection, you should pass it to GeoDataFrame as well.

    import geopandas
    
    gdf = geopandas.GeoDataFrame(geometry=outmulti)
    # gdf = geopandas.GeoDataFrame(geometry=outmulti, crs="EPSG:4326") # if you have coordinates in degrees
    gdf.to_file("my_file.shp")
    

    To see your geometries in space, you can plot them with geopandas.

    gdf.plot()
    

    Check the documentation for details.