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

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

Made StationConvertor? class use the Station.py module.

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
10class NPStation:
11    '''Represents a ground meteorological station, identified by id, with description (desc), located at (lon,lat)'''
12
13    def __init__(self, id = None, desc = None, lon = None, lat = None):
14        '''
15        The initialiser. It is possible (and typical) not to initialise using it but rather leave
16        values undefined and parse them in from GML.
17        '''
18        self.id = id
19        self.desc = desc
20        self.lon = lon; self.lat = lat
21
22    def parseString(self, str):
23        stationElement = XML(str)
24        self.parseXML(stationElement)
25
26    def parseXML(self, stationElement):
27        (stationNameElement, stationIDElement, locationElement) = stationElement.getchildren()
28        pointElement = locationElement.getchildren()[0]
29        posElement = pointElement.getchildren()[0]
30        (self.lon,self.lat) = map(float, posElement.text.split())
31        self.id = stationElement.get('{http://www.opengis.net/gml}id')
32        self.desc = stationNameElement.text
33
34    def __repr__(self):
35        return '<np:Station> %s ("%s")' % (self.id, self.desc)
36
37class WFSStationCollection:
38
39    def __init__(self):
40        self.stations = []
41
42    def parseString(self, str):
43        '''
44        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
45        Use if your WFS subset of GML is stored in a string.
46        '''
47        featureCollectionElement = XML(str)
48        self.parseXML(featureCollectionElement)
49
50    def parseXML(self, featureCollectionElement):
51        '''
52        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
53        Use if your WFS subset of XML is stored in an ElementTree.Element object.
54        '''
55        for featureMember in featureCollectionElement.getchildren():
56            stationElement = featureMember.getchildren()[0]
57            station = NPStation()
58            station.parseXML(stationElement)
59            self.stations.append(station)
Note: See TracBrowser for help on using the repository browser.