Skip to content

Commit

Permalink
add support for item schema string format (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
tomkralidis committed Jun 23, 2024
1 parent c607be7 commit 0026b41
Show file tree
Hide file tree
Showing 11 changed files with 81 additions and 11 deletions.
6 changes: 6 additions & 0 deletions locale/bs/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,9 @@ msgstr ""

msgid "Record collections in this service"
msgstr ""

msgid "Display Schema"
msgstr ""

msgid "Display Schema of"
msgstr ""
6 changes: 6 additions & 0 deletions locale/de/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -688,3 +688,9 @@ msgstr ""

msgid "Record collections in this service"
msgstr ""

msgid "Display Schema"
msgstr ""

msgid "Display Schema of"
msgstr ""
6 changes: 6 additions & 0 deletions locale/en/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -690,3 +690,9 @@ msgstr ""

msgid "Record collections in this service"
msgstr ""

msgid "Display Schema"
msgstr ""

msgid "Display Schema of"
msgstr ""
6 changes: 6 additions & 0 deletions locale/fr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -697,3 +697,9 @@ msgstr ""

msgid "Record collections in this service"
msgstr ""

msgid "Display Schema"
msgstr ""

msgid "Display Schema of"
msgstr ""
6 changes: 6 additions & 0 deletions locale/sr/LC_MESSAGES/messages.po
Original file line number Diff line number Diff line change
Expand Up @@ -638,3 +638,9 @@ msgstr ""

msgid "Record collections in this service"
msgstr ""

msgid "Display Schema"
msgstr ""

msgid "Display Schema of"
msgstr ""
2 changes: 2 additions & 0 deletions pygeoapi/api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1353,6 +1353,8 @@ def get_collection_schema(self, request: Union[APIRequest, Any],

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

if k == p.id_field:
schema['properties'][k]['x-ogc-role'] = 'id'
Expand Down
2 changes: 2 additions & 0 deletions pygeoapi/api/itemtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ def get_collection_queryables(api: API, request: Union[APIRequest, Any],
'title': k,
'type': v['type']
}
if v.get('format') is not None:
queryables['properties'][k]['format'] = v['format']
if 'values' in v:
queryables['properties'][k]['enum'] = v['values']

Expand Down
42 changes: 32 additions & 10 deletions pygeoapi/provider/postgresql.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,39 +199,61 @@ def get_fields(self):
:returns: dict of fields
"""

LOGGER.debug('Get available fields/properties')

fields = {}

# sql-schema only allows these types, so we need to map from sqlalchemy
# string, number, integer, object, array, boolean, null,
# https://json-schema.org/understanding-json-schema/reference/type.html
column_type_map = {
str: 'string',
float: 'number',
int: 'integer',
bool: 'boolean',
bool: 'boolean'
}
default_type = 'string'

# https://json-schema.org/understanding-json-schema/reference/string#built-in-formats # noqa
column_format_map = {
'date': 'date',
'timestamp': 'date-time',
'time': 'time',
'interval': 'duration'
}
default_value = 'string'

def _column_type_to_json_schema_type(column_type):
try:
python_type = column_type.python_type
except NotImplementedError:
LOGGER.warning(f'Unsupported column type {column_type}')
return default_value
return default_type
else:
try:
return column_type_map[python_type]
except KeyError:
LOGGER.warning(f'Unsupported column type {column_type}')
return default_value
return default_type

return {
str(column.name): {
'type': _column_type_to_json_schema_type(column.type)
def _column_format_to_json_schema_format(column_type):
try:
ct = str(column_type).lower()
return column_format_map[ct]
except KeyError:
LOGGER.warning(f'Unsupported column type {column_type}')
return None

for column in self.table_model.__table__.columns:
if column.name == self.geom:
continue

fields[column.name] = {
'type': _column_type_to_json_schema_type(column.type),
'format': _column_format_to_json_schema_format(column.type)
}
for column in self.table_model.__table__.columns
if column.name != self.geom # Exclude geometry column
}

return fields

def get(self, identifier, crs_transform_spec=None, **kwargs):
"""
Expand Down
8 changes: 8 additions & 0 deletions pygeoapi/templates/collections/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@ <h3>{% trans %}Queryables{% endtrans %}</h3>
{% trans %}Display Queryables of{% endtrans %} "{{ data['title'] }}"</a></div>
</li>
</ul>
<h3>{% trans %}Schema{% endtrans %}</h3>
<ul>
<li>
<div>
<a title="{% trans %}Display Schema{% endtrans %}" href="{{ data['collections_path'] }}/{{ data['id'] }}/schema">
{% trans %}Display Schema of{% endtrans %} "{{ data['title'] }}"</a></div>
</li>
</ul>
{% for provider in config['resources'][data['id']]['providers'] %}
{% if 'tile' in provider['type'] %}
<h3>{% trans %}Tiles{% endtrans %}</h3>
Expand Down
3 changes: 3 additions & 0 deletions pygeoapi/templates/collections/queryables.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ <h3>{% trans %}Queryables{% endtrans %}</h3>
<li><a href="{{ qinfo['$ref'] }}">{{ qname }} </a></li>
{% else %}
<li>{{ qname }} (<code>{{ qinfo['type'] }}</code>)
{% if 'format' in qinfo %}
(<code>{{ qinfo['format'] }}</code>)
{% endif %}
{% if 'enum' in qinfo %}
<ul>
{% for value in qinfo['enum'] %}
Expand Down
5 changes: 4 additions & 1 deletion pygeoapi/templates/collections/schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ <h3>{% trans %}Schema{% endtrans %}</h3>
{% if qname == 'geometry' %}
<td><a href="{{ qinfo['$ref'] }}">{{ qname }} </a></td>
{% else %}
<td><code>{{ qinfo['type'] }}</code></td>
<td><code>{{ qinfo['type'] }}</code>
{% if qinfo['format'] is not none %}
(<code>{{ qinfo['format'] }}</code>)
{% endif %}
{% endif %}
<td>{{ qinfo['x-ogc-unit'] }}</td>
<td>
Expand Down

0 comments on commit 0026b41

Please sign in to comment.