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

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

Removed dependency on configuration.py from the GridSeriesConvertor? module, in order to make it more like the PointSeriesConvertor? module.

Line 
1import os
2from cElementTree import ElementTree, Element, SubElement, tostring, dump
3
4import csml.parser
5import kmlfeatures
6from csmlwrappers import GridSeriesFeatureWrapper
7from configuration import Configuration
8
9# [UNSAFE] No exception handling.
10class GridSeriesConvertor:
11
12    ''' Defines a class which performs conversion of a CSML file to a KML file.'''
13
14    def __init__(self, csmlFilename, configFilename, kmlFilename):
15
16        self.csmlFilename = csmlFilename
17        self.kmlFilename = kmlFilename
18        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
19        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
20
21        # Read in the config file, this as simple XML element. Can be different for each CSML dataset;
22        # it depends on how the convertor is run.
23        configTree = ElementTree() 
24        configTree.parse(configFilename)
25        self.config = configTree.getroot()
26
27    # Auxiliary function, indents XML
28    def _indentXML(self, elem, level=0):
29        i = "\n" + level * "  "
30        if len(elem):
31            if not elem.text or not elem.text.strip():
32                elem.text = i + "  "
33            for child in elem:
34                self._indentXML(child, level+1)
35            if not child.tail or not child.tail.strip():
36                child.tail = i
37            if not elem.tail or not elem.tail.strip():
38                elem.tail = i
39        else:
40            if level and (not elem.tail or not elem.tail.strip()):
41                elem.tail = i
42
43    def writeKML(self):
44       
45        # Start with an empty KML document
46        kmlDocumentElement=Element('Document')
47        SubElement(kmlDocumentElement, 'name').text=self.ds.name.CONTENT
48        SubElement(kmlDocumentElement, 'open').text='1'
49
50        #!!datasetName = os.path.split(self.csmlFilename)[1].split('.')[0]
51
52        # Create and populate a separate folder for each view
53        for viewConfig in self.config.findall('View'):
54            kmlViewElement = Element('Folder')
55            SubElement(kmlViewElement, 'name').text=viewConfig.get('name')
56            SubElement(kmlViewElement, 'open').text='1'
57           
58            # Create a separate KML folder for each feature
59            features = self.ds.featureCollection.featureMembers
60            for feature in features:
61               
62                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
63                gsFeature=GridSeriesFeatureWrapper(feature)
64                description = feature.description.CONTENT   
65                name = feature.name.CONTENT
66                bBox = gsFeature.getBBox()
67                timeSteps = gsFeature.getTimeSteps()
68                timeBounds = gsFeature.getTimeBounds()
69
70                # Instantiate a GridSeriesKML object with this information
71                gsKML = kmlfeatures.GridSeriesKML(
72                    self.config, viewConfig, self.csmlFilename, 
73                    name, description, bBox, timeSteps, timeBounds
74                    )
75
76                # Generate a populated KML folder containing the feature, append it to the view
77                kmlFeatureElement = gsKML.exportFeature(viewConfig)
78                kmlViewElement.append(kmlFeatureElement)
79           
80            kmlDocumentElement.append(kmlViewElement)
81           
82        # Attach the Document element as a subelement of the root 'kml' element
83        kmlRootElement=Element('kml', xmlns='http://earth.google.com/kml/2.2')
84        kmlRootElement.append(kmlDocumentElement)
85        self._indentXML(kmlRootElement, 0)
86
87        # Write the KML document to a file
88        tree = ElementTree(kmlRootElement)       
89        f = open(self.kmlFilename, 'w')
90        tree.write(f)
91        f.close()
Note: See TracBrowser for help on using the repository browser.