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

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

updating csml publish directory config option

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