From fe7ea108a747364be29464488eaeeaa91a37c298 Mon Sep 17 00:00:00 2001 From: Sarah Jordan Date: Tue, 9 Jan 2024 15:14:02 -0600 Subject: [PATCH] Data return for STAC collections (#1483) * render data viewing page for collections #1409 render data viewing page for STAC collections if there is no STAC item below. * flake8 * flake8 * add additional check for collections * render data viewing page for collections #1409 render data viewing page for STAC collections if there is no STAC item below. * flake8 * flake8 * add additional check for collections --- pygeoapi/api.py | 26 ++++- pygeoapi/provider/hateoas.py | 42 +++++-- pygeoapi/templates/stac/collection_base.html | 114 +++++++++++++++++++ 3 files changed, 172 insertions(+), 10 deletions(-) create mode 100644 pygeoapi/templates/stac/collection_base.html diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 979d7205b..7a1203231 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -3810,9 +3810,29 @@ def get_stac_path(self, request: Union[APIRequest, Any], if request.format == F_HTML: # render content['path'] = path if 'assets' in content: # item view - content = render_j2_template(self.tpl_config, - 'stac/item.html', - content, request.locale) + if content['type'] == 'Collection': + content = render_j2_template( + self.tpl_config, + 'stac/collection_base.html', + content, + request.locale + ) + elif content['type'] == 'Feature': + content = render_j2_template( + self.tpl_config, + 'stac/item.html', + content, + request.locale + ) + else: + msg = f'Unknown STAC type {content.type}' + LOGGER.error(msg) + return self.get_exception( + HTTPStatus.INTERNAL_SERVER_ERROR, + headers, + request.format, + 'NoApplicableCode', + msg) else: content = render_j2_template(self.tpl_config, 'stac/catalog.html', diff --git a/pygeoapi/provider/hateoas.py b/pygeoapi/provider/hateoas.py index 539b37e79..307119d46 100644 --- a/pygeoapi/provider/hateoas.py +++ b/pygeoapi/provider/hateoas.py @@ -169,15 +169,17 @@ def get_data_path(self, baseurl, urlpath, entrypath): 'entry:type': 'Item' }) + if resource_type == "Collection" and len(link_href_list) == 0: + content = jsondata + content = _modify_content_for_display( + content, + baseurl, + urlpath + ) + elif resource_type == 'Assets': content = jsondata - content['assets']['default'] = { - 'href': os.path.join(baseurl, urlpath).replace('\\', '/'), - } - - for key in content['assets']: - content['assets'][key]['file:size'] = 0 - content['assets'][key]['created'] = jsondata["properties"]["datetime"] # noqa + content = _modify_content_for_display(content, baseurl, urlpath) content['links'].extend(child_links) @@ -187,6 +189,32 @@ def __repr__(self): return f' {self.data}' +def _modify_content_for_display( + content: dict, + baseurl: str, + urlpath: str) -> dict: + """ + Helper function to fill in required information for HTML display. + + :param content: `dict` of JSON item + :param baseurl: base URL of endpoint + :param urlpath: base path of URL + + :returns: `dict` of JSON item + """ + content['assets']['default'] = { + 'href': os.path.join(baseurl, urlpath).replace('\\', '/'), + } + for key in content['assets']: + content['assets'][key]['file:size'] = 0 + try: + content['assets'][key]['created'] = content["properties"]["datetime"] # noqa + except Exception as err: + LOGGER.debug(err) + LOGGER.debug('no properties included in STAC') + return content + + def _get_json_data(jsonpath): """ Helper function used to load a json file that is located on the WEB diff --git a/pygeoapi/templates/stac/collection_base.html b/pygeoapi/templates/stac/collection_base.html new file mode 100644 index 000000000..bbcfe4168 --- /dev/null +++ b/pygeoapi/templates/stac/collection_base.html @@ -0,0 +1,114 @@ +{% extends "_base.html" %} +{% block title %}{{ super() }} stac/{{ data['path'] }} {% endblock %} +{% block crumbs %}{{ super() }} +/ {% trans %}SpatioTemporal Asset Catalog{% endtrans %} +{% for link in get_breadcrumbs(data['path']) %} +/ {{ link['title'] }} +{% endfor %} +{% endblock %} + +{% block extrahead %} + + +{% endblock %} + +{% block body %} +
+
+
+

{% trans %}Collections{% endtrans %}: {{ data['id'] }}

+
+
+
+
+
+
+
+
+

{% trans %}Assets{% endtrans %}

+ + + + + + + + + + {% for k, link in data['assets'].items() %} + + + + + + {% endfor %} + +
{% trans %}URL{% endtrans %}{% trans %}Last Modified{% endtrans %}{% trans %}Size{% endtrans %}
+ + {{ link['href'] | get_path_basename }} + {{ link['created'] }}{{ link['file:size'] | human_size }}
+
+
+
+
+
+ + + + + + + + + + + + + + + + + + + + + {% if data['cube:dimensions'] %} + + + + + {% endif %} + {% if data['cube:variables'] %} + + + + + {% endif %} + + +
{% trans %}Property{% endtrans %}{% trans %}Value{% endtrans %}
{% trans %}id{% endtrans %}{{ data.id }}
{% trans %}description{% endtrans %}{{ data.description }}
{% trans %}extent{% endtrans %}{{ data.extent }}
{% trans %}cube:dimensions{% endtrans %}{{ data['cube:dimensions'] }}
{% trans %}cube:variables{% endtrans %}{{ data['cube:variables'] }}
+
+
+
+{% endblock %} + +{% block extrafoot %} + +{% endblock %}