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

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

Changed GridSeriesConvertor? and kmlfeatures to use KML.

Line 
1import os
2from cElementTree import ElementTree, Element, SubElement, tostring, dump
3
4import csml.parser
5from KML import *
6import kmlfeatures
7from csmlwrappers import GridSeriesFeatureWrapper
8
9
10class GridSeriesConvertor:
11    '''
12    Defines a class which conversion a CSML file containing solely csml:GridSeriesFeature elements
13    into a KML file. The data in the KML file will be loaded *dynamically* from a WMS server, which
14    is read in from the config file.
15    '''
16
17    def __init__(self, csmlFilename, configFilename, kmlFilename):
18
19        self.csmlFilename = csmlFilename
20        self.kmlFilename = kmlFilename
21        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
22        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
23
24        # Read in the config file, this as simple XML element. Can be different for each CSML dataset;
25        # it depends on how the convertor is run.
26        configTree = ElementTree() 
27        configTree.parse(configFilename)
28        self.config = configTree.getroot()
29
30    def convert(self):
31       
32        # Create a named KML document, with no styles.
33        kmlDocument = KMLDocument(self.ds.name.CONTENT, [])
34       
35        for viewConfig in self.config.findall('View'):
36
37            kmlViewFolder = KMLFolder(viewConfig.get('name'), [], opened = False, visible = False)
38
39            features = self.ds.featureCollection.featureMembers
40            for feature in features:
41
42                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
43                gsFeature = GridSeriesFeatureWrapper(feature)
44                description = feature.description.CONTENT
45                name = feature.name.CONTENT
46                bBox = gsFeature.getBBox()
47                timeSteps = gsFeature.getTimeSteps()
48                timeBounds = gsFeature.getTimeBounds()
49
50                # Instantiate a GridSeriesKML object with this information
51                gsKML = kmlfeatures.GridSeriesKML(
52                    self.config, viewConfig, self.csmlFilename, 
53                    name, description, bBox, timeSteps, timeBounds
54                    )
55
56                # Generate a populated KML folder containing the feature, append it to the view
57                kmlFeatureFolder = gsKML.toKML(viewConfig)
58                kmlViewFolder.children.append(kmlFeatureFolder)
59           
60            kmlDocument.elements.append(kmlViewFolder)
61
62        kmlDocument.save(self.kmlFilename)
Note: See TracBrowser for help on using the repository browser.