3131Returns content as linked data representations
3232"""
3333
34- import json
3534import logging
3635from typing import Callable
3736
38- from pygeoapi .util import is_url , render_j2_template
37+ from pygeoapi .util import is_url , render_j2_template , url_join
3938from pygeoapi import l10n
4039from shapely .geometry import shape
4140from shapely .ops import unary_union
@@ -189,30 +188,22 @@ def geojson2jsonld(cls, data: dict, dataset: str,
189188 :returns: string of rendered JSON (GeoJSON-LD)
190189 """
191190
192- LOGGER .debug ('Fetching context and template from resource configuration' )
193- jsonld = cls .config ['resources' ][dataset ].get ('linked-data' , {})
194- ds_url = f"{ cls .get_collections_url ()} /{ dataset } "
195-
196- context = jsonld .get ('context' , []).copy ()
197- template = jsonld .get ('item_template' , None )
191+ LOGGER .debug ('Fetching context from resource configuration' )
192+ context = cls .config ['resources' ][dataset ].get ('context' , []).copy ()
193+ templates = cls .get_dataset_templates (dataset )
198194
199195 defaultVocabulary = {
200196 'schema' : 'https://schema.org/' ,
197+ 'gsp' : 'http://www.opengis.net/ont/geosparql#' ,
201198 'type' : '@type'
202199 }
203200
204201 if identifier :
205- # Single jsonld
206- defaultVocabulary .update ({
207- 'gsp' : 'http://www.opengis.net/ont/geosparql#'
208- })
209-
210202 # Expand properties block
211203 data .update (data .pop ('properties' ))
212204
213205 # Include multiple geometry encodings
214206 if (data .get ('geometry' ) is not None ):
215- data ['type' ] = 'schema:Place'
216207 jsonldify_geometry (data )
217208
218209 data ['@id' ] = identifier
@@ -224,6 +215,7 @@ def geojson2jsonld(cls, data: dict, dataset: str,
224215 'FeatureCollection' : 'schema:itemList'
225216 })
226217
218+ ds_url = url_join (cls .get_collections_url (), dataset )
227219 data ['@id' ] = ds_url
228220
229221 for i , feature in enumerate (data ['features' ]):
@@ -233,9 +225,15 @@ def geojson2jsonld(cls, data: dict, dataset: str,
233225 if not is_url (str (identifier_ )):
234226 identifier_ = f"{ ds_url } /items/{ feature ['id' ]} " # noqa
235227
228+ # Include multiple geometry encodings
229+ if feature .get ('geometry' ) is not None :
230+ jsonldify_geometry (feature )
231+
236232 data ['features' ][i ] = {
237233 '@id' : identifier_ ,
238- 'type' : 'schema:Place'
234+ 'type' : 'schema:Place' ,
235+ ** feature .pop ('properties' ),
236+ ** feature
239237 }
240238
241239 if data .get ('timeStamp' , False ):
@@ -248,17 +246,21 @@ def geojson2jsonld(cls, data: dict, dataset: str,
248246 ** data
249247 }
250248
251- if None in (template , identifier ):
252- return ldjsonData
249+ if identifier :
250+ # Render jsonld template for single item
251+ LOGGER .debug ('Rendering JSON-LD item template' )
252+ content = render_j2_template (
253+ cls .tpl_config , templates ,
254+ 'collections/items/item.jsonld' , ldjsonData )
255+
253256 else :
254- # Render jsonld template for single item with template configured
255- LOGGER .debug (f 'Rendering JSON-LD template: { template } ' )
257+ # Render jsonld template for /items
258+ LOGGER .debug ('Rendering JSON-LD items template' )
256259 content = render_j2_template (
257- cls .config , cls . config [ 'server' ][ ' templates' ] ,
258- template , ldjsonData )
260+ cls .tpl_config , templates ,
261+ 'collections/items/index.jsonld' , ldjsonData )
259262
260- ldjsonData = json .loads (content )
261- return ldjsonData
263+ return content
262264
263265
264266def jsonldify_geometry (feature : dict ) -> None :
@@ -271,6 +273,8 @@ def jsonldify_geometry(feature: dict) -> None:
271273 :returns: None
272274 """
273275
276+ feature ['type' ] = 'schema:Place'
277+
274278 geo = feature .get ('geometry' )
275279 geom = shape (geo )
276280
@@ -287,7 +291,11 @@ def jsonldify_geometry(feature: dict) -> None:
287291 }
288292
289293 # Schema geometry
290- feature ['schema:geo' ] = geom2schemageo (geom )
294+ try :
295+ feature ['schema:geo' ] = geom2schemageo (geom )
296+ except AttributeError :
297+ msg = f'Unable to parse schema geometry for { feature ["id" ]} '
298+ LOGGER .warning (msg )
291299
292300
293301def geom2schemageo (geom : shape ) -> dict :
0 commit comments