Skip to content

Commit

Permalink
initial commit (#93)
Browse files Browse the repository at this point in the history
update wsgi for better time and error handling

update to code

updates

Co-authored-by: Louis-Philippe Rousseau Lambert <louis-philippe.rousseaulambert@canada.ca>
  • Loading branch information
RousseauLambertLP and Louis-Philippe Rousseau Lambert authored Feb 21, 2024
1 parent 73d6245 commit cbadf1f
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
2 changes: 1 addition & 1 deletion debian/control
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Vcs-Git: https://github.com/ECCC-CCCS/geomet-climate.git

Package: geomet-climate
Architecture: all
Depends: ${python3:Depends}, mapserver-bin, python3-all, python3-click, python3-gdal, python3-mappyfile, python3-mapscript, python3-matplotlib, python3-numpy, python3-pyproj, python3-yaml, proj-bin, proj-data, ${misc:Depends}
Depends: ${python3:Depends}, mapserver-bin, python3-all, python3-click, python3-dateutil, python3-gdal, python3-mappyfile, python3-mapscript, python3-matplotlib, python3-numpy, python3-pyproj, python3-yaml, proj-bin, proj-data, ${misc:Depends}
Suggests: gdal-bin
Homepage: https://github.com/ECCC-CCCS/geomet-climate
Description: MSC GeoMet climate services
Expand Down
84 changes: 83 additions & 1 deletion geomet_climate/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import os

import click
from dateutil.parser import isoparse
from dateutil.relativedelta import relativedelta
import mapscript

from geomet_climate.env import BASEDIR
Expand Down Expand Up @@ -100,6 +102,19 @@ def metadata_lang(m, lg):
m.getMetaData('wcs_description_{}'.format(lg)))


def get_custom_service_exception(code, locator, text):
"""return custom wms:ServiceExceptionReport"""

return bytes('''<?xml version='1.0' encoding="utf-8"?>
<ogc:ServiceExceptionReport version="1.3.0"
xmlns:ogc="http://www.opengis.net/ogc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.opengis.net/ogc
http://schemas.opengis.net/wms/1.3.0/exceptions_1_3_0.xsd">
<ogc:ServiceException code="{code}" locator="{locator}">{text}</ogc:ServiceException>
</ogc:ServiceExceptionReport>'''.format(code=code, locator=locator, text=text), encoding='utf-8') # noqa


def application(env, start_response):
"""WSGI application for WMS/WCS"""
for key in MAPSERV_ENV:
Expand All @@ -122,6 +137,7 @@ def application(env, start_response):
coverageid_ = request.getValueByName('COVERAGEID')
format_ = request.getValueByName('FORMAT')
style_ = request.getValueByName('STYLE')
time_ = request.getValueByName('TIME')

if lang_ is not None and lang_ in ['f', 'fr', 'fra']:
lang = 'fr'
Expand Down Expand Up @@ -199,14 +215,80 @@ def application(env, start_response):
else:
LOGGER.debug('Loading mapfile: {}'.format(mapfile_))
mapfile = mapscript.mapObj(mapfile_)
layerobj = mapfile.getLayerByName(layer)
if request_ == 'GetCapabilities' and lang == 'fr':
metadata_lang(mapfile, lang)
layerobj = mapfile.getLayerByName(layer)
layerobj.setMetaData('ows_title',
layerobj.getMetaData('ows_title_{}'.format(lang))) # noqa
layerobj.setMetaData('ows_layer_group',
layerobj.getMetaData('ows_layer_group_{}'.format(lang))) # noqa

if time_ and 'ows_timeextent' in layerobj.metadata.keys():
try:
dates = []
timeextent = layerobj.getMetaData('ows_timeextent')

start_date, end_date, duration = timeextent.split('/')
start_date = isoparse(start_date)
end_date = isoparse(end_date)
time_iso = isoparse(time_)

end_year_month = (end_date.year - start_date.year) * 12
end_month = end_date.month - start_date.month
end_date = end_year_month + end_month

if duration == 'P1Y':
if time_ != time_iso.strftime('%Y'):
time_error = 'Format de temps invalide, ' \
'format attendu : YYYY / ' \
'Invalid time format, ' \
'expected format: YYYY'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type',
'text/xml')])
return [response]

for i in range(0, end_date + 1, 12):
date_ = start_date + relativedelta(months=i)
dates.append(date_.strftime('%Y'))

else:
if time_ != time_iso.strftime('%Y-%m'):
time_error = 'Format de temps invalide, ' \
'format attendu' \
' YYYY-MM / Invalid time format, ' \
'expected format: YYYY-MM'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type',
'text/xml')])
return [response]

for i in range(0, end_date + 1, 1):
date_ = start_date + relativedelta(months=i)
dates.append(date_.strftime('%Y-%m'))

if time_ not in dates:
time_error = 'Temps en dehors des heures valides /' \
' Time outside valid hours'
response = get_custom_service_exception('NoMatch',
'time',
time_error)
start_response('200 OK', [('Content-type', 'text/xml')])
return [response]

except ValueError:
time_error = 'Valeur de temps invalide /' \
' Time value is invalid'
response = get_custom_service_exception('InvalidDimensionValue', # noqa
'time',
time_error)
start_response('200 OK', [('Content-type', 'text/xml')])
return [response]

mapscript.msIO_installStdoutToBuffer()
request.loadParamsFromURL(env['QUERY_STRING'])

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ click
mappyfile
matplotlib
numpy
python-dateutil
pyyaml

0 comments on commit cbadf1f

Please sign in to comment.