Skip to content

Commit 7644d1d

Browse files
committed
Add JSON-LD UI enhancements for STAC items
1 parent ccaf7be commit 7644d1d

File tree

2 files changed

+54
-5
lines changed

2 files changed

+54
-5
lines changed

pygeoapi/api/stac.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ def get_stac_path(api: API, request: APIRequest,
201201
stac_collections[dataset].get('links', []))
202202

203203
linked_data = api.config['resources'][dataset].get('linked-data')
204+
content['linked_data'] = linked_data
204205

205206
if request.format == F_HTML: # render
206207
content['path'] = path

pygeoapi/templates/stac/item.html

Lines changed: 53 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,38 @@
66
/ <a class="crumbs-path" href="{{config['server']['url'] }}/stac/{{ link['href'] }}">{{ link['title'] }}</a>
77
{% endfor %}
88
{% endblock %}
9+
{% set linked_data = data.get('linked_data') %}
10+
11+
{% macro render_item_value(v, width, additional_classes='') -%}
12+
{% set val = v | string | trim %}
13+
{% if val|length and val.lower().endswith(('.jpg', '.jpeg', '.png', '.gif', '.bmp')) %}
14+
{# Ends with image extension: render img element with link to image #}
15+
<span class="literal-value">
16+
<a href="{{ val }}"><img src="{{ val }}" alt="{{ val.split('/') | last }}" width="{{ width }}"/></a>
17+
</span>
18+
{% elif v is string or v is number %}
19+
<span class="literal-value">{{ val | urlize() }}</span>
20+
{% elif v is mapping %}
21+
<table class="table table-striped table-border object-table {{ additional_classes }}">
22+
{% for i,j in v.items() %}
23+
<tr>
24+
<td class="object-property" data-property="{{ i }}">{{ i }}</td>
25+
<td class="object-value">{{ render_item_value(j, 60) }}</td>
26+
</tr>
27+
{% endfor %}
28+
</table>
29+
{% elif v is iterable %}
30+
{% for i in v %}
31+
{{ render_item_value(i, 60, 'array-entry') }}
32+
{% endfor %}
33+
{% else %}
34+
<span class="literal-value">{{ val | urlize() }}</span>
35+
{% endif %}
36+
{%- endmacro %}
937

1038
{% block extrahead %}
1139
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css"/>
40+
<link rel="stylesheet" href="{{ config['server']['url'] }}/static/css/linked-data.css">
1241
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
1342
{% endblock %}
1443

@@ -52,7 +81,7 @@ <h4>{% trans %}Assets{% endtrans %}</h4>
5281
</div>
5382
</div>
5483
<div class="col-md-6 col-sm-12">
55-
<table class="table table-striped table-bordered">
84+
<table class="table table-striped table-bordered object-table" id="item-properties-table">
5685
<thead>
5786
<tr>
5887
<th>{% trans %}Property{% endtrans %}</th>
@@ -61,12 +90,12 @@ <h4>{% trans %}Assets{% endtrans %}</h4>
6190
</thead>
6291
<tbody>
6392
<tr>
64-
<td>{% trans %}id{% endtrans %}</td>
65-
<td>{{ data.id }}</td>
93+
<td class="object-property" data-property="id">{% trans %}id{% endtrans %}</td>
94+
<td class="object-value">{{ data.id }}</td>
6695
</tr>
6796
{% for k, v in data['properties'].items() %}
6897
<tr>
69-
<td>{{ k }}</td>
98+
<td class="object-property" data-property="{{ k | striptags }}">{{ k }}</td>
7099
{% if k == 'links' %}
71100
<td>
72101
<ul>
@@ -76,7 +105,7 @@ <h4>{% trans %}Assets{% endtrans %}</h4>
76105
</ul>
77106
</td>
78107
{% else %}
79-
<td>{{ v }}</td>
108+
<td class="object-value">{{ render_item_value(v, 80) }}</td>
80109
{% endif %}
81110
</tr>
82111
{% endfor %}
@@ -106,4 +135,23 @@ <h4>{% trans %}Assets{% endtrans %}</h4>
106135
map.addLayer(bbox_layer);
107136
map.fitBounds(bbox_layer.getBounds(), {maxZoom: 10});
108137
</script>
138+
{% if linked_data and linked_data.get('context') | length %}
139+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/rdflib.min.js"></script>
140+
<script src="https://github.com/avillar/jsonld-ui-utils/releases/latest/download/jsonld-ui-utils.min.js"></script>
141+
<script>
142+
const rootElem = document.getElementById('item-properties-table');
143+
if (rootElem) {
144+
const linkedDataConfig = {{ linked_data | tojson }}
145+
const options = {};
146+
if (linkedDataConfig.fallback_sparql_endpoint) {
147+
options.fallbackSparqlEndpoint = linkedDataConfig.fallback_sparql_endpoint;
148+
}
149+
const context = { '@context': linkedDataConfig['context'] };
150+
jsonldUIUtils.loadContext(context)
151+
.then(loadedContext => {
152+
jsonldUIUtils.augment(rootElem, loadedContext, options);
153+
});
154+
}
155+
</script>
156+
{% endif %}
109157
{% endblock %}

0 commit comments

Comments
 (0)