Skip to main content

Outputting properly formatted data via templating

tip

It is recommended to take a look at the geoconnex JSON-LD reference or the pygeoapi templating docs before beginning to template your data.

pygeoapi can leverage Jinja2 templates to change the structure of the JSON-LD data output.

  • Templating is needed in order to change the default JSON-LD output by pygeoapi into either location-oriented or dataset-oriented JSON-LD output format for Geoconnex.
  • To add extra templates, open your pygeoapi config file and create a new collection block that links to the new item_template you made for your JSON-LD linked-data.

An example template walkthrough

In this example we will show how to template the output of a USGS SensorThings API. This general process will be analogous templating your own data.

tip

To follow along with this example, clone our sample repo and run docker compose up --build

Before we start, we first need to determine whether our example should be location-oriented or dataset-oriented. You will need to do the same for your own data. In this example it is location-oriented.

First, make sure you are ingesting your data properly into pygeoapi. This is done by editing the pygeoapi config file. Check out the ingesting common providers section or the pygeoapi docs for more guidance on how to do this.

In this example we will be templating the data from https://labs.waterdata.usgs.gov/sta/v1.1/Things

local.config.yml
# ... Rest of configuration file abbreviated for brevity
USGS-LocationOriented/Things:
type: collection
title: Location oriented JSON-LD output for USGS Sensor Things code
description: United States Geological Survey (USGS.) SensorThings API
keywords:
- Things
- SensorThings
- USGS
# Don't include any templates for the time being until we've created it
# linked-data:
# item_template: usgs-location-oriented.j2
links:
- type: application/html
rel: canonical
title: data source
href: https://labs.waterdata.usgs.gov
hreflang: en-US
extents:
spatial:
bbox: [-170, 15, -51, 72]
crs: http://www.opengis.net/def/crs/OGC/1.3/CRS84
temporal:
begin: null
end: null
providers:
- type: feature
name: SensorThings

# Your existing API URL goes here
data: https://labs.waterdata.usgs.gov/sta/v1.1/Things