0

I have a list of sublist which contains the LineString from Shapely such as following:

My_list = [
           [(0, 0), (1, 1), (1,2), (2,2)], 
           [(0, 0), (1, 1), (2,1), (2,2)], 
           [(-1, 0), (1, -1), (-2,1), (2,0)]
          ]

In my list, I have three (in this example they are three, in my case they are thousands) sets which are defined as LineString in Shapely.

Now I like to iteratively find the intersection between each sublist, for example as I said, here I have three LineStrings. Lets call them a1, a2, a3.

a1 = [(0, 0), (1, 1), (1,2), (2,2)]

a2 = [(0, 0), (1, 1), (2,1), (2,2)]

a3 = [(-1, 0), (1, -1), (-2,1), (2,0)]

I want to find the intersection check (and find the intersection as well) between each pair: (a1,a2), (a1,a3), (a2,a1), (a2,a3), (a3,a1), (a3,a2).

Saif Faidi
  • 509
  • 4
  • 15
ash
  • 55
  • 5
  • Have you tried any code? What was the problem with it? From what I see, there are two problems you have to solve: 1) getting [all possible pairs](https://stackoverflow.com/q/27974126), and 2) getting [intersections](https://stackoverflow.com/q/22417842) between LineString's. The first part and its variations were already asked and answered several times here. And the second one is quite trivial, easily found in Google, and is present in Shapely documentation. As it stands now, I don't see any value in this question for the community. I vote to close it as too broad. – Georgy Sep 01 '19 at 15:17

1 Answers1

0

Below a function which finds the intersection between 2 lists of iterables:

def find_intersection(list1,list2):
    lst =[]
    for el1 in list1 :
            if any(el1 is el2 for el2 in list2):
                lst.append(el1)
    return lst

you can use it in a loop as I did to return all the possible intersections between all the sublists:

result = []
for index1 in range(0,len(My_list)):
    for index2 in range(0,len(My_list)) :
        if index1 is not index2 :
            inter = find_intersection(My_list[index1],My_list[index2])
            if inter :
                result.append({('a'+str(index1 +1 ),'a'+str(index2 + 1 )): inter})

to make the result readable I made it return a list of dictionaries like : [ { (a1,a2) :inter1 } (a1,a3) :inter2 } ect .. ]

Note that I ignored couples with no intersection in the result list.

Saif Faidi
  • 509
  • 4
  • 15
  • I do not see that you use any intersection command from shapely to find the intersection, is that correct? – ash Sep 05 '19 at 23:28