source: cows/trunk/cows/service/imps/csml_geoplot_backend/csml_geoplot_layer_mapper.py @ 5632

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

Adding CSML Geoplot backend to COWS trunk from Qesdi branch

Line 
1'''
2Created on 9 Jun 2009
3
4@author: pnorton
5'''
6
7import logging
8from copy import copy
9
10import csml
11
12from cows.service.wxs_iface import ILayerMapper
13
14from cows.service.imps.csml_geoplot_backend.csml_geoplot_wms_layer import CSMLGeoplotWmsLayer
15
16from cows.service.imps.csmlbackend.csmlcommon import CSMLConnector
17
18from cows.service.imps.csmlbackend.wms_csmllayer import CSMLwmsDimension
19
20log = logging.getLogger(__name__)
21
22class CSMLGeoplotLayerMapper(ILayerMapper):
23   
24    def __init__(self):
25        self.layermapcache={}
26        self.connector = globalCSMLConnector
27   
28    def _getFeatureTitleAndAbstract(self, feature):
29        ''' given a csml feature, return basic info about the layer/feature/coverage
30        @return:   title, abstract'''
31
32        try:
33            title=feature.name.CONTENT
34        except:
35            title=''
36           
37        try:
38            abstract=feature.description.CONTENT
39        except:
40            abstract=title
41       
42        return title, abstract   
43   
44    def _getInfo(self, feature):
45        ''' given a csml feature, return info about the layer/feature
46        @return:   title, abstract, dimensions, units, crss '''
47
48        title, abstract = self._getFeatureTitleAndAbstract(feature)
49        units=feature.getDomainUnits() 
50        dimensions={}
51        tmpunits=copy(units)
52        tmpunits.reverse()
53        domain = feature.getDomain()
54       
55        for dim in feature.getAxisLabels():
56            nextdim=CSMLwmsDimension(domain, dim, tmpunits.pop())
57           
58            if dim not in ['latitude', 'longitude']:
59                dimensions[dim]=nextdim
60               
61        crs=feature.getNativeCRS()
62        crss=[self._crscat.getCRS(crs).twoD]
63       
64        if 'EPSG:4326' in crss:
65            crss.append('CRS:84')
66            crss.append('WGS84')
67                   
68        #the units to return are the units of measure.
69        try:
70            units=feature.value.rangeSet.valueArray.valueComponent.uom
71        except:
72            units='unknown units'
73           
74        return title, abstract, dimensions, units, crss
75       
76       
77     
78    def map(self, **kwargs):
79        """
80        Given csml.parser.Dataset object list the names of
81        all layers available.
82       
83        @return: A mapping of layer names to ILayer implementations.
84        @raise ValueError: If no layers are available for these keywords.
85        """
86        fileoruri=kwargs['fileoruri']
87               
88        if fileoruri in self.layermapcache.keys():
89           
90            log.debug("cached layermap used for fileoruri = %s" % (fileoruri,))
91           
92            #we've accessed this layer map before, get it from the cache dictionary
93            return self.layermapcache[fileoruri]
94       
95        log.debug("building map for fileoruri = %s" % (fileoruri,))
96       
97        ds = self.connector.getCsmlDoc(fileoruri)
98        layermap={}
99        self._crscat=csml.csmllibs.csmlcrs.CRSCatalogue()
100       
101        for feature in csml.csmllibs.csmlextra.listify(ds.featureCollection.featureMembers):
102           
103            log.debug("creating layer for feature.id = %s" % (feature.id, ))
104           
105            title, abstract, dimensions, units, crss=self._getInfo(feature)           
106            layermap[feature.id] = CSMLGeoplotWmsLayer(title,abstract, dimensions, units, crss, feature,
107                                               name=feature.id)
108           
109        if len(layermap) > 0:
110            self.layermapcache[fileoruri]=layermap
111            return layermap
112        else:
113            raise ValueError
114       
115#
116# Global CSML connector instance
117#
118globalCSMLConnector = CSMLConnector()
Note: See TracBrowser for help on using the repository browser.