source: cows/trunk/cows/service/imps/csmlbackend/wfs_csmllayer.py @ 6138

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

xlinks now resolved for all aspects of the domain.

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"""
9implementation of ILayerMapper, IwfsLayer, IDimension, ILayerSlab interfaces, as defined in wfs_iface.py & wxs_iface.py
10
11"""
12import cows.service.imps.csmlbackend.wfs_csmlstoredqueries as CSMLQueries
13from cows.service.imps.csmlbackend.csmlcommon import CSMLLayerMapper, CSMLConnector
14from cows.service.wfs_iface import *
15from cows.model.storedquery import *
16import csml.parser
17from xml.etree import ElementTree as etree
18from shapely.geometry import Polygon, Point
19from cows.bbox_util import union
20from csml.csmllibs.csmlextra import listify, stringify, cleanString2
21
22import logging
23log = logging.getLogger(__name__)
24
25class CSMLwfsLayerMapper(CSMLLayerMapper):
26    """
27    Map keyword arguments to a collection of layers.
28    Supports the retrieval of sets of layers according to arbitrary
29    keyword/value pairs.
30    Implements  ILayerMapper (does it? TODO: check)
31   
32    WFS differs from WMS/WCS in that the 'layers' are feature types, not instances.
33    So the CSMLwfsLayerMapper map method returns both a map of feature types and a map
34    of feature instances as both are needed for the GetFeature method to work.
35   
36    """
37    def __init__(self):
38        super(CSMLwfsLayerMapper, self).__init__()
39        self.featureinstancecache={}
40        self.queryDescriptions=CSMLStoredQueries()
41   
42
43    def map(self, **kwargs):
44        """
45        Given csml.parser.Dataset object list the names of
46        all layers available.
47       
48        @return: A mapping of layer names to ILayer implementations.
49        @raise ValueError: If no layers are available for these keywords.
50        """
51        fileoruri=kwargs['fileoruri']
52        if fileoruri in self.layermapcache.keys():
53            #we've accessed this layer map before, get it from the cache dictionary
54            return self.layermapcache[fileoruri], self.featureinstancecache[fileoruri]
55         
56        ds = self.connector.getCsmlDoc(fileoruri)
57        try:
58            self.datasetName=ds.name.CONTENT
59        except AttributeError:
60            self.datasetName = 'CSML WFS Service'
61       
62        #The WFS differs from WMS & WCS in that the contents layer is a list of
63        #feature *types* not *instances*. However a record of instances is also
64        #needed to fulfil GetFeature requests:
65        featureset=CSMLFeatureSet() #holds feature instances               
66        layermap={} #feature types       
67        aggregatedBBoxes={}#for aggregations of bounding boxes.
68       
69
70        for feature in csml.csmllibs.csmlextra.listify(ds.featureCollection.featureMembers):
71            title, abstract=self.getInfo(feature)
72            featureset.featureinstances[feature.id]=CSMLFeatureInstance(title, abstract, feature)             
73        for id, instance in featureset.featureinstances.iteritems():
74            ftype=instance.featuretype #namespaced type e.g. 'csml:PointSeriesFeature'
75            if ftype not in layermap.keys():
76                layermap[ftype]=CSMLwfsLayer(ftype, instance.wgs84BBox)
77                #instantiate an aggregator to compare future bounding boxes with.
78                aggregatedBBoxes[ftype]= instance.wgs84BBox               
79            else:
80                #the featuretype has already been stored in the dictionary.
81                #but, the bounding box may need changing to accommodate this new feature instance.
82#                log.debug('Checking bbox for feature id: %s and title: %s'%(instance._feature.id, instance.title))
83                currentbbox=aggregatedBBoxes[ftype]
84                newbbox=union(currentbbox, instance.wgs84BBox)
85                aggregatedBBoxes[ftype]= newbbox
86                layermap[ftype]=CSMLwfsLayer(ftype, newbbox)
87           
88
89        if len(layermap) > 0:
90            #cache results
91            self.layermapcache[fileoruri]=layermap
92            self.featureinstancecache[fileoruri]=featureset
93            return layermap, featureset
94        else:
95            raise ValueError
96
97class CSMLStoredQueries(object):
98    """ Holds definitions of supported WFS stored queries and mappings to functional implementations"""
99    def __init__(self):
100        #self.queries is a dictionary of form: {id:(StoredQuery, func)}
101        #where func is the name of the implementation of the stored query (typically) in wfs_csmlstoredqueries     
102        self.queries={}
103       
104        #simple example query
105        qid='queryOne'       
106        qet=QueryExpressionText('xyzml:SomeFeatureType') 
107        pex1=ParameterExpression('arg1', 'xsd:string')
108        pex2=ParameterExpression('arg2', 'xsd:string')
109        query=StoredQuery(qid, title='test query', abstract = 'my test query', queryExpressionText = qet, parameter=[pex1, pex2])
110        self.queries[qid]=(query, CSMLQueries.query_one_func)
111     
112         
113        #mandatory get feature by id
114        qid='urn-x:wfs:StoredQueryId:ISO:GetFeatureById'
115        qet=QueryExpressionText('csml:AbstractFeature')
116        pex=ParameterExpression('id', 'xsd:anyURI')
117        query=StoredQuery(qid, title='GetFeatureById', abstract = 'Get any feature by id', queryExpressionText = qet, parameter=[pex])
118        self.queries[qid]=(query, CSMLQueries.query_getFeatureById)
119
120        #query to select features by phenomenon
121        qid='urn-x:wfs:StoredQueryId:badc.nerc.ac.uk:phenomenonQuery'
122        qet=QueryExpressionText('csml:AbstractFeature')#TODO, this could definitely differ for different features...
123        pex=ParameterExpression('phenomenon', 'xsd:string')
124        query=StoredQuery(qid, title='SelectFeaturesByPhenomenon', abstract = 'Select features based on their phenomenon type, e.g. "temperature"', queryExpressionText = qet, parameter=[pex])
125        self.queries[qid]=(query, CSMLQueries.query_getFeatureByPhenomenon)
126       
127        #query to extract a PointFeature from a PointSeriesFeature
128        qid='urn-x:wfs:StoredQueryId:badc.nerc.ac.uk:extractPointFromPointSeries'
129        qet=QueryExpressionText('csml:PointFeature')
130        pex1=ParameterExpression('featureid', 'xsd:anyURI')
131        pex2=ParameterExpression('timeinstance', 'gml:TimePositionUnion')
132        query=StoredQuery(qid, title='ExtractPointFromPointSeries', abstract = 'Extract a csml:PointFeature for a single time instance from a csml:PointSeriesFeature', queryExpressionText = qet, parameter=[pex1, pex2])
133        self.queries[qid]=(query, CSMLQueries.query_extractPointFromPointSeries)
134
135        #query to extract a GridSeriesFeature from a GridSeriesFeature
136        qid='urn-x:wfs:StoredQueryId:badc.nerc.ac.uk:extractGridSeriesFromGridSeries'
137        qet=QueryExpressionText('csml:GridSeriesFeature')
138        pex1=ParameterExpression('featureid', 'xsd:anyURI')
139        pex2=ParameterExpression('mintime', 'gml:TimePositionUnion')
140        pex3=ParameterExpression('maxtime', 'gml:TimePositionUnion')
141        pex4=ParameterExpression('bbox', 'xsd:string')
142        paramlist=[pex1,pex2,pex3, pex4]
143        query=StoredQuery(qid, title='ExtractGridSeriesFromGridSeries', abstract = 'Extract a csml:GridSeries from a csml:GridSeriesFeature', queryExpressionText = qet, parameter=paramlist)
144        self.queries[qid]=(query, CSMLQueries.query_extractGridSeriesFromGridSeries)
145
146        #query to extract a PointSeriesFeature from a GridSeriesFeature
147        qid='urn-x:wfs:StoredQueryId:badc.nerc.ac.uk:extractPointSeriesFromGridSeries'
148        qet=QueryExpressionText('csml:PointSeriesFeature')
149        pex1=ParameterExpression('featureid', 'xsd:anyURI')
150        pex2=ParameterExpression('mintime', 'gml:TimePositionUnion')
151        pex3=ParameterExpression('maxtime', 'gml:TimePositionUnion')
152        pex4=ParameterExpression('latitude', 'xsd:string')
153        pex5=ParameterExpression('longitude', 'xsd:string')
154        paramlist=[pex1,pex2,pex3, pex4, pex5]
155        query=StoredQuery(qid, title='ExtractPointSeriesFromGridSeries', abstract = 'Extract a csml:PointSeries from a csml:GridSeriesFeature', queryExpressionText = qet, parameter=paramlist)
156        self.queries[qid]=(query, CSMLQueries.query_extractPointSeriesFromGridSeries)
157       
158
159class CSMLFeatureSet(IFeatureSet):
160    """ A set of features available via a WFS. Supports querying methods as used by OGG filters """
161    def __init__(self):
162        self.featureinstances={}
163
164    def getFeatureList(self):
165        results=[]
166        for featureid,feature in self.featureinstances.iteritems():
167            results.append(feature)
168        return results
169
170    def getFeatureByGMLid(self, gmlid):
171        return self.featureinstances[gmlid]
172   
173    def _checkforOverlap(self, filterbbox, featurebbox):
174        """ Uses Shapely Polygons to calculate bounding box intersections """
175        log.debug('comparing against %s'%str(filterbbox))       
176        filterpolygon=Polygon(((filterbbox[0],filterbbox[1]),(filterbbox[0],filterbbox[3]),(filterbbox[2],filterbbox[3]),(filterbbox[2],filterbbox[1])))
177        featurepolygon=Polygon(((featurebbox[0],featurebbox[1]),(featurebbox[0],featurebbox[3]),(featurebbox[2],featurebbox[3]),(featurebbox[2],featurebbox[1])))       
178        log.debug(dir(filterpolygon))
179        log.debug('intersect result%s'%featurepolygon.intersects(filterpolygon))
180        return filterpolygon.intersects(featurepolygon)
181   
182    def _checkforPointinBox(self, filterbbox, location):
183        """ Uses Shapely Polygons to calculate bounding box intersections """
184        log.debug('comparing against %s'%str(filterbbox))       
185        filterpolygon=Polygon(((filterbbox[0],filterbbox[1]),(filterbbox[0],filterbbox[3]),(filterbbox[2],filterbbox[3]),(filterbbox[2],filterbbox[1])))
186        featurepoint=Point(float(location[0]), float(location[1]))
187        log.debug(featurepoint.within(filterpolygon))
188        log.debug('intersect result%s'%featurepoint.intersects(filterpolygon))
189        return featurepoint.intersects(filterpolygon)
190   
191    def getFeaturesByBBox(self,bboxtuple, srsname):         
192        log.debug('GET FEATURES BY BBOX')
193        result=[]
194        log.debug('Request BBOX: %s %s'%(bboxtuple,srsname))
195        for featureid,feature in self.featureinstances.iteritems():
196            if hasattr(feature._feature, 'boundedBy'):
197                log.debug('Checking bbox for feature %s: '%feature._feature.id)
198                lowercorner=feature._feature.boundedBy.lowerCorner.CONTENT.split()
199                uppercorner=feature._feature.boundedBy.upperCorner.CONTENT.split()
200                featurebbox=(float(lowercorner[0]), float(lowercorner[1]), float(uppercorner[0]), float(uppercorner[1]))               
201                if self._checkforOverlap(bboxtuple, featurebbox):
202                    result.append(feature) 
203            elif hasattr(feature._feature, 'location'):
204                log.debug('Checking location for feature %s: '%feature._feature.id)
205                log.debug(feature._feature.location)
206                location=feature._feature.location.CONTENT.split()
207                if self._checkforPointinBox(bboxtuple, location):
208                    result.append(feature)                       
209        return result
210
211    def getFeaturesByPropertyBetween(self, propertyname, minvalue, maxvalue):
212        log.debug('GET FEATURES BY PropertyBetween')
213        log.debug('propertyname, min, max: %s, %s, %s'%(propertyname, minvalue, maxvalue))
214        result = []
215        #Identifies times overlapping between filter and feature times
216        if propertyname=='csml:value/csml:PointSeriesCoverage/csml:pointSeriesDomain/csml:TimeSeries/csml:timePositionList':
217            for featureid, feature in self.featureinstances.iteritems():
218                featuretimes=feature._feature.value.pointSeriesDomain.timePositionList.CONTENT.split()
219                featureMinTime=featuretimes[0]
220                featureMaxTime=featuretimes[len(featuretimes)-1]
221                if csml.csmllibs.csmltime.compareTimes(featureMinTime, minvalue ,featureMaxTime) == 1:
222                    result.append(feature)
223                elif csml.csmllibs.csmltime.compareTimes(featureMinTime, maxvalue ,featureMaxTime) == 1:
224                    result.append(feature)   
225        return result
226       
227    def getFeaturesByPropertyEqualTo(self, propertyname, value):
228        log.debug('GET FEATURES BY PropertyEqualTo, value=%s'%value)
229        result=[]
230        value=value #value may be unicode
231        if propertyname == 'csml:parameter':
232            log.debug('filtering on csml:parameter')         
233            for featureid,feature in self.featureinstances.iteritems():
234                if feature._feature.parameter.getStandardName() == value:
235                    result.append(feature)     
236                elif feature._feature.parameter.getNonStandardName() == value:                   
237                    result.append(feature) 
238            return result
239   
240class CSMLFeatureInstance(IFeatureInstance):
241    def __init__(self, title, abstract, feature):
242        """ representing a CSML Feature Instance
243        @ivar title: The title of the feature instance
244        @ivar abstract: The abstract of the feature instance
245        @ivar feature: the csml feature instance object
246         """
247        self.title=title
248        self.abstract=abstract
249        self._feature=feature
250        self.featuretype='csml:'+self._feature.__class__.__name__
251        try:
252            bb= self._feature.getCSMLBoundingBox().getBox()
253            #convert 0 - 360 to -180, 180 as per common WMS convention
254            if abs(bb[2]-bb[0]) >= 359 and abs(bb[2]-bb[0]) < 361:
255                bb[0], bb[2]=-180, 180
256            self.wgs84BBox = bb
257        except AttributeError:
258            log.debug('there is a problem getting the bounding box for feature id %s'%self._feature.id)
259            self.wgs84BBox=()
260   
261    def _fixXlinks(self):
262        ''' replaces xlinks in feature domain with inline content'''
263        for att in ['gridSeriesDomain', 'pointDomain', 'profileSeriesDomain','sectionDomain','trajectoryDomain']:
264            if hasattr(self._feature.value, att):
265                domain=getattr(self._feature.value,att)
266                for ordinate in listify(domain.coordTransformTable.gridOrdinates):
267                    #insertedExtract is the 'hidden' resolved xlink data, expose inline
268                    if hasattr(ordinate.coordAxisValues, 'insertedExtract'):
269                        ordinate.coordAxisValues.CONTENT=cleanString2(str(ordinate.coordAxisValues.insertedExtract.getData()[0].tolist()))                           
270   
271    def toGML(self):
272        """ Create a GML (CSML) representation of the feature """
273        nonamespaceFType=self.featuretype.split(':')[1] #remove the csml: prefix
274        qualifiedFeatureType='{http://ndg.nerc.ac.uk/csml}' + nonamespaceFType
275        emptyelem=etree.Element(qualifiedFeatureType)
276        #modify self._feature so that any references to xlinks are replaced with inline content
277        self._fixXlinks()       
278        csmlelem=self._feature.toXML(emptyelem)
279        return etree.tostring(csmlelem)
280       
281       
282     
283class CSMLwfsLayer(IwfsLayer):
284    """ representing a WFS FeatureType (termed layer here). Implements IwfsLayer
285    @ivar featuretype: The namespaced name of the feature type, e.g. csml:PointSeriesFeature
286   
287    """
288    def __init__(self, featuretype, wgs84bb):
289        self.type=featuretype
290        #Have to hard code some definitions for CSML feature types to
291        #use in the capabilities document as they don't exist anywhere else.
292        #Hardcoding is okay as this is a CSML specific interface, so will only ever deal
293        #with CSML feature types.
294        #TODO: However, might be better to move to some sort of schema.cfg file?
295        self.wgs84BBox=wgs84bb     
296        self.outputformats=['text/xml: subtype=csml/2.0']   
297        if self.type=='csml:GridSeriesFeature':
298            self.title='GridSeriesFeature as defined in Climate Science Modelling Language.'
299            self.abstract='The CSML GridSeriesFeature is used to represent 4D gridded data such as atmospheric model output.'
300            self.keywords=['Grid', 'Climate', 'CSML']
301        elif self.type=='csml:PointSeriesFeature':
302            self.title='PointSeriesFeature as defined in Climate Science Modelling Language.'
303            self.abstract='The CSML PointSeriesFeature represents a time series of measurements at a single point in space.'
304            self.keywords=['Point', 'Timeseries', 'Climate', 'CSML']
305        #TODO: definitions for all feature types.
306       
307       
308
309
Note: See TracBrowser for help on using the repository browser.