Skip to content

Commit 03110b1

Browse files
committed
Use verbatim context and inject it even in plain JSON
1 parent 46eaaf5 commit 03110b1

File tree

2 files changed

+70
-50
lines changed

2 files changed

+70
-50
lines changed

pygeoapi/api/itemtypes.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,6 +956,9 @@ def get_collection_item(api: API, request: APIRequest,
956956
# locale (or fallback default locale)
957957
l10n.set_response_language(headers, prv_locale, request.locale)
958958

959+
config_dataset = api.config['resources'][dataset]
960+
linked_data = config_dataset.get('linked-data')
961+
959962
if request.format == F_HTML: # render
960963
tpl_config = api.get_dataset_templates(dataset)
961964
content['title'] = l10n.translate(collections[dataset]['title'],
@@ -973,7 +976,12 @@ def get_collection_item(api: API, request: APIRequest,
973976
content, request.locale)
974977
return headers, HTTPStatus.OK, content
975978

976-
elif request.format == F_JSONLD:
979+
elif request.format == F_JSONLD or (
980+
request.format == F_JSON
981+
and linked_data
982+
and all(linked_data.get(k)
983+
for k in ('context', 'inject_verbatim_context'))
984+
):
977985
content = geojson2jsonld(
978986
api, content, dataset, uri, (p.uri_field or 'id')
979987
)

pygeoapi/linked_data.py

Lines changed: 61 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -195,59 +195,71 @@ def geojson2jsonld(cls, data: dict, dataset: str,
195195
context = linked_data.get('context', []).copy()
196196
templates = cls.get_dataset_templates(dataset)
197197

198-
defaultVocabulary = {
199-
'schema': 'https://schema.org/',
200-
'gsp': 'http://www.opengis.net/ont/geosparql#',
201-
'type': '@type'
202-
}
203-
204-
if identifier:
205-
# Expand properties block
206-
data.update(data.pop('properties'))
207-
208-
# Include multiple geometry encodings
209-
if (data.get('geometry') is not None):
210-
jsonldify_geometry(data)
211-
212-
data['@id'] = identifier
213-
198+
if (identifier and context
199+
and linked_data.get('inject_verbatim_context', False)):
200+
ldjsonData = {
201+
'@context': context,
202+
**data
203+
}
204+
if replace_id_field := linked_data.get('replace_id_field'):
205+
ldjsonData[replace_id_field] = identifier
214206
else:
215-
# Collection of jsonld
216-
defaultVocabulary.update({
217-
'features': 'schema:itemListElement',
218-
'FeatureCollection': 'schema:itemList'
219-
})
220-
221-
ds_url = url_join(cls.get_collections_url(), dataset)
222-
data['@id'] = ds_url
223-
224-
for i, feature in enumerate(data['features']):
225-
# Get URI for each feature
226-
identifier_ = feature.get(id_field,
227-
feature['properties'].get(id_field, ''))
228-
if not is_url(str(identifier_)):
229-
identifier_ = f"{ds_url}/items/{feature['id']}" # noqa
230207

231-
# Include multiple geometry encodings
232-
if feature.get('geometry') is not None:
233-
jsonldify_geometry(feature)
234-
235-
data['features'][i] = {
236-
'@id': identifier_,
237-
'type': 'schema:Place',
238-
**feature.pop('properties'),
239-
**feature
240-
}
241-
242-
if data.get('timeStamp', False):
243-
data['https://schema.org/sdDatePublished'] = data.pop('timeStamp')
208+
defaultVocabulary = {
209+
'schema': 'https://schema.org/',
210+
'gsp': 'http://www.opengis.net/ont/geosparql#',
211+
'type': '@type'
212+
}
244213

245-
data['links'] = data.pop('links')
214+
if identifier:
215+
# Expand properties block
216+
data.update(data.pop('properties'))
246217

247-
ldjsonData = {
248-
'@context': [defaultVocabulary, *(context or [])],
249-
**data
250-
}
218+
# Include multiple geometry encodings
219+
if (data.get('geometry') is not None):
220+
jsonldify_geometry(data)
221+
222+
data['@id'] = identifier
223+
224+
else:
225+
# Collection of jsonld
226+
defaultVocabulary.update({
227+
'features': 'schema:itemListElement',
228+
'FeatureCollection': 'schema:itemList'
229+
})
230+
231+
ds_url = url_join(cls.get_collections_url(), dataset)
232+
data['@id'] = ds_url
233+
234+
for i, feature in enumerate(data['features']):
235+
# Get URI for each feature
236+
identifier_ = feature.get(
237+
id_field,
238+
feature['properties'].get(id_field, '')
239+
)
240+
if not is_url(str(identifier_)):
241+
identifier_ = f"{ds_url}/items/{feature['id']}" # noqa
242+
243+
# Include multiple geometry encodings
244+
if feature.get('geometry') is not None:
245+
jsonldify_geometry(feature)
246+
247+
data['features'][i] = {
248+
'@id': identifier_,
249+
'type': 'schema:Place',
250+
**feature.pop('properties'),
251+
**feature
252+
}
253+
254+
if data.get('timeStamp', False):
255+
data['https://schema.org/sdDatePublished'] = data.pop('timeStamp')
256+
257+
data['links'] = data.pop('links')
258+
259+
ldjsonData = {
260+
'@context': [defaultVocabulary, *(context or [])],
261+
**data
262+
}
251263

252264
if identifier:
253265
# Render jsonld template for single item

0 commit comments

Comments
 (0)