Skip to content

Commit 1d592ae

Browse files
Assign nil geometries to obs with malformed coordinates (#1922) (#1923)
* Assign nil geometries to obs with malformed coordinates Fixes #1922 * Update test_csv__provider.py * Update test_csv__provider.py * Update test_csv__provider.py --------- Co-authored-by: Tom Kralidis <tomkralidis@gmail.com>
1 parent 7096af2 commit 1d592ae

3 files changed

Lines changed: 42 additions & 3 deletions

File tree

pygeoapi/provider/csv_.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,9 @@ def _load(self, offset=0, limit=10, resulttype='results',
143143
float(row.pop(self.geometry_y)),
144144
]
145145
except ValueError:
146-
msg = f'Skipping row with invalid geometry: {row.get(self.id_field)}' # noqa
147-
LOGGER.error(msg)
148-
continue
146+
msg = f'Row with invalid geometry: {row.get(self.id_field)}, setting coordinates to None' # noqa
147+
LOGGER.warning(msg)
148+
coordinates = None
149149

150150
feature = {'type': 'Feature'}
151151
feature['id'] = row.pop(self.id_field)

tests/data/obs_malformatted.csv

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
id,stn_id,datetime,value,lat,long
2+
371,35,"2001-10-30T14:24:55Z",89.9,45,-75
3+
377,35,"2002-10-30T18:31:38Z",93.9,45,-75
4+
238,2147,"2007-10-30T08:57:29Z",103.5,43,-79
5+
297,2147,"2003-10-30T07:37:29Z",93.5,,
6+
964,604,"2000-10-30T18:24:39Z",99.9,A,X

tests/test_csv__provider.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#
2828
# =================================================================
2929

30+
import logging
31+
3032
import pytest
3133

3234
from pygeoapi.provider.base import (ProviderItemNotFoundError,
@@ -35,8 +37,11 @@
3537

3638
from .util import get_test_file_path
3739

40+
LOGGER = logging.getLogger(__name__)
41+
3842
path = get_test_file_path('data/obs.csv')
3943
stations_path = get_test_file_path('data/station_list.csv')
44+
malformatted_path = get_test_file_path('data/obs_malformatted.csv')
4045

4146

4247
@pytest.fixture()
@@ -67,6 +72,20 @@ def station_config():
6772
}
6873

6974

75+
@pytest.fixture()
76+
def malformatted_config():
77+
return {
78+
'name': 'CSV',
79+
'type': 'feature',
80+
'data': malformatted_path,
81+
'id_field': 'id',
82+
'geometry': {
83+
'x_field': 'long',
84+
'y_field': 'lat'
85+
}
86+
}
87+
88+
7089
def test_query(config):
7190
p = CSVProvider(config)
7291

@@ -153,3 +172,17 @@ def test_get_station(station_config):
153172

154173
result = p.get('0-454-2-AWSNAMITAMBO')
155174
assert result['properties']['station_name'] == 'NAMITAMBO'
175+
176+
177+
def test_get_malformed(malformatted_config, caplog):
178+
p = CSVProvider(malformatted_config)
179+
180+
with caplog.at_level(logging.WARNING):
181+
results = p.query()
182+
183+
assert 'Row with invalid geometry' in caplog.text
184+
assert len(results['features']) == 5
185+
assert results['numberMatched'] == 5
186+
assert results['numberReturned'] == 5
187+
assert results['features'][3]['geometry']['coordinates'] is None
188+
assert results['features'][4]['geometry']['coordinates'] is None

0 commit comments

Comments
 (0)