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

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

Prepared file hierarchy for creating an egg "csml2kml"

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, csmlFeatureIds = 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        self.csmlFeatureIds = csmlFeatureIds
22
23    def parseString(self, str):
24        stationElement = XML(str)
25        self.parseXML(stationElement)
26
27    def parseXML(self, stationElement):
28        (stationNameElement, stationIDElement, locationElement, featuresElement) = stationElement.getchildren()
29        pointElement = locationElement.getchildren()[0]
30        posElement = pointElement.getchildren()[0]
31        (self.lon,self.lat) = map(float, posElement.text.split())
32        self.id = stationElement.get('{http://www.opengis.net/gml}id')
33        self.desc = stationNameElement.text
34        self.csmlFeatureIds = []
35        for stationsFeatureElement in featuresElement.getchildren():
36            (featureIdElement, featureTypeElement) = stationsFeatureElement.getchildren()
37            self.csmlFeatureIds.append(featureIdElement.text)
38
39    def __repr__(self):
40        return '<np:Station> %s ("%s") located at (%f, %f) with %d features.' % (
41            self.id, self.desc, self.lon, self.lat, len(self.csmlFeatureIds)
42            )
43
44class WFSStationCollection:
45
46    def __init__(self):
47        self.stations = []
48
49    def parseString(self, str):
50        '''
51        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
52        Use if your WFS subset of GML is stored in a string.
53        '''
54        featureCollectionElement = XML(str)
55        self.parseXML(featureCollectionElement)
56
57    def parseXML(self, featureCollectionElement):
58        '''
59        Parse in the <wfs:FeatureCollection> element returned from GeoServer, save individual stations.
60        Use if your WFS subset of XML is stored in an ElementTree.Element object.
61        '''
62        featureMembersElement = featureCollectionElement.getchildren()[0]
63        for npStationElement in featureMembersElement.getchildren():
64            npStation = NPStation()
65            npStation.parseXML(npStationElement)
66            self.stations.append(npStation)
Note: See TracBrowser for help on using the repository browser.