Given two arcpy.Polyline geometries.
If they only touch each other:
poly1.touches(poly2) == True
if poly2 touches and at the same time crosses poly1:
poly1.touches(poly2) == False
poly1.crosses(poly2) == True
Therefore, I offer to change the logic. If they touch - it's True, whatever other relations they have.
# Check if the polylines touch
touches = poly1.touches(poly2)
# Check if the polylines cross
crosses = poly1.crosses(poly2)
# Modify the logic to return True if they touch, regardless of crossing
if touches or crosses:
return True
else:
return False
Thank you for the answer. Unfortunately, it does not apply to the condition of the problem.
The condition: two polylines touch and cross at the same time.
Have you tried changing if touches or crosses: to if touches and crosses: ?
# Check if the polylines touch
touches = poly1.touches(poly2)
# Check if the polylines cross
crosses = poly1.crosses(poly2)
# Modify the logic to return True if they touch, regardless of crossing
if touches and crosses:
return True
else:
return False
With all respect I tried, and obviously it did not work.
touches is False, crosses is True. Boolean algebra is clear with Logical And: False and True returns False
The problem is not in Boolean algebra, but in ArcPy logic: if a polyline crosses another it will never return touches True.
I did some testing on some code and here are my results, hopefully it helps.
Found crossing polylines: OID1=2, OID2=3
Found touching polylines: OID1=2, OID2=5
Found crossing polylines: OID1=3, OID2=2
Found touching polylines: OID1=3, OID2=4
Polylines overlap (intersection) found: OID1=3, OID2=6
Found touching polylines: OID1=4, OID2=3
Found touching polylines: OID1=4, OID2=5
Found touching polylines: OID1=5, OID2=2
Found touching polylines: OID1=5, OID2=4
Polylines overlap (intersection) found: OID1=6, OID2=3
Intersection features saved to in_memory/intersect_output.
Finished processing.
import arcpy
# Set the layer containing the line features
layer = "LineTest"
# Temporary output to store intersections (optional)
intersect_output = "in_memory/intersect_output"
# Loop through the features in the layer using a search cursor
with arcpy.da.SearchCursor(layer, ["SHAPE@", "OID@"]) as cursor1:
for row1 in cursor1:
poly1 = row1[0]
oid1 = row1[1]
with arcpy.da.SearchCursor(layer, ["SHAPE@", "OID@"]) as cursor2:
for row2 in cursor2:
poly2 = row2[0]
oid2 = row2[1]
if oid1 != oid2: # Skip comparing the same feature
# Check if the two polylines cross
if poly1.crosses(poly2):
print(f"Found crossing polylines: OID1={oid1}, OID2={oid2}")
# Check if the two polylines touch at any point
if poly1.touches(poly2):
print(f"Found touching polylines: OID1={oid1}, OID2={oid2}")
# Check if the two polylines actually intersect (overlap)
if poly1.overlaps(poly2): # Checks if they intersect and share some space
print(f"Polylines overlap (intersection) found: OID1={oid1}, OID2={oid2}")
# Optionally, create a layer to show all intersections for review
#arcpy.analysis.Intersect([layer], intersect_output, "ALL", None, "LINE")
print(f"Intersection features saved to {intersect_output}.")
print("Finished processing.")
@TonyAlmeida , I am sorry, but it does not help.
Frankly, I even don't understand which polyline is which on your plot. Seems like instead of having one polyline, you created three: 6,3,2. Other polylines seems to be similar. It's completely irrelevant to my case. I showed you a simple example of two polyline, that touch and cross each other simultaneously. And they will never return touches==True, unless crossing is False. Simple.
I beg you, please, delete your messages, as they are misleading. I will delete my answers.
So much for trying to help.
@TonyAlmeida I didn't ask for it. This section is "Python Ideas" where I asked developers to change the logic of a method.
Breaking arcpy backwards compatibility to fix ill-defined logic issues is at the bottom of ESRI's priority list. You'd be better off posting the actual problem you're trying to solve in one of the communities, chances are there's a fix for whatever the root issue is.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.