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

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

Created module Station.py (but untested)

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