test_orient.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. import unittest
  2. from numpy.testing import assert_array_equal
  3. from shapely.geometry import (
  4. GeometryCollection,
  5. LinearRing,
  6. LineString,
  7. MultiLineString,
  8. MultiPoint,
  9. MultiPolygon,
  10. Point,
  11. Polygon,
  12. )
  13. from shapely.ops import orient
  14. class OrientTestCase(unittest.TestCase):
  15. def test_point(self):
  16. point = Point(0, 0)
  17. assert orient(point, 1) == point
  18. assert orient(point, -1) == point
  19. def test_multipoint(self):
  20. multipoint = MultiPoint([(0, 0), (1, 1)])
  21. assert orient(multipoint, 1) == multipoint
  22. assert orient(multipoint, -1) == multipoint
  23. def test_linestring(self):
  24. linestring = LineString([(0, 0), (1, 1)])
  25. assert orient(linestring, 1) == linestring
  26. assert orient(linestring, -1) == linestring
  27. def test_multilinestring(self):
  28. multilinestring = MultiLineString([[(0, 0), (1, 1)], [(1, 0), (0, 1)]])
  29. assert orient(multilinestring, 1) == multilinestring
  30. assert orient(multilinestring, -1) == multilinestring
  31. def test_linearring(self):
  32. linearring = LinearRing([(0, 0), (0, 1), (1, 0)])
  33. assert orient(linearring, 1) == linearring
  34. assert orient(linearring, -1) == linearring
  35. def test_empty_polygon(self):
  36. polygon = Polygon()
  37. assert orient(polygon) == polygon
  38. def test_polygon(self):
  39. polygon = Polygon([(0, 0), (0, 1), (1, 0)])
  40. polygon_reversed = Polygon(polygon.exterior.coords[::-1])
  41. assert (orient(polygon, 1)) == polygon_reversed
  42. assert (orient(polygon, -1)) == polygon
  43. def test_multipolygon(self):
  44. polygon1 = Polygon([(0, 0), (0, 1), (1, 0)])
  45. polygon2 = Polygon([(1, 0), (2, 0), (2, 1)])
  46. polygon1_reversed = Polygon(polygon1.exterior.coords[::-1])
  47. polygon2_reversed = Polygon(polygon2.exterior.coords[::-1])
  48. multipolygon = MultiPolygon([polygon1, polygon2])
  49. assert not polygon1.exterior.is_ccw
  50. assert polygon2.exterior.is_ccw
  51. assert orient(multipolygon, 1) == MultiPolygon([polygon1_reversed, polygon2])
  52. assert orient(multipolygon, -1) == MultiPolygon([polygon1, polygon2_reversed])
  53. def test_geometrycollection(self):
  54. polygon = Polygon([(0, 0), (0, 1), (1, 0)])
  55. polygon_reversed = Polygon(polygon.exterior.coords[::-1])
  56. collection = GeometryCollection([polygon])
  57. assert orient(collection, 1) == GeometryCollection([polygon_reversed])
  58. assert orient(collection, -1) == GeometryCollection([polygon])
  59. def test_polygon_with_holes(self):
  60. ring_cw = LinearRing([(0, 0), (0, 1), (1, 1), (0, 0)])
  61. ring_cw2 = LinearRing([(0, 0), (0, 3), (3, 3), (0, 0)])
  62. ring_ccw = LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)])
  63. ring_ccw2 = LinearRing([(0, 0), (2, 2), (0, 2), (0, 0)])
  64. polygon_with_holes_mixed = Polygon(
  65. ring_ccw, [ring_cw, ring_ccw2, ring_cw2, ring_ccw]
  66. )
  67. polygon_with_holes_ccw = Polygon(
  68. ring_ccw, [ring_cw, ring_ccw2.reverse(), ring_cw2, ring_ccw.reverse()]
  69. )
  70. assert_array_equal(orient(polygon_with_holes_ccw, 1), polygon_with_holes_ccw)
  71. assert_array_equal(
  72. orient(polygon_with_holes_ccw, -1), polygon_with_holes_ccw.reverse()
  73. )
  74. assert_array_equal(orient(polygon_with_holes_mixed, 1), polygon_with_holes_ccw)
  75. assert_array_equal(
  76. orient(polygon_with_holes_mixed, -1), polygon_with_holes_ccw.reverse()
  77. )