test_operators.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. import unittest
  2. from shapely.geometry import LineString, MultiPoint, Point, Polygon
  3. class OperatorsTestCase(unittest.TestCase):
  4. def test_point(self):
  5. point = Point(0, 0)
  6. point2 = Point(-1, 1)
  7. assert point.union(point2).equals(point | point2)
  8. assert (point & point2).is_empty
  9. assert point.equals(point - point2)
  10. assert point.symmetric_difference(point2).equals(point ^ point2)
  11. assert point != point2
  12. point_dupe = Point(0, 0)
  13. assert point, point_dupe
  14. def test_multipoint(self):
  15. mp1 = MultiPoint([(0, 0), (1, 1)])
  16. mp1_dup = MultiPoint([(0, 0), (1, 1)])
  17. mp1_rev = MultiPoint([(1, 1), (0, 0)])
  18. mp2 = MultiPoint([(0, 0), (1, 1), (2, 2)])
  19. mp3 = MultiPoint([(0, 0), (1, 1), (2, 3)])
  20. assert mp1 == mp1_dup
  21. assert mp1 != mp1_rev
  22. assert mp1 != mp2
  23. assert mp2 != mp3
  24. p = Point(0, 0)
  25. mp = MultiPoint([(0, 0)])
  26. assert p != mp
  27. assert mp != p
  28. def test_polygon(self):
  29. shell = ((0, 0), (3, 0), (3, 3), (0, 3))
  30. hole = ((1, 1), (2, 1), (2, 2), (1, 2))
  31. p_solid = Polygon(shell)
  32. p2_solid = Polygon(shell)
  33. p_hole = Polygon(shell, holes=[hole])
  34. p2_hole = Polygon(shell, holes=[hole])
  35. assert p_solid == p2_solid
  36. assert p_hole == p2_hole
  37. assert p_solid != p_hole
  38. shell2 = ((-5, 2), (10.5, 3), (7, 3))
  39. p3_hole = Polygon(shell2, holes=[hole])
  40. assert p_hole != p3_hole
  41. def test_linestring(self):
  42. line1 = LineString([(0, 0), (1, 1), (2, 2)])
  43. line2 = LineString([(0, 0), (2, 2)])
  44. line2_dup = LineString([(0, 0), (2, 2)])
  45. # .equals() indicates these are the same
  46. assert line1.equals(line2)
  47. # but != indicates these are different
  48. assert line1 != line2
  49. # but dupes are the same with ==
  50. assert line2 == line2_dup