source: cows/trunk/cows/service/imps/csmlbackend/wfs_csmlstoredqueries.py @ 4517

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/service/imps/csmlbackend/wfs_csmlstoredqueries.py@4517
Revision 4517, 4.5 KB checked in by domlowe, 13 years ago (diff)

Tidying up DescribeQuery? and allowing multiple ParameterExpressions?

Line 
1""" This module contains CSML WFS 2.0 stored query implementations.
2To add a new stored query, write the functional code here, then describe the functionality as
3a new query in:
4
5wfs_csmllayer.CSMLStoredQueries.queries
6
7These query functions should either return a resultset (python list) containing the features,
8or 2 lists, one being the resultset, and the other containing string representations of XML addtional
9objects - in CSML these are going to be StorageDescriptors.
10"""
11import wfs_csmllayer
12
13import logging
14log = logging.getLogger(__name__)
15import csml.parser
16from xml.etree import ElementTree as etree
17from pylons import request, config
18
19
20class dummyfeaturefortesting(object):
21    def __init__(self, x):
22        self.x=x
23    def toGML(self):
24        return '<someGML>%s</someGML>'%self.x
25
26def query_one_func(featureset, arg1='string1', arg2='string2'):
27    result = [dummyfeaturefortesting(arg1 + ' ' + arg2)]
28    return result
29
30def query_getFeatureByPhenomenon(featureset, phenomenon):
31    return featureset.getFeaturesByPropertyEqualTo('csml:parameter', phenomenon)   
32
33def query_getFeatureById(featureset, id):
34    return [featureset.getFeatureByGMLid(id)]
35
36def query_extractPointFromPointSeries(featureset, featureid, timeinstance):
37    #TODO:  configure output directory
38    #TODO: error handling
39    csmloutputdir=config['publish_dir']
40    feature=featureset.getFeatureByGMLid(featureid)._feature
41    newfeature, netcdfpath, storagedescriptor=feature.subsetToPoint(time=str(timeinstance), outputdir=csmloutputdir)       
42   
43    #modify the rangeset of the feature to store the data inline and discard the storage descriptor.
44    storagedescriptor.fileName.CONTENT=csmloutputdir+'/'+storagedescriptor.fileName.CONTENT   
45    data=storagedescriptor.getData()
46    qlist=csml.parser.MeasureOrNullList()
47    qlist.uom=newfeature.value.rangeSet.valueArray.valueComponent.uom
48    qlist.CONTENT=str(data[0])
49    rs=csml.parser.RangeSet()
50    rs.quantityList=qlist   
51    newfeature.value.rangeSet=rs
52   
53    #wrap the feature in a wfs CSMLFeatureInstance object:
54    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
55   
56   
57    return [csmlfi]
58    #This following code is used to maintain the storage descriptor filename consistency, but has been commented out for this
59    #operation as there is only a single point value. However code is left here as reminder as it will be needed
60    #for other feature types soon
61    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
62   
63    #And serialise the storage descriptor as XML.
64#    qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + storagedescriptor.__class__.__name__
65#    emptyelem=etree.Element(qualifiedFeatureType)
66#    log.debug(request.environ)   
67#    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
68#    csmlelem=storagedescriptor.toXML(emptyelem)
69#    storagedescXML=etree.tostring(csmlelem)     
70#    return [csmlfi], [storagedescXML]
71
72
73def query_extractGridSeriesFromGridSeries(featureset, featureid, bbox, mintime, maxtime):
74    #TODO factor out code in common with other subsetting queries
75    csmloutputdir=config['publish_dir']
76    feature=featureset.getFeatureByGMLid(featureid)._feature
77    timerange=(mintime, maxtime,)
78    #break the bounding box into latitude and longitude:
79    #TODO, this really needs to be generic and refer to the srs of the underlying feature.
80    bb=str(bbox).split(',')
81    lat=(float(bb[1]),float(bb[3]))
82    lon=(float(bb[0]),float(bb[2]))
83    log.debug('requesting latitude: %s, longitude: %s'%(lat, lon))
84    newfeature, netcdfpath, storagedescriptor=feature.subsetToGridSeries(time=timerange, latitude=lat, longitude=lon, outputdir=csmloutputdir)
85    #wrap this in a wfs CSMLFeatureInstance object:
86    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
87    #And serialise the storage descriptor as XML.
88    qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + storagedescriptor.__class__.__name__
89    emptyelem=etree.Element(qualifiedFeatureType)
90    log.debug(request.environ)
91    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
92    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
93    csmlelem=storagedescriptor.toXML(emptyelem)
94    storagedescXML=etree.tostring(csmlelem)
95    return [csmlfi], [storagedescXML]
96   
Note: See TracBrowser for help on using the repository browser.