Skip to content

Commit 58289e3

Browse files
committed
add support for item schema string format (#1691)
1 parent c607be7 commit 58289e3

File tree

11 files changed

+82
-12
lines changed

11 files changed

+82
-12
lines changed

locale/bs/LC_MESSAGES/messages.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,9 @@ msgstr ""
638638

639639
msgid "Record collections in this service"
640640
msgstr ""
641+
642+
msgid "Display Schema"
643+
msgstr ""
644+
645+
msgid "Display Schema of"
646+
msgstr ""

locale/de/LC_MESSAGES/messages.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,3 +688,9 @@ msgstr ""
688688

689689
msgid "Record collections in this service"
690690
msgstr ""
691+
692+
msgid "Display Schema"
693+
msgstr ""
694+
695+
msgid "Display Schema of"
696+
msgstr ""

locale/en/LC_MESSAGES/messages.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -690,3 +690,9 @@ msgstr ""
690690

691691
msgid "Record collections in this service"
692692
msgstr ""
693+
694+
msgid "Display Schema"
695+
msgstr ""
696+
697+
msgid "Display Schema of"
698+
msgstr ""

locale/fr/LC_MESSAGES/messages.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -697,3 +697,9 @@ msgstr ""
697697

698698
msgid "Record collections in this service"
699699
msgstr ""
700+
701+
msgid "Display Schema"
702+
msgstr ""
703+
704+
msgid "Display Schema of"
705+
msgstr ""

locale/sr/LC_MESSAGES/messages.po

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,3 +638,9 @@ msgstr ""
638638

639639
msgid "Record collections in this service"
640640
msgstr ""
641+
642+
msgid "Display Schema"
643+
msgstr ""
644+
645+
msgid "Display Schema of"
646+
msgstr ""

pygeoapi/api/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,6 +1353,8 @@ def get_collection_schema(self, request: Union[APIRequest, Any],
13531353

13541354
for k, v in p.fields.items():
13551355
schema['properties'][k] = v
1356+
if v.get('format') is None:
1357+
schema['properties'][k].pop('format')
13561358

13571359
if k == p.id_field:
13581360
schema['properties'][k]['x-ogc-role'] = 'id'

pygeoapi/api/itemtypes.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ def get_collection_queryables(api: API, request: Union[APIRequest, Any],
167167
'title': k,
168168
'type': v['type']
169169
}
170+
if v.get('format') is not None:
171+
queryables['properties'][k]['format'] = v['format']
170172
if 'values' in v:
171173
queryables['properties'][k]['enum'] = v['values']
172174

pygeoapi/provider/postgresql.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -199,39 +199,61 @@ def get_fields(self):
199199
200200
:returns: dict of fields
201201
"""
202+
202203
LOGGER.debug('Get available fields/properties')
203204

205+
fields = {}
206+
204207
# sql-schema only allows these types, so we need to map from sqlalchemy
205208
# string, number, integer, object, array, boolean, null,
206209
# https://json-schema.org/understanding-json-schema/reference/type.html
207210
column_type_map = {
208211
str: 'string',
209212
float: 'number',
210213
int: 'integer',
211-
bool: 'boolean',
214+
bool: 'boolean'
215+
}
216+
default_type = 'string'
217+
218+
# https://json-schema.org/understanding-json-schema/reference/string#built-in-formats # noqa
219+
column_format_map = {
220+
'date': 'date',
221+
'timestamp': 'date-time',
222+
'time': 'time',
223+
'interval': 'duration'
212224
}
213-
default_value = 'string'
214225

215226
def _column_type_to_json_schema_type(column_type):
216227
try:
217228
python_type = column_type.python_type
218229
except NotImplementedError:
219230
LOGGER.warning(f'Unsupported column type {column_type}')
220-
return default_value
231+
return default_type
221232
else:
222233
try:
223234
return column_type_map[python_type]
224235
except KeyError:
225236
LOGGER.warning(f'Unsupported column type {column_type}')
226-
return default_value
237+
return default_type
227238

228-
return {
229-
str(column.name): {
230-
'type': _column_type_to_json_schema_type(column.type)
239+
def _column_format_to_json_schema_format(column_type):
240+
try:
241+
ct = str(column_type).lower()
242+
return column_format_map[ct]
243+
except KeyError:
244+
LOGGER.warning(f'Unsupported column type {column_type}')
245+
return None
246+
247+
for column in self.table_model.__table__.columns:
248+
if column.name == self.geom:
249+
continue
250+
251+
fields[column.name] = {
252+
'type': _column_type_to_json_schema_type(column.type),
253+
'format': _column_format_to_json_schema_format(column.type)
231254
}
232-
for column in self.table_model.__table__.columns
233-
if column.name != self.geom # Exclude geometry column
234-
}
255+
256+
return fields
235257

236258
def get(self, identifier, crs_transform_spec=None, **kwargs):
237259
"""

pygeoapi/templates/collections/collection.html

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@ <h3>{% trans %}Queryables{% endtrans %}</h3>
6464
{% trans %}Display Queryables of{% endtrans %} "{{ data['title'] }}"</a></div>
6565
</li>
6666
</ul>
67+
<h3>{% trans %}Schema{% endtrans %}</h3>
68+
<ul>
69+
<li>
70+
<div>
71+
<a title="{% trans %}Display Schema{% endtrans %}" href="{{ data['collections_path'] }}/{{ data['id'] }}/schema">
72+
{% trans %}Display Schema of{% endtrans %} "{{ data['title'] }}"</a></div>
73+
</li>
74+
</ul>
6775
{% for provider in config['resources'][data['id']]['providers'] %}
6876
{% if 'tile' in provider['type'] %}
6977
<h3>{% trans %}Tiles{% endtrans %}</h3>

pygeoapi/templates/collections/queryables.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@ <h3>{% trans %}Queryables{% endtrans %}</h3>
2121
<li><a href="{{ qinfo['$ref'] }}">{{ qname }} </a></li>
2222
{% else %}
2323
<li>{{ qname }} (<code>{{ qinfo['type'] }}</code>)
24+
{% if 'format' in qinfo %}
25+
(<code>{{ qinfo['format'] }}</code>)
26+
{% endif %}
2427
{% if 'enum' in qinfo %}
2528
<ul>
2629
{% for value in qinfo['enum'] %}

pygeoapi/templates/collections/schema.html

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,18 @@ <h3>{% trans %}Schema{% endtrans %}</h3>
2121
<th>Type</th>
2222
<th>Units</th>
2323
<th>Values</th>
24-
<ul>
2524
{% for qname, qinfo in data['properties'].items() %}
2625
<tr>
2726
<td>{{ qname }}</td>
2827
<td>{{ qinfo['title'] }}</td>
2928
{% if qname == 'geometry' %}
3029
<td><a href="{{ qinfo['$ref'] }}">{{ qname }} </a></td>
3130
{% else %}
32-
<td><code>{{ qinfo['type'] }}</code></td>
31+
<td><code>{{ qinfo['type'] }}</code>
32+
{% if 'format' in qinfo %}
33+
(<code>{{ qinfo['format'] }}</code>)
34+
{% endif %}
35+
</td>
3336
{% endif %}
3437
<td>{{ qinfo['x-ogc-unit'] }}</td>
3538
<td>

0 commit comments

Comments
 (0)