Skip to main content
5 of 5
added 113 characters in body
nanunga
  • 533
  • 2
  • 11

Flipping line that touches certain points using ArcPy

I have a layer of points and a layer of lines (shapefiles), that have been split so the lines have 2 vertices. I would require to change the direction of those lines (flip), only if they touch (intersect) one point. There are no lines with more than 2 vertices, There are no 2 or more points in the same position. I have seen several codes but in this site and I have tried to adapt them to my case, but not succeed. This code runs without error but it does nothing.

I am using ArcMap 10.6.1. Standard License

enter image description here

points = []
with arcpy.da.SearchCursor(layerP, "SHAPE@") as p_cur:
    for row in p_cur:
        points.append(row[0])

lines_to_flip = []
sr = arcpy.Describe(layerD).spatialReference
##try:
with arcpy.da.UpdateCursor(layerD, ['OID@', 'SHAPE@']) as dropCursor:
    for dropRow in dropCursor:
        print str(dropRow[0])
        shpLine = dropRow[1]      
        shpLinePart = shpLine.getPart(0)
        
        first_point = shpLine.firstPoint
        last_point = shpLine.lastPoint
        for pt in points:
            if pt == first_point: # or pt == last_point:
                rPnts=arcpy.Array()
                for i in range(len(shpLinePart)): rPnts.append(shpLinePart[len(shpLinePart)-i-1])
                rPoly =arcpy.Polyline(rPnts)
                dropRow[1].shape= rPoly
                dropCursor.updateRow(dropRow)
                print "Updated"
nanunga
  • 533
  • 2
  • 11