source: cows/trunk/cows/service/imps/csmlbackend/ @ 4481

Subversion URL:
Revision 4481, 4.6 KB checked in by domlowe, 13 years ago (diff)

Fixes to enable Point and GridSeries? subsetting in WFS.

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:
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.
11import wfs_csmllayer
13import logging
14log = logging.getLogger(__name__)
15import csml.parser
16from xml.etree import ElementTree as etree
17from pylons import request, config
19class dummyfeaturefortesting(object):
20    def __init__(self, x):
21        self.x=x
22    def toGML(self):
23        return '<someGML>%s</someGML>'%self.x
25def query_one_func(featureset, arg1='blah1', arg2=None):
26    result = [dummyfeaturefortesting(arg1 + ' ' + arg2)]
27    return result
29def query_two_func(featureset, arg1=None, arg2=None):
30    result = [dummyfeaturefortesting('blah2')]
31    return result
33def query_getFeatureByPhenomenon(featureset, phenomenon):
34    return featureset.getFeaturesByPropertyEqualTo('csml:parameter', phenomenon)   
36def query_getFeatureById(featureset, id):
37    return [featureset.getFeatureByGMLid(id)]
39def query_extractPointFromPointSeries(featureset, featureid, timeinstance):
40    #TODO:  configure output directory
41    #TODO: error handling
42    csmloutputdir=config['publish_dir']
43    feature=featureset.getFeatureByGMLid(featureid)._feature
44    newfeature, netcdfpath, storagedescriptor=feature.subsetToPoint(time=str(timeinstance), outputdir=csmloutputdir)       
46    #modify the rangeset of the feature to store the data inline and discard the storage descriptor.
47    storagedescriptor.fileName.CONTENT=csmloutputdir+'/'+storagedescriptor.fileName.CONTENT   
48    data=storagedescriptor.getData()
49    qlist=csml.parser.MeasureOrNullList()
50    qlist.uom=newfeature.value.rangeSet.valueArray.valueComponent.uom
51    qlist.CONTENT=str(data[0])
52    rs=csml.parser.RangeSet()
53    rs.quantityList=qlist   
54    newfeature.value.rangeSet=rs
56    #wrap the feature in a wfs CSMLFeatureInstance object:
57    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
60    return [csmlfi]
61    #This following code is used to maintain the storage descriptor filename consistency, but has been commented out for this
62    #operation as there is only a single point value. However code is left here as reminder as it will be needed
63    #for other feature types soon
64    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
66    #And serialise the storage descriptor as XML.
67#    qualifiedFeatureType='{}' + storagedescriptor.__class__.__name__
68#    emptyelem=etree.Element(qualifiedFeatureType)
69#    log.debug(request.environ)   
70#    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
71#    csmlelem=storagedescriptor.toXML(emptyelem)
72#    storagedescXML=etree.tostring(csmlelem)     
73#    return [csmlfi], [storagedescXML]
76def query_extractGridSeriesFromGridSeries(featureset, featureid, bbox, timerange):
77    #TODO factor out code in common with other subsetting queries
78    csmloutputdir=config['publish_dir']
79    feature=featureset.getFeatureByGMLid(featureid)._feature
80    #break the bounding box into latitude and longitude:
81    #TODO, this really needs to be generic and refer to the srs of the underlying feature.
82    bb=str(bbox).split(',')
83    lat=(float(bb[1]),float(bb[3]))
84    lon=(float(bb[0]),float(bb[2]))
85    log.debug('requesting latitude: %s, longitude: %s'%(lat, lon))
86    newfeature, netcdfpath, storagedescriptor=feature.subsetToGridSeries(time=str(timerange), latitude=lat, longitude=lon, outputdir=csmloutputdir)
87    #wrap this in a wfs CSMLFeatureInstance object:
88    csmlfi=wfs_csmllayer.CSMLFeatureInstance('subset feature title', 'subset feature abstract', newfeature)
89    #And serialise the storage descriptor as XML.
90    qualifiedFeatureType='{}' + storagedescriptor.__class__.__name__
91    emptyelem=etree.Element(qualifiedFeatureType)
92    log.debug(request.environ)
93    #change the path of the storage descriptor to the download url - assumes routes maps to filestore
94    storagedescriptor.fileName.CONTENT='http://'+request.environ['HTTP_HOST']+'/filestore/' +storagedescriptor.fileName.CONTENT
95    csmlelem=storagedescriptor.toXML(emptyelem)
96    storagedescXML=etree.tostring(csmlelem)
97    return [csmlfi], [storagedescXML]
Note: See TracBrowser for help on using the repository browser.