I am trying to split a LineString into single segments:

from shaply import LineString,MultiPoint
from shapely.ops import split
xp = [-2,1,2,2.5,4,1,-1,2,4,5,6]
yp = [-2,1,1,0,1,2,2,3,4,4,4]
list_of_point = list(zip(xp,yp))
profile = LineString(list_of_point)

At the moment I am using split:

points_to_split = MultiPoint([Point(x,y) for x,y in profile.coords[1:]])
splitted = split(profile,points_to_split)
for i in splitted:print(i)

I've got the expected result but it seems quite overcomplicating a process that should be quite easy to handle internally.

Am I missing something?


you can simplify it to a one-liner, by building geometries without split

splitted = [geom.LineString([profile.coords[i], profile.coords[i+1]]) for i in range(len(profile.coords) - 1)]
  • Thanks but I've got 840 µs for your solution vs 54.9 ns for mine in my machine so is not really efficient. – G M Jul 5 '19 at 16:01

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.