diff options
| author | Lex Neva <github.com@lexneva.name> | 2018-08-09 15:16:36 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2018-08-09 15:17:33 -0400 |
| commit | 49d1092f1cb9e09bebaa4684d466238a2c000751 (patch) | |
| tree | b69e5c13ecf2801436bd9381032d97b0516c6a05 /lib/utils | |
| parent | b40c3739497439b2eee9e4d78116a8d6ee27b178 (diff) | |
make cut() work properly even if the cut point is nearly at the end
Diffstat (limited to 'lib/utils')
| -rw-r--r-- | lib/utils/geometry.py | 15 |
1 files changed, 8 insertions, 7 deletions
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. |
