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

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

Extended documentation of Station and StationConvertor?.

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