Lines to polygon tool? (reverse for Polygon To Line)

4582
1
01-15-2015 12:27 AM
LauriKajan
New Contributor

Hi all,

I'm looking for a geoprocessing tool to convert line layer to polygons.

My line layer is a result of a Polygon To Lines tool so it has attributes LEFT_FID and RIGHT_FID. So I want to generate those lines back to polygons with those FIDs.

Actually I'm simplifying the polygon boundaries before converting those back to polygons. The reason why I'm doing this is I need to simplify my polygons as lines together with other line layer to keep lines and polygons topologically connected.

This is also the reason why Feature To Polygons wont work with labels coming from polygon centroid points since after simplifying those centroids might not to be inside simplified polygon boundaries anymore.

Is there any ready made tools to create polygons based on LEFT_FID and RIGHT_FID fields?

I have also tought to solve this with arcpy.

arcpy.AddIndex_management(inputLines, left_fid, 'LEFT_FID_IDX')
arcpy.AddIndex_management(inputLines, right_fid, 'RIGHT_FID_IDX')

fids = set()
with SearchCursor(inputLines, [left_fid, right_fid]) as cursor:
    for i, row in enumerate(cursor, 0):
        if row[0] and row[0] != -1:
            fids.add(row[0])
        if row[1] and row[1] != -1:
            fids.add(row[1])

fl = arcpy.MakeFeatureLayer_management(inputLines, 'fl')
with InsertCursor(outputfc, ['ORIG_FID', 'SHAPE@']) as ic:
    for i, fid in enumerate(fids):
        arcpy.SelectLayerByAttribute_management(fl, '', '"{0}" = {fid} or "{1}" = {fid}'.format(left_fid, right_fid, fid=fid))
        geom = arcpy.FeatureToPolygon_management(fl, arcpy.Geometry())
        for g in geom:
            ic.insertRow((fid, g))

My script first reads all FIDs from line layer.

Then iterates all FIDs and then selects all lines containing current FID.

Feature To Polygon is then used to create polygons from selected lines.

This seems to be really slow. Is there someway to improve the performance of my script?

And this creates polygons also from holes of the original polygons.

Thank you for your help.

-Lauri

0 Kudos
1 Reply
BenRufenacht
New Contributor III

I am not sure if this solves your problem, but this is how I convert a polygon to lines for smoothing, then convert back to polygon with the same attributes using a spatial join with the original basins.

# Convert polygon to line
arcpy.PolygonToLine_management(basins,basinsLine, "IDENTIFY_NEIGHBORS")

# Smooth line
arcpy.SmoothLine_cartography(basinsLine, smoothLine, "PAEK", smoothTolerance)

# Simplify line
arcpy.SimplifyLine_cartography(smoothLine, simplifyLine, "POINT_REMOVE", simplifyTolerance, "", "NO_KEEP", "")

# Convert line back to polygon
arcpy.FeatureToPolygon_management(simplifyLine, smoothBasins, "", "ATTRIBUTES", "")

# Join attributes from original basins into new basins
arcpy.SpatialJoin_analysis(smoothBasins, basins, newBasins, "", "KEEP_ALL", "", "HAVE_THEIR_CENTER_IN")