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

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

unfinished implementation of getFeature

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__)
11 
12try:
13    from ndgUtils import ndgObject, ndgRetrieve
14except:
15    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.")
16
17from cows.service.wxs_iface import ILayerMapper
18
19class CSMLLayerMapper(ILayerMapper):
20    """
21    Map keyword arguments to a collection of layers.
22    Supports the retrieval of sets of layers according to arbitary
23    keyword/value pairs.
24    Implements  ILayerMapper
25   
26    """
27    def __init__(self):
28        self.layermapcache={}
29        self.connector=CSMLConnector()
30   
31    def _getInfo(self, feature):
32        ''' given a csml feature, return basic info about the layer/feature
33        @return:   title, abstract'''
34
35        try:
36            title=feature.name.CONTENT
37        except:
38            title=''
39        try:
40            abstract=feature.description.CONTENT
41        except:
42            abstract=title
43       
44        return title, abstract
45
46
47class CSMLConnector(object):
48    """ Contains common methods for CSML backend used across all WXS services """   
49   
50    def __init__(self):
51        pass
52   
53    def get_csml_doc(self, fileoruri):
54        """
55        Gets a csml document from file or exist when passed a file name or uri         
56               
57        Note, access control is not implemented on file access, only on exist access.
58       
59        """
60        if string.find(fileoruri,'__NDG-A0__') == -1:
61            #it's a local file not an identifier, read from local csmlstore
62            file=fileoruri
63            csml_dir = config['csmlstore']
64            path = os.path.join(csml_dir, file)
65            if os.path.exists(path+'.csml'):
66                f = path+'.csml'
67            elif os.path.exists(path+'.xml'):
68                f = path +'.xml'
69            else:
70                raise ValueError("Cannot find CSML file %s" % file)
71           
72            d = csml.parser.Dataset()
73            d.parse(f)
74       
75        else:
76            #it's an NDG identifier, get the document from exist.
77            uri=fileoruri
78            uriN=ndgObject(uri)
79            conflocation=config['ndgconfig']
80            cf=ConfigParser.ConfigParser()
81            cf.read(conflocation)
82            status,x=ndgRetrieve(uriN, cf, discovery=0)
83            d=csml.parser.Dataset()
84            d.parse(x)                         
85        return d
86   
87class BboxAggregator(object):
88    """ class to handle aggregations of bounding boxes (in the same crs)"""
89    def __init__(self, initialbbox):
90        """
91        @ivar wgs84bbox: bounding box tuple (llx, lly, urx, ury) """
92        self.bbox=initialbbox
93       
94    def aggregate(self, newbbox):
95        """
96        @ivar newbbox: bounding box tuple (llx, lly, urx, ury) to compare against current bbox.
97        @return: aggregated bbox tuple, (llx, lly, urx, ury)
98        """
99#        log.debug('Comparing new bbox %s with old bbox %s'%(newbbox, self.bbox))
100        #compare new box with old.
101        if newbbox[0] < self.bbox[0]:
102            self.bbox[0]=newbbox[0]
103        if newbbox[1] < self.bbox[1]:
104            self.bbox[1]=newbbox[1]
105        if newbbox[2] > self.bbox[2]:
106            self.bbox[2]=newbbox[2]
107        if newbbox[3] > self.bbox[3]:
108            self.bbox[3]=newbbox[3]
109        return self.bbox
Note: See TracBrowser for help on using the repository browser.