-
Notifications
You must be signed in to change notification settings - Fork 560
/
test_geometry_base.py
159 lines (115 loc) · 4.09 KB
/
test_geometry_base.py
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import platform
import weakref
import numpy as np
import pytest
import shapely
from shapely import (
GeometryCollection,
LinearRing,
LineString,
MultiLineString,
MultiPoint,
MultiPolygon,
Point,
Polygon,
)
from shapely.errors import ShapelyDeprecationWarning
def test_polygon():
assert bool(Polygon()) is False
def test_linestring():
assert bool(LineString()) is False
def test_point():
assert bool(Point()) is False
def test_geometry_collection():
assert bool(GeometryCollection()) is False
geometries_all_types = [
Point(1, 1),
LinearRing([(0, 0), (1, 1), (0, 1), (0, 0)]),
LineString([(0, 0), (1, 1), (0, 1), (0, 0)]),
Polygon([(0, 0), (1, 1), (0, 1), (0, 0)]),
MultiPoint([(1, 1)]),
MultiLineString([[(0, 0), (1, 1), (0, 1), (0, 0)]]),
MultiPolygon([Polygon([(0, 0), (1, 1), (0, 1), (0, 0)])]),
GeometryCollection([Point(1, 1)]),
]
@pytest.mark.skipif(
platform.python_implementation() == "PyPy",
reason="Setting custom attributes doesn't fail on PyPy",
)
@pytest.mark.parametrize("geom", geometries_all_types)
def test_setattr_disallowed(geom):
with pytest.raises(AttributeError):
geom.name = "test"
@pytest.mark.parametrize("geom", geometries_all_types)
def test_weakrefable(geom):
_ = weakref.ref(geom)
@pytest.mark.parametrize("geom", geometries_all_types)
def test_comparison_notimplemented(geom):
# comparing to a non-geometry class should return NotImplemented in __eq__
# to ensure proper delegation to other (eg to ensure comparison of scalar
# with array works)
# https://github.com/shapely/shapely/issues/1056
assert geom.__eq__(1) is NotImplemented
# with array
arr = np.array([geom, geom], dtype=object)
result = arr == geom
assert isinstance(result, np.ndarray)
assert result.all()
result = geom == arr
assert isinstance(result, np.ndarray)
assert result.all()
result = arr != geom
assert isinstance(result, np.ndarray)
assert not result.any()
result = geom != arr
assert isinstance(result, np.ndarray)
assert not result.any()
def test_base_class_not_callable():
with pytest.raises(TypeError):
shapely.Geometry("POINT (1 1)")
def test_GeometryType_deprecated():
geom = Point(1, 1)
with pytest.warns(ShapelyDeprecationWarning):
geom_type = geom.geometryType()
assert geom_type == geom.geom_type
def test_type_deprecated():
geom = Point(1, 1)
with pytest.warns(ShapelyDeprecationWarning):
geom_type = geom.type
assert geom_type == geom.geom_type
@pytest.mark.skipif(shapely.geos_version < (3, 10, 0), reason="GEOS < 3.10")
def test_segmentize():
line = LineString([(0, 0), (0, 10)])
result = line.segmentize(max_segment_length=5)
assert result.equals(LineString([(0, 0), (0, 5), (0, 10)]))
@pytest.mark.skipif(shapely.geos_version < (3, 7, 0), reason="GEOS < 3.7")
def test_reverse():
coords = [(0, 0), (1, 2)]
line = LineString(coords)
result = line.reverse()
assert result.coords[:] == coords[::-1]
@pytest.mark.skipif(shapely.geos_version < (3, 9, 0), reason="GEOS < 3.9")
@pytest.mark.parametrize(
"op", ["union", "intersection", "difference", "symmetric_difference"]
)
@pytest.mark.parametrize("grid_size", [0, 1, 2])
def test_binary_op_grid_size(op, grid_size):
geom1 = shapely.box(0, 0, 2.5, 2.5)
geom2 = shapely.box(2, 2, 3, 3)
result = getattr(geom1, op)(geom2, grid_size=grid_size)
expected = getattr(shapely, op)(geom1, geom2, grid_size=grid_size)
assert result == expected
@pytest.mark.skipif(shapely.geos_version < (3, 10, 0), reason="GEOS < 3.10")
def test_dwithin():
point = Point(1, 1)
line = LineString([(0, 0), (0, 10)])
assert point.dwithin(line, 0.5) is False
assert point.dwithin(line, 1.5) is True
@pytest.mark.parametrize(
"op", ["convex_hull", "envelope", "oriented_envelope", "minimum_rotated_rectangle"]
)
def test_constructive_properties(op):
geom = LineString([(0, 0), (0, 10), (10, 10)])
result = getattr(geom, op)
expected = getattr(shapely, op)(geom)
assert result == expected