the well known function
from shapely.geometry import *
from shapely.wkt import loads
def cut(line, distance):
# Cuts a line in two at a distance from its starting point
if distance <= 0.0 or distance >= line.length:
return [LineString(line)]
coords = list(line.coords)
for i, p in enumerate(coords):
pd = line.project(Point(p))
if pd == distance:
return [
LineString(coords[:i+1]),
LineString(coords[i:])]
if pd > distance:
cp = line.interpolate(distance)
return [
LineString(coords[:i] + [(cp.x, cp.y)]),
LineString([(cp.x, cp.y)] + coords[i:])]
splits a shapely linestring into two lines at distance.
What I need to do is to cut a piece of a certain length from the line, at a certain position along the line
Example line:
line = loads("LINESTRING (12.0133696 47.8217147, 12.0132944 47.8216655, 12.0132056 47.8215749, 12.0131542 47.8215034, 12.0130522 47.8212931, 12.0129941 47.8211294, 12.0130381 47.8209553, 12.0131116 47.8208718, 12.013184 47.8208107, 12.0133547 47.8207312, 12.0135537 47.8206727, 12.013915 47.8206019, 12.0141624 47.8205671, 12.0144317 47.8204965)")
I tried an approach with getting the difference between some linestrings that i got by appliyng above cut functio, but the results are not good due to shapely limitations.
Any ideas?
I'll answer myself, and am happy about improvements:
def cut_piece(line,distance, lgth):
""" From a linestring, this cuts a piece of length lgth at distance.
Needs cut(line,distance) func from above ;-)
"""
precut = cut(line,distance)[1]
result = cut(precut,lgth)[0]
return result
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With