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

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

Changed naming to "GridSeriesConvertor?" from previously ambiguous one.

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        self.csmlFilename = csmlFilename
16        self.kmlFilename = kmlFilename
17        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
18        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
19        self.config = Configuration(configFilename)  # read in the config file (on per CSML file basis)
20
21    # Auxiliary function, indents XML
22    def _indentXML(self, elem, level=0):
23        i = "\n" + level * "  "
24        if len(elem):
25            if not elem.text or not elem.text.strip():
26                elem.text = i + "  "
27            for child in elem:
28                self._indentXML(child, level+1)
29            if not child.tail or not child.tail.strip():
30                child.tail = i
31            if not elem.tail or not elem.tail.strip():
32                elem.tail = i
33        else:
34            if level and (not elem.tail or not elem.tail.strip()):
35                elem.tail = i
36
37    def writeKML(self):
38       
39        # Start with an empty KML document
40        kmlDocumentElement=Element('Document')
41        SubElement(kmlDocumentElement, 'name').text=self.ds.name.CONTENT
42        SubElement(kmlDocumentElement, 'open').text='1'
43
44        #!!datasetName = os.path.split(self.csmlFilename)[1].split('.')[0]
45
46        # Create and populate a separate folder for each view
47        for viewConfig in self.config.getRoot().findall('View'):
48            kmlViewElement = Element('Folder')
49            SubElement(kmlViewElement, 'name').text=viewConfig.get('name')
50            SubElement(kmlViewElement, 'open').text='1'
51           
52            # Create a separate KML folder for each feature
53            features = self.ds.featureCollection.featureMembers
54            for feature in features:
55               
56                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
57                gsFeature=GridSeriesFeatureWrapper(feature)
58                description = feature.description.CONTENT   
59                name = feature.name.CONTENT
60                bBox = gsFeature.getBBox()
61                timeSteps = gsFeature.getTimeSteps()
62                timeBounds = gsFeature.getTimeBounds()
63
64                # Instantiate a GridSeriesKML object with this information
65                gsKML = kmlfeatures.GridSeriesKML(
66                    self.config, viewConfig, self.csmlFilename, 
67                    name, description, bBox, timeSteps, timeBounds
68                    )
69
70                # Generate a populated KML folder containing the feature, append it to the view
71                kmlFeatureElement = gsKML.exportFeature(viewConfig)
72                kmlViewElement.append(kmlFeatureElement)
73           
74            kmlDocumentElement.append(kmlViewElement)
75           
76        # Attach the Document element as a subelement of the root 'kml' element
77        kmlRootElement=Element('kml', xmlns='http://earth.google.com/kml/2.2')
78        kmlRootElement.append(kmlDocumentElement)
79        self._indentXML(kmlRootElement, 0)
80
81        # Write the KML document to a file
82        tree = ElementTree(kmlRootElement)       
83        f = open(self.kmlFilename, 'w')
84        tree.write(f)
85        f.close()
Note: See TracBrowser for help on using the repository browser.