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

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

Creating FeatureSet? class to provide query methods on collection of FeatureInstances?.

Line 
1"""
2implementation of ILayerMapper, IwfsLayer, IDimension, ILayerSlab interfaces, as defined in wfs_iface.py & wxs_iface.py
3
4"""
5
6from cows.service.imps.csmlbackend.csmlcommon import CSMLLayerMapper, CSMLConnector, BboxAggregator
7from cows.service.wfs_iface import *
8import csml
9from xml.etree import ElementTree as etree
10
11import logging
12log = logging.getLogger(__name__)
13
14class CSMLwfsLayerMapper(CSMLLayerMapper):
15    """
16    Map keyword arguments to a collection of layers.
17    Supports the retrieval of sets of layers according to arbitrary
18    keyword/value pairs.
19    Implements  ILayerMapper
20   
21    WFS differs from WMS/WCS in that the 'layers' are feature types, not instances.
22    So the CSMLwfsLayerMapper map method returns both a map of feature types and a map
23    of feature instances as both are needed for the GetFeature method to work.
24   
25    """
26    def __init__(self):
27        super(CSMLwfsLayerMapper, self).__init__()
28        self.featureinstancecache={}
29   
30
31    def map(self, **kwargs):
32        """
33        Given csml.parser.Dataset object list the names of
34        all layers available.
35       
36        @return: A mapping of layer names to ILayer implementations.
37        @raise ValueError: If no layers are available for these keywords.
38        """
39        fileoruri=kwargs['fileoruri']
40        if fileoruri in self.layermapcache.keys():
41            #we've accessed this layer map before, get it from the cache dictionary
42            return self.layermapcache[fileoruri], self.featureinstancecache[fileoruri]
43         
44        ds = self.connector.get_csml_doc(fileoruri)
45       
46        #The WFS differs from WMS & WCS in that the contents layer is a list of
47        #feature *types* not *instances*. However a record of instances is also
48        #needed to fulfil GetFeature requests:
49        featureset=CSMLFeatureSet() #holds feature instances               
50        layermap={} #feature types       
51        bboxAggregators={}#for aggregations of bounding boxes.
52
53        for feature in csml.csmllibs.csmlextra.listify(ds.featureCollection.featureMembers):
54            title, abstract=self._getInfo(feature)
55            featureset.featureinstances[feature.id]=CSMLFeatureInstance(title, abstract, feature)             
56        for id, instance in featureset.featureinstances.iteritems():
57            ftype='csml:' + instance.featuretype
58            if ftype not in layermap.keys():
59                layermap[ftype]=CSMLwfsLayer(ftype, instance.wgs84BBox)
60                #instantiate an aggregator to compare future bounding boxes with.
61                bboxAggregators[ftype]= BboxAggregator(instance.wgs84BBox)               
62            else:
63                #the featuretype has already been stored in the dictionary.
64                #but, the bounding box may need changing to accommodate this new feature instance.
65#                log.debug('Checking bbox for feature id: %s and title: %s'%(instance._feature.id, instance.title))
66                aggregator=bboxAggregators[ftype]
67                aggregator.aggregate(instance.wgs84BBox)
68                layermap[ftype]=CSMLwfsLayer(ftype, aggregator.bbox)
69           
70
71        if len(layermap) > 0:
72            #cache results
73            self.layermapcache[fileoruri]=layermap
74            self.featureinstancecache[fileoruri]=featureset
75            return layermap, featureset
76        else:
77            raise ValueError
78
79class CSMLFeatureSet(IFeatureSet):
80    """ A set of features available via a WFS. Supports querying methods as used by OGG filters """
81    def __init__(self):
82        self.featureinstances={}
83   
84    def getFeatureByGMLid(self, gmlid):
85        pass
86   
87
88class CSMLFeatureInstance(IFeatureInstance):
89    def __init__(self, title, abstract, feature):
90        """ representing a CSML Feature Instance
91        @ivar title: The title of the feature instance
92        @ivar abstract: The abstract of the feature instance
93        @ivar feature: the csml feature instance object
94         """
95        self.title=title
96        self.abstract=abstract
97        self._feature=feature
98        self.featuretype=self._feature.__class__.__name__
99        bb= self._feature.getCSMLBoundingBox().getBox()
100        #convert 0 - 360 to -180, 180 as per common WMS convention
101        if abs(bb[2]-bb[0]) >= 359 and abs(bb[2]-bb[0]) < 361:
102            bb[0], bb[2]=-180, 180
103        self.wgs84BBox = bb
104       
105    def toGML(self):
106        """ Create a GML (CSML) representation of the feature """
107       
108        #TODO un-hardcode this:
109        emptyelem=etree.Element('{http://ndg.nerc.ac.uk/csml}GridSeriesFeature')
110        csmlelem=self._feature.toXML(emptyelem)
111        return etree.tostring(csmlelem)
112       
113       
114     
115class CSMLwfsLayer(IwfsLayer):
116    """ representing a WFS FeatureType (termed layer here). Implements IwfsLayer
117    @ivar featuretype: The namespaced name of the feature type, e.g. csml:PointSeriesFeature
118   
119    """
120    def __init__(self, featuretype, wgs84bb):
121        self.type=featuretype
122        #Have to hard code some definitions for CSML feature types to
123        #use in the capabilities document as they don't exist anywhere else.
124        #Hardcoding is okay as this is a CSML specific interface, so will only ever deal
125        #with CSML feature types.
126        #TODO: However, might be better to move to some sort of schema.cfg file?
127        self.wgs84BBox=wgs84bb     
128        self.outputformats=['text/xml: subtype=csml/2.0']   
129        if self.type=='csml:GridSeriesFeature':
130            self.title='GridSeriesFeature as defined in Climate Science Modelling Language.'
131            self.abstract='The CSML GridSeriesFeature is used to represent 4D gridded data such as atmospheric model output.'
132            self.keywords=['Grid', 'Climate', 'CSML']
133        elif self.type=='csml:PointSeriesFeature':
134            self.title='PointSeriesFeature as defined in Climate Science Modelling Language.'
135            self.abstract='The CSML PointSeriesFeature represents a time series of measurements at a single point in space.'
136            self.keywords=['Point', 'Timeseries', 'Climate', 'CSML']
137        #TODO: definitions for all feature types.
138       
139       
140
Note: See TracBrowser for help on using the repository browser.