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

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

Extended csmlGrapher to allow multiple datasets (that is, data served from multiple WFSs).

Line 
1'''
2Classes for work with I{<np:Station>}'s.
3'''
4
5import utils
6from ET import XML
7from matplotlib import dates     # a very good date/time module from matplotviz -- allows years < 1970
8
9npXmlNamespace = 'http://ndg.nerc.ac.uk/np'  # the namespace in which the Station element is defined
10
11class NPStation:
12    '''
13    Represents a ground meteorological station, identified by C{id}, with description C{desc},
14    located at (C{lon},C{lat}), and containing CSML features with identifiers C{csmlFeatureIds}.
15    @ivar id: Station identifier (e.g. "midas.station.1")
16    @type id: str
17    @ivar desc: Station description (e.g. "FAIR ISLE")
18    @type desc: str
19    @ivar lon: Station longitude
20    @type lon: float
21    @ivar lat: Station latitude
22    @type lat: float
23    @type features: C{NPStationFeature} list
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
96        (featureTypeElement, featureIdElement, 
97         collectBeginDateElement, collectEndDateElement) = stationFeatureElement.getchildren()
98        self.featureType = featureTypeElement.text
99        self.featureId = featureIdElement.text
100        self.collectBeginDate = utils.parseTimestamp(collectBeginDateElement.text)
101        self.collectEndDate = utils.parseTimestamp(collectEndDateElement.text)
102
103    def __repr__(self):
104        return str(vars(self))
105
106class WFSStationCollection:
107    '''
108    Represents a <wfs:FeatureCollection> element that contains *specifically* <np:Station> elements!
109    @ivar stations: A list of C{NPStation}'s contained in the collection.
110    '''
111
112    def __init__(self):
113        self.stations = []
114
115    def parseString(self, str):
116        '''
117        Parse in the <wfs:FeatureCollection> element returned from GeoServer, and save individual stations.
118        @param str: A string containing the the <wfs:FeatureCollection> element.
119
120        '''
121        featureCollectionElement = XML(str)
122        self.parseXML(featureCollectionElement)
123
124    def parseXML(self, featureCollectionElement):
125        '''
126        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
127        Use if your WFS subset of XML is stored in an C{cElementTree.Element} object.
128        '''
129        featureMembersElement = featureCollectionElement.getchildren()[0]
130        for npStationElement in featureMembersElement.getchildren():
131            npStation = NPStation()
132            npStation.parseXML(npStationElement)
133            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.