Skip to content

Instantly share code, notes, and snippets.

@tomkralidis
Last active March 24, 2021 12:11
Show Gist options
  • Save tomkralidis/fcce80919d4e39b11a735544a552b814 to your computer and use it in GitHub Desktop.
Save tomkralidis/fcce80919d4e39b11a735544a552b814 to your computer and use it in GitHub Desktop.
pygeoapi Sample Process Example
# =================================================================
#
# Authors: Tom Kralidis <[email protected]>
#
# Copyright (c) 2021 Tom Kralidis
#
# Permission is hereby granted, free of charge, to any person
# obtaining a copy of this software and associated documentation
# files (the "Software"), to deal in the Software without
# restriction, including without limitation the rights to use,
# copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following
# conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
# OTHER DEALINGS IN THE SOFTWARE.
#
# =================================================================
import logging
import math
from pygeoapi.process.base import BaseProcessor, ProcessorExecuteError
LOGGER = logging.getLogger(__name__)
#: Process metadata and description
PROCESS_METADATA = {
'version': '0.2.0',
'id': 'my-sqrt-process',
'title': 'My square root Process',
'description': 'My square root process',
'keywords': ['process', 'square root', 'sqrt'],
'links': [{
'type': 'text/html',
'rel': 'canonical',
'title': 'information',
'href': 'https://example.org/my-sqrt-process',
'hreflang': 'en-US'
}],
'inputs': [{
'id': 'number',
'title': 'Number',
'abstract': 'Integer value',
'input': {
'literalDataDomain': {
'dataType': 'string',
'valueDefinition': {
'anyValue': True
}
}
},
'minOccurs': 1,
'maxOccurs': 1,
'metadata': None,
'keywords': ['number input']
}],
'outputs': [{
'id': 'sqrt',
'title': 'Square root result',
'description': 'Square root result',
'output': {
'formats': [{
'mimeType': 'application/json'
}]
}
}],
'example': {
'inputs': [{
'id': 'number',
'value': 64,
'type': 'text/plain'
}]
}
}
class MySquareRootProcessor(BaseProcessor):
"""My Square Root Processor example"""
"""
Add to pygeoapi configuration with:
my-sqrt-process:
type: process
processor:
name: pygeoapi.process.my_proc.MySquareRootProcessor
"""
def __init__(self, processor_def):
"""
Initialize object
:param processor_def: provider definition
:returns: pygeoapi.process.my_proc.MySquareRootProcessor
"""
super().__init__(processor_def, PROCESS_METADATA)
def execute(self, data):
mimetype = 'application/json'
# data is the incoming request payload (JSON dict)
# extract the number parameter and test validity
number = data.get('number', None)
if number is None:
raise ProcessorExecuteError('Missing input number')
# calculate square root
value = math.sqrt(number)
outputs = [{
'id': 'sqrt',
'value': value
}]
return mimetype, outputs
def __repr__(self):
return '<MySquareRootProcessor> {}'.format(self.name)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment