65 lines
1.7 KiB
Python
65 lines
1.7 KiB
Python
from math import hypot
|
|
from shapely.geometry import LineString
|
|
|
|
from .spatial import Index
|
|
|
|
def load_paths(filename):
|
|
paths = []
|
|
with open(filename) as fp:
|
|
for line in fp:
|
|
points = filter(None, line.strip().split(';'))
|
|
if not points:
|
|
continue
|
|
path = [tuple(map(float, x.split(','))) for x in points]
|
|
paths.append(path)
|
|
return paths
|
|
|
|
def simplify_path(points, tolerance):
|
|
if len(points) < 2:
|
|
return points
|
|
line = LineString(points)
|
|
line = line.simplify(tolerance)
|
|
return list(line.coords)
|
|
|
|
def simplify_paths(paths, tolerance):
|
|
return [simplify_path(x, tolerance) for x in paths]
|
|
|
|
def sort_paths(paths, reversable=True):
|
|
first = paths[0]
|
|
paths.remove(first)
|
|
result = [first]
|
|
points = []
|
|
for path in paths:
|
|
x1, y1 = path[0]
|
|
x2, y2 = path[-1]
|
|
points.append((x1, y1, path, False))
|
|
if reversable:
|
|
points.append((x2, y2, path, True))
|
|
index = Index(points)
|
|
while index.size > 0:
|
|
x, y, path, reverse = index.nearest(result[-1][-1])
|
|
x1, y1 = path[0]
|
|
x2, y2 = path[-1]
|
|
index.remove((x1, y1, path, False))
|
|
if reversable:
|
|
index.remove((x2, y2, path, True))
|
|
if reverse:
|
|
result.append(list(reversed(path)))
|
|
else:
|
|
result.append(path)
|
|
return result
|
|
|
|
def join_paths(paths, tolerance):
|
|
if len(paths) < 2:
|
|
return paths
|
|
result = [list(paths[0])]
|
|
for path in paths[1:]:
|
|
x1, y1 = result[-1][-1]
|
|
x2, y2 = path[0]
|
|
d = hypot(x2 - x1, y2 - y1)
|
|
if d <= tolerance:
|
|
result[-1].extend(path)
|
|
else:
|
|
result.append(list(path))
|
|
return result
|