1

I would like to find a better solution for what I am proposing below. I am trying to find the indices associated with a line intersection when using the shapely library. Solutions from other libraries are welcome.

Right now I am iterating through the location coordinates and storing the index where an intersection is observed. I would like to do away with the loop and create a more streamlined function.

The code below results in a single intersection/crossing.

line_crossings = []

latitude = [10, 11, 12, 13, 14, 15, 16, 17 ,18]
longitude = [7, 9, 11, 13, 17, 19, 23, 25 ,29]
location = np.column_stack((latitude, longitude))

C = (14.5, 14.5)
D = (12.3, 12.5)
line2 = LineString([C, D])

for idx in range(0, len(location)-1):
    A = (latitude[idx], longitude[idx])
    B = (latitude[idx+1], longitude[idx+1])
    line1 = LineString([A, B])
    int_pt = line2.intersection(line1)
    if int_pt.type == 'Point':
        print(int_pt)
        line_crossings.append(idx)

Update

It would seem the quickest way to get the coordinates of the crossings is as follows:

latitude = [10, 11, 12, 13, 14, 15, 16, 17 ,16, 15, 14, 13, 12, 11, 10]
longitude = [7, 9, 11, 13, 17, 19, 23, 25 ,29, 25, 23, 13, 13, 13, 11]
location = LineString([i for i in zip(latitude,longitude)])

C = (14.5, 14.5)
D = (12.3, 12.5)
gate = LineString([C, D])

[[i.x, i.y] for i in location.intersection(gate)]

But I need to be able to get the index in the location variable where the intersection occurs. Is it possible to get this using the list comprehension?

Tim
  • 133
  • 10
  • 1
    A previous question on [finding the intersection between line segments](https://stackoverflow.com/questions/3252194/numpy-and-line-intersections) has pure numpy answers which can act on an array of points at the same time, .e.g, `location[:-1]` and `location[1:]`. Most focus on finding the intersection of the lines extending past the starting and ending points. so you want to filter your results with a check of whether each intersection point falls within any of your `line1` and `line2`. – Reti43 Nov 16 '21 at 23:17
  • Thanks Reti43. I have found a very efficient method, but I think the problem is now related to list comprehenisons. – Tim Nov 30 '21 at 22:49

0 Answers0