From 49d1092f1cb9e09bebaa4684d466238a2c000751 Mon Sep 17 00:00:00 2001 From: Lex Neva Date: Thu, 9 Aug 2018 15:16:36 -0400 Subject: make cut() work properly even if the cut point is nearly at the end --- lib/utils/geometry.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) (limited to 'lib/utils/geometry.py') diff --git a/lib/utils/geometry.py b/lib/utils/geometry.py index d0cb96cf..bfdcd3c0 100644 --- a/lib/utils/geometry.py +++ b/lib/utils/geometry.py @@ -9,21 +9,22 @@ def cut(line, distance): """ if distance <= 0.0 or distance >= line.length: return [LineString(line), None] - coords = list(line.coords) - for i, p in enumerate(coords): - # TODO: I think this doesn't work if the path doubles back on itself - pd = line.project(ShapelyPoint(p)) - if pd == distance: + coords = list(ShapelyPoint(p) for p in line.coords) + traveled = 0 + last_point = coords[0] + for i, p in enumerate(coords[1:], 1): + traveled += p.distance(last_point) + last_point = p + if traveled == distance: return [ LineString(coords[:i+1]), LineString(coords[i:])] - if pd > distance: + if traveled > distance: cp = line.interpolate(distance) return [ LineString(coords[:i] + [(cp.x, cp.y)]), LineString([(cp.x, cp.y)] + coords[i:])] - def cut_path(points, length): """Return a subsection of at the start of the path that is length units long. -- cgit v1.2.3