Changeset 3451


Ignore:
Timestamp:
18/02/08 16:52:06 (12 years ago)
Author:
mkochan
Message:

Changing GridSeriesConvertor? to use KML (unfinished).

Location:
DPPP/kml/csml2kml/python
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/GridSeriesConvertor.py

    r3439 r3451  
    66from csmlwrappers import GridSeriesFeatureWrapper 
    77 
    8 # [UNSAFE] No exception handling. 
    98class GridSeriesConvertor: 
    10  
    11     ''' Defines a class which performs conversion of a CSML file to a KML file.''' 
     9    '''  
     10    Defines a class which conversion a CSML file containing solely csml:GridSeriesFeature elements 
     11    into a KML file. The data in the KML file will be loaded *dynamically* from a WMS server, which 
     12    is read in from the config file. 
     13    ''' 
    1214 
    1315    def __init__(self, csmlFilename, configFilename, kmlFilename): 
     
    2426        self.config = configTree.getroot() 
    2527 
    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 
     28    def convert(self): 
     29         
     30        # Create a named KML document, with no styles. 
     31        kmlDocument = KMLDocument(self.ds.name.CONTENT, []) 
     32         
    5233        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 
     34            kmlViewFolder = KMLFolder() 
    5835            features = self.ds.featureCollection.featureMembers 
    5936            for feature in features: 
    60                  
     37 
    6138                # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object 
    62                 gsFeature=GridSeriesFeatureWrapper(feature) 
     39                gsFeature = GridSeriesFeatureWrapper(feature) 
    6340                description = feature.description.CONTENT    
    6441                name = feature.name.CONTENT 
     
    7451 
    7552                # Generate a populated KML folder containing the feature, append it to the view 
    76                 kmlFeatureElement = gsKML.exportFeature(viewConfig) 
    77                 kmlViewElement.append(kmlFeatureElement) 
     53                kmlFeatureFolder = gsKML.exportFeature(viewConfig) 
     54                kmlViewFolder.children.append(kmlFeatureFolder) 
    7855             
    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) 
     56            kmlDocument.elements.append(kmlViewFolder) 
    8557 
    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() 
     58        kmlDocument.save(self.kmlFilename) 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/KML.py

    r3439 r3451  
    228228        regionElement.append(lodElement) 
    229229        return regionElement 
     230 
     231class KMLGroundOverlay(KMLElement): 
     232    '''Represents the <kml:GroundOverlay> tag.''' 
     233 
     234    def __init__(self, name, sourceUrl, timespanStart, timespanEnd, west, south, east, north): 
     235        self.name = name 
     236        self.timespanStart = timespanStart 
     237        self.timespanEnd = timespanEnd 
     238        self.sourceUrl = sourceUrl 
     239        self.west = west 
     240        self.south = south 
     241        self.east = east 
     242        self.north = north 
     243 
     244    def build(self): 
     245 
     246        groundOverlayElement = Element('GroundOverlay') 
     247 
     248        SubElement(groundOverlayElement, 'name').text = self.name 
     249        SubElement(groundOverlayElement, 'open').text = '0' 
     250        SubElement(groundOverlayElement, 'visibility').text = '0' 
     251 
     252        timespanElement = SubElement(groundOverlayElement, 'TimeSpan') 
     253        SubElement(timespanElement, 'begin').text = ('%04d-%02d-%02d') % timespanStart.utctimetuple()[0:3] 
     254        SubElement(timespanElement, 'end').text = ('%04d-%02d-%02d') % timespanEnd.utctimetuple()[0:3] 
     255 
     256        # Include the WMS service call address 
     257        iconElement = SubElement(groundOverlayElement, 'icon') 
     258        SubElement(iconElement, 'href').text = sourceUrl 
     259        SubElement(iconElement, 'refreshMode').text = 'onExpire' 
     260 
     261        latlonboxElement=SubElement(groundOverlayElement, 'LatLonBox') 
     262        SubElement(latlonboxElement, 'north').text = str(self.north) 
     263        SubElement(latlonboxElement, 'south').text = str(self.south) 
     264        SubElement(latlonboxElement, 'east' ).text = str(self.east) 
     265        SubElement(latlonboxElement, 'west' ).text = str(self.west) 
     266 
     267        return groundOverlayElement 
     268         
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/kmlfeatures.py

    r3439 r3451  
    1 '''KML representatitions of various CSML features''' 
     1''' 
     2KML representatitions of those CSML features which can be represented directly in KML. 
     3Not all CSML features satisfy this requirement -- e.g. csml:GridSeriesFeature does, while 
     4csml:PointSeriesFeature does not (it has to be represented by a graph-plotting web service). 
     5''' 
    26 
    37import string 
     
    112116 
    113117        return wmsRequest 
     118 
     119    def toKML(self, viewConfig): 
     120        ''' 
     121        Returns the KML representation of this CSML feature. 
     122        It will be a KMLFolder object, containing KMLGroundOverlay objects -- each of them stands for  
     123        a grid for a single time step. 
     124        [MORE EXPLANATION] 
     125        [RESOLVE VISIBILITY] 
     126        ''' 
     127        if self.description: 
     128            kmlFeatureFolder = KMLFolder(self.description, []) 
     129        else: 
     130            kmlFeatureFolder = KMLFolder(self.name, []) 
     131         
     132        kmlFeatureFolder.opened = kmlFeatureFolder.visible = False 
     133         
     134        if self.splitTimeStepsBy == None: 
     135 
     136            for timeStep in self.timeSteps: 
     137                kmlGroundOverlay = self.buildKmlGroundOverlay(timeStep) 
     138                kmlFeatureFolder.children.append(kmlGroundOverlay) 
     139                 
     140        else: 
     141             
     142            def getCategory(timeStep): 
     143                (year, month, day) = self.getYMD(timeStep) 
     144                if self.splitTimeStepsBy == None: 
     145                    return timeStep 
     146                elif self.splitTimeStepsBy == 'year': 
     147                    return year 
     148                elif self.splitTimeStepsBy == 'month': 
     149                    return month 
     150 
     151            dict = {} 
     152 
     153            for timeStep in self.timeSteps: 
     154                category = getCategory(timeStep) 
     155                if not dict.has_key(category): 
     156                    dict[category] = [] 
     157                dict[category].append(timeStep) 
     158            dictKeys = dict.keys() 
     159            dictKeys.sort() 
     160 
     161            for category in dictKeys:             
     162 
     163                if self.categoryNamingPattern == None: 
     164                    categoryName = 'Category ' + str(category) 
     165                else: 
     166                    categoryName = self.categoryNamingPattern.replace('#VERBATIM#', str(category)) 
     167                    if (category >= 1 and category <= 12): 
     168                        categoryName = categoryName.replace('#MONTH#', getMonthCommonName(category)) 
     169                 
     170                kmlCategoryFolder = KMLFolder(categoryName, []) 
     171                kmlCategoryFolder.opened = kmlCategoryFolder.visible = False 
     172                for timeStep in dict[category]: 
     173                    kmlGroundOverlay = self.buildGroundOverlayElement(timeStep) 
     174                    kmlCategoryFolder.children.append(kmlGroundOverlay) 
     175                kmlFeatureFolder.children.append(kmlCategoryFolder) 
     176 
     177        return kmlFeatureFolder 
    114178 
    115179    def exportFeature(self, viewConfig): 
     
    232296    return names[monthInt-1] 
    233297 
     298# -------------------------------------------------------------------------------------------------------------- 
     299 
    234300class PointSeriesKML: 
    235301    ''' 
  • DPPP/kml/csml2kml/python/notes.txt

    r3370 r3451  
    66 
    77---------------------------------------------------------------------------------------------------------------------------- 
    8 || _ / 
     8|| GridSeriesConvertor ___/ 
     9 
     10[O] Make GridSeriesConvertor use the KML module 
     11    * Resolve visibility -- will require adding open/visible fields into the KML.KMLElement class. 
     12[O] Create a version of GridSeriesConvertor that takes in the Layers XML and a config file and spits out 
     13    either a single KML file or a directory structure with KML files as leaves. 
     14 
     15---------------------------------------------------------------------------------------------------------------------------- 
     16|| KML ___/ 
     17 
     18[O] Resolve the 'visibility' and 'open' tags of KML elements -- implement default behaviour, 
     19    use 'opened' and 'visible' fields in KMLElement 
     20    (hard-coded in the KMLElement class abd overrideable in the __init__() calls). 
     21 
     22---------------------------------------------------------------------------------------------------------------------------- 
     23|| * ___/ 
    924     
    10 ---------------------------------------------------------------------------------------------------------------------------- 
    11  
    1225[+] Assertion and type checking in Python 
    1326[o] Resolve slow functioning of wget() 
     
    1528    (otherwise need code to translate between the "namespace:name" notation and "{namespace_path}name" notation). 
    1629[o] Resolve security in pylons controllers 
    17 [.] Consider issues with visibility and open tags for elements -- how should they be set? 
    1830[x] Possibility of clicking on a globe and getting info for a certain feature (i.e. the value profile for that 
    1931    location) -- Dominic said there is a function in the CSML API which allows doing that -- investigate 
Note: See TracChangeset for help on using the changeset viewer.