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

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

Changed WMS request-building code; outsourced most request parameters to a config file, config.xml. (This will later be bound to each CSML file.)

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