Skip to content

Commit

Permalink
Allow configurable expand to SensorThings provider (geopython#1879)
Browse files Browse the repository at this point in the history
* Allow configurable expand to SensorThings provider

* Update license year
  • Loading branch information
webb-ben authored Dec 15, 2024
1 parent f032628 commit a1c94fa
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/source/data-publishing/ogcapi-features.rst
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,10 @@ If ``intralink`` is true for an adjacent STA provider collection within a
pygeoapi instance, the expanded entity is instead represented by an intra-pygeoapi
link to the other entity or it's ``uri_field`` if declared.

Additionally there is the optional field ``expand``. This field will overwrite the default
pygeoapi expand behavior and instead implement the configured expand strategy. This is
particularly useful if you have Datastreams with many observations.

.. code-block:: yaml
providers:
Expand All @@ -677,6 +681,7 @@ link to the other entity or it's ``uri_field`` if declared.
entity: Datastreams
time_field: phenomenonTime
intralink: true
expand: Thing/Locations,Observations($select=result,phenomenonTime;$orderby=phenomenonTime desc;$top=1)
If all three entities are configured, the STA provider will represent a complete STA
endpoint as OGC-API feature collections. The ``Things`` features will include links
Expand Down
6 changes: 5 additions & 1 deletion pygeoapi/provider/sensorthings.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Authors: Benjamin Webb <[email protected]>
# Authors: Tom Kralidis <[email protected]>
#
# Copyright (c) 2023 Benjamin Webb
# Copyright (c) 2024 Benjamin Webb
# Copyright (c) 2022 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
Expand Down Expand Up @@ -518,6 +518,10 @@ def _generate_mappings(self, provider_def: dict):
LOGGER.debug('Using default @iot.id for id field')
self.id_field = '@iot.id'

# Custom expand
if provider_def.get('expand'):
EXPAND[self.entity] = provider_def['expand']

# Create intra-links
self.intralink = provider_def.get('intralink', False)
if self.intralink and provider_def.get('rel_link'):
Expand Down
26 changes: 25 additions & 1 deletion tests/test_sensorthings_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# Authors: Benjamin Webb <[email protected]>
#
# Copyright (c) 2022 Benjamin Webb
# Copyright (c) 2024 Benjamin Webb
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -138,3 +138,27 @@ def test_get(config):
assert result['id'] == '9'
assert result['properties']['name'] == 'Depth Below Surface'
assert isinstance(result['properties']['Thing'], dict)


def test_custom_expand(config):
p = SensorThingsProvider(config)
fields = p.get_fields()
assert 'Observations' in fields
assert 'ObservedProperty' in fields
assert 'Sensor' in fields

config['expand'] = 'Thing/Locations'
p = SensorThingsProvider(config)
fields = p.get_fields()
assert len(fields) == 12
assert 'Observations' not in fields
assert 'ObservedProperty' not in fields
assert 'Sensor' not in fields

config['expand'] = 'Thing/Locations,Observations'
p = SensorThingsProvider(config)
fields = p.get_fields()
assert len(fields) == 14
assert 'Observations' in fields
assert 'ObservedProperty' not in fields
assert 'Sensor' not in fields

0 comments on commit a1c94fa

Please sign in to comment.