1

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"
1
  • Try replacing if pt == first_point: with if pt.equals(first_point):
    – Hornbydd
    Commented Jan 13, 2023 at 17:56

1 Answer 1

1

As you don't describe your data sufficiently (a few images would have helped) I can immediately see flaws in your logic. What happens if a line has points at either end of the line, it would flip then flip again? Do you have the scenario of multiple polylines intersecting a single point? Do you have stacked points that lines intersect? Finally your code appears to be only considering the starting end of a polyline.

Your code needs to deal with all these scenarios if they exist.

Assuming you only ever have one point intersecting one end of a polyline then you could crush this code down to 2 lines.

First a select by location, followed by calling the flip line tool?

4
  • Thanks. I do not use select by location because I need to control the selection on the first point of the line features. Besides, I thought flip line tool would flip the entire layer, isn´t it?. I added an image.
    – nanunga
    Commented Jan 13, 2023 at 11:15
  • Like all tools, flip line will process selections first, or all data if no selection exists.
    – Hornbydd
    Commented Jan 13, 2023 at 11:18
  • @nanunga, Image is very helpful, in your comment above are you saying you want to flip a line if 1) it touches a point and 2) its "flowing away" from the point? If so that is important information you need to put in your question because as it stands you are asking to flip a line if only touches, you are not stating that the direction is also important. If this is correct you need to add that to the question.
    – Hornbydd
    Commented Jan 13, 2023 at 11:24
  • Also you seem to have more points than lines, you don't indicate how many lines there are that could potentially require flipping. If what you show is truly what you have do it manually! You need to be stating, your license level, size of problem (e.g. numbers of features), important rules and showing code/images to be a good question that gets a quick answer.
    – Hornbydd
    Commented Jan 13, 2023 at 11:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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