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.