Fixes to path_connect() and added units tests for it.

This commit is contained in:
jpcaram
2015-01-26 17:52:26 -05:00
parent 6b51f03db2
commit 573581ca80
2 changed files with 70 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ from shapely.geometry import LineString, Polygon
from shapely.ops import cascaded_union, unary_union
from matplotlib.pyplot import plot, subplot, show, cla, clf, xlim, ylim, title
from camlib import *
from random import random
class PathConnectTest1(unittest.TestCase):
@@ -13,15 +14,44 @@ class PathConnectTest1(unittest.TestCase):
def test_simple_connect(self):
paths = [
LineString([[0, 0], [0, 1]]),
LineString([[0, 1], [0, 2]])
LineString([[0, 0], [1, 1]]),
LineString([[1, 1], [2, 1]])
]
result = Geometry.path_connect(paths)
self.assertEqual(len(result), 1)
self.assertTrue(result[0].equals(LineString([[0, 0], [0, 2]])))
self.assertTrue(result[0].equals(LineString([[0, 0], [1, 1], [2, 1]])))
def test_interfere_connect(self):
paths = [
LineString([[0, 0], [1, 1]]),
LineString([[1, 1], [2, 1]]),
LineString([[-0.5, 0.5], [0.5, 0]])
]
result = Geometry.path_connect(paths)
self.assertEqual(len(result), 2)
matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
self.assertEqual(len(matches), 1)
def test_simple_connect_offset1(self):
for i in range(20):
offset_x = random()
offset_y = random()
paths = [
LineString([[0 + offset_x, 0 + offset_y], [1 + offset_x, 1 + offset_y]]),
LineString([[1 + offset_x, 1 + offset_y], [2 + offset_x, 1 + offset_y]])
]
result = Geometry.path_connect(paths)
self.assertEqual(len(result), 1)
self.assertTrue(result[0].equals(LineString([[0 + offset_x, 0 + offset_y],
[1 + offset_x, 1 + offset_y],
[2 + offset_x, 1 + offset_y]])))
if __name__ == "__main__":
unittest.main()