From 96675af97014b7abd5712130caedfae43fddc11e Mon Sep 17 00:00:00 2001 From: paul van genuchten Date: Thu, 18 Feb 2021 12:12:40 +0100 Subject: [PATCH] a proposal for #133 (#608) indicate which field to use as label for the item --- docs/source/configuration.rst | 3 +- pygeoapi-config.yml | 1 + pygeoapi/api.py | 13 +++++++ .../templates/collections/items/index.html | 21 ++++++++---- .../templates/collections/items/item.html | 34 ++++++++++--------- 5 files changed, 48 insertions(+), 24 deletions(-) diff --git a/docs/source/configuration.rst b/docs/source/configuration.rst index 3735bda..ee2de85 100644 --- a/docs/source/configuration.rst +++ b/docs/source/configuration.rst @@ -175,7 +175,8 @@ default. name: CSV data: tests/data/obs.csv # required: the data filesystem path or URL, depending on plugin setup id_field: id # required for vector data, the field corresponding to the ID - time_field: datetimestamp # optional field corresponding to the temporal propert of the dataset + time_field: datetimestamp # optional field corresponding to the temporal property of the dataset + title_field: foo # optional field of which property to display as title/label on HTML pages format: # optional default format name: GeoJSON # required: format name mimetype: application/json # required: format mimetype diff --git a/pygeoapi-config.yml b/pygeoapi-config.yml index 5eb7bc6..b493393 100644 --- a/pygeoapi-config.yml +++ b/pygeoapi-config.yml @@ -151,6 +151,7 @@ resources: name: GeoJSON data: tests/data/ne_110m_lakes.geojson id_field: id + title_field: name gdps-temperature: type: collection diff --git a/pygeoapi/api.py b/pygeoapi/api.py index 18e8e71..bb5dd6b 100644 --- a/pygeoapi/api.py +++ b/pygeoapi/api.py @@ -837,6 +837,7 @@ class API: return headers_, 400, to_json(exception, self.pretty_print) LOGGER.debug('Loading provider') + try: p = load_plugin('provider', get_provider_by_type( collections[dataset]['providers'], 'feature')) @@ -1053,6 +1054,11 @@ class API: content['dataset_path'] = '/'.join(path_info.split('/')[:-1]) content['collections_path'] = '/'.join(path_info.split('/')[:-2]) content['startindex'] = startindex + prv = get_provider_by_type(collections[dataset]['providers'], + 'feature') + if 'title_field' in prv: + content['title_field'] = prv['title_field'] + content['id_field'] = prv['id_field'] content = render_j2_template(self.config, 'collections/items/index.html', @@ -1120,6 +1126,7 @@ class API: return headers_, 400, to_json(exception, self.pretty_print) LOGGER.debug('Loading provider') + try: p = load_plugin('provider', get_provider_by_type( collections[dataset]['providers'], 'feature')) @@ -1211,6 +1218,12 @@ class API: headers_['Content-Type'] = 'text/html' content['title'] = collections[dataset]['title'] + prv = get_provider_by_type(collections[dataset]['providers'], + 'feature') + content['id_field'] = prv['id_field'] + if 'title_field' in prv: + content['title_field'] = prv['title_field'] + content = render_j2_template(self.config, 'collections/items/item.html', content) diff --git a/pygeoapi/templates/collections/items/index.html b/pygeoapi/templates/collections/items/index.html index e55bbb1..97ffbcf 100644 --- a/pygeoapi/templates/collections/items/index.html +++ b/pygeoapi/templates/collections/items/index.html @@ -74,10 +74,14 @@ id - {% for k, v in data['features'][0]['properties'].items() %} - {% if loop.index < 5 %} - {{ k }} + {% if data['title_field'] %} + {{ data['title_field'] }} {% endif %} + {% for k, v in data['features'][0]['properties'].items() %} + {# start with id & title then take first 5 columns for table #} + {% if loop.index < 5 and k != data['id_field'] and k != data['title_field'] %} + {{ k }} + {% endif %} {% endfor %} @@ -85,11 +89,14 @@ {% for ft in data['features'] %} {{ ft.id }} - {% for k, v in ft['properties'].items() %} - {% if loop.index < 5 %} + {% if data['title_field'] %} + {{ ft['properties'][data['title_field']] }} + {% endif %} + {% for k, v in ft['properties'].items() %} + {% if loop.index < 5 and k != data['id_field'] and k != data['title_field'] %} {{ v | urlize(20) }} {% endif %} - {% endfor %} + {% endfor %} {% endfor %} @@ -118,7 +125,7 @@ var items = new L.GeoJSON(geojson_data, { onEachFeature: function (feature, layer) { var url = '{{ data['items_path'] }}/' + feature.id + '?f=html'; - var html = '' + feature.id + ''; + var html = '' + feature.id + '. ' + feature['properties']['{{ data['title_field'] }}'] + ''; layer.bindPopup(html); } }); diff --git a/pygeoapi/templates/collections/items/item.html b/pygeoapi/templates/collections/items/item.html index eaeacae..aa3637a 100644 --- a/pygeoapi/templates/collections/items/item.html +++ b/pygeoapi/templates/collections/items/item.html @@ -10,7 +10,7 @@ {{ v | urlize() }} {% endif %} {%- endmacro %} -{% block title %}{{ super() }} {{ data['title'] }} - {{ data['id'] }}{% endblock %} +{% block title %}{{ super() }} {{ data['title'] }} - {{ data['id'] }}. {{ data['properties'][data['title_field']] }}{% endblock %} {% block crumbs %}{{ super() }} / Collections {% for link in data['links'] %} @@ -19,7 +19,7 @@ {% endif %} {% endfor %} / Items -/ Item {{ data['id'] }} +/ {{ data['id'] }}. {{ data['properties'][data['title_field']] }} {% endblock %} {% block extrahead %} @@ -30,7 +30,7 @@
-

Item {{ data['id'] }}

+

{{ data['id'] }}. {{ data['properties'][data['title_field']] }}

@@ -72,20 +72,22 @@ {{ data.id }} {% for k, v in data['properties'].items() %} - - {{ k }} - {% if k == 'links' %} - - - - {% else %} - {{ render_item_value(v, 80) }} + {% if k != data['id_field'] %} + + {{ k }} + {% if k == 'links' %} + + + + {% else %} + {{ render_item_value(v, 80) }} + {% endif %} + {% endif %} - {% endfor %}