source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/Station.py @ 3654

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/csml2kml/csml2kml/Station.py@3654
Revision 3654, 5.4 KB checked in by mkochan, 12 years ago (diff)

Changed modules to use ET. Added a run-all-tests script (run_tests.sh).

Line 
1'''
2Classes for work with I{<np:Station>}'s.
3'''
4
5from ET import XML
6from matplotlib import dates     # a very good date/time module from matplotviz -- allows years < 1970
7
8npXmlNamespace = 'http://ndg.nerc.ac.uk/np'  # the namespace in which the Station element is defined
9
10class NPStation:
11    '''
12    Represents a ground meteorological station, identified by C{id}, with description C{desc},
13    located at (C{lon},C{lat}), and containing CSML features with identifiers C{csmlFeatureIds}.
14    @ivar id: Station identifier (e.g. "midas.station.1")
15    @type id: str
16    @ivar desc: Station description (e.g. "FAIR ISLE")
17    @type desc: str
18    @ivar lon: Station longitude
19    @type lon: float
20    @ivar lat: Station latitude
21    @type lat: float
22    @type features: C{NPStationFeature} list
23
24    '''
25
26    def __init__(self, id = None, desc = None, lon = None, lat = None, stationFeatures = None):
27        '''
28        The initialiser (use empty call C{NPStation()} and later call the C{parseString} or C{parseXML} method).
29        '''
30        self.id = id
31        self.desc = desc
32        self.lon = lon; self.lat = lat
33        self.stationFeatures = stationFeatures
34
35    def parseString(self, str):
36        '''
37        Parse in the station from a string C{str} containing XML.
38        '''
39        stationElement = XML(str)
40        self.parseXML(stationElement)
41
42    def parseXML(self, stationElement):
43        '''
44        Parse in the station directly from an C{cElementTree.Element} object.
45        @param stationElement: An element containing station in XML form.
46        @type stationElement: C{cElementTree.Element}
47        '''
48
49        # Extract the identifier, short description, and longitude and latitute
50        (stationNameElement, stationIDElement, locationElement, stationFeaturesElement) = stationElement.getchildren()
51        pointElement = locationElement.getchildren()[0]
52        posElement = pointElement.getchildren()[0]
53        (self.lon,self.lat) = map(float, posElement.text.split())
54        self.id = stationElement.get('{http://www.opengis.net/gml}id')
55        self.desc = stationNameElement.text
56
57        self.stationFeatures = []
58        for stationFeatureElement in stationFeaturesElement.getchildren():
59            npStationFeature = NPStationFeature()
60            npStationFeature.parseXML(stationFeatureElement)
61            self.stationFeatures.append(npStationFeature)
62
63    def __repr__(self):
64        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % (
65            self.id, self.desc, self.lon, self.lat, len(self.stationFeatures), str(self.stationFeatures)
66            )
67
68class NPStationFeature:
69    '''
70    Represents a I{<np:StationFeature>} element.
71    @ivar featureId: Unique ID of the feature
72    @type featureId: C{str}
73    @ivar collectBeginDate: Data when feature collection started.
74    @type collectBeginDate: C{matplotlib.dates.datetime.datetime}
75    @ivar collectEndDate: Data when feature collection started.
76    @type collectEndDate: C{matplotlib.dates.datetime.datetime}
77    '''
78
79    def __init__(self, featureId = None, featureType = None, collectBeginDate = None, collectEndDate = None):
80        '''
81        The initialiser (use empty call C{NPStationFeature()} and later call the C{parseXML} method).
82        '''
83        self.featureId = featureId
84        self.featureType = featureType
85        self.collectBeginDate = collectBeginDate
86        self.collectEndDate = collectEndDate
87
88    def parseXML(self, stationFeatureElement):
89        '''
90        Parse in the station feature (I{np:StationFeature}) directly from an C{cElementTree.Element} object.
91        Note that if the station is the "GENERIC MARINE SOURCE", the elements will have empty content.
92        @param stationFeatureElement: An element containing a station feature in XML form.
93        @type stationElement: C{cElementTree.Element}
94        '''
95        (featureTypeElement, featureIdElement, 
96         collectBeginDateElement, collectEndDateElement) = stationFeatureElement.getchildren()
97        self.featureType = featureTypeElement.text
98        self.featureId = featureIdElement.text
99        self.collectBeginDate = dates.dateutil.parser.parse(collectBeginDateElement.text)
100        self.collectEndDate = dates.dateutil.parser.parse(collectEndDateElement.text)
101
102    def __repr__(self):
103        return str(vars(self))
104
105class WFSStationCollection:
106    '''
107    Represents a <wfs:FeatureCollection> element that contains *specifically* <np:Station> elements!
108    @ivar stations: A list of C{NPStation}'s contained in the collection.
109    '''
110
111    def __init__(self):
112        self.stations = []
113
114    def parseString(self, str):
115        '''
116        Parse in the <wfs:FeatureCollection> element returned from GeoServer, and save individual stations.
117        @param str: A string containing the the <wfs:FeatureCollection> element.
118
119        '''
120        featureCollectionElement = XML(str)
121        self.parseXML(featureCollectionElement)
122
123    def parseXML(self, featureCollectionElement):
124        '''
125        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
126        Use if your WFS subset of XML is stored in an C{cElementTree.Element} object.
127        '''
128        featureMembersElement = featureCollectionElement.getchildren()[0]
129        for npStationElement in featureMembersElement.getchildren():
130            npStation = NPStation()
131            npStation.parseXML(npStationElement)
132            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.