source: cows/trunk/cows/pylons/wfs_controller.py @ 5133

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/pylons/wfs_controller.py@5133
Revision 5133, 13.2 KB checked in by domlowe, 10 years ago (diff)

supporting maxfeatures keyword

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"""
9WFS controller for OGC Web Services (OWS).
10
11@author: Dominic Lowe
12"""
13
14import re
15from cStringIO import StringIO
16from sets import Set
17from pylons import request, response, c
18import ConfigParser
19import xml.etree.ElementTree as etree
20import logging
21log = logging.getLogger(__name__)
22
23import Image
24
25from cows.model.wfs import WfsFeatureSummary
26from cows.model.filterencoding import FEQueryProcessor
27from cows.model import PossibleValues, WGS84BoundingBox, BoundingBox, Contents
28from cows.pylons import ows_controller
29from cows.exceptions import *
30from cows import bbox_util
31from cows.service.imps.csmlbackend.config import config
32
33class WFSController(ows_controller.OWSController):
34    """
35    Subclass this controller in a pylons application and set the layerMapper
36    class attribute to implement a WFS. Each layer can be mapped to a Feature for the WFS.
37
38    @cvar layerMapper: an cows.service.wxs_iface.ILayerMapper object.
39   
40
41    """
42    layerMapper = None
43    _layerSlabCache = {}
44
45    #-------------------------------------------------------------------------
46    # Attributes required by OWSController
47
48    service = 'WFS'
49    owsOperations = (ows_controller.OWSController.owsOperations + ['DescribeFeatureType', 'GetFeature', 'DescribeStoredQueries','ListStoredQueries', 'GetPropertyValue'])
50    validVersions = ['1.1.0', '2.0.0']
51   
52
53    #-------------------------------------------------------------------------
54
55    def __before__(self, **kwargs):
56        """
57        This default implementation of __before__() will pass all routes
58        arguments to the layer mapper to retrieve a list of layers for
59        this WFS.
60
61        It will be called automatically by pylons before each action method.
62
63
64        """
65        log.debug('loading layers')
66        self.layers, self.featureset = self.layerMapper.map(**kwargs)
67        log.debug('Feature instances %s'%self.featureset.featureinstances)
68   
69        #-------------------------------------------------------------------------
70        # Methods implementing stubs in OWSController
71
72    def _renderCapabilities(self, version, format):
73        """
74        Renders capabilities document.
75        """
76        if version == '1.1.0':
77            t = ows_controller.templateLoader.load('wfs_capabilities_1_1_0.xml')
78        elif version == '2.0.0':
79            t = ows_controller.templateLoader.load('wfs_capabilities_2_0_0.xml')
80        else:
81            # We should never get here!  The framework should raise an exception before now.
82            raise RuntimeError("Version %s not supported" % version)
83       
84        return t.generate(c=c).render()
85
86    def _loadCapabilities(self):
87        """
88        @note: Assumes self.layers has already been created by __before__().
89        Builds capabilities document.
90
91        """
92        log.info('Loading WFS Capabilites')
93       
94        ows_controller.addOperation('GetFeature')
95        ows_controller.addOperation('DescribeFeature')
96        ows_controller.addOperation('DescribeStoredQueries')
97        ows_controller.addOperation('ListStoredQueries')
98        ows_controller.addOperation('GetPropertyValue')
99       
100       
101        featureInfoFormats = Set()
102
103        log.debug('Loading capabilities contents')
104        c.capabilities.contents = Contents()
105       
106       
107        ftlist={}
108        #       
109       
110        for layerName, layer in self.layers.items():
111            log.info('Loading layer %s' % layerName)
112#            log.info('feature type %s'%layer._feature)
113
114            wgs84BBox = WGS84BoundingBox(layer.wgs84BBox[:2],
115                                         layer.wgs84BBox[2:])
116           
117            ds = WfsFeatureSummary(keywords=layer.keywords, 
118                                   outputformats=layer.outputformats, 
119                                   identifier=layerName,
120                                   titles=[layer.title],
121                                   abstracts=[layer.abstract],                                   
122                                   wgs84BoundingBoxes=[wgs84BBox])
123
124            c.capabilities.contents.datasetSummaries.append(ds)
125
126    def _getSchema(self, typename):
127        namespace = typename.split(':')[0]
128        schemalocation = conf
129       
130    def _parsetypename(self, typename):
131        """ parse feature type name into schema and name and return schema"""       
132        if typename not in self.layers.keys():
133            raise InvalidParameterValue('Invalid typename parameter: %s. Typename must consist of namespace and featuretype separated with a colon, as displayed in the GetCapabilities response.'%typename, 'typename')
134   
135        namespace, ft = typename.split(':')
136        wfsconfiglocation=config['wfsconfig']
137        wfscfg = ConfigParser.ConfigParser()
138        wfscfg.read(wfsconfiglocation)     
139        xmlschema=open(wfscfg.get('application_schemas', namespace)).read()     
140        log.debug('location of application schema %s' %(xmlschema))
141        return xmlschema
142   
143    def _runQuery(self, queryxml = None, storedqueryid=None, typename=None, maxfeatures=None,**kwargs):
144        """ this is used by both the GetFeature and GetPropertyValue methods to
145        run a wfs:query over a featurecollection and return a subset of the collection.
146        The query may be defined as an xml <wfs:query> or may be referenced by a StoredQuery_id
147        """
148        additionalobjects=[] #not implemented for filter encoding, just for storedqueries (e.g. subsetting)
149        if queryxml:
150            qp=FEQueryProcessor()
151            resultset=qp.evaluate(self.featureset, queryxml)
152            log.debug('Final resultset from query processor %s'%resultset)
153        elif storedqueryid:
154            storedquery, func=self.layerMapper.queryDescriptions.queries[storedqueryid]
155            storedqueryresult=func(self.featureset, **kwargs)
156            if len(storedqueryresult) == 2:
157                resultset=storedqueryresult[0]
158                additionalobjects=storedqueryresult[1]
159            else:
160                resultset=storedqueryresult
161            log.debug('Final resultset from stored query %s'%resultset)
162        else:           
163            #If neither query or storedquery_id are provided then return entire featureset, filtered on 'typename' and/or 'maxfeatures' if supplied.
164            resultset=self.featureset.getFeatureList()
165            if typename:
166                templist = resultset[:]
167                for feature in templist:   #remove any features of the wrong feature type
168                    if feature.featuretype != typename:
169                        resultset.remove(feature)
170                resultset=resultset
171                if maxfeatures: #reduce the response to the requested number of feature instances
172                    maxf=int(maxfeatures)
173                    if len(resultset) >maxf:
174                        resultset=resultset[:maxf]
175        return resultset, additionalobjects
176
177    def _applyXPath(self, xpath, features):
178        """ applies an xpath expression to a set of features and returns
179        a set of wfs:members containing property values expressed in the xpath"""
180        resultset=[]       
181        #if xpath is looking for an attribute, handle it differently - this should be unnecessary when migrated to element tree 1.3
182        #i.e. if of the form /somepath/path[@attribute]
183        attstart=xpath.find('[')
184        if attstart == -1: #no attribute           
185            for feature in features:
186                featureXML=feature._feature.elem #the underlying XML
187                valuecomponent=featureXML.find(xpath)
188                if valuecomponent is not None:
189                    valstr=etree.tostring(valuecomponent)           
190                    resultset.append(valstr)
191        else:
192            #there is an attribute:
193            attributeName=xpath.split('[')[1][1:-1]
194            xpath=xpath.split('[')[0]
195            for feature in features:
196                featureXML=feature._feature.elem #the underlying XML
197                log.debug('xpath path %s'%xpath)
198                log.debug('attribute %s'%attributeName)
199                if len(xpath) ==0:
200                    valuecomponent=featureXML
201                else:
202                    valuecomponent=featureXML.find(xpath)
203                log.debug('valuecomponent %s'%valuecomponent)
204                if valuecomponent is not None:
205                    attribute=valuecomponent.get(attributeName)         
206                    resultset.append(attribute)           
207        log.debug(resultset)
208        return resultset     
209   
210   
211    def DescribeFeatureType(self):
212        """ DescribeFeatureType """
213        version = self.getOwsParam('version', default=self.validVersions[0])
214        if version not in self.validVersions:
215            raise InvalidParameterValue('Version %s not supported' % version,
216                                        'version')
217        typename=self.getOwsParam('typename')
218        ftschema =self._parsetypename(typename)
219        log.debug(self.layers.items())       
220               
221        outputformat=self.getOwsParam('outputformat', default='text/xml')
222       
223        #temporarily returns entire schema
224        #TODO: return single featuretype definition
225        msg  = ftschema
226        response.headers['content-type'] = 'text/xml'
227        return msg
228           
229 
230    def GetPropertyValue(self):
231        """ GetPropertyValue request - similar to get feature but only returns chosen property
232        values """
233        valueReference = self.getOwsParam('valueReference')
234        queryxml=self.getOwsParam('query',default=None)
235        storedqueryid=self.getOwsParam('storedquery_id', default=None)
236        typename=self.getOwsParam('typename', default=None)
237        #get any other parameters from self._owsParams and pass them to the stored query
238        otherparams={}
239        for key in self._owsParams.keys():
240            if key not in ['query', 'request', 'service', 'version', 'storedquery_id', 'typename', 'valuereference']:
241                otherparams[key]=self._owsParams[key]     
242        featureresultset, additionalobjects =self._runQuery(queryxml, storedqueryid, typename **otherparams)       
243       
244        #Now need to take account of valueReferencexpath, and distill the
245        #resultset down to just the requested properties.
246        c.resultset=self._applyXPath(valueReference, featureresultset)
247       
248        response.headers['content-type'] = 'text/xml'
249        #TODO, new template for values
250        t = ows_controller.templateLoader.load('wfs_valuecollection.xml')
251        return t.generate(c=c).render() 
252 
253    def GetFeature(self):
254        """ GetFeature request
255        """
256        version = self.getOwsParam('version', default=self.validVersions[0])
257        if version not in self.validVersions:
258            raise InvalidParameterValue('Version %s not supported' % version,
259                                        'version')
260
261        #The GetFeature request may either use the 'query' filter encoding or a
262        #storedquery, but not both:     
263        #Parse the query to analyse the filters it contains
264        queryxml=self.getOwsParam('query',default=None)
265        storedqueryid=self.getOwsParam('storedquery_id', default=None)
266        typename=self.getOwsParam('typename', default=None)
267        maxfeatures=self.getOwsParam('maxfeatures', default=None)
268       
269        #retrieve any other parameters and pass them off to the stored query (they are ignored in the case of the queryxml option
270        otherparams={}
271        for key in self._owsParams.keys():
272            if key not in ['query', 'request', 'service', 'version', 'storedquery_id', 'typename', 'maxfeatures']:
273                otherparams[key]=self._owsParams[key]       
274        c.resultset, c.additionalobjects=self._runQuery(queryxml, storedqueryid, typename, maxfeatures, **otherparams)
275               
276        #Group resultset together in a wfs feature collection (use template)
277        response.headers['content-type'] = 'text/xml'
278        t = ows_controller.templateLoader.load('wfs_featurecollection.xml')
279        return t.generate(c=c).render()           
280
281   
282    def DescribeStoredQueries(self):
283        """ DescribeStoredQueries method. Takes zero or more stored query ids as args"""
284        allqueries=self.layerMapper.queryDescriptions.queries       
285        storedqueryid=self.getOwsParam('storedqueryid', default=None)
286        if storedqueryid is None:
287            c.storedqueries = self.layerMapper.queryDescriptions.queries
288        else:
289            c.storedqueries={}
290            for queryid in storedqueryid.split(','):
291                c.storedqueries[queryid]=self.layerMapper.queryDescriptions.queries[queryid]       
292        response.headers['Content-Type'] = 'text/xml'
293        t = ows_controller.templateLoader.load('wfs_describestoredqueries.xml')
294        return t.generate(c=c).render() 
295         
296    def ListStoredQueries(self):
297        """ DescribeStoredQueries method. Takes zero or more stored query ids as args"""
298        c.storedqueries = self.layerMapper.queryDescriptions.queries
299        t = ows_controller.templateLoader.load('wfs_liststoredqueries.xml')
300        response.headers['Content-Type'] = 'text/xml'
301        return t.generate(c=c).render() 
Note: See TracBrowser for help on using the repository browser.