Skip to content

Commit 561d818

Browse files
authored
Merge pull request #140 from leahgibson/mesowest-points-from-geometry-bug
Fix: Handle empty Mesowest point responses in points_from_geometry
2 parents 3fa94ea + 1da49c1 commit 561d818

File tree

2 files changed

+80
-43
lines changed

2 files changed

+80
-43
lines changed

metloom/pointdata/mesowest.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,18 @@ def points_from_geometry(
335335
points = []
336336
if resp:
337337
jdata = resp.json()
338-
data = jdata['STATION']
339338

340-
points = []
341-
for sta in data:
342-
points.append(MesowestPointData(station_id=sta['STID'],
343-
name=sta['NAME'],
344-
token_json=kwargs['token_json']))
339+
if jdata["SUMMARY"]["NUMBER_OF_OBJECTS"] > 0:
340+
data = jdata['STATION']
341+
342+
points = []
343+
for sta in data:
344+
points.append(MesowestPointData(station_id=sta['STID'],
345+
name=sta['NAME'],
346+
token_json=kwargs['token_json']))
347+
else:
348+
points = []
349+
345350
# build the result geodataframe
346351
result_df = gpd.GeoDataFrame.from_dict({'STID': [p.id for p in points],
347352
'NAME': [p.name for p in points]},

tests/test_mesowest.py

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,49 +53,58 @@ def _meta_response(self, *args, **kwargs):
5353
if 'stid' in params.keys():
5454
stid = params['stid']
5555
if stid == 'KMYL':
56-
response = {'STATION': [{'ELEVATION': '5020',
57-
'NAME': 'McCall Airport',
58-
'STID': 'KMYL',
59-
'ELEV_DEM': '5006.6',
60-
'LONGITUDE': '-116.09978',
61-
'LATITUDE': '44.89425',
62-
'TIMEZONE': 'America/Boise'}]}
56+
response = {
57+
'SUMMARY': {'NUMBER_OF_OBJECTS': 1},
58+
'STATION': [{'ELEVATION': '5020',
59+
'NAME': 'McCall Airport',
60+
'STID': 'KMYL',
61+
'ELEV_DEM': '5006.6',
62+
'LONGITUDE': '-116.09978',
63+
'LATITUDE': '44.89425',
64+
'TIMEZONE': 'America/Boise'}]
65+
}
6366

6467
elif stid == 'INTRI':
65-
response = {'STATION': [{'ELEVATION': '9409',
66-
'NAME': 'IN TRIANGLE',
67-
'STID': 'INTRI',
68-
'LONGITUDE': '-119.5',
69-
'LATITUDE': '38.0',
70-
'TIMEZONE': 'America/Los_Angeles',
71-
}]}
68+
response = {
69+
'SUMMARY': {'NUMBER_OF_OBJECTS': 1},
70+
'STATION': [{'ELEVATION': '9409',
71+
'NAME': 'IN TRIANGLE',
72+
'STID': 'INTRI',
73+
'LONGITUDE': '-119.5',
74+
'LATITUDE': '38.0',
75+
'TIMEZONE': 'America/Los_Angeles'}]
76+
}
7277

7378
elif stid == 'OUTTRI':
74-
response = {'STATION': [
75-
{'ELEVATION': '7201',
76-
'NAME': 'OUT TRIANGLE W/IN BOUNDS',
77-
'STID': 'OUTTRI',
78-
'TIMEZONE': 'America/Los_Angeles',
79-
'LONGITUDE': '-119.7',
80-
'LATITUDE': '38.0',
81-
}]}
79+
response = {
80+
'SUMMARY': {'NUMBER_OF_OBJECTS': 1},
81+
'STATION': [{'ELEVATION': '7201',
82+
'NAME': 'OUT TRIANGLE W/IN BOUNDS',
83+
'STID': 'OUTTRI',
84+
'TIMEZONE': 'America/Los_Angeles',
85+
'LONGITUDE': '-119.7',
86+
'LATITUDE': '38.0'}]
87+
}
8288

8389
elif 'bbox' in params.keys():
84-
response = {'STATION': [{'ELEVATION': '9409',
85-
'NAME': 'IN TRIANGLE',
86-
'STID': 'INTRI',
87-
'LONGITUDE': '-119.5',
88-
'LATITUDE': '38.0',
89-
'TIMEZONE': 'America/Los_Angeles',
90-
},
91-
{'ELEVATION': '7201',
92-
'NAME': 'OUT TRIANGLE W/IN BOUNDS',
93-
'STID': 'OUTTRI',
94-
'TIMEZONE': 'America/Los_Angeles',
95-
'LONGITUDE': '-119.7',
96-
'LATITUDE': '38.0',
97-
}
98-
]}
90+
response = {
91+
'SUMMARY': {'NUMBER_OF_OBJECTS': 2},
92+
'STATION': [{'ELEVATION': '9409',
93+
'NAME': 'IN TRIANGLE',
94+
'STID': 'INTRI',
95+
'LONGITUDE': '-119.5',
96+
'LATITUDE': '38.0',
97+
'TIMEZONE': 'America/Los_Angeles',
98+
},
99+
{'ELEVATION': '7201',
100+
'NAME': 'OUT TRIANGLE W/IN BOUNDS',
101+
'STID': 'OUTTRI',
102+
'TIMEZONE': 'America/Los_Angeles',
103+
'LONGITUDE': '-119.7',
104+
'LATITUDE': '38.0',
105+
}]
106+
}
107+
99108
else:
100109
raise ValueError('Invalid test STID provided')
101110

@@ -272,6 +281,29 @@ def test_points_from_geometry(self, token_file, shape_obj, w_geom, expected_sid)
272281
df = pnts.to_dataframe()
273282
assert df['id'].values == pytest.approx(expected_sid)
274283

284+
def test_points_from_geometry_no_points(self, token_file, shape_obj):
285+
"""
286+
Tests behavior when there are no points within the given geometry
287+
"""
288+
289+
mock_response = MagicMock()
290+
mock_response.json.return_value = {
291+
'SUMMARY': {'NUMBER_OF_OBJECTS': 0}
292+
}
293+
294+
with patch(
295+
"metloom.pointdata.mesowest.requests.get",
296+
return_value=mock_response
297+
):
298+
points = MesowestPointData.points_from_geometry(
299+
shape_obj,
300+
[MesowestVariables.TEMP],
301+
within_geometry=True,
302+
token_json=token_file,
303+
)
304+
305+
assert len(points) == 0
306+
275307
def test_points_from_geometry_buffer(self, token_file, shape_obj):
276308

277309
with patch("metloom.pointdata.mesowest.requests") as mock_requests:

0 commit comments

Comments
 (0)