|
1 | | -from . import unittest |
| 1 | +import pytest |
| 2 | + |
2 | 3 | from shapely.geometry import shape, Polygon |
3 | 4 |
|
4 | | -# numpy is an optional dependency |
5 | | -try: |
6 | | - import numpy as np |
7 | | -except ImportError: |
8 | | - _has_numpy = False |
9 | | -else: |
10 | | - _has_numpy = True |
11 | | - |
12 | | - |
13 | | -class ShapeTestCase(unittest.TestCase): |
14 | | - |
15 | | - def test_polygon_no_coords(self): |
16 | | - # using None |
17 | | - d = {"type": "Polygon", "coordinates": None} |
18 | | - p = shape(d) |
19 | | - self.assertEqual(p, Polygon()) |
20 | | - |
21 | | - # using empty list |
22 | | - d = {"type": "Polygon", "coordinates": []} |
23 | | - p = shape(d) |
24 | | - self.assertEqual(p, Polygon()) |
25 | | - |
26 | | - # using empty array |
27 | | - d = {"type": "Polygon", "coordinates": np.array([])} |
28 | | - p = shape(d) |
29 | | - self.assertEqual(p, Polygon()) |
30 | | - |
31 | | - def test_polygon_with_coords_list(self): |
32 | | - # list |
33 | | - d = {"type": "Polygon", "coordinates": [[[5, 10], [10, 10], [10, 5]]]} |
34 | | - p = shape(d) |
35 | | - self.assertEqual(p, Polygon([(5, 10), (10, 10), (10, 5)])) |
36 | | - |
37 | | - # numpy array |
38 | | - if _has_numpy: |
39 | | - d = {"type": "Polygon", "coordinates": np.array([[[5, 10], |
40 | | - [10, 10], |
41 | | - [10, 5]]])} |
42 | | - p = shape(d) |
43 | | - self.assertEqual(p, Polygon([(5, 10), (10, 10), (10, 5)])) |
44 | | - |
45 | | - |
46 | | -def test_suite(): |
47 | | - return unittest.TestLoader().loadTestsFromTestCase(ShapeTestCase) |
| 5 | + |
| 6 | +@pytest.mark.parametrize( |
| 7 | + "geom", |
| 8 | + [{"type": "Polygon", "coordinates": None}, {"type": "Polygon", "coordinates": []}], |
| 9 | +) |
| 10 | +def test_polygon_no_coords(geom): |
| 11 | + assert shape(geom) == Polygon() |
| 12 | + |
| 13 | + |
| 14 | +def test_polygon_empty_np_array(): |
| 15 | + np = pytest.importorskip("numpy") |
| 16 | + geom = {"type": "Polygon", "coordinates": np.array([])} |
| 17 | + assert shape(geom) == Polygon() |
| 18 | + |
| 19 | + |
| 20 | +def test_polygon_with_coords_list(): |
| 21 | + geom = {"type": "Polygon", "coordinates": [[[5, 10], [10, 10], [10, 5]]]} |
| 22 | + obj = shape(geom) |
| 23 | + assert obj == Polygon([(5, 10), (10, 10), (10, 5)]) |
| 24 | + |
| 25 | + |
| 26 | +def test_polygon_not_empty_np_array(): |
| 27 | + np = pytest.importorskip("numpy") |
| 28 | + geom = {"type": "Polygon", "coordinates": np.array([[[5, 10], [10, 10], [10, 5]]])} |
| 29 | + obj = shape(geom) |
| 30 | + assert obj == Polygon([(5, 10), (10, 10), (10, 5)]) |
0 commit comments