source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/geoUtilities.py @ 3073

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/geoUtilities.py@3073
Revision 3073, 3.3 KB checked in by lawrence, 12 years ago (diff)

Support for viewing xhtml description sections within stub-B

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   
11def moveBox(w,e):
12    ''' Take a 0,360 bounding box and force into -180,180 '''
13    ww,ee=float(w),float(e)
14    if ww<180.0 and ee>180.0:
15        return ww-180.0,ee-180.0
16    else:
17       if ww>180.0:
18           return ww-360.,ee-360.
19       else: return ww,ee
20
21class Bounding:
22    ''' Separated out because this is NDG specific really
23    NB needs to support multiple bounding boxes properly and doesn't ... '''
24    def __init__(self,elem,entity='stubB',getter=wrapGetText):
25        '''Parse a data entity and load a bounding box '''
26        match={'stubB':
27                 {'North':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitNorth',
28                  'South':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitSouth',
29                  'West':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitWest',
30                  'East':'dgDataSummary/dgDataCoverage/dgSpatialCoverage/BoundingBox/LimitEast'},
31                'DIF':
32                 {'North':'Spatial_Coverage/Northernmost_Latitude',
33                  'South':'Spatial_Coverage/Southernmost_Latitude',
34                  'West':'Spatial_Coverage/Westernmost_Longitude',
35                  'East':'Spatial_Coverage/Easternmost_Longitude'}
36                }
37        #try:
38        North=getter(elem,match[entity]['North'])
39        South=getter(elem,match[entity]['South'])
40        West=getter(elem,match[entity]['West'])
41        East=getter(elem,match[entity]['East'])
42        self.boxes=[]
43        self.nboxes=0
44       
45        self.set([North,West,East,South])
46       
47    def set(self,box):
48        try: 
49            self.boxes.append([geoString2float(i) for i in box])
50            self.nboxes+=1
51        except:
52            pass
53       
54    def toHTMLbox(self):
55        if self.nboxes!=1:
56            return '<p> Record includes %s bounding boxes </p>'%self.nboxes
57        else:
58            return '''<table><tbody><tr><td colspan="2" align="center">%s</td></tr>
59                      <tr><td>%s,</td><td>&nbsp;%s</td></tr>
60                      <tr><td colspan="2" align="center">%s</td></tr>
61                    </tbody></table>'''%tuple(self.boxes[0])
62                   
63    def __str__(self):
64        return '%s,%s'%(self.nboxes,self.boxes)
65
66class TimeCoverage:
67    def __init__(self,tc):
68        ''' Takes a tuple of (start date, end date, status) '''
69        copy=[]
70        for i in tc:
71            if i=='': 
72                copy.append('Unknown')
73            else: copy.append(i)
74        self.data=tuple(copy)
75    def __str__(self):
76        if self.data==('Unknown','Unknown','Unknown'): 
77            return 'Unknown'
78        else:
79            print self.data
80            return 'Start Date:%s, End Date:%s<br/>Status:%s'%self.data
81    def __getitem__(self,i):
82        return self.data[i]
83   
84import unittest
85
86class TestCase(unittest.TestCase):
87   
88    def testMoveBox(self):
89        w,e=1,359
90        w,e=moveBox(w,e)
91        self.assertEqual((w,e),(-179.,179.))
92        w,e=190,200
93        w,e=moveBox(w,e)
94        self.assertEqual((w,e),(-170.,-160.))
95        w,e=10,20
96        w,e=moveBox(w,e)
97        self.assertEqual((w,e),(10.,20.))
98   
99if __name__=="__main__":
100
101    unittest.main()
102       
Note: See TracBrowser for help on using the repository browser.