-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_intersecting_geometries.py
More file actions
246 lines (192 loc) · 7.55 KB
/
test_intersecting_geometries.py
File metadata and controls
246 lines (192 loc) · 7.55 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""Test get_intersecting_geometries function against R cancensus."""
import os
import sys
import pytest
from pathlib import Path
from shapely.geometry import Point, Polygon
import geopandas as gpd
# Add pycancensus to path
sys.path.insert(0, str(Path(__file__).parent.parent))
import pycancensus as pc
class TestIntersectingGeometries:
"""Test get_intersecting_geometries function."""
def test_point_geometry(self):
"""Test with a Point geometry."""
try:
# Vancouver coordinates
point = Point(-123.25149, 49.27026)
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=point,
simplified=False,
quiet=True
)
# Should return a dictionary with CT as key
assert isinstance(regions, dict)
assert "CT" in regions or "ct" in regions.keys()
# Should have some region IDs
region_list = list(regions.values())[0]
assert len(region_list) > 0
print(f"✅ Point intersection found {len(region_list)} census tracts")
except Exception as e:
pytest.skip(f"API call failed: {e}")
def test_point_geometry_simplified(self):
"""Test with simplified output."""
try:
point = Point(-123.25149, 49.27026)
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=point,
simplified=True,
quiet=True
)
# Should return a list of strings
assert isinstance(regions, list)
assert len(regions) > 0
assert all(isinstance(r, str) for r in regions)
print(f"✅ Simplified output: {len(regions)} region IDs")
except Exception as e:
pytest.skip(f"API call failed: {e}")
def test_polygon_geometry(self):
"""Test with a Polygon geometry (bounding box)."""
try:
# Small bounding box in Vancouver
bbox = Polygon([
(-123.30, 49.25),
(-123.25, 49.25),
(-123.25, 49.30),
(-123.30, 49.30),
(-123.30, 49.25)
])
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=bbox,
simplified=False,
quiet=True
)
assert isinstance(regions, dict)
region_list = list(regions.values())[0]
assert len(region_list) > 0
print(f"✅ Polygon intersection found {len(region_list)} census tracts")
except Exception as e:
pytest.skip(f"API call failed: {e}")
def test_geodataframe_input(self):
"""Test with GeoDataFrame input."""
try:
# Create a GeoDataFrame with a point
gdf = gpd.GeoDataFrame(
{'id': [1]},
geometry=[Point(-123.25149, 49.27026)],
crs='EPSG:4326'
)
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=gdf,
simplified=True,
quiet=True
)
assert isinstance(regions, list)
assert len(regions) > 0
print(f"✅ GeoDataFrame input: {len(regions)} region IDs")
except Exception as e:
pytest.skip(f"API call failed: {e}")
def test_geoseries_input(self):
"""Test with GeoSeries input."""
try:
# Create a GeoSeries with a point
gs = gpd.GeoSeries([Point(-123.25149, 49.27026)], crs='EPSG:4326')
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=gs,
simplified=True,
quiet=True
)
assert isinstance(regions, list)
assert len(regions) > 0
print(f"✅ GeoSeries input: {len(regions)} region IDs")
except Exception as e:
pytest.skip(f"API call failed: {e}")
def test_integration_with_get_census(self):
"""Test using results with get_census."""
try:
point = Point(-123.25149, 49.27026)
# Get intersecting regions
regions = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=point,
simplified=False,
quiet=True
)
# Use regions to get census data
census_data = pc.get_census(
dataset="CA21",
regions=regions,
vectors=["v_CA21_1"],
level="CT",
quiet=True
)
assert len(census_data) > 0
assert "v_CA21_1" in census_data.columns
print(f"✅ Integration test: retrieved {len(census_data)} regions with census data")
except Exception as e:
pytest.skip(f"Integration test failed: {e}")
def test_invalid_geometry(self):
"""Test with invalid geometry input."""
with pytest.raises(ValueError, match="geometry parameter must be"):
pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry="invalid",
quiet=True
)
def test_caching(self):
"""Test caching functionality."""
try:
point = Point(-123.25149, 49.27026)
# First call - should query API
regions1 = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=point,
use_cache=True,
quiet=False # Should show API message
)
# Second call - should use cache
regions2 = pc.get_intersecting_geometries(
dataset="CA21",
level="CT",
geometry=point,
use_cache=True,
quiet=False # Should show cache message
)
# Results should be identical
assert regions1 == regions2
print("✅ Caching test passed")
except Exception as e:
pytest.skip(f"Caching test failed: {e}")
if __name__ == "__main__":
# Run basic tests
test = TestIntersectingGeometries()
print("Testing point geometry...")
test.test_point_geometry()
print("\nTesting simplified output...")
test.test_point_geometry_simplified()
print("\nTesting polygon geometry...")
test.test_polygon_geometry()
print("\nTesting GeoDataFrame input...")
test.test_geodataframe_input()
print("\nTesting GeoSeries input...")
test.test_geoseries_input()
print("\nTesting integration with get_census...")
test.test_integration_with_get_census()
print("\nTesting invalid geometry...")
test.test_invalid_geometry()
print("\nTesting caching...")
test.test_caching()
print("\n🎉 All tests passed!")