source: TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/imps/StationCollection.py @ 3786

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/branches/ows_framework-refactor/ows_common/ows_common/service/imps/StationCollection.py@3786
Revision 3786, 1.9 KB checked in by spascoe, 12 years ago (diff)
Line 
1from csml2kml.Station import  WFSStationCollection
2import math
3
4class StationCollection(WFSStationCollection):
5    def __init__(self, xml_stringorelem):
6        super(StationCollection, self).__init__();
7        if type(xml_stringorelem) is str:
8            self.parseString(xml_stringorelem)
9        else:
10            self.parseXML(xml_stringorelem)
11           
12    def listAllLatLons(self):
13        ''' returns a list of lat lon pairs [lat0, lon0, lat1, lon1, lat2, lon2,...latn, lonn] '''
14        latlons=[]
15        for station in self.stations:
16            latlons.append(station.lat)
17            latlons.append(station.lon)
18        return latlons
19       
20    def getStationsInBBox(self, minlat, minlon, maxlat, maxlon):
21        #print [minlat, minlon, maxlat, maxlon]
22        stationsInBbox = []
23        for station in self.stations:
24            if station.lat >= minlat:
25                if station.lat <= maxlat:
26                    if station.lon >= minlon:
27                        if station.lon <= maxlon:
28                            stationsInBbox.append(station)
29        return stationsInBbox
30
31    def getNearestStation(self,lat,lon):
32        ''' Determines the station nearest to the given geospatial point in the station collection '''
33
34        curNearest=None
35        curDist = -1
36
37        for station in self.stations:
38                #calculate distance
39                tempDist = self.getDist(lat, lon, station.lat, station.lon)
40                #if distance is smaller than the current shortest distance
41                if curDist < 0 or tempDist < curDist:
42                        curNearest = station
43                        curDist = tempDist
44        #print curDist
45        return curNearest
46
47
48    def getDist(self, srcLat, srcLon, destLat, destLon):
49        '''Calculates the distance between to geospatial points'''     
50        dlon = destLon - srcLon
51        dlat = destLat - srcLat
52        #a = (math.sin(dlat / 2))**2 + math.cos(srcLat) * math.cos(destLat) * (math.sin(dlon / 2))**2       
53        #c = 2 * math.asin(min(1, math.sqrt(a)))
54        #dist = 3956 * c
55        dist = math.sqrt(dlat**2 + dlon**2)
56        return dist
57               
58       
Note: See TracBrowser for help on using the repository browser.