File: test_invalid_geometries.py

package info (click to toggle)
python-shapely 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,528 kB
  • sloc: python: 18,648; ansic: 6,615; makefile: 88; sh: 62
file content (26 lines) | stat: -rw-r--r-- 891 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
"""Test recovery from operation on invalid geometries"""

import unittest

import pytest

import shapely
from shapely.errors import TopologicalError
from shapely.geometry import Polygon


class InvalidGeometriesTestCase(unittest.TestCase):
    def test_invalid_intersection(self):
        # Make a self-intersecting polygon
        polygon_invalid = Polygon([(0, 0), (1, 1), (1, -1), (0, 1), (0, 0)])
        assert not polygon_invalid.is_valid

        # Intersect with a valid polygon
        polygon = Polygon([(-0.5, -0.5), (-0.5, 0.5), (0.5, 0.5), (0.5, -5)])
        assert polygon.is_valid
        assert polygon_invalid.intersects(polygon)

        with pytest.raises((TopologicalError, shapely.GEOSException)):
            polygon_invalid.intersection(polygon)
        with pytest.raises((TopologicalError, shapely.GEOSException)):
            polygon.intersection(polygon_invalid)