I have a series of polylines(contours) that i want to convert to polygons using Python (GeoPandas, Fiona, Shapely). Because these are contours, there will be donut polygons, can anyone point me to how to do this? Each contour is a polygon, with there is no overlap of polygonsdonut polygons are polygons with holes in them. the holes are other polygons that have been created by inner contours
my non working code
def polyline_to_polygon(input_file, output_file):
polylines = fiona.open(input_file)
geom = [x['geometry'] for x in polylines]
coords = [x['coordinates']for x in geom[:-1]]
# 'ndviChange': 'float'}} should be carried over from the polyline
schema = {'geometry': 'Polygon','properties': {'id': 'int'}}
with fiona.open(output_file, 'w', 'ESRI Shapefile', schema) as c:
for poly_id,polygon in enumerate(polygonize(coords)):
c.write({
'geometry': mapping(polygon),
'properties': {'id': poly_id},
})
This works to generate the correct polygons, but doesn't include the ndvi value in the output shapefile. So there should be a ndviChange value in the polygon. it is in the polyline fc but doesn't carry over.
Hicham, i tried that and received
Must have equal len keys and value when setting with an iterable
gdf.loc[index, 'geometry'] = gdf.loc[index, 'geometry'].difference(gdf.loc[index_, 'geometry'])