source: DPPP/kml/csml2kml/python/csml2kml/csml2kml/GridSeriesConvertor.py @ 3451

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

Changing GridSeriesConvertor? to use KML (unfinished).

Line 
1import os
2from cElementTree import ElementTree, Element, SubElement, tostring, dump
3
4import csml.parser
5import kmlfeatures
6from csmlwrappers import GridSeriesFeatureWrapper
7
8class GridSeriesConvertor:
9    '''
10    Defines a class which conversion a CSML file containing solely csml:GridSeriesFeature elements
11    into a KML file. The data in the KML file will be loaded *dynamically* from a WMS server, which
12    is read in from the config file.
13    '''
14
15    def __init__(self, csmlFilename, configFilename, kmlFilename):
16
17        self.csmlFilename = csmlFilename
18        self.kmlFilename = kmlFilename
19        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
20        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
21
22        # Read in the config file, this as simple XML element. Can be different for each CSML dataset;
23        # it depends on how the convertor is run.
24        configTree = ElementTree() 
25        configTree.parse(configFilename)
26        self.config = configTree.getroot()
27
28    def convert(self):
29       
30        # Create a named KML document, with no styles.
31        kmlDocument = KMLDocument(self.ds.name.CONTENT, [])
32       
33        for viewConfig in self.config.findall('View'):
34            kmlViewFolder = KMLFolder()
35            features = self.ds.featureCollection.featureMembers
36            for feature in features:
37
38                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
39                gsFeature = GridSeriesFeatureWrapper(feature)
40                description = feature.description.CONTENT   
41                name = feature.name.CONTENT
42                bBox = gsFeature.getBBox()
43                timeSteps = gsFeature.getTimeSteps()
44                timeBounds = gsFeature.getTimeBounds()
45
46                # Instantiate a GridSeriesKML object with this information
47                gsKML = kmlfeatures.GridSeriesKML(
48                    self.config, viewConfig, self.csmlFilename, 
49                    name, description, bBox, timeSteps, timeBounds
50                    )
51
52                # Generate a populated KML folder containing the feature, append it to the view
53                kmlFeatureFolder = gsKML.exportFeature(viewConfig)
54                kmlViewFolder.children.append(kmlFeatureFolder)
55           
56            kmlDocument.elements.append(kmlViewFolder)
57
58        kmlDocument.save(self.kmlFilename)
Note: See TracBrowser for help on using the repository browser.