source: cows/trunk/cows/service/imps/csmlbackend/csmlcommon.py @ 4351

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

various small changes to csml backend

Line 
1""" Common CSML code shared between WMS, WFS and WCS backends
2@Author: Dominic Lowe
3"""
4
5import os, string
6import logging
7import csml
8from pylons import config, request, session  #config must have tmpfilebuffer and csmlstore values
9import ConfigParser
10log = logging.getLogger(__name__)
11from cows.bbox_util import union
12 
13try:
14    from ndgUtils import ndgObject, ndgRetrieve
15except:
16    log.warning("ndgUtils library could not be loaded, files in the eXist database won't be available, although you should still be able to access files from the csmlstore directory referenced in the ini file.")
17
18from cows.service.wxs_iface import ILayerMapper
19
20class CSMLLayerMapper(ILayerMapper):
21    """
22    Map keyword arguments to a collection of layers.
23    Supports the retrieval of sets of layers according to arbitary
24    keyword/value pairs.
25    Implements  ILayerMapper
26   
27    """
28    def __init__(self):
29        self.layermapcache={}
30        self.connector=CSMLConnector()
31   
32    def getInfo(self, feature):
33        ''' given a csml feature, return basic info about the layer/feature
34        @return:   title, abstract'''
35
36        try:
37            title=feature.name.CONTENT
38        except:
39            title=''
40        try:
41            abstract=feature.description.CONTENT
42        except:
43            abstract=title
44       
45        return title, abstract
46
47
48class CSMLConnector(object):
49    """ Contains common methods for CSML backend used across all WXS services """   
50   
51    def __init__(self):
52        pass
53   
54    def getCsmlDoc(self, fileoruri):
55        """
56        Gets a csml document from file or exist when passed a file name or uri         
57               
58        Note, access control is not implemented on file access, only on exist access.
59       
60        """
61        if string.find(fileoruri,'__NDG-A0__') == -1:
62            #it's a local file not an identifier, read from local csmlstore
63            file=fileoruri
64            csml_dir = config['csmlstore']
65            path = os.path.join(csml_dir, file)
66            if os.path.exists(path+'.csml'):
67                f = path+'.csml'
68            elif os.path.exists(path+'.xml'):
69                f = path +'.xml'
70            else:
71                raise ValueError("Cannot find CSML file %s" % file)
72           
73            d = csml.parser.Dataset()
74            d.parse(f)
75       
76        else:
77            #it's an NDG identifier, get the document from exist.
78            uri=fileoruri
79            uriN=ndgObject(uri)
80            conflocation=config['ndgconfig']
81            cf=ConfigParser.ConfigParser()
82            cf.read(conflocation)
83            status,x=ndgRetrieve(uriN, cf, discovery=0)
84            d=csml.parser.Dataset()
85            d.parse(x)                         
86        return d
87   
88class BboxAggregator(object):
89    """ class to handle aggregations of bounding boxes (in the same crs) and store state for further comparisons"""
90    def __init__(self, initialbbox):
91        """
92        @ivar wgs84bbox: bounding box tuple (llx, lly, urx, ury) """
93        self.bbox=initialbbox
94       
95    def aggregate(self, newbbox):
96        """
97        @ivar newbbox: bounding box tuple (llx, lly, urx, ury) to compare against current bbox.
98        @return: aggregated bbox tuple, (llx, lly, urx, ury)
99        """
100#        log.debug('Comparing new bbox %s with old bbox %s'%(newbbox, self.bbox))
101        #compare new box with old.
102        return union(self.bbox, newbbox)
Note: See TracBrowser for help on using the repository browser.