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

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

Re-tested GridSeriesConvertor?

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