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

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

Removing tabs from python source

Line 
1# BSD Licence
2# Copyright (c) 2009, Science & Technology Facilities Council (STFC)
3# All rights reserved.
4#
5# See the LICENSE file in the source distribution of this software for
6# the full license text.
7
8from csml2kml.Station import  WFSStationCollection
9import math
10
11class StationCollection(WFSStationCollection):
12    def __init__(self, xml_stringorelem):
13        super(StationCollection, self).__init__();
14        if type(xml_stringorelem) is str:
15            self.parseString(xml_stringorelem)
16        else:
17            self.parseXML(xml_stringorelem)
18           
19    def listAllLatLons(self):
20        ''' returns a list of lat lon pairs [lat0, lon0, lat1, lon1, lat2, lon2,...latn, lonn] '''
21        latlons=[]
22        for station in self.stations:
23            latlons.append(station.lat)
24            latlons.append(station.lon)
25        return latlons
26       
27    def getStationsInBBox(self, minlat, minlon, maxlat, maxlon):
28        stationsInBbox = []
29        for station in self.stations:
30            if station.lat >= minlat:
31                if station.lat <= maxlat:
32                    if station.lon >= minlon:
33                        if station.lon <= maxlon:
34                            stationsInBbox.append(station)
35        return stationsInBbox
36
37    def getNearestStation(self,lat,lon):
38        ''' Determines the station nearest to the given geospatial point in the station collection '''
39
40        curNearest=None
41        curDist = -1
42
43        for station in self.stations:
44                #calculate distance
45                tempDist = self.getDist(lat, lon, station.lat, station.lon)
46                #if distance is smaller than the current shortest distance
47                if curDist < 0 or tempDist < curDist:
48                        curNearest = station
49                        curDist = tempDist
50        return curNearest
51
52
53    def getDist(self, srcLat, srcLon, destLat, destLon):
54        '''Calculates the distance between to geospatial points'''     
55        dlon = destLon - srcLon
56        dlat = destLat - srcLat
57        #a = (math.sin(dlat / 2))**2 + math.cos(srcLat) * math.cos(destLat) * (math.sin(dlon / 2))**2       
58        #c = 2 * math.asin(min(1, math.sqrt(a)))
59        #dist = 3956 * c
60        dist = math.sqrt(dlat**2 + dlon**2)
61        return dist
62               
63       
Note: See TracBrowser for help on using the repository browser.