Skip to content

Commit

Permalink
Merge pull request #26 from wmo-im/process-wmo-get-ra
Browse files Browse the repository at this point in the history
add process for querying WMO RA by geometry
  • Loading branch information
david-i-berry authored Apr 4, 2023
2 parents 695a216 + bcfb273 commit a0737b4
Show file tree
Hide file tree
Showing 4 changed files with 149 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docker/entrypoint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ cd ~/.pywcmp/wis2-topic-hierarchy && unzip -j /tmp/all.json.zip
echo "Caching WCMP2 schema"
mkdir ~/.pywcmp/wcmp-2 && curl --output-dir ~/.pywcmp/wcmp-2 -O https://raw.githubusercontent.com/wmo-im/wcmp2/main/schemas/wcmp2-bundled.json

echo "Caching WMO RA GeoJSON"
curl -o /data/wmo-ra.geojson https://raw.githubusercontent.com/OGCMetOceanDWG/wmo-ra/master/wmo-ra.geojson

echo "Trying to generate OpenAPI document"
pygeoapi openapi generate ${PYGEOAPI_CONFIG} --output-file ${PYGEOAPI_OPENAPI}
# pygeoapi openapi validate ${PYGEOAPI_OPENAPI}
Expand Down
5 changes: 5 additions & 0 deletions docker/pygeoapi-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,8 @@ resources:
type: process
processor:
name: pywcmp.pygeoapi_plugin.WCMP2ETSProcessor

wmo-get-ra:
type: process
processor:
name: wis2box_api.plugins.process.wmo_ra.WMORAProcessor
8 changes: 7 additions & 1 deletion wis2box_api/plugins/process/station_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,12 @@ class StationInfoProcessor(BaseProcessor):
def __init__(self, processor_def):
"""
Initialize object
:param processor_def: provider definition
:returns: wis2box_api.plugins.process.station_info.StationInfoProcessor
"""

super().__init__(processor_def, PROCESS_DEF)

host = os.environ['WIS2BOX_API_BACKEND_URL']
Expand All @@ -122,10 +125,13 @@ def __init__(self, processor_def):

def execute(self, data):
"""
Execute River Runner Process
Execute Process
:param data: processor arguments
:returns: 'application/json'
"""

mimetype = 'application/json'
outputs = {
'id': 'path',
Expand Down
134 changes: 134 additions & 0 deletions wis2box_api/plugins/process/wmo_ra.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
###############################################################################
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
###############################################################################

import logging

from osgeo import ogr
from pygeoapi.process.base import BaseProcessor, ProcessorExecuteError

LOGGER = logging.getLogger(__name__)

wmo_ra_geojson = '/data/wmo-ra.geojson'

PROCESS_DEF = {
'version': '0.1.0',
'id': 'wmo-get-ra',
'title': {
'en': 'Get WMO RA by geometry'
},
'description': {
'en': 'Get WMO RA by geometry'
},
'keywords': ['wmo', 'ra'],
'links': [{
'type': 'text/html',
'rel': 'about',
'title': 'information',
'href': 'https://github.com/OGCMetOceanDWG/wmo-ra',
'hreflang': 'en-US'
}],
'inputs': {
'geometry': {
'title': 'Geometry (WKT)',
'description': 'Geometry (WKT)',
'schema': {
'type': 'string'
},
'minOccurs': 1,
'maxOccurs': 1,
'metadata': None,
'keywords': ['wmo', 'ra']
}
},
'outputs': {
'result': {
'title': 'WMO RA result',
'description': 'WMO RA result',
'schema': {
'type': 'object',
'contentMediaType': 'application/json'
}
}
},
'example': {
'inputs': {
'geometry': 'POINT(-79 43)',
}
}
}


class WMORAProcessor(BaseProcessor):
"""WMO RA Processor"""

def __init__(self, processor_def):
"""
Initialize object
:param processor_def: provider definition
:returns: wis2box_api.plugins.process.wmo_ra.WMORAProcessor
"""

super().__init__(processor_def, PROCESS_DEF)

def execute(self, data):
"""
Execute Process
:param data: processor arguments
:returns: 'application/json'
"""

mimetype = 'application/json'

outputs = {
'wmo-ra': []
}

geometry = data.get('geometry')

if geometry is None:
msg = 'Missing WKT geometry'
LOGGER.error(msg)
raise ProcessorExecuteError(msg)

driver = ogr.GetDriverByName('GeoJSON')
dataSource = driver.Open(wmo_ra_geojson, 0)
layer = dataSource.GetLayer()

geometry = ogr.CreateGeometryFromWkt(data['geometry'])
LOGGER.debug(f'Geometry: {geometry}')

if geometry is None:
msg = 'Invalid WKT geometry'
LOGGER.error(msg)
raise ProcessorExecuteError(msg)

for feature in layer:
if geometry.Intersects(feature.GetGeometryRef()):
outputs['wmo-ra'].append(feature.GetField('roman_num'))

return mimetype, outputs

def __repr__(self):
return '<StationInfoProcessor> {}'.format(self.name)

0 comments on commit a0737b4

Please sign in to comment.