# Splitting a MultiLineString with Shapely Split preserving the original MultiLineString composition

I have a gdf with Streets. I want to split a street with other streets (left side: Split the brown line at intersection with the green line):

Whenever i split it (using shapely split()) I recieve all the Single LineStrings (the numbers in the images as single Linestrings) and don't know how to reassign them to get the yellow, orange and green line (right side)

How can I achieve that?

## 1 Answer

If we decompose the MultiLineString, the result is:

`````` for i in multiline:
print(i)
LINESTRING (0 0, 1 0, 2 0, 3 0, 4 0) # green line
LINESTRING (0.5 0, 0.5 -1) # red line
LINESTRING (1.5 0, 2 2) # blue line
LINESTRING (3 0, 2 3) # purple line
``````

We can see that the points (0.5,0, red) and (1.5,0, blue) are not nodes of the green line, even if the lines intersect/touche.

``````list(multiline[0].coords) # nodes of the green line
[(0.0, 0.0), (1.0, 0.0), (2.0, 0.0), (3.0, 0.0), (4.0, 0.0)]
print(list(multiline[0].intersection(multiline[1]).coords))
[(0.5, 0.0)] # not a node of the green line
print(list(multiline[0].intersection(multiline[2]).coords))
[(1.5, 0.0)] # not a node of the green line
``````

As the splitting points are only on the green line (yellow points),

the result of splitres is normal. Only the green line is splitted, the others, red,blue and purple lines, remain untouched and,apart from intersections, there are no relationships between the lines (for `MULTILINESTRING ((0 0, 1 0), (0.5 0, 0.5 -1))` or `MULTILINESTRING ((1 0, 2 0, 3 0), (1.5 0, 2 2))`)

``````for i in splitres:
print(i)

LINESTRING (0 0, 1 0)  # green line 1
LINESTRING (1 0, 2 0, 3 0) # green line 2
LINESTRING (3 0, 4 0) # green line 3
LINESTRING (0.5 0, 0.5 -1) # red line
LINESTRING (1.5 0, 2 2) # blue line
LINESTRING (3 0, 2 3) # purple line
``````

A solution is to use the unary_union predicate. The solution will split all self-intersection geometries

``````print(unary_union(splitres))
MULTILINESTRING ((0 0, 0.5 0), (0.5 0, 1 0), (1 0, 1.5 0), (1.5 0, 2 0, 3 0), (3 0, 4 0), (0.5 0, 0.5 -1), (1.5 0, 2 2), (3 0, 2 3))
``````

``````for i in unary_union(splitres):
print(i)
LINESTRING (0 0, 0.5 0)
LINESTRING (0.5 0, 1 0)
LINESTRING (1 0, 1.5 0)
LINESTRING (1.5 0, 2 0, 3 0)
LINESTRING (3 0, 4 0)
LINESTRING (0.5 0, 0.5 -1)
LINESTRING (1.5 0, 2 2)
LINESTRING (3 0, 2 3)
``````
• Thanks for your detailed answer. Eventhough I might not have made my problem clear enough. I edited my question. I would probably have to compare all the beginning and endpoints and see if they are not a split point, then assign them a subsection_id. But I don't get that to work. Can you help me? Nov 24, 2023 at 18:13
• Sorry, but is another question.
– gene
Nov 27, 2023 at 8:34
• Should I repost as a new question? Nov 27, 2023 at 10:32
• That's not the question I answered
– gene
Nov 27, 2023 at 12:40