0

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)):
        c.write({
            '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

0.12
0

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

But:

  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)):
        c.write({
            'geometry': mapping(polygon),
            'properties': {
                'id': poly_id,
                'ExtraData': polygon['properties']['ExtraData']
            },
        })
6
  • 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 Nov 5 '19 at 19:50
  • Read and write from the same loop so you have access to the source feature and its original properties. – mikewatt Nov 5 '19 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 Nov 6 '19 at 1:11
  • @Messak, is the source file a shapefile? – countrytoast Nov 6 '19 at 14:35
  • Yes, it is a shapefile – Messak Nov 6 '19 at 14:50
0

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']
        coord_lst.append(coords)
        extraData_lst.append(extraData)
    else:
        pass

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:
        c.write({
        '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, privacy policy and cookie policy

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