diff --git a/nldi/api.py b/nldi/api.py index 72aa407..4e8e362 100644 --- a/nldi/api.py +++ b/nldi/api.py @@ -32,7 +32,7 @@ import logging from typing import Any, Tuple, Union -from pygeoapi.api import APIRequest, FORMAT_TYPES, F_HTML, F_JSON +from pygeoapi.api import APIRequest, FORMAT_TYPES, F_HTML, F_JSON, F_JSONLD from pygeoapi.util import get_base_url, render_j2_template from nldi import __version__ @@ -501,8 +501,13 @@ def get_source_features(self, request: Union[APIRequest, Any], HTTPStatus.INTERNAL_SERVER_ERROR, headers, request.format, 'NoApplicableCode', msg) - # content = stream_j2_template('FeatureGraph.j2', features) - content = stream_j2_template('FeatureCollection.j2', features) + if request.format == F_JSONLD: + if identifier: + content = stream_j2_template('FeatureGraph.j2', features) + else: + content = stream_j2_template('FeatureCollectionGraph.j2', features) # noqa + else: + content = stream_j2_template('FeatureCollection.j2', features) return headers, HTTPStatus.OK, content @@ -642,9 +647,9 @@ def get_navigation_info(self, request: Union[APIRequest, Any], return headers, HTTPStatus.OK, to_json(content, self.pretty_print) @pre_process - def get_fl_navigation(self, request: Union[APIRequest, Any], - source_name: str, identifier: str, nav_mode: str - ) -> Tuple[dict, int, str]: + def get_flowlines(self, request: Union[APIRequest, Any], + source_name: str, identifier: str, nav_mode: str + ) -> Tuple[dict, int, str]: """ Provide navigation query @@ -826,7 +831,11 @@ def get_navigation(self, request: Union[APIRequest, Any], nav_results = self.func.get_navigation(nav_mode, start_comid, distance) features = plugin.lookup_navigation(nav_results) - content = stream_j2_template('FeatureCollection.j2', features) + + if request.format == F_JSONLD: + content = stream_j2_template('FeatureCollectionGraph.j2', features) + else: + content = stream_j2_template('FeatureCollection.j2', features) return headers, HTTPStatus.OK, content diff --git a/nldi/flask_app.py b/nldi/flask_app.py index 8f261a2..825dd15 100644 --- a/nldi/flask_app.py +++ b/nldi/flask_app.py @@ -204,7 +204,7 @@ def get_flowline_navigation(source_name=None, identifier=None, nav_mode=None): :returns: HTTP response """ - return get_response(API_.get_fl_navigation( + return get_response(API_.get_flowlines( request, source_name, identifier, nav_mode)) diff --git a/nldi/openapi/__init__.py b/nldi/openapi/__init__.py index 4aab97f..2d8ba84 100644 --- a/nldi/openapi/__init__.py +++ b/nldi/openapi/__init__.py @@ -262,6 +262,11 @@ def get_oas(cfg): 'schema': { '$ref': '#/components/schemas/FeatureCollection' # noqa } + }, + 'application/ld+json': { + 'schema': { + '$ref': '#/components/schemas/FeatureCollection' # noqa + } } } }, @@ -334,6 +339,11 @@ def get_oas(cfg): '$ref': '#/components/schemas/FeatureCollection' # noqa } }, + 'application/ld+json': { + 'schema': { + '$ref': '#/components/schemas/FeatureCollection' # noqa + } + }, 'application/vnd.geo+json': { 'schema': { '$ref': '#/components/schemas/FeatureCollection' # noqa @@ -511,10 +521,12 @@ def get_oas(cfg): 'operationId': f'{src_title}NavigationFlowlines', 'parameters': [ *parameters, - {'$ref': '#/components/parameters/navigationMode'}, + {'$ref': '#/components/parameters/navigationModePP'}, {'$ref': '#/components/parameters/distance'}, + {'$ref': '#/components/parameters/stopComid'}, {'$ref': '#/components/parameters/trimStart'}, - {'$ref': '#/components/parameters/trimTolerance'} + {'$ref': '#/components/parameters/trimTolerance'}, + {'$ref': '#/components/parameters/legacy'} ], 'responses': { '200': { diff --git a/nldi/templates/FeatureCollectionGraph.j2 b/nldi/templates/FeatureCollectionGraph.j2 new file mode 100644 index 0000000..f67b324 --- /dev/null +++ b/nldi/templates/FeatureCollectionGraph.j2 @@ -0,0 +1 @@ +{"@context":[{"schema":"https://schema.org/","geo":"schema:geo","hyf":"https://www.opengis.net/def/schema/hy_features/hyf/","gsp":"http://www.opengis.net/ont/geosparql#","name":"schema:name","comid":{"@id":"schema:geoWithin","@type":"@id"},"hyf:linearElement":{"@type":"@id"}}],"@id":"_:graph","@graph":[{% for feature in data %}{% set props = feature.properties %}{"@id":"{{ props.uri }}","@type":"https://www.opengis.net/def/schema/hy_features/hyf/HY_HydroLocation","schema:subjectOf":{"@type":"schema:CreativeWork","schema:identifier":"{{ props.source }}","schema:name":"{{ props.sourceName }}"},"name":"{{ props.name }}",{% if props.comid %}"comid":"https://geoconnex.us/nhdplusv2/comid/{{ props.comid }}",{% endif %}"hyf:referencedPosition":[{% if props.mainstem %}{"hyf:HY_IndirectPosition":{"hyf:linearElement":"{{ props.mainstem }}"}}{% endif %}{% if props.measure and props.reachcode %}{% if props.mainstem %},{% endif %}{"hyf:HY_IndirectPosition":{"hyf:distanceExpression":{"hyf:HY_IndirectPosition":{"hyf:interpolative":{{ props.measure }}}},"hyf:distanceDescription":{"hyf:HY_DistanceDescription":"upstream"},"hyf:HY_IndirectPosition":{"hyf:linearElement":"https://geoconnex.us/nhdplusv2/reachcode/{{ props.reachcode }}"}}}{% endif %}]{% if feature.geometry.type == "Point" %}{% set lon = feature.geometry.coordinates[0] %}{% set lat = feature.geometry.coordinates[1] %},"geo":{"@type":"schema:GeoCoordinates","schema:longitude":{{ lon }},"schema:latitude":{{ lat }}},"gsp:hasGeometry":{"@type":"http://www.opengis.net/ont/sf#Point","gsp:asWKT":{"@value":"POINT({{ lon }} {{ lat }})","@type":"http://www.opengis.net/ont/geosparql#wktLiteral"}}{% endif %}}{% if not loop.last %},{% endif %}{% endfor %}]} diff --git a/nldi/templates/FeatureGraph.j2 b/nldi/templates/FeatureGraph.j2 new file mode 100644 index 0000000..df22b08 --- /dev/null +++ b/nldi/templates/FeatureGraph.j2 @@ -0,0 +1 @@ +{"@context":[{"schema":"https://schema.org/","geo":"schema:geo","hyf":"https://www.opengis.net/def/schema/hy_features/hyf/","gsp":"http://www.opengis.net/ont/geosparql#","name":"schema:name","comid":{"@id":"schema:geoWithin","@type":"@id"},"hyf:linearElement":{"@type":"@id"}}],{% for feature in data %}{% set props = feature.properties %}"@id":"{{ props.uri }}","@type":"https://www.opengis.net/def/schema/hy_features/hyf/HY_HydroLocation","schema:subjectOf":{"@type":"schema:CreativeWork","schema:identifier":"{{ props.source }}","schema:name":"{{ props.sourceName }}"},"name":"{{ props.name }}",{% if props.comid %}"comid":"https://geoconnex.us/nhdplusv2/comid/{{ props.comid }}",{% endif %}"hyf:referencedPosition":[{% if props.mainstem %}{"hyf:HY_IndirectPosition":{"hyf:linearElement":"{{ props.mainstem }}"}}{% endif %}{% if props.measure and props.reachcode %}{% if props.mainstem %},{% endif %}{"hyf:HY_IndirectPosition":{"hyf:distanceExpression":{"hyf:HY_IndirectPosition":{"hyf:interpolative":{{ props.measure }}}},"hyf:distanceDescription":{"hyf:HY_DistanceDescription":"upstream"},"hyf:HY_IndirectPosition":{"hyf:linearElement":"https://geoconnex.us/nhdplusv2/reachcode/{{ props.reachcode }}"}}}{% endif %}]{% if feature.geometry.type == "Point" %}{% set lon = feature.geometry.coordinates[0] %}{% set lat = feature.geometry.coordinates[1] %},"geo":{"@type":"schema:GeoCoordinates","schema:longitude":{{ lon }},"schema:latitude":{{ lat }}},"gsp:hasGeometry":{"@type":"http://www.opengis.net/ont/sf#Point","gsp:asWKT":{"@value":"POINT({{ lon }} {{ lat }})","@type":"http://www.opengis.net/ont/geosparql#wktLiteral"}}{% endif %}{% endfor %}}