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

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

More improvements to wsgi discovery, some unit tests, improved
namespace handling in DIF.py ...

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        self.data=tc
58    def __str__(self):
59        return 'Start:%s, End:%s, Status:%s'%self.data
60    def __getitem__(self,i):
61        return self.data[i]
62       
63import unittest
64class TestCase(unittest.TestCase):
65   
66    def testgeoString2float(self):
67        self.assertEqual(geoString2float('12S'),-12.0)
68       
69    def testBounding(self):
70        bbox=Bounding('dummy')
71        bbox.set(['15N','12W','12E','15S'])
72        y=bbox.toHTML()
73       
74    def testTimeCoverage(self):
75        ''' Test time coverage class '''
76        tc=TimeCoverage(('01-01-2001','01-02-2001','Complete'))
77        print tc
78        self.assertEqual(tc[2],'Complete')
79   
80if __name__=="__main__":
81    unittest.main()
Note: See TracBrowser for help on using the repository browser.