Skip to content

Commit e91a384

Browse files
committed
OGC API: add support for sorting
1 parent 2d77b1c commit e91a384

File tree

2 files changed

+21
-8
lines changed

2 files changed

+21
-8
lines changed

docs/source/usage.rst

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ OGC API - Features - Part 1: Core 1.0
211211
>>> w.url
212212
'https://demo.pygeoapi.io/master'
213213
>>> conformance = w.conformance()
214-
{u'conformsTo': [u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/html', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson']}
214+
{'conformsTo': ['http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/html', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson']}
215215
>>> api = w.api() # OpenAPI document/
216216
>>> collections = w.collections()
217217
>>> len(collections['collections'])
@@ -231,7 +231,11 @@ OGC API - Features - Part 1: Core 1.0
231231
6
232232
>>> lakes_query = w.collection_items('lakes')
233233
>>> lakes_query['features'][0]['properties']
234-
{u'scalerank': 0, u'name_alt': None, u'admin': None, u'featureclass': u'Lake', u'id': 0, u'name': u'Lake Baikal'}
234+
{'scalerank': 0, 'name_alt': None, 'admin': None, 'featureclass': 'Lake', 'id': 0, 'name': 'Lake Baikal'}
235+
>>> lakes_query = w.collection_items('lakes', name='L. Ontario')
236+
>>> len(lakes_querylakes_query['features'][0]['properties']
237+
{'id': 0, 'scalerank': 0, 'name': 'Lake Baikal', 'name_alt': 'https://en.wikipedia.org/wiki/Lake_Baikal', 'admin': None, 'featureclass': 'Lake'}
238+
235239
236240
OGC API - Coverages - Part 1: Core 1.0
237241
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -275,7 +279,7 @@ OGC API - Records - Part 1: Core 1.0
275279
>>> w.url
276280
'https://example.org/records-api'
277281
>>> conformance = w.conformance()
278-
{'conformsTo': [u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/html', u'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson', u'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/core', u'http://www.opengis.net/spec/ogcapi-records/1.0/req/oas30', u'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/json', u'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/html']}
282+
{'conformsTo': ['http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/core', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/oas30', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/html', 'http://www.opengis.net/spec/ogcapi-features-1/1.0/conf/geojson', 'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/core', 'http://www.opengis.net/spec/ogcapi-records/1.0/req/oas30', 'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/json', 'http://www.opengis.net/spec/ogcapi-records-1/1.0/req/html']}
279283
>>> api = w.api() # OpenAPI document
280284
>>> collections = w.collections()
281285
>>> len(collections)
@@ -295,20 +299,20 @@ OGC API - Records - Part 1: Core 1.0
295299
8
296300
>>> my_catalogue_query = w.collection_items('my-catalogue')
297301
>>> my_catalogue_query['features'][0]['properties'].keys()
298-
[u'title', u'abstract', u'keywords']
302+
['title', 'abstract', 'keywords']
299303
>>> my_catalogue_query['features'][0]['properties']['title']
300-
u'Roadrunner ambush locations'
304+
'Roadrunner ambush locations'
301305
>>> my_catalogue_query2 = w.collection_items('my-catalogue', q='birds')
302306
>>> msc_wis_dcpc_query2['numberMatched']
303307
2
304308
>>> msc_wis_dcpc_query2['numberReturned']
305309
2
306310
>>> my_catalogue_cql_text_query = w.collection_items('my-catalogue', filter="title LIKE 'Roadrunner%'")
307311
>>> my_catalogue_cql_text_query['features'][0]['properties']['title']
308-
u'Roadrunner ambush locations'
312+
'Roadrunner ambush locations'
309313
>>> my_catalogue_cql_json_query = w.collection_items('my-catalogue', limit=1, cql={'eq': [{ 'property': 'title' }, 'Roadrunner ambush locations']})
310314
>>> my_catalogue_cql_json_query['features'][0]['properties']['title']
311-
u'Roadrunner ambush locations'
315+
'Roadrunner ambush locations'
312316

313317
>>> import json
314318

owslib/ogcapi/features.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,23 @@ def collection_items(self, collection_id: str, **kwargs: dict) -> dict:
124124
@param filter: CQL TEXT expression
125125
@type cql: dict
126126
@param cql: CQL JSON payload
127+
@type sortby: tuple
128+
@param sortby: sort property and direction (`asc`, `desc`)
127129
128130
@returns: feature results
129131
"""
130132

131133
if 'bbox' in kwargs:
132134
kwargs['bbox'] = ','.join(list(map(str, kwargs['bbox'])))
133135
if 'datetime_' in kwargs:
134-
kwargs['datetime'] = kwargs['datetime_']
136+
kwargs['datetime'] = kwargs.pop('datetime_')
137+
if 'sortby' in kwargs:
138+
sort_property, sort_order = kwargs.pop('sortby')
139+
if sort_order == 'desc':
140+
sortby = f'-{sort_property}'
141+
else:
142+
sortby = sort_property
143+
kwargs['sortby'] = sortby
135144

136145
if 'cql' in kwargs:
137146
LOGGER.debug('CQL query detected')

0 commit comments

Comments
 (0)