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

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

updating version no

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        if 'EPSG:4326' in crs:
68            crss.append('CRS:84')
69            crss.append('WGS84')
70                   
71        #the units to return are the units of measure.
72        try:
73            units=feature.value.rangeSet.valueArray.valueComponent.uom
74        except:
75            units='unknown units'
76           
77        return title, abstract, dimensions, units, crss
78       
79       
80     
81    def map(self, **kwargs):
82        """
83        Given csml.parser.Dataset object list the names of
84        all layers available.
85       
86        @return: A mapping of layer names to ILayer implementations.
87        @raise ValueError: If no layers are available for these keywords.
88        """
89        fileoruri=kwargs['fileoruri']
90               
91        if fileoruri in self.layermapcache.keys():
92           
93            log.debug("cached layermap used for fileoruri = %s" % (fileoruri,))
94           
95            #we've accessed this layer map before, get it from the cache dictionary
96            return self.layermapcache[fileoruri]
97       
98        log.debug("building map for fileoruri = %s" % (fileoruri,))
99       
100        ds = self.connector.getCsmlDoc(fileoruri)
101        layermap={}
102        self._crscat=csml.csmllibs.csmlcrs.CRSCatalogue()
103       
104        for feature in csml.csmllibs.csmlextra.listify(ds.featureCollection.featureMembers):
105           
106            log.debug("creating layer for feature.id = %s" % (feature.id, ))
107           
108            title, abstract, dimensions, units, crss=self._getInfo(feature)           
109            layermap[feature.id] = CSMLGeoplotWmsLayer(title,abstract, dimensions, units, crss, feature,
110                                               name=feature.id)
111           
112        if len(layermap) > 0:
113            self.layermapcache[fileoruri]=layermap
114            return layermap
115        else:
116            raise ValueError
117       
118#
119# Global CSML connector instance
120#
121globalCSMLConnector = CSMLConnector()
Note: See TracBrowser for help on using the repository browser.