test_pathconnect.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import unittest
  2. from shapely.geometry import LineString, Polygon
  3. from shapely.ops import cascaded_union, unary_union
  4. from matplotlib.pyplot import plot, subplot, show, cla, clf, xlim, ylim, title
  5. from camlib import *
  6. from random import random
  7. class PathConnectTest1(unittest.TestCase):
  8. def setUp(self):
  9. pass
  10. def test_simple_connect(self):
  11. paths = [
  12. LineString([[0, 0], [1, 1]]),
  13. LineString([[1, 1], [2, 1]])
  14. ]
  15. result = Geometry.path_connect(paths)
  16. self.assertEqual(len(result), 1)
  17. self.assertTrue(result[0].equals(LineString([[0, 0], [1, 1], [2, 1]])))
  18. def test_interfere_connect(self):
  19. paths = [
  20. LineString([[0, 0], [1, 1]]),
  21. LineString([[1, 1], [2, 1]]),
  22. LineString([[-0.5, 0.5], [0.5, 0]])
  23. ]
  24. result = Geometry.path_connect(paths)
  25. self.assertEqual(len(result), 2)
  26. matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
  27. self.assertEqual(len(matches), 1)
  28. def test_simple_connect_offset1(self):
  29. for i in range(20):
  30. offset_x = random()
  31. offset_y = random()
  32. paths = [
  33. LineString([[0 + offset_x, 0 + offset_y], [1 + offset_x, 1 + offset_y]]),
  34. LineString([[1 + offset_x, 1 + offset_y], [2 + offset_x, 1 + offset_y]])
  35. ]
  36. result = Geometry.path_connect(paths)
  37. self.assertEqual(len(result), 1)
  38. self.assertTrue(result[0].equals(LineString([[0 + offset_x, 0 + offset_y],
  39. [1 + offset_x, 1 + offset_y],
  40. [2 + offset_x, 1 + offset_y]])))
  41. def test_ring_interfere_connect(self):
  42. print
  43. print "TEST STARTING ..."
  44. paths = [
  45. LineString([[0, 0], [1, 1]]),
  46. LineString([[1, 1], [2, 1]]),
  47. LinearRing([[1, 1], [2, 2], [1, 3], [0, 2]])
  48. ]
  49. result = Geometry.path_connect(paths)
  50. self.assertEqual(len(result), 2)
  51. matches = [p for p in result if p.equals(LineString([[0, 0], [1, 1], [2, 1]]))]
  52. self.assertEqual(len(matches), 1)
  53. if __name__ == "__main__":
  54. unittest.main()