3

enter image description hereI 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'])
0

1 Answer 1

1

A simpler way would be to use geopandas and shapely:

 import geopandas as gpd
 import shapely.geometry as geom
 def polyline_to_polygone(input_file, output_file):
     gdf = gpd.read_file(input_file)
     gdf['geometry'] = gdf['geometry'].apply(lambda x: geom.Polygon(x.coords))
     # in order to have donut geometries, subset polygons from each others
     for index in list(gdf.index):
           indices = list(gdf.index)
           indices.remove(index)
           for index_ in indices:
                try:
                    gdf.loc[index, 'geometry'] = gdf.loc[index, 'geometry'].difference(gdf.loc[index_, 'geometry'])
                except ValueError:
                    pass
     gdf.to_file(output_file)
6
  • This will create overlapping polygons, it will not create holes in them to get "donut-like" Polygons OP wanted. Commented Sep 5, 2019 at 14:23
  • @martinfleis It seems to me that the geometries would be the same as what the code provided by OP generates, and it lacked only the part where it gets the ndvi attribute Commented Sep 5, 2019 at 14:36
  • 1
    That is true, but that is not intended result as far as I understand the question. Commented Sep 5, 2019 at 14:40
  • 1
    @martinfleis yes indeed, I've edited it to generate donut geometries Commented Sep 5, 2019 at 14:56
  • @Messak the error is generated when the difference geometry is empty, I've edited the code to pass it Commented Sep 12, 2019 at 14:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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