Skip to content

Commit cd6d88f

Browse files
authored
Fix SensorThings URI field retention (geopython#1924)
1 parent 49bbf78 commit cd6d88f

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

pygeoapi/provider/sensorthings.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -484,8 +484,13 @@ def _expand_properties(self, feature, keys=(), uri='',
484484
self._expand_location(feature['Thing'])
485485

486486
# Retain URI if present
487-
if feature.get('properties') and self.uri_field:
488-
uri = feature['properties']
487+
try:
488+
if self.uri_field is not None:
489+
uri = feature['properties'][self.uri_field]
490+
except KeyError:
491+
msg = f'Unable to find uri field: {self.uri_field}'
492+
LOGGER.error(msg)
493+
raise ProviderInvalidDataError(msg)
489494

490495
# Create intra links
491496
for k, v in feature.items():
@@ -506,7 +511,7 @@ def _expand_properties(self, feature, keys=(), uri='',
506511
ret = {k: feature.pop(k) for k in keys}
507512
feature = ret
508513

509-
if self.uri_field is not None and uri != '':
514+
if self.uri_field is not None:
510515
feature[self.uri_field] = uri
511516

512517
return feature

tests/test_sensorthings_provider.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import pytest
3131

32+
from pygeoapi.provider.base import ProviderInvalidDataError
3233
from pygeoapi.provider.sensorthings import SensorThingsProvider
3334

3435

@@ -185,6 +186,25 @@ def test_custom_expand(config):
185186
assert 'Sensor' not in fields
186187

187188

189+
def test_custom_uri_field(config):
190+
config['uri_field'] = 'uri'
191+
config['properties'] = ['name']
192+
p = SensorThingsProvider(config)
193+
194+
result = p.get('9')
195+
assert result['id'] == '9'
196+
assert result['properties']['name'] == 'Depth Below Surface'
197+
assert result['properties']['uri'] == \
198+
'https://geoconnex.us/iow/sta-demo/timeseries/9'
199+
assert len(result['properties']) == 2
200+
201+
config['uri_field'] = 'bad_uri'
202+
p = SensorThingsProvider(config)
203+
with pytest.raises(ProviderInvalidDataError,
204+
match=".*Unable to find uri field: bad_uri"):
205+
result = p.get('9')
206+
207+
188208
def test_transactions(config, post_body):
189209
p = SensorThingsProvider(config)
190210
results = p.query(resulttype='hits')

0 commit comments

Comments
 (0)