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@3459
Revision 3459, 2.5 KB checked in by mkochan, 12 years ago (diff)

Changed GridSeriesConvertor? and kmlfeatures to use KML.

RevLine 
[3191]1import os
[3204]2from cElementTree import ElementTree, Element, SubElement, tostring, dump
[3191]3
4import csml.parser
[3459]5from KML import *
[3191]6import kmlfeatures
7from csmlwrappers import GridSeriesFeatureWrapper
8
[3459]9
[3307]10class GridSeriesConvertor:
[3451]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    '''
[3191]16
[3204]17    def __init__(self, csmlFilename, configFilename, kmlFilename):
[3308]18
[3191]19        self.csmlFilename = csmlFilename
20        self.kmlFilename = kmlFilename
[3204]21        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object
22        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object
[3191]23
[3308]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
[3451]30    def convert(self):
31       
32        # Create a named KML document, with no styles.
33        kmlDocument = KMLDocument(self.ds.name.CONTENT, [])
34       
[3308]35        for viewConfig in self.config.findall('View'):
[3459]36
37            kmlViewFolder = KMLFolder(viewConfig.get('name'), [], opened = False, visible = False)
38
[3219]39            features = self.ds.featureCollection.featureMembers
[3214]40            for feature in features:
[3451]41
[3214]42                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object
[3451]43                gsFeature = GridSeriesFeatureWrapper(feature)
[3459]44                description = feature.description.CONTENT
[3214]45                name = feature.name.CONTENT
46                bBox = gsFeature.getBBox()
47                timeSteps = gsFeature.getTimeSteps()
48                timeBounds = gsFeature.getTimeBounds()
[3191]49
[3214]50                # Instantiate a GridSeriesKML object with this information
51                gsKML = kmlfeatures.GridSeriesKML(
[3228]52                    self.config, viewConfig, self.csmlFilename, 
53                    name, description, bBox, timeSteps, timeBounds
[3214]54                    )
[3191]55
[3219]56                # Generate a populated KML folder containing the feature, append it to the view
[3459]57                kmlFeatureFolder = gsKML.toKML(viewConfig)
[3451]58                kmlViewFolder.children.append(kmlFeatureFolder)
[3214]59           
[3451]60            kmlDocument.elements.append(kmlViewFolder)
[3191]61
[3451]62        kmlDocument.save(self.kmlFilename)
Note: See TracBrowser for help on using the repository browser.