Skip to content

Commit

Permalink
Merge pull request #18 from internetofwater/json-ld
Browse files Browse the repository at this point in the history
Update JSON-LD responses
  • Loading branch information
webb-ben authored Sep 26, 2023
2 parents 1b3e637 + b484877 commit a51d16f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 10 deletions.
23 changes: 16 additions & 7 deletions nldi/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion nldi/flask_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))


Expand Down
16 changes: 14 additions & 2 deletions nldi/openapi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,11 @@ def get_oas(cfg):
'schema': {
'$ref': '#/components/schemas/FeatureCollection' # noqa
}
},
'application/ld+json': {
'schema': {
'$ref': '#/components/schemas/FeatureCollection' # noqa
}
}
}
},
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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': {
Expand Down
1 change: 1 addition & 0 deletions nldi/templates/FeatureCollectionGraph.j2
Original file line number Diff line number Diff line change
@@ -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 %}]}
1 change: 1 addition & 0 deletions nldi/templates/FeatureGraph.j2
Original file line number Diff line number Diff line change
@@ -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 %}}

0 comments on commit a51d16f

Please sign in to comment.