source: TI07-MOLES/trunk/PythonCode/wsgi/geoUtilities.py @ 2386

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/geoUtilities.py@2522
Revision 2386, 3.0 KB checked in by lawrence, 13 years ago (diff)

Improvements in date and person handling.

Line 
1from Utilities import wrapGetText
2
3def geoString2float(x):
4    if x[-1:] in 'NE':
5        return float(x[:-1])
6    elif x[-1:] in 'SW':
7        return -float(x[:-1])
8    else:
9        return float(x)
10
11class Bounding:
12    ''' Separated out because this is NDG specific really '''
13    def __init__(self,elem,entity='stubB',getter=wrapGetText):
14        '''Parse a data entity and load a bounding box '''
15        match={'stubB':
16                 {'North':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitNorth',
17                  'South':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitSouth',
18                  'West':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitWest',
19                  'East':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitEast'},
20                'DIF':
21                 {'North':'Spatial_Coverage/Northernmost_Latitude',
22                  'South':'Spatial_Coverage/Southernmost_Latitude',
23                  'West':'Spatial_Coverage/Westernmost_Longitude',
24                  'East':'Spatial_Coverage/Easternmost_Longitude'}
25                }
26        #try:
27        North=getter(elem,match[entity]['North'])
28        South=getter(elem,match[entity]['South'])
29        West=getter(elem,match[entity]['West'])
30        East=getter(elem,match[entity]['East'])
31        self.set([North,West,East,South])
32        #except:
33        #    self.box=None
34
35    def set(self,box):
36        try: 
37            self.box=[geoString2float(i) for i in box]
38        except:
39            self.box=None
40
41    def toHTML(self):
42        return self.toHTMLbox()
43   
44    def toHTMLbox(self):
45        if self.box is not None:
46            html='''<table><tbody><tr><td colspan="2" align="center">%s</td></tr>
47                      <tr><td>%s,</td><td>&nbsp;%s</td></tr>
48                      <tr><td colspan="2" align="center">%s</td></tr>
49                    </tbody></table>'''%tuple(self.box)
50        else:
51            html=''
52        return html
53
54class TimeCoverage:
55    def __init__(self,tc):
56        ''' Takes a tuple of (start date, end date, status) '''
57        copy=[]
58        for i in tc:
59            if i=='': 
60                copy.append('Unknown')
61            else: copy.append(i)
62        self.data=tuple(copy)
63    def __str__(self):
64        if self.data==('Unknown','Unknown','Unknown'): 
65            return 'Unknown'
66        else:
67            print self.data
68            return 'Start Date:%s, End Date:%s<br/>Status:%s'%self.data
69    def __getitem__(self,i):
70        return self.data[i]
71       
72import unittest
73class TestCase(unittest.TestCase):
74   
75    def testgeoString2float(self):
76        self.assertEqual(geoString2float('12S'),-12.0)
77       
78    def testBounding(self):
79        bbox=Bounding('dummy')
80        bbox.set(['15N','12W','12E','15S'])
81        y=bbox.toHTML()
82       
83    def testTimeCoverage(self):
84        ''' Test time coverage class '''
85        tc=TimeCoverage(('01-01-2001','01-02-2001','Complete'))
86        print tc
87        self.assertEqual(tc[2],'Complete')
88   
89if __name__=="__main__":
90    unittest.main()
Note: See TracBrowser for help on using the repository browser.