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

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

Added NPStationFeature class into Station.py. Removed dependency on the standard "datetime" module from StationConvertor?.py.

Line 
1'''
2Classes for work with I{<np:Station>}'s.
3'''
4
5from cElementTree import XML
6from pylab 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, features = 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.csmlFeatureIds = csmlFeatureIds
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, featuresElement) = 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        # Extract the IDs of the associated CSML features
58        self.csmlFeatureIds = []
59        for stationsFeatureElement in featuresElement.getchildren():
60            (featureTypeElement, featureParameterElement) = stationsFeatureElement.getchildren()
61            if featureTypeElement.text != 'csml:PointSeriesFeature':
62                raise TypeError('Feature type not "csml:PointSeriesFeature"')
63            self.csmlFeatureIds.append(featureParameterElement.text)
64
65    def __repr__(self):
66        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % (
67            self.id, self.desc, self.lon, self.lat, len(self.csmlFeatureIds), str(self.csmlFeatureIds)
68            )
69
70class NPStationFeature:
71    '''
72    Represents a I{<np:StationFeature>} element.
73    @ivar featureId: Unique ID of the feature
74    @type featureId: C{str}
75    @ivar collectBeginDate: Data when feature collection started.
76    @type collectBeginDate: C{pylab.dates.datetime.datetime}
77    @ivar collectEndDate: Data when feature collection started.
78    @type collectEndDate: C{pylab.dates.datetime.datetime}
79    '''
80
81    def __init__(self, featureId = None, featureType = None, collectBeginDate = None, collectEndDate = None):
82        '''
83        The initialiser (use empty call C{NPStationFeature()} and later call the C{parseXML} method).
84        '''
85        self.featureId = featureId
86        self.featureType = featureType
87        self.collectBeginDate = collectBeginDate
88        self.collectEndDate = collectEndDate
89
90    def parseXML(self, stationFeatureElement):
91        '''
92        Parse in the station directly from an C{cElementTree.Element} object.
93        @param stationFeatureElement: An element containing a station feature in XML form.
94        @type stationElement: C{cElementTree.Element}
95        '''
96        (featureTypeElement, featureIdElement, 
97         collectBeginDateElement, collectEndDateElement) = stationFeatureElement.getchildren()
98        if featureTypeElement != 'csml:PointSeriesFeature':
99            raise NotImplementedError('No other than "csml:PointSeriesFeature" feature type is supported')
100        self.featureID = featureIdElement.text
101        self.collectBeginDate = dates.dateutil.parser.parse(collectBeginDateElement.text)
102        self.collectEndDate = dates.dateutil.parser.parse(collectEndDateElement.text)
103
104class WFSStationCollection:
105    '''
106    Represents a <wfs:FeatureCollection> element that contains *specifically* <np:Station> elements!
107    @ivar stations: A list of C{NPStation}'s contained in the collection.
108    '''
109
110    def __init__(self):
111        self.stations = []
112
113    def parseString(self, str):
114        '''
115        Parse in the <wfs:FeatureCollection> element returned from GeoServer, and save individual stations.
116        @param str: A string containing the the <wfs:FeatureCollection> element.
117
118        '''
119        featureCollectionElement = XML(str)
120        self.parseXML(featureCollectionElement)
121
122    def parseXML(self, featureCollectionElement):
123        '''
124        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
125        Use if your WFS subset of XML is stored in an C{cElementTree.Element} object.
126        '''
127        featureMembersElement = featureCollectionElement.getchildren()[0]
128        for npStationElement in featureMembersElement.getchildren():
129            npStation = NPStation()
130            npStation.parseXML(npStationElement)
131            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.