source: DPPP/kml/csml2kml/python/csml2kml/PointSeriesConvertor.py @ 3306

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

Finished the testPointSeriesConvertor.py test script. Have running version of configurable PointSeriesConvertor? module.

Line 
1from cElementTree import ElementTree, Element, SubElement, XML
2from pylab import *
3from datetime import datetime, timedelta
4import re
5import csml
6import urllib
7
8from KMLDocument import *
9from utils import wget
10
11class PointSeriesConvertor:
12   
13    def __init__(self, config, kmlFilename):
14        self.config = config
15        self.kmlFilename = kmlFilename
16
17    def convert(self):
18        '''
19        Convert GML input from GeoServer (containing the <Station> tags with station information)
20        to the KML representation.
21        [NOTE] At the moment, since the <Station> GML-feature element does not contain a list of CSML features,
22               the output of this code is not entirely correct. In particular, the HTTP reference to the grapher web-service
23               cannot be correctly determined.
24        '''
25
26        def _parseGMLStationsCollection(textGml):
27            '''
28            Parse the GML returned from GeoServer and yield tuples containing information (gml_id, name, lon, lat)
29            about each station.
30            '''
31            root = XML(textGml)
32            featureCollectionElement = root
33            for featureMember in featureCollectionElement.getchildren():
34                stationElement = featureMember.getchildren()[0]
35                (stationNameElement, stationIDElement, locationElement) = stationElement.getchildren()
36                pointElement = locationElement.getchildren()[0]
37                posElement = pointElement.getchildren()[0]
38                (lon,lat) = map(float, posElement.text.split())
39                yield (stationElement.get('{http://www.opengis.net/gml}id'), stationNameElement.text, lon, lat)
40
41        # Read conversion configuration
42        documentName = self.config.find('name').text
43        url = self.config.find('GeoServerRequest/URL').text
44        balloonTemplate = self.config.find('GeoServerRequest/BalloonTemplate').text
45        stationData = {}
46        data = self.config.findall('GeoServerRequest/StationData/Datum')
47        for datum in data:
48            datumName = datum.get('name')
49            datumValue = datum.text
50            stationData[datumName] = datumValue
51
52        # Perform the conversion
53        textGml = wget(url)
54        placemarkKmlStyle = createDefaultPlacemarKMLStyle(balloonTemplate = balloonTemplate)
55        kmlDocument = KMLDocument(documentName, [placemarkKmlStyle])
56        generator = _parseGMLStationsCollection(textGml)
57        for (gml_id, name, lon, lat) in generator:
58            kmlStation = KMLPlacemark(gml_id, name, lon, lat, styleID = placemarkKmlStyle.id, data = stationData)
59            kmlDocument.elements.append(kmlStation)
60
61        # Save the KML document
62        kmlDocument.save(self.kmlFilename)
Note: See TracBrowser for help on using the repository browser.