python - Shapely: Cut a piece from a linestring at two cutting points -
the known function
from shapely.geometry import * shapely.wkt import loads def cut(line, distance): # cuts line in 2 @ distance starting point if distance <= 0.0 or distance >= line.length: return [linestring(line)] coords = list(line.coords) 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 shapely linestring 2 lines @ distance.
what need cut piece of length line, @ position along 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 approach getting difference between linestrings got appliyng above cut functio, results not due shapely limitations.
any ideas?
i'll answer myself, , happy improvements:
def cut_piece(line,distance, lgth): """ linestring, cuts piece of length lgth @ distance. needs cut(line,distance) func above ;-) """ precut = cut(line,distance)[1] result = cut(precut,lgth)[0] return result
Comments
Post a Comment