2

I create a list of Polygons and MultiPolygons outmulti as follows:

for pol in polygonToMulti:
    for pol2 in tre:
        if pol.intersects(pol2)==True:
            counter = counter+1
            # If they intersect, create a new polygon that is
            # essentially pol minus the intersection
            nonoverlap = (pol.symmetric_difference(pol2)).difference(pol2)
            outmulti.append(nonoverlap)


finalpol = MultiPolygon(outmulti)

I now want to save these Polygons and MultiPolygons of the outmulti list in a shp file & then be able to plot and visualize all the objects. Firstly, I thought of keeping the values of wkt into a different list like this:

list = []
for i in range(len(outmulti)):
    list.append(outmulti[i].wkt)

and then I thought of creating a GeoDataFrame having as the geometry attribute the above list list.

gdf3=gpd.GeoDataFrame(geometry=list)

however this gives a TypeError saying it's not a valid geometry object.

Secondly, I tried creating a GeoDataFrame with a column name "geometry" and then writing all the wkt values of outmulti into that column and then saving the final GeoDataFrame into a file like:

#Make list of Polygons -> Geo Data Frame
gdf3=gpd.GeoDataFrame(columns={"geometry"})

for i in range(len(outmulti)):
    temp = gpd.GeoSeries(outmulti[i].wkt)
    gdf3= gdf3.append({'geometry':temp}, ignore_index=True)

    gpd.geometry


gdf3.to_file(filename='dPolygons.shp', driver='ESRI Shapefile')

But in that case I get the error:


AttributeError: 'Series' object has no attribute 'has_z'

I guess this is because the shp file must have some specific attributes in order to be created, but I cannot understand which and where to get those from.

2
  • Welcome to Geographic Information Systems! Welcome to GIS SE! We're a little different from other sites; this isn't a discussion forum but a Q&A site. Your questions should as much as possible describe not just what you want to do, but precisely what you have tried and where you are stuck trying that. Please check out our short tour for more about how the site works
    – Ian Turton
    Apr 16 at 8:58
  • @IanTurton thanks for the clarification. I edited the question. Is that better now? Apr 16 at 9:51
2

You already have the geometry as a shapely multipolygon/list of polygons. Add it directly to the geodataframe.

gdf3 = gpd.GeoDataFrame(geometry=[finalpol])  # Note GeoDataFrame geometry requires a list
gdf3.to_file(filename='dPolygons.shp', driver='ESRI Shapefile')

# Or

gdf3 = gpd.GeoDataFrame(geometry=outmulti)  # outmulti is already a list
gdf3.to_file(filename='dPolygons.shp', driver='ESRI Shapefile')

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.