test_cga.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import unittest
  2. import pytest
  3. from shapely.geometry.polygon import LinearRing, Polygon, orient, signed_area
  4. class SignedAreaTestCase(unittest.TestCase):
  5. def test_triangle(self):
  6. tri = LinearRing([(0, 0), (2, 5), (7, 0)])
  7. assert signed_area(tri) == pytest.approx(-7 * 5 / 2)
  8. def test_square(self):
  9. xmin, xmax = (-1, 1)
  10. ymin, ymax = (-2, 3)
  11. rect = LinearRing(
  12. [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax), (xmin, ymin)]
  13. )
  14. assert signed_area(rect) == pytest.approx(10.0)
  15. class RingOrientationTestCase(unittest.TestCase):
  16. def test_ccw(self):
  17. ring = LinearRing([(1, 0), (0, 1), (0, 0)])
  18. assert ring.is_ccw
  19. def test_cw(self):
  20. ring = LinearRing([(0, 0), (0, 1), (1, 0)])
  21. assert not ring.is_ccw
  22. class PolygonOrienterTestCase(unittest.TestCase):
  23. def test_no_holes(self):
  24. ring = LinearRing([(0, 0), (0, 1), (1, 0)])
  25. polygon = Polygon(ring)
  26. assert not polygon.exterior.is_ccw
  27. polygon = orient(polygon, 1)
  28. assert polygon.exterior.is_ccw
  29. def test_holes(self):
  30. # fmt: off
  31. polygon = Polygon(
  32. [(0, 0), (0, 1), (1, 0)],
  33. [[(0.5, 0.25), (0.25, 0.5), (0.25, 0.25)]]
  34. )
  35. # fmt: on
  36. assert not polygon.exterior.is_ccw
  37. assert polygon.interiors[0].is_ccw
  38. polygon = orient(polygon, 1)
  39. assert polygon.exterior.is_ccw
  40. assert not polygon.interiors[0].is_ccw