source: DPPP/kml/csml2kml/python/convertor.py @ 3204

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

Added config per CSML file

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''' Defines a class which performs conversion of a CSML file to a KML file.'''
10
11# [UNSAFE] No exception handling.
12class Convertor:
13    def __init__(self, csmlFilename, configFilename, kmlFilename):
14        self.csmlFilename = csmlFilename
15        self.kmlFilename = kmlFilename
16        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
17        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
18        self.config = Configuration(configFilename)  # read in the config file (on per CSML file basis)
19
20    # Auxiliary function, indents XML
21    def _indentXML(self, elem, level=0):
22        i = "\n" + level * "  "
23        if len(elem):
24            if not elem.text or not elem.text.strip():
25                elem.text = i + "  "
26            for child in elem:
27                self._indentXML(child, level+1)
28            if not child.tail or not child.tail.strip():
29                child.tail = i
30            if not elem.tail or not elem.tail.strip():
31                elem.tail = i
32        else:
33            if level and (not elem.tail or not elem.tail.strip()):
34                elem.tail = i
35
36    def writeKML(self):
37       
38        # Start with an empty KML document:
39        kmldocname='TEST GridSeries KML'
40        kml_document=Element('Document')
41        SubElement(kml_document, 'name').text=kmldocname
42        SubElement(kml_document, 'open').text='1'
43
44        datasetName = os.path.split(self.csmlFilename)[1].split('.')[0]
45
46        # Create a separate KML folder for each feature
47        for feature in self.ds.featureCollection.featureMembers:
48
49            # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
50            gsFeature=GridSeriesFeatureWrapper(feature) 
51
52            description = feature.description.CONTENT     
53            name = feature.name.CONTENT
54            bBox = gsFeature.getBBox()
55            timeSteps = gsFeature.getTimeSteps()
56            timeBounds = gsFeature.getTimeBounds()
57
58            # Instantiate a GridSeriesKML object with this information
59            gsKML = kmlfeatures.GridSeriesKML(self.config, self.csmlFilename, name, description, bBox, timeSteps, timeBounds)
60
61            # Call the outputKML method of this object to generate a populated KML Folder element
62            kml_folder = gsKML.outputKML(kmlfeatures.getDecadeSince1900)
63
64            # Append the folder to the kml document.
65            kml_document.append(kml_folder)
66
67        # Attach the Document element as a subelement of the root 'kml' element
68        kml_root=Element('kml', xmlns='http://earth.google.com/kml/2.2')
69        kml_root.append(kml_document)
70        self._indentXML(kml_root, 0)
71        tree = ElementTree(kml_root)
72       
73        f = open(self.kmlFilename, 'w')
74        tree.write(f)
75        f.close()
Note: See TracBrowser for help on using the repository browser.