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).
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.