source: cows/trunk/cows/service/imps/StationCollection.py @ 4228

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/trunk/cows/service/imps/StationCollection.py@4228
Revision 4228, 1.9 KB checked in by spascoe, 11 years ago (diff)

Changed print statements to loggging calls where appropriate.

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        stationsInBbox = []
22        for station in self.stations:
23            if station.lat >= minlat:
24                if station.lat <= maxlat:
25                    if station.lon >= minlon:
26                        if station.lon <= maxlon:
27                            stationsInBbox.append(station)
28        return stationsInBbox
29
30    def getNearestStation(self,lat,lon):
31        ''' Determines the station nearest to the given geospatial point in the station collection '''
32
33        curNearest=None
34        curDist = -1
35
36        for station in self.stations:
37                #calculate distance
38                tempDist = self.getDist(lat, lon, station.lat, station.lon)
39                #if distance is smaller than the current shortest distance
40                if curDist < 0 or tempDist < curDist:
41                        curNearest = station
42                        curDist = tempDist
43        return curNearest
44
45
46    def getDist(self, srcLat, srcLon, destLat, destLon):
47        '''Calculates the distance between to geospatial points'''     
48        dlon = destLon - srcLon
49        dlat = destLat - srcLat
50        #a = (math.sin(dlat / 2))**2 + math.cos(srcLat) * math.cos(destLat) * (math.sin(dlon / 2))**2       
51        #c = 2 * math.asin(min(1, math.sqrt(a)))
52        #dist = 3956 * c
53        dist = math.sqrt(dlat**2 + dlon**2)
54        return dist
55               
56       
Note: See TracBrowser for help on using the repository browser.