From 3359f0d3b418666118bd505270bae4e9a89659d4 Mon Sep 17 00:00:00 2001 From: Tom Kralidis Date: Wed, 4 Apr 2018 02:53:59 +0000 Subject: [PATCH] add bbox/time API support to ES backend, enhance HTML representation --- pygeoapi/api.py | 27 +++++++++++++-- pygeoapi/provider/csv_.py | 5 +-- pygeoapi/provider/elasticsearch_.py | 11 +++--- pygeoapi/provider/geojson.py | 3 +- pygeoapi/provider/sqlite.py | 3 +- pygeoapi/templates/base.html | 37 ++++++++++++++++++++ pygeoapi/templates/collection.html | 32 ++++-------------- pygeoapi/templates/collections.html | 29 +++------------- pygeoapi/templates/conformance.html | 29 +++------------- pygeoapi/templates/items.html | 52 +++++++++++++++++++++++++++++ pygeoapi/templates/root.html | 29 +++------------- 11 files changed, 145 insertions(+), 112 deletions(-) create mode 100644 pygeoapi/templates/base.html create mode 100644 pygeoapi/templates/items.html diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 4a3ba99..50d6dc5 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -98,7 +98,7 @@ class API(object): 'rel': 'self', 'type': 'text/html', 'title': 'this document as HTML', - 'href': '{}?f=html'.format(self.config['server']['url']) + 'href': '{}/?f=html'.format(self.config['server']['url']) }, { 'rel': 'self', 'type': 'application/openapi+json;version=3.0', @@ -266,11 +266,21 @@ class API(object): :returns: tuple of headers, status code, content """ - print(args) headers_ = { 'Content-type': 'application/json' } + formats = ['json', 'html'] + + format_ = args.get('f') + if format_ is not None and format_ not in formats: + exception = { + 'code': 'InvalidParameterValue', + 'description': 'Invalid format' + } + LOGGER.error(exception) + return headers_, 400, json.dumps(exception) + LOGGER.debug('Processing query parameters') try: startindex = int(args.get('startindex')) @@ -282,7 +292,12 @@ class API(object): limit = self.config['server']['limit'] resulttype = args.get('resulttype') or 'results' - bbox = args.get('bbox') + + try: + bbox = args.get('bbox').split(',') + except AttributeError: + bbox = [] + time = args.get('time') if dataset not in self.config['datasets'].keys(): @@ -338,6 +353,12 @@ class API(object): content['timeStamp'] = datetime.utcnow().isoformat() + if format_ == 'html': # render + headers_['Content-type'] = 'text/html' + content = _render_j2_template(self.config, 'items.html', + content) + return headers_, 200, content + return headers_, 200, json.dumps(content) def get_feature(self, headers, args, dataset, identifier): diff --git a/pygeoapi/provider/csv_.py b/pygeoapi/provider/csv_.py index 7826f86..952a107 100644 --- a/pygeoapi/provider/csv_.py +++ b/pygeoapi/provider/csv_.py @@ -54,7 +54,7 @@ class CSVProvider(BaseProvider): BaseProvider.__init__(self, provider_def) def _load(self, startindex=0, limit=10, resulttype='results', - identifier=None): + identifier=None, bbox=[], time=None): """ Load CSV data @@ -98,7 +98,8 @@ class CSVProvider(BaseProvider): feature_collection['numberReturned'] = limit return feature_collection - def query(self, startindex=0, limit=10, resulttype='results'): + def query(self, startindex=0, limit=10, resulttype='results', + bbox=[], time=None): """ CSV query diff --git a/pygeoapi/provider/elasticsearch_.py b/pygeoapi/provider/elasticsearch_.py index b0388c1..d5e765d 100644 --- a/pygeoapi/provider/elasticsearch_.py +++ b/pygeoapi/provider/elasticsearch_.py @@ -65,14 +65,14 @@ class ElasticsearchProvider(BaseProvider): self.es = Elasticsearch(self.es_host) def query(self, startindex=0, limit=10, resulttype='results', - bbox=None, time=None): + bbox=[], time=None): """ query Elasticsearch index :param startindex: starting record to return (default 0) :param limit: number of records to return (default 10) :param resulttype: return results or hit limit (default results) - :param bbox: bounding box (minx,miny,maxx,maxy) + :param bbox: bounding box [minx,miny,maxx,maxy] :param time: temporal (datestamp or extent) :returns: dict of 0..n GeoJSON features @@ -90,9 +90,9 @@ class ElasticsearchProvider(BaseProvider): LOGGER.debug('hits only specified') limit = 0 - if bbox is not None: + if bbox: LOGGER.debug('processing bbox parameter') - minx, miny, maxx, maxy = bbox.split(',') + minx, miny, maxx, maxy = bbox bbox_filter = { 'geo_shape': { 'geometry': { @@ -140,6 +140,9 @@ class ElasticsearchProvider(BaseProvider): except exceptions.RequestError as err: LOGGER.error(err) raise ProviderQueryError() + except exceptions.NotFoundError as err: + LOGGER.error(err) + raise ProviderQueryError() feature_collection['numberMatched'] = results['hits']['total'] diff --git a/pygeoapi/provider/geojson.py b/pygeoapi/provider/geojson.py index 67e3977..6d3deea 100644 --- a/pygeoapi/provider/geojson.py +++ b/pygeoapi/provider/geojson.py @@ -85,7 +85,8 @@ class GeoJSONProvider(BaseProvider): return data - def query(self, startindex=0, limit=10, resulttype='results'): + def query(self, startindex=0, limit=10, resulttype='results', + bbox=[], time=None): """ query the provider diff --git a/pygeoapi/provider/sqlite.py b/pygeoapi/provider/sqlite.py index e6b4b90..91205ec 100644 --- a/pygeoapi/provider/sqlite.py +++ b/pygeoapi/provider/sqlite.py @@ -130,7 +130,8 @@ class SQLiteProvider(BaseProvider): return cursor - def query(self, startindex=0, limit=10, resulttype='results'): + def query(self, startindex=0, limit=10, resulttype='results', + bbox=[], time=None): """ Query Sqlite for all the content. e,g: http://localhost:5000/collections/countries/items? diff --git a/pygeoapi/templates/base.html b/pygeoapi/templates/base.html new file mode 100644 index 0000000..9fc9421 --- /dev/null +++ b/pygeoapi/templates/base.html @@ -0,0 +1,37 @@ + + + + + {% block title %}{{ config['metadata']['identification']['title'] }} -{% endblock %} + + + + + + + + + + {% block extrahead %} + {% endblock %} + + + +
+

{{ config['metadata']['identification']['title'] }}

+ {{ config['metadata']['identification']['description'] }} +
+ + {% block body %} + {% endblock %} + +
+ + + {% block extrafoot %} + {% endblock %} + + + diff --git a/pygeoapi/templates/collection.html b/pygeoapi/templates/collection.html index d298a6d..e92005f 100644 --- a/pygeoapi/templates/collection.html +++ b/pygeoapi/templates/collection.html @@ -1,27 +1,10 @@ - - - - - {{ config['metadata']['identification']['title'] }} - data['title'] - - - - - - - - - - -
-

{{ config['metadata']['identification']['title'] }}

- {{ config['metadata']['identification']['description'] }} -
+{% extends "base.html" %} +{% block title %}{{ super() }} {{ data['title'] }} {% endblock %} +{% block body %}

{{ data['title'] }}

- {{ data['description'] }} +
{{ data['description'] }}
+
Browse Features

Links

-
- - - +{% endblock %} diff --git a/pygeoapi/templates/collections.html b/pygeoapi/templates/collections.html index 8640edd..d746e27 100644 --- a/pygeoapi/templates/collections.html +++ b/pygeoapi/templates/collections.html @@ -1,24 +1,6 @@ - - - - - {{ config['metadata']['identification']['title'] }} - Collections - - - - - - - - - - -
-

{{ config['metadata']['identification']['title'] }}

- {{ config['metadata']['identification']['description'] }} -
+{% extends "base.html" %} +{% block title %}{{ super() }} Collections {% endblock %} +{% block body %}

Collections

-
- - - +{% endblock %} diff --git a/pygeoapi/templates/conformance.html b/pygeoapi/templates/conformance.html index 8721258..5108773 100644 --- a/pygeoapi/templates/conformance.html +++ b/pygeoapi/templates/conformance.html @@ -1,24 +1,6 @@ - - - - - {{ config['metadata']['identification']['title'] }} - Conformance - - - - - - - - - - -
-

{{ config['metadata']['identification']['title'] }}

- {{ config['metadata']['identification']['description'] }} -
+{% extends "base.html" %} +{% block title %}{{ super() }} Conformance {% endblock %} +{% block body %}

Conformance

-
- - - +{% endblock %} diff --git a/pygeoapi/templates/items.html b/pygeoapi/templates/items.html new file mode 100644 index 0000000..698ea62 --- /dev/null +++ b/pygeoapi/templates/items.html @@ -0,0 +1,52 @@ +{% extends "base.html" %} +{% block title %}{{ super() }} {{ data['title'] }} {% endblock %} +{% block extrahead %} + + + +{% endblock %} + +{% block body %} +
+

{{ data['title'] }}

+ {{ data['description'] }} +

Features

+ + + + + +
+ + +
+
+
+{% endblock %} + +{% block extrafoot %} + +{% endblock %} diff --git a/pygeoapi/templates/root.html b/pygeoapi/templates/root.html index d2ec0d0..95a1a21 100644 --- a/pygeoapi/templates/root.html +++ b/pygeoapi/templates/root.html @@ -1,24 +1,6 @@ - - - - - {{ config['metadata']['identification']['title'] }} - - - - - - - - - - -
-

{{ config['metadata']['identification']['title'] }}

- {{ config['metadata']['identification']['description'] }} -
+{% extends "base.html" %} +{% block title %}{{ super() }} Home {% endblock %} +{% block body %}

Service Metadata

@@ -42,7 +24,4 @@ {% endfor %} -
- - - +{% endblock %}