Connect polyline endpoints between features

4618
4
Jump to solution
07-22-2014 08:11 AM
JamesCrandall
MVP Frequent Contributor

Not sure how best to word this.  I am looking for arcpy solutions/ideas on connecting the endpoints of stacked polyine features together in order to build it into a polygon feature class.

This task done manually is quite simple: just add a new polyline(s) from one start point to the startpoint of the polyline below it and continue this until an entire "side" of the stacked polylines are connected.  Then it's just a matter of doing this same thing to the other "side" on the endpoints of each polyline feature.  See:

stackedpolylines.png

0 Kudos
1 Solution

Accepted Solutions
JamesCrandall
MVP Frequent Contributor

Thanks but I really am unintersted in model builder and need to implement this into a Python Add-In.  Here's what I came up with that works just fine:


 ptarrL = arcpy.Array()
ptarrR = arcpy.Array()

        for l in listoflayers:
            for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.name == str(l):
                    print "layer: " + str(lyr.name)
                    with arcpy.da.SearchCursor(lyr, ["SHAPE@"]) as lyrCur:
                        for row in lyrCur:
                            xL, yL = row[0].firstPoint.X,  row[0].firstPoint.Y
                            xR, yR = row[0].lastPoint.X, row[0].lastPoint.Y
                            ptarrL.add(arcpy.Point(xL, yL))
                            ptarrR.add(arcpy.Point(xR, yR))


                    with arcpy.da.InsertCursor(lyr, ["SHAPE@"]) as inscur:
                        polyline = arcpy.Polyline(ptarrL)
                        inscur.insertRow([polyline])
                    with arcpy.da.InsertCursor(lyr, ["SHAPE@"]) as inscur:
                        polyline = arcpy.Polyline(ptarrR)
                        inscur.insertRow([polyline])    
                    del inscur, lyrCur

View solution in original post

0 Kudos
4 Replies
DuncanHornby
MVP Notable Contributor

So in your example you are trying to create that green polyline, in this case it is compose of only 3 vertices?

OK several questions:

  1. are your blue polylines whose end vertices you are trying to connect always stacked precisely one above each other, i.e. their X coordinates are always the same?
  2. Which line do you start from, the very top or the bottom?
  3. Are these bunches of lines all over the place in a single datasets or is it a bunch per shapefile?
0 Kudos
JamesCrandall
MVP Frequent Contributor


Yes, I would like to create the green line you see between each feature.

1. Yes, always stacked one above the other.

2. Doesn't matter really.  The next step would simply run this polyline FC thru a ToPolygon conversion.

3. Each "set" of polylines is a single feature class.  The 3 polylines you see in the image is 1 FC.

Thanks!

0 Kudos
DuncanHornby
MVP Notable Contributor

This can be done with a very simple model (if you have the Advanced license, which you do not state). The model is shown below with the final result (green polyline)

Capture.PNG

The only assumption is that the order of the polylines in the shapefile dictates the order of the extracted nodes which is the order the vertices are in your green line.

0 Kudos
JamesCrandall
MVP Frequent Contributor

Thanks but I really am unintersted in model builder and need to implement this into a Python Add-In.  Here's what I came up with that works just fine:


 ptarrL = arcpy.Array()
ptarrR = arcpy.Array()

        for l in listoflayers:
            for lyr in arcpy.mapping.ListLayers(mxd):
                if lyr.name == str(l):
                    print "layer: " + str(lyr.name)
                    with arcpy.da.SearchCursor(lyr, ["SHAPE@"]) as lyrCur:
                        for row in lyrCur:
                            xL, yL = row[0].firstPoint.X,  row[0].firstPoint.Y
                            xR, yR = row[0].lastPoint.X, row[0].lastPoint.Y
                            ptarrL.add(arcpy.Point(xL, yL))
                            ptarrR.add(arcpy.Point(xR, yR))


                    with arcpy.da.InsertCursor(lyr, ["SHAPE@"]) as inscur:
                        polyline = arcpy.Polyline(ptarrL)
                        inscur.insertRow([polyline])
                    with arcpy.da.InsertCursor(lyr, ["SHAPE@"]) as inscur:
                        polyline = arcpy.Polyline(ptarrR)
                        inscur.insertRow([polyline])    
                    del inscur, lyrCur

0 Kudos