2

I have a pair of lines, which I like to find the intersect of each of one them with each other. I know for two lines I can do the following:

from shapely.geometry import LineString

line1 = LineString([(0,0), (1,0), (1,1)])

line2 = LineString([(0,1), (1,1)])

print(line1.intersection(line2))

Let's say I have the following multilinestring, and I am looking for finding the intersection between each pair?

MULTILINESTRING ((0 0, 0 20), (20 0, 20 20), (7.49080237694725 11.97316968394073, 5.67208473076472 5.229834926470677), (19.01428612819832 3.12037280884873, 25.96681259449994 4.024633854021577), (14.6398788362281 3.119890406724053, 21.51766222234933 1.803845175041164))
1

You can use itertools.combinations to combine all possible line pairs extracted from the multiline:

from shapely.geometry import MultiLineString
from itertools import combinations

multiline = MultiLineString([[(614633.1598889811, 6614684.232110311), (614585.0239559432, 6615176.69973293), (614244.3696605981, 6615210.024609649), (614174.0171430812, 6615058.211282375)], 
                             [(614849.2836035677, 6614574.273030049), (615163.3363697577, 6614591.624011607), (615477.7302093033, 6614608.993836996), (615475.8039105758, 6614892.159749944), 
                              (615474.6318041045, 6615064.459401229), (614967.3343471865, 6615119.389699113)], 
                             [(615054.1363645532, 6614185.399992246), (615163.3363697577, 6614591.624011607), (615227.7403992868, 6614831.207001455), (615475.8039105758, 6614892.159749944), 
                              (615835.3545208545, 6614980.506471327), (615867.958614701, 6615021.869873968), (615474.6318041045, 6615064.459401229), (615474.2581286087, 6615119.389699113), 
                              (615286.7657710963, 6615227.024200648)], 
                             [(616057.5676853136, 6615001.338955494), (615867.958614701, 6615021.869873968), (616067.9839273975, 6615275.633330373)]])


for line1, line2 in combinations([line for line in multiline],2):
    if line1.intersects(line2):
        print(line1.intersection(line2))

Will output:

MULTIPOINT (615163.3363697577 6614591.624011607, 615474.6318041045 6615064.459401229, 615475.8039105758 6614892.159749944)
POINT (615867.9586147011 6615021.869873968)

enter image description here

  • Thank you so much for your prompt answer – koorosh esteki Apr 16 at 23:45
  • @kooroshesteki nice! Did it give you the results you wanted? If so please accept my answer – BERA Apr 17 at 5:05
  • 1
    yes it did and I accepted your answer – koorosh esteki Apr 19 at 17:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.