Skip to content

Commit

Permalink
Data return for STAC collections (geopython#1483)
Browse files Browse the repository at this point in the history
* render data viewing page for collections geopython#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 geopython#1409

render data viewing page for STAC collections if there is no STAC item below.

* flake8

* flake8

* add additional check for collections
  • Loading branch information
sjordan29 authored Jan 9, 2024
1 parent e9bb5dc commit fe7ea10
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 10 deletions.
26 changes: 23 additions & 3 deletions pygeoapi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
42 changes: 35 additions & 7 deletions pygeoapi/provider/hateoas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -187,6 +189,32 @@ def __repr__(self):
return f'<HateoasProvider> {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
Expand Down
114 changes: 114 additions & 0 deletions pygeoapi/templates/stac/collection_base.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
{% extends "_base.html" %}
{% block title %}{{ super() }} stac/{{ data['path'] }} {% endblock %}
{% block crumbs %}{{ super() }}
/ <a href="{{ config['server']['url'] }}/stac">{% trans %}SpatioTemporal Asset Catalog{% endtrans %}</a>
{% for link in get_breadcrumbs(data['path']) %}
/ <a class="crumbs-path" href="{{config['server']['url'] }}/stac/{{ link['href'] }}">{{ link['title'] }}</a>
{% endfor %}
{% endblock %}

{% block extrahead %}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.1/dist/leaflet.css"/>
<script src="https://unpkg.com/leaflet@1.3.1/dist/leaflet.js"></script>
{% endblock %}

{% block body %}
<section id="item">
<div class="row">
<div class="col-sm">
<h2>{% trans %}Collections{% endtrans %}: {{ data['id'] }}</h2>
</div>
</div>
<div class="row">
<div class="col-md-6 col-sm-12">
<div class="row">
<div class="col-sm-12">
<div id="items-map"></div>
<div id="assets">
<h4>{% trans %}Assets{% endtrans %}</h4>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>{% trans %}URL{% endtrans %}</th>
<th>{% trans %}Last Modified{% endtrans %}</th>
<th>{% trans %}Size{% endtrans %}</th>
</tr>
</thead>
<tbody>
{% for k, link in data['assets'].items() %}
<tr>
<td data-label="name">
<a title="{{ link['href'] }}" href="{{ link['href'] }}">
<span>{{ link['href'] | get_path_basename }}</span></a>
</td>
<td data-label="created">{{ link['created'] }}</td>
<td data-label="size">{{ link['file:size'] | human_size }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
</div>
<div class="col-md-6 col-sm-12">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>{% trans %}Property{% endtrans %}</th>
<th>{% trans %}Value{% endtrans %}</th>
</tr>
</thead>
<tbody>
<tr>
<td>{% trans %}id{% endtrans %}</td>
<td>{{ data.id }}</td>
</tr>
<tr>
<td>{% trans %}description{% endtrans %}</td>
<td>{{ data.description }}</td>
</tr>
<tr>
<td>{% trans %}extent{% endtrans %}</td>
<td>{{ data.extent }}</td>
</tr>
{% if data['cube:dimensions'] %}
<tr>
<td>{% trans %}cube:dimensions{% endtrans %}</td>
<td>{{ data['cube:dimensions'] }}</td>
</tr>
{% endif %}
{% if data['cube:variables'] %}
<tr>
<td>{% trans %}cube:variables{% endtrans %}</td>
<td>{{ data['cube:variables'] }}</td>
</tr>
{% endif %}

</tbody>
</table>
</div>
</div>
</section>
{% endblock %}

{% block extrafoot %}
<script>
//var map = L.map('items-map').setView([{{ 45 }}, {{ -75 }}], 2);
var map = L.map('items-map').setView([{{ 0 }}, {{ 0 }}], 1);
map.addLayer(new L.TileLayer(
'{{ config['server']['map']['url'] }}', {
maxZoom: 18,
attribution: '{{ config['server']['map']['attribution'] | safe }}'
}
));
var bbox_layer = L.polygon([
[{{ data['extent']['spatial']['bbox'][0][1] }}, {{ data['extent']['spatial']['bbox'][0][0] }}],
[{{ data['extent']['spatial']['bbox'][0][3] }}, {{ data['extent']['spatial']['bbox'][0][0] }}],
[{{ data['extent']['spatial']['bbox'][0][3] }}, {{ data['extent']['spatial']['bbox'][0][2] }}],
[{{ data['extent']['spatial']['bbox'][0][1] }}, {{ data['extent']['spatial']['bbox'][0][2] }}],
]);
map.addLayer(bbox_layer);
map.fitBounds(bbox_layer.getBounds(), {maxZoom: 10});
</script>
{% endblock %}

0 comments on commit fe7ea10

Please sign in to comment.