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

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

Changing the grapher (unfinished).

Line 
1'''
2Contains class NPStation, which represents the <np:Station> element
3(the schema for namespace "np" is located at: http://130.246.76.98:8084/geoserver/schemas/wfs/1.1.0/wfs.xsd).
4Also, for convenience, contains class WFSStationCollection, which represents a <wfs:FeatureCollection>
5that contain *specifically* <np:Station> elements!
6'''
7
8from cElementTree import XML
9
10npXmlNamespace = 'http://ndg.nerc.ac.uk/np'
11
12class NPStation:
13    '''Represents a ground meteorological station, identified by id, with description (desc), located at (lon,lat)'''
14
15    def __init__(self, id = None, desc = None, lon = None, lat = None, csmlFeatureIds = None):
16        '''
17        The initialiser. It is possible (and typical) not to initialise using it but rather leave
18        values undefined and parse them in from GML.
19        '''
20        self.id = id
21        self.desc = desc
22        self.lon = lon; self.lat = lat
23        self.csmlFeatureIds = csmlFeatureIds
24
25    def parseString(self, str):
26        stationElement = XML(str)
27        self.parseXML(stationElement)
28
29    def parseXML(self, stationElement):
30        (stationNameElement, stationIDElement, locationElement, featuresElement) = stationElement.getchildren()
31        pointElement = locationElement.getchildren()[0]
32        posElement = pointElement.getchildren()[0]
33        (self.lon,self.lat) = map(float, posElement.text.split())
34        self.id = stationElement.get('{http://www.opengis.net/gml}id')
35        self.desc = stationNameElement.text
36        self.csmlFeatureIds = []
37        for stationsFeatureElement in featuresElement.getchildren():
38            (featureTypeElement, featureParameterElement) = stationsFeatureElement.getchildren()
39            if featureTypeElement.text != 'csml:PointSeriesFeature':
40                raise TypeError('Feature type not "csml:PointSeriesFeature"')
41            self.csmlFeatureIds.append(featureParameterElement.text)
42
43    def __repr__(self):
44        return '<np:Station> %s ("%s") located at (%f, %f) with %d features (%s).' % (
45            self.id, self.desc, self.lon, self.lat, len(self.csmlFeatureIds), str(self.csmlFeatureIds)
46            )
47
48class WFSStationCollection:
49
50    def __init__(self):
51        self.stations = []
52
53    def parseString(self, str):
54        '''
55        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
56        Use if your WFS subset of GML is stored in a string.
57        '''
58        featureCollectionElement = XML(str)
59        self.parseXML(featureCollectionElement)
60
61    def parseXML(self, featureCollectionElement):
62        '''
63        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
64        Use if your WFS subset of XML is stored in an ElementTree.Element object.
65        '''
66        featureMembersElement = featureCollectionElement.getchildren()[0]
67        for npStationElement in featureMembersElement.getchildren():
68            npStation = NPStation()
69            npStation.parseXML(npStationElement)
70            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.