Skip to content

Commit

Permalink
Merge pull request #1692 from geopython/item-schema-format
Browse files Browse the repository at this point in the history
add support for item schema string format (#1691)
  • Loading branch information
tomkralidis authored Jun 24, 2024
2 parents c607be7 + 3790b45 commit d36ee2a
Show file tree
Hide file tree
Showing 13 changed files with 98 additions and 25 deletions.
2 changes: 1 addition & 1 deletion docs/source/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ Features
* certified OGC Compliant and Reference Implementation
* OGC API - Features
* OGC API - Environmental Data Retrieval
* OGC API - Tiles
* additionally implements
* OGC API - Coverages
* OGC API - Maps
* OGC API - Tiles
* OGC API - Processes
* OGC API - Records
* SpatioTemporal Asset Library
Expand Down
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', None)

if k == p.id_field:
schema['properties'][k]['x-ogc-role'] = 'id'
Expand Down
5 changes: 5 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 Expand Up @@ -1479,6 +1481,9 @@ def get_oas_30(cfg: dict, locale: str) -> tuple[list[dict[str, str]], dict[str,
else:
schema = type_

if schema.get('format') is None:
schema.pop('format', None)

path_ = f'{collection_name_path}/items'
paths[path_]['get']['parameters'].append({
'name': field,
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[str(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
7 changes: 5 additions & 2 deletions pygeoapi/templates/collections/schema.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,18 @@ <h3>{% trans %}Schema{% endtrans %}</h3>
<th>Type</th>
<th>Units</th>
<th>Values</th>
<ul>
{% for qname, qinfo in data['properties'].items() %}
<tr>
<td>{{ qname }}</td>
<td>{{ qinfo['title'] }}</td>
{% if qname == 'geometry' %}
<td><a href="{{ qinfo['$ref'] }}">{{ qname }} </a></td>
{% else %}
<td><code>{{ qinfo['type'] }}</code></td>
<td><code>{{ qinfo['type'] }}</code>
{% if 'format' in qinfo %}
(<code>{{ qinfo['format'] }}</code>)
{% endif %}
</td>
{% endif %}
<td>{{ qinfo['x-ogc-unit'] }}</td>
<td>
Expand Down
24 changes: 12 additions & 12 deletions tests/test_postgresql_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,18 +343,18 @@ def test_query_cql_properties_bbox_filters(config):
def test_get_fields(config):
# Arrange
expected_fields = {
'blockage': {'type': 'string'},
'covered': {'type': 'string'},
'depth': {'type': 'string'},
'layer': {'type': 'string'},
'name': {'type': 'string'},
'natural': {'type': 'string'},
'osm_id': {'type': 'integer'},
'tunnel': {'type': 'string'},
'water': {'type': 'string'},
'waterway': {'type': 'string'},
'width': {'type': 'string'},
'z_index': {'type': 'string'}
'blockage': {'type': 'string', 'format': None},
'covered': {'type': 'string', 'format': None},
'depth': {'type': 'string', 'format': None},
'layer': {'type': 'string', 'format': None},
'name': {'type': 'string', 'format': None},
'natural': {'type': 'string', 'format': None},
'osm_id': {'type': 'integer', 'format': None},
'tunnel': {'type': 'string', 'format': None},
'water': {'type': 'string', 'format': None},
'waterway': {'type': 'string', 'format': None},
'width': {'type': 'string', 'format': None},
'z_index': {'type': 'string', 'format': None}
}

# Act
Expand Down

0 comments on commit d36ee2a

Please sign in to comment.