2
Line1_string=LineString([(1,4),(3,4),(3,3),(3.5,2),(4.5,1),(6,1)])

line2_string=LineString([(5,4),(2,1)])

these two objects have been created using LineString from shapely.geometry

I can find the intersection point using Line1_string.intersection(line2_string).coords, but I would also like to define the line segment of the Line_string1 that the intersection of those two lines is taking place in an automatic way.

So far I have come across this answer [a link] (Get the vertices on a LineString either side of a Point), but I get a none output from the defined split function.

Any ideas of what could be wrong or any other suggestions?

Community
  • 1
  • 1
FLR
  • 23
  • 4

1 Answers1

1

Break up Line1_string into a list of simple line segments, and check if each segment intersects.

Use a pairwise recipe from itertools:

from itertools import tee
def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

# Convert input line into list of line segments
Line1_segs = [LineString(p) for p in pairwise(Line1_string.coords[:])]

# Find the intersections
intersect_segs = [i for i, s in enumerate(Line1_segs) if line2_string.intersects(s)]
print(intersect_segs)  # [2]

The intersection happens on item 2, or the third segment. Note that you may get more than one intersection, particularly if the intersection is on a vertex connecting two or more segments.

Mike T
  • 41,085
  • 18
  • 152
  • 203