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

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

Most of the coding for geospatial operators. Improvements
to results display for multiple bounding boxes.

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,helper,entity='moles'):
25        '''Parse a data entity and load a boundimatch={'moles':
26                 {'North':'*//dgSpatialCoverage/BoundingBox/LimitNorth',
27                  'South':'*//dgSpatialCoverage/BoundingBox/LimitSouth',
28                  'West':'*//dgSpatialCoverage/BoundingBox/LimitWest',
29                  'East':'*//dgSpatialCoverage/BoundingBox/LimitEast'},
30                'DIF':
31                 {'North':'Spatial_Coverage/Northernmost_Latitude',
32                  'South':'Spatial_Coverage/Southernmost_Latitude',
33                  'West':'Spatial_Coverage/Westernmost_Longitude',
34                  'East':'Spatial_Coverage/Easternmost_Longitude'}
35                }ng box '''
36        #don't think this will work for multiple bounding boxes in a DIF ... yet
37        parent={'moles':'*//dgSpatialCoverage/BoundingBox',
38                'DIF':'Spatial_Coverage'}
39        #N,W,E,S
40        ebox={'moles':['LimitNorth','LimitWest','LimitEast','LimitSouth'],
41              'DIF':['Northernmost_Latitude','Westernmost_Longitude',
42                     'Easternmost_Longitude','Southernmost_Latitude']}
43        coverages=helper.findall(elem,parent[entity])
44        self.boxes=[]
45        self.nboxes=0
46        for coverage in coverages:
47            self.set([helper.getText(coverage,i) for i in ebox[entity]])
48       
49    def set(self,box):
50        try: 
51            self.boxes.append([geoString2float(i) for i in box])
52            self.nboxes+=1
53        except:
54            pass
55       
56    def toHTMLbox(self):
57        if self.nboxes!=1:
58            return '<p> Record includes %s bounding boxes </p>'%self.nboxes
59        else:
60            return '''<table><tbody><tr><td colspan="2" align="center">%s</td></tr>
61                      <tr><td>%s,</td><td>&nbsp;%s</td></tr>
62                      <tr><td colspan="2" align="center">%s</td></tr>
63                    </tbody></table>'''%tuple(self.boxes[0])
64                   
65    def __str__(self):
66        return '%s,%s'%(self.nboxes,self.boxes)
67
68   
69import unittest
70
71class TestCase(unittest.TestCase):
72   
73    def testMoveBox(self):
74        w,e=1,359
75        w,e=moveBox(w,e)
76        self.assertEqual((w,e),(-179.,179.))
77        w,e=190,200
78        w,e=moveBox(w,e)
79        self.assertEqual((w,e),(-170.,-160.))
80        w,e=10,20
81        w,e=moveBox(w,e)
82        self.assertEqual((w,e),(10.,20.))
83   
84if __name__=="__main__":
85
86    unittest.main()
87       
Note: See TracBrowser for help on using the repository browser.