test_invalid_geometries.py 891 B

1234567891011121314151617181920212223242526
  1. """Test recovery from operation on invalid geometries"""
  2. import unittest
  3. import pytest
  4. import shapely
  5. from shapely.errors import TopologicalError
  6. from shapely.geometry import Polygon
  7. class InvalidGeometriesTestCase(unittest.TestCase):
  8. def test_invalid_intersection(self):
  9. # Make a self-intersecting polygon
  10. polygon_invalid = Polygon([(0, 0), (1, 1), (1, -1), (0, 1), (0, 0)])
  11. assert not polygon_invalid.is_valid
  12. # Intersect with a valid polygon
  13. polygon = Polygon([(-0.5, -0.5), (-0.5, 0.5), (0.5, 0.5), (0.5, -5)])
  14. assert polygon.is_valid
  15. assert polygon_invalid.intersects(polygon)
  16. with pytest.raises((TopologicalError, shapely.GEOSException)):
  17. polygon_invalid.intersection(polygon)
  18. with pytest.raises((TopologicalError, shapely.GEOSException)):
  19. polygon.intersection(polygon_invalid)