I have a series of contour linestrings some are closed and some are not. The linestring values are between -1 and 1. I am trying to create polygons from the linestrings if they close and keep the value. I have asked this question similar question before and haven't been able to find a solution. because the polylines are contours, there will be donut polygons. The code below will generate the correct polygons, but cannot get the 'ExtraData' value into the newly created polygons.

polylines = fiona.open(input_file)
geom = [x['geometry'] for x in polylines]
coords = [x['coordinates']for x in geom[:-1]]

# {{'ExtraData': '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)):
            'geometry': mapping(polygon),
            'properties': {'id': poly_id},

there is no extradata ouput in the coords/polygon. now only coordinates like

POLYGON ((3102900.92 23355541.16, 3102900.900615673 23355541.220965....) 

but the extradata is like


2 Answers 2


I'm not too familiar with the technologies you're using...


  1. shouldn't your schema include the "ExtraData" field in the properties object?
  2. If the source has a ExtraData property, you should access that in your for loop and set it on the new polygon features.

what about-

schema =  {'geometry': 'Polygon','properties': {'id': 'int', 'ExtraData': 'float'}}
with fiona.open(output_file, 'w', 'ESRI Shapefile', schema) as c:
    for poly_id,polygon in enumerate(polygonize(coords)):
            'geometry': mapping(polygon),
            'properties': {
                'id': poly_id,
                'ExtraData': polygon['properties']['ExtraData']
  • hmm, right, but it's not available in polygon anymore for whatever reason now only the coords are there POLYGON ((3102900.92 23355541.16, 3102900.900615673 23355541.220965....) so i need a way to get the extradata value back in there
    – Messak
    Commented Nov 5, 2019 at 19:50
  • Read and write from the same loop so you have access to the source feature and its original properties.
    – mikewatt
    Commented Nov 5, 2019 at 22:27
  • Mike can you give an example of that converting the linestring geometry to polygon geometry while keeping attributes in the same loop?
    – Messak
    Commented Nov 6, 2019 at 1:11
  • @Messak, is the source file a shapefile? Commented Nov 6, 2019 at 14:35
  • Yes, it is a shapefile
    – Messak
    Commented Nov 6, 2019 at 14:50

So, to answer my own question in case I forget how I did it and need to come back.

I created two lists one for the coords and the other for the data. Then i checked to make sure the polylines are closed and only keep those. Then i polygonize to coords and zip the two lists together and loop through the zipped lists and write them to my shapefile

coord_lst = []
extraData_lst = []

for line in polylines:
    if (line['geometry']['coordinates'][0] == line['geometry']['coordinates'][-1]):
        coords = line['geometry']['coordinates']
        extraData = line['properties']['extraData']

d = polygonize(coord_lst)
b = zip(d, extraData_lst)

coordlst = None
ndvi_lst = None
schema =  {'geometry': 'Polygon','properties': {'extraData': 'float'}}
with fiona.open(outfc, 'w', crs=from_epsg(2278), driver='ESRI Shapefile', schema=schema) as c:
    for i, j in b:
        'geometry': mapping(i),
        'properties': {'extraData':j},
print ("finished")

This seems to has been a working solution.

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.