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

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

Added Epydoc fields to comments.

Line 
1'''
2Classes for work with I{<np:Station>}'s.
3'''
4
5from cElementTree import XML
6
7npXmlNamespace = 'http://ndg.nerc.ac.uk/np'  # the namespace in which the Station element is defined
8
9class NPStation:
10    '''
11    Represents a ground meteorological station, identified by C{id}, with description C{desc},
12    located at (C{lon},C{lat}), and containing CSML features with identifiers C{csmlFeatureIds}.
13    '''
14
15    def __init__(self, id = None, desc = None, lon = None, lat = None, csmlFeatureIds = None):
16        '''
17        The initialiser. It is recommended not to pass any optional value (i.e. use only C{NPStation()})
18        and later call C{parseString} or C{parseXML}.
19        @param id: Station identifier (e.g. "midas.station.1")
20        @type id: str
21        @param desc: Station description (e.g. "FAIR ISLE")
22        @type desc: str
23        @type lon: float
24        @type lat: float
25        @type csmlFeatureIds: str list
26        '''
27        self.id = id
28        self.desc = desc
29        self.lon = lon; self.lat = lat
30        self.csmlFeatureIds = csmlFeatureIds
31
32    def parseString(self, str):
33        '''
34        Parse in the station from a string C{str} containing XML.
35        '''
36        stationElement = XML(str)
37        self.parseXML(stationElement)
38
39    def parseXML(self, stationElement):
40        '''
41        Parse in the station directly from an C{cElementTree.Element} object.
42        @param stationElement: An element containing station in XML form.
43        @type stationElement: C{cElementTree.Element}
44        '''
45
46        # Extract the identifier, short description, and longitude and latitute
47        (stationNameElement, stationIDElement, locationElement, featuresElement) = stationElement.getchildren()
48        pointElement = locationElement.getchildren()[0]
49        posElement = pointElement.getchildren()[0]
50        (self.lon,self.lat) = map(float, posElement.text.split())
51        self.id = stationElement.get('{http://www.opengis.net/gml}id')
52        self.desc = stationNameElement.text
53
54        # Extract the IDs of the associated CSML features
55        self.csmlFeatureIds = []
56        for stationsFeatureElement in featuresElement.getchildren():
57            (featureTypeElement, featureParameterElement) = stationsFeatureElement.getchildren()
58            if featureTypeElement.text != 'csml:PointSeriesFeature':
59                raise TypeError('Feature type not "csml:PointSeriesFeature"')
60            self.csmlFeatureIds.append(featureParameterElement.text)
61
62    def __repr__(self):
63        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % (
64            self.id, self.desc, self.lon, self.lat, len(self.csmlFeatureIds), str(self.csmlFeatureIds)
65            )
66
67class WFSStationCollection:
68    '''
69    Represents a <wfs:FeatureCollection> element that contains *specifically* <np:Station> elements!
70    @ivar stations: A list of C{NPStation}'s contained in the collection.
71    '''
72
73    def __init__(self):
74        self.stations = []
75
76    def parseString(self, str):
77        '''
78        Parse in the <wfs:FeatureCollection> element returned from GeoServer, and save individual stations.
79        @param str: A string containing the the <wfs:FeatureCollection> element.
80
81        '''
82        featureCollectionElement = XML(str)
83        self.parseXML(featureCollectionElement)
84
85    def parseXML(self, featureCollectionElement):
86        '''
87        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
88        Use if your WFS subset of XML is stored in an C{cElementTree.Element} object.
89        '''
90        featureMembersElement = featureCollectionElement.getchildren()[0]
91        for npStationElement in featureMembersElement.getchildren():
92            npStation = NPStation()
93            npStation.parseXML(npStationElement)
94            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.