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

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

updating version no, includes faster rendering code

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, csml.csmllibs.csmlextra
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 _getCSMLFilename():
34    return 'csml%s.nc'%(csml.csmllibs.csmlextra.getRandomID())
35
36
37def query_one_func(featureset, arg1='string1', arg2='string2'):
38    result = [dummyfeaturefortesting(arg1 + ' ' + arg2)]
39    return result
40
41def query_getFeatureByPhenomenon(featureset, phenomenon):
42    return featureset.getFeaturesByPropertyEqualTo('csml:parameter', phenomenon)   
43
44def query_getFeatureById(featureset, id):
45    return [featureset.getFeatureByGMLid(id)]
46
47def query_extractPointFromPointSeries(featureset, featureid, timeinstance):
48    #TODO:  configure output directory
49    #TODO: error handling
50    csmloutputdir=config['cows.csml.publish_dir']
51    feature=featureset.getFeatureByGMLid(featureid)._feature
52    ncname=_getCSMLFilename()
53    newfeature, netcdfpath, storagedescriptor=feature.subsetToPoint(time=str(timeinstance), ncname=ncname, outputdir=csmloutputdir)       
54   
55    #modify the rangeset of the feature to store the data inline and discard the storage descriptor.
56    storagedescriptor.fileName.CONTENT=csmloutputdir+'/'+storagedescriptor.fileName.CONTENT   
57    data=storagedescriptor.getData()
58    qlist=csml.parser.MeasureOrNullList()
59    qlist.uom=newfeature.value.rangeSet.valueArray.valueComponent.uom
60    qlist.CONTENT=str(data[0])
61    rs=csml.parser.RangeSet()
62    rs.quantityList=qlist   
63    newfeature.value.rangeSet=rs
64   
65    #wrap the feature in a wfs CSMLFeatureInstance object:
66    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
67   
68   
69    return [csmlfi]
70    #This following code is used to maintain the storage descriptor filename consistency, but has been commented out for this
71    #operation as there is only a single point value. However code is left here as reminder as it will be needed
72    #for other feature types soon
73    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
74   
75    #And serialise the storage descriptor as XML.
76#    qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + storagedescriptor.__class__.__name__
77#    emptyelem=etree.Element(qualifiedFeatureType)
78#    log.debug(request.environ)   
79#    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
80#    csmlelem=storagedescriptor.toXML(emptyelem)
81#    storagedescXML=etree.tostring(csmlelem)     
82#    return [csmlfi], [storagedescXML]
83
84
85def query_extractPointSeriesFromGridSeries(featureset, featureid, latitude, longitude, mintime, maxtime):
86    csmloutputdir=config['cows.csml.publish_dir']
87    feature=featureset.getFeatureByGMLid(featureid)._feature
88    timerange=(mintime, maxtime,)
89    lat=float(latitude)
90    lon=float(longitude)
91    subsetdictionary={'time':timerange, 'latitude':lat, 'longitude':lon}
92    ncname=_getCSMLFilename()
93    newfeature, netcdfpath, storagedescriptor=feature.subsetToPointSeries(outputdir=csmloutputdir, ncname=ncname,**subsetdictionary)
94    #wrap this in a wfs CSMLFeatureInstance object:
95    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
96    #And serialise the storage descriptor as XML.
97    qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + storagedescriptor.__class__.__name__
98    emptyelem=etree.Element(qualifiedFeatureType)
99    log.debug(request.environ)
100    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
101    #TODO: THIS SHOULD HANDLE SERVER PROXIES.
102    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +ncname
103    csmlelem=storagedescriptor.toXML(emptyelem)
104    storagedescXML=etree.tostring(csmlelem)
105    return [csmlfi], [storagedescXML]
106 
107
108def query_extractGridSeriesFromGridSeries(featureset, featureid, bbox, mintime, maxtime):
109    #TODO factor out code in common with other subsetting queries
110    csmloutputdir=config['cows.csml.publish_dir']
111    feature=featureset.getFeatureByGMLid(featureid)._feature
112    timerange=(mintime, maxtime,)
113    #break the bounding box into latitude and longitude:
114    #TODO, this really needs to be generic and refer to the srs of the underlying feature.
115    bb=str(bbox).split(',')
116    lat=(float(bb[1]),float(bb[3]))
117    lon=(float(bb[0]),float(bb[2]))
118    log.debug('requesting latitude: %s, longitude: %s'%(lat, lon))
119    ncname=_getCSMLFilename()
120    newfeature, netcdfpath, storagedescriptor=feature.subsetToGridSeries(time=timerange, latitude=lat, longitude=lon, ncname=ncname, outputdir=csmloutputdir)
121    #wrap this in a wfs CSMLFeatureInstance object:
122    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
123    #And serialise the storage descriptor as XML.
124    qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + storagedescriptor.__class__.__name__
125    emptyelem=etree.Element(qualifiedFeatureType)
126    log.debug(request.environ)
127    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
128    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
129    csmlelem=storagedescriptor.toXML(emptyelem)
130    storagedescXML=etree.tostring(csmlelem)
131    return [csmlfi], [storagedescXML]
132   
Note: See TracBrowser for help on using the repository browser.