How to code Lines to polygon?

3733
15
Jump to solution
07-25-2016 04:31 PM
TieshengWu
Occasional Contributor

Hi all I want to convert lines feature that is directioned at random to polygons by python script, but my arcmap has no a license of 'feature to polygon'. How to realize it?

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

And here is the code to cut all the pie polygons with the fault polylines:

import arcpy

def main():
    fc_pie = r'C:\GeoNet\SplitBalls\shp\focal_sml_pie.shp'
    fc_faults = r'C:\GeoNet\SplitBalls\shp\focal_sml_faults.shp'
    fc_result = r'C:\GeoNet\SplitBalls\shp\pie_pieces01.shp'

    arcpy.env.overwriteOutput = True

    # create dct with faults
    dct_faults = {r[0]: r[1] for r in arcpy.da.SearchCursor(fc_faults, ('OID@', 'SHAPE@'))}

    # loop through pie polygons
    lst_polygons = []
    with arcpy.da.SearchCursor(fc_pie, ('SHAPE@')) as curs_pie:
        for row_pie in curs_pie:
            polygon = row_pie[0]

            # get list of faults for current pie
            lst_faults_pie = getFaults(polygon, dct_faults.values())

            # cut pie based on faults
            lst_polygons_pie = [polygon]
            for fault in lst_faults_pie:
                lst_polygons_pie = splitPolygon(lst_polygons_pie, fault)

            # add polygons to list
            lst_polygons.extend(lst_polygons_pie)

    # write the pie pieces to a new featureclass
    arcpy.CopyFeatures_management(lst_polygons, fc_result)


def getFaults(polygon, lst_faults):
    '''Get list of fault polylines for pie polygon'''
    lst_faults_pie = []
    for fault in lst_faults:
        mid_pnt = fault.positionAlongLine(0.5, True)
        if polygon.contains(mid_pnt):
            lst_faults_pie.append(fault)
    return lst_faults_pie


def splitPolygon(lst_polygons, polyline):
    '''split pie polygon with fault polyline'''
    lst_pols = []
    for polygon in lst_polygons:
        try:
            lst_pols += polygon.cut(polyline)
        except Exception as e:
            print "Error:", e
            pass
    return lst_pols


if __name__ == '__main__':
    main()

Also find attached the resulting shapefile.

View solution in original post

15 Replies
DanPatterson_Retired
MVP Emeritus

are you familiar with ... Writing geometries—Help | ArcGIS for Desktop

and insertcursors? do you have a way of orientating your polylines so that they connect? or are they disconnected and you wish to get a bounding container from the features?  Perhaps an image of what you are working with might help, and a statement of your python and arcpy knowledge

TieshengWu
Occasional Contributor

Thanks for  your reply. What i want is  to make a code run at standard arcmap version which using  a simple script   'feature to polygon' or 'split polygon by lines '  that make things perfectly at a advanced version.

0 Kudos
DanPatterson_Retired
MVP Emeritus

I understand your request, but the remaining questions in my post need to be answered.  Replicating the functionality of tools that are only available at an advanced license level is not simple... otherwise they would be available at the lower level already.

XanderBakker
Esri Esteemed Contributor

I have worked on a toolbox with functionality that is normally available with the Advanced license level only to make it available at a Basic license. However, the tool to convert unstructured lines to polygons, creating intersections, etc, is one of the tools that I will not even try to implement. As Dan mentions, this tool is very complex and not easy to implement with arcpy and only the Basic license level.

TieshengWu
Occasional Contributor

Thank you all. Now I know it's not easy to work out  a general arcpy codes for 'lines to polygon' , and now try to use POLYGON method to construct polygons from theose structured points array directly, rather than construct lines at first, then convert this line feature to polygons.

0 Kudos
GrantHerbert
Occasional Contributor II

There are also third party tools like ETGeowizards and Xtools that have this sort of functionality.

0 Kudos
TieshengWu
Occasional Contributor

Hi, thanks for the suggestion. Actually I need the function in an arcpy appliction.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Could you post an example of some lines that you wish to convert to polygons, just to see how complex the situation is? If not too complex, there are ways to do this with arcpy and a Basic license.

0 Kudos
TieshengWu
Occasional Contributor

I have many balls must be divided by the red lines of varied oriented. In my original codes, construct balls and lines in memory at first, then copy them as polygon feature and line feature seperately, 'arcpy.FeatureToPolygon_management([balls,lines],"in_memory")' finally.  It's simple. Don't care where and how the lines intersect with balls. But need advanced license.

With POLYGON method, may be the intersect or the nearest points between lines, lines and ball,  must be got, then regroup these  point arrays to form polygons. Don't have idea yet about what's the best way to get the 'intersect' points (all the geometries are constructed from points, they may have no common point) .

ball.JPG

0 Kudos