File: test_cga.py

package info (click to toggle)
python-shapely 2.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,528 kB
  • sloc: python: 18,648; ansic: 6,615; makefile: 88; sh: 62
file content (51 lines) | stat: -rw-r--r-- 1,509 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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import unittest

import pytest

from shapely.geometry.polygon import LinearRing, Polygon, orient, signed_area


class SignedAreaTestCase(unittest.TestCase):
    def test_triangle(self):
        tri = LinearRing([(0, 0), (2, 5), (7, 0)])
        assert signed_area(tri) == pytest.approx(-7 * 5 / 2)

    def test_square(self):
        xmin, xmax = (-1, 1)
        ymin, ymax = (-2, 3)
        rect = LinearRing(
            [(xmin, ymin), (xmax, ymin), (xmax, ymax), (xmin, ymax), (xmin, ymin)]
        )
        assert signed_area(rect) == pytest.approx(10.0)


class RingOrientationTestCase(unittest.TestCase):
    def test_ccw(self):
        ring = LinearRing([(1, 0), (0, 1), (0, 0)])
        assert ring.is_ccw

    def test_cw(self):
        ring = LinearRing([(0, 0), (0, 1), (1, 0)])
        assert not ring.is_ccw


class PolygonOrienterTestCase(unittest.TestCase):
    def test_no_holes(self):
        ring = LinearRing([(0, 0), (0, 1), (1, 0)])
        polygon = Polygon(ring)
        assert not polygon.exterior.is_ccw
        polygon = orient(polygon, 1)
        assert polygon.exterior.is_ccw

    def test_holes(self):
        # fmt: off
        polygon = Polygon(
            [(0, 0), (0, 1), (1, 0)],
            [[(0.5, 0.25), (0.25, 0.5), (0.25, 0.25)]]
        )
        # fmt: on
        assert not polygon.exterior.is_ccw
        assert polygon.interiors[0].is_ccw
        polygon = orient(polygon, 1)
        assert polygon.exterior.is_ccw
        assert not polygon.interiors[0].is_ccw