Ignore:
Timestamp:
22/02/08 17:15:57 (12 years ago)
Author:
mkochan
Message:

Progressed on implementing WMS Layers -> KML conversion (before testing).

File:
1 edited

Legend:

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

    r3499 r3505  
    3535        self.children = children 
    3636 
     37    def __repr__(self): 
     38        return str(vars(self)) 
     39 
    3740    def parseXML(self, layerElement): 
    3841        raise NotImplementedError('Use the wmsLayerFactory() function instead.') 
    3942 
    40     def __repr__(self): 
    41         return str(vars(self)) 
     43    def toKML(self, wmsRequestConfigElement, views): 
     44        # ignore wmsRequestConfigElement, views 
     45        kmlLayerFolder = KMLFolder(wmsLayer.name, [], opened = False, visible = False) 
     46        for childWmsLayer in wmsLayer.children: 
     47            kmlFolder.children.append( childWmsLayer.toKML() ) 
     48        return kmlLayerFolder 
    4249 
    4350class BottomWMSLayer(WMSLayer): 
     
    4552    '''[DOC]''' 
    4653     
    47     def __init__(self, name, title, abstract, times): 
     54    def __init__(self, name, title, abstract, timesteps): 
    4855 
    4956        self.name = name 
     
    5158        self.abstract = abstract 
    5259        # but no self.children 
    53         self.times = times 
     60        self.timesteps = timesteps 
    5461 
    5562    def _parseName(self): 
     
    7582        return self._parseName()[2] 
    7683 
    77     def toKML(self, views): 
     84    def toKML(self, wmsRequestConfigElement, views): 
    7885        ''' 
    7986        @param views: A list of View objects, which define how we are going to look at the data. 
    80         @return: a KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> elements, 
    81         each standing for a different time segment. 
     87        @return: a KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> 
     88        elements, each standing for a different time segment. 
    8289        [NOTE] Should replace timeStep with a timedate object? 
    8390        [NOTE] Please! Use dates.datetime all around here -- the self.times are of such type! 
    84         [NOTE] Let us have this function one the class View is defined:  
    85                  (startDatetime, endDatetime) = view.getLogicalTimespan(datetime) 
    86         ''' 
    87  
    88         def buildWMSRequest(timeStep): 
     91        ''' 
     92 
     93        def buildWMSRequest(timestep): 
    8994            ''' Build a WMS request ''' 
    9095 
    9196            # We will be using configuration for WMS request 
    92             c = self.config.find('CSMLGridSeriesFeatureWMSRequest') 
     97            c = wmsRequestConfigElement 
    9398 
    9499            # Set request configuration parameters 
     
    100105            crs = c.find('CRS').text 
    101106 
    102             # If timeStep contains a time part, make sure that the "Z" for "Zulu" is appended, as required by WMS 
    103             if re.search("T\d+\:\d+:\d+(\.\d+)$", timeStep): 
    104                 timeStep = timeStep + 'Z' 
    105  
    106107            bBox = '-180,-90,180,90' 
    107108             
     
    109110 
    110111            return wmsRequest 
     112 
     113        def buildKmlGroundOverlay(view, timestep): 
     114            (timespanStart, timespanEnd) = view.getLogicalTimeSpan(timestep) 
     115            return KMLGroundOverlay( 
     116                self.name + ' ' + timeStep, 
     117                buildWMSRequest(timestep), 
     118                timespanStart, timespanEnd, 
     119                -180, -90, 180, 90, 
     120                visible = False 
     121                ) 
     122 
     123        def buildLayerViewFolder(view): 
     124 
     125            kmlLayerViewFolder = KMLFolder(view.name, [], visible = False, opened = False)             
     126 
     127            # Create a categorisation dictionary, dict, which will contain categories (as returned by 
     128            # view.getCategory()) as keys, and timesteps belonging into those categories as values. 
     129            dict = {} 
     130            for timestep in self.timesteps: 
     131                category = view.getCategory(timestep) 
     132                if not dict.has_key(category): 
     133                    dict[category] = [] 
     134                dict[category].append(timestep) 
     135            categories = dict.keys() 
     136            categories.sort() 
     137 
     138            for category in categories: 
     139                categoryName = view.getCategoryName(category) 
     140                kmlCategoryFolder = KMLFolder(categoryName, [], visible = False, opened = False) 
     141                for timestep in dict[category]: 
     142                    kmlGroundOverlay = buildKmlGroundOverlay(view, timestep) 
     143                    kmlCategoryFolder.children.append(kmlGroundOverlay) 
     144                kmlLayerViewFolder.children.append(kmlCategoryFolder) 
     145 
     146            return kmlLayerViewFolder 
    111147         
    112148        # ------------ 
    113149 
     150        if self.description: 
     151            layerName = self.description 
     152        else: 
     153            layerName = self.name 
     154        kmlLayerFolder = KMLFolder(layerName, [], visible = False, opened = False) 
    114155        for view in views: 
    115             ... 
    116             kmlFolder = # representation of time points using this view 
    117             
     156            kmlLayerFolder.append( buildLayerViewFolder(view) ) 
     157        return kmlLayerFolder 
     158     
    118159class View: 
    119160    ''' 
     
    121162    in Google Earth. In particular, it defines logical transforms of time-points into time-spans. 
    122163    ''' 
    123     pass 
     164    def getLogicalTimespan(timestep): 
     165        ''' 
     166        Abstract method, defined in derived classes. 
     167        Translates a single time step into a time span. 
     168        @param timestep: The date step (a datetime object) 
     169        @return: The (timespanStart, timespanEnd) tuple (both are datetime objects) 
     170        ''' 
     171        pass 
     172 
     173    def getCategory(timestep): 
     174        pass 
     175 
     176    def getCategoryName(category): 
     177        ''' 
     178        Abstract method, defined in derived classes.  
     179        Get a human-readable name for this viewing category. 
     180        For instance, a value 2 could be replaced with a 'February' (in case of split-by-month view),  
     181        or a value 1990 could be replaced with a 'Decade of 1990' (in case of split-by-decade view). 
     182        ''' 
     183        pass 
     184 
     185    def _getSameDate(timestep): 
     186        return timestep 
     187 
     188    def _getFirstDayOfMonth(timestep): 
     189        return timestep.replace(day=1) 
     190 
     191    def _get20thCenturyDecade(timestep): 
     192        replYear = (timestep.year - 1900) / 10 + 1       # get decade as a "logical" year 
     193        return timestep.replace(year=replYear, day=1)    # start a month on the 1st of the month 
     194 
     195    def _getMonthHence(timestep): 
     196        if timestep.month+1 <= 12: 
     197            return timestep.replace(month=timestep.month+1) 
     198        else: 
     199            return timestep.replace(year=timestep.year+1, month=1) 
     200     
     201    def _getYearHence(timestep): 
     202        return timestep.replace(year=timestep.year+1) 
     203 
     204    def _getDecadeHence(timestep): 
     205        return timestep.replace(year=timestep.year+10) 
     206 
     207class ViewSplittedByMonth(View): 
     208    def getLogicalTimespan(self, timestep): 
     209        timespanStart = _getFirstDayOfMonth(timestep) 
     210        timespanEnd = _getDecadeHence(timespanStart) 
     211        return (timespanStart, timespanEnd) 
     212 
     213    def getCategory(self, timestep): 
     214        return timestep.month 
     215 
     216    def getCategoryName(self, timestep): 
     217        monthNames = ['January', 'February', 'March', 'April', 'May', 'June',  
     218                      'July', 'August', 'September', 'October', 'November', 'December'] 
     219        return monthNames[self.getCategory(timestep)-1] 
     220 
     221class ViewSplittedByDecade(View): 
     222    def getLogicalTimespan(self, timestep): 
     223        timespanStart = _getFirstDayOfMonth(timestep) 
     224        timespanEnd = _getMonthHence(timespanStart) 
     225        return (timespanStart, timespanEnd) 
     226 
     227    def getCategory(self, timestep): 
     228        return timestep.year 
     229 
     230    def getCategoryName(self, category): 
     231        return 'Decade of ' + str(self.getCategory(timestep)) 
     232 
     233class ViewAllDecades(View): 
     234    def getLogicalTimespan(timestep): 
     235        timespanStart = _getDecadeSince(timestep, 1900) 
     236        timespanEnd = _getMonthHence(timespanStart) 
     237        return (timespanStart, timespanEnd) 
     238 
     239    def getCategory(timestep): 
     240        return dt.isoformat() 
     241 
     242    def getCategoryName(timestep): 
     243        return dt.isoformat() 
    124244 
    125245class WMSCapabilities: 
     
    142262class WMSLayersConvertor: 
    143263     
    144     def __init__(self, topWmsLayer, config, baseKmlOutputDirectory): 
     264    # [RESOLVE] maxDirDepth 
     265    def __init__(self, topWmsLayer, wmsRequestConfigElement, baseKmlOutputDirectory): 
    145266        self.topWmsLayer = topWmsLayer 
    146         self.config = config 
     267        self.wmsRequestConfigElement = wmsRequestConfigElement 
    147268        self.baseKmlOutputDirectory = baseKmlOutputDirectory 
    148269        self.maxDirDepth = 10 
     
    152273 
    153274        def _convertToKML(wmsLayer): 
    154             return kmlElement 
     275            views = [ViewAllDecades, ViewSplittedByMonth, ViewSplittedByDecade] 
     276            return wmsLayer.toKML(wmsRequestConfigElement, views) 
    155277         
    156278        def _convertToDirectory(wmsLayer, parentDir, currentLevel): 
    157              
     279            '''recursive''' 
    158280            if currentLevel < self.maxDirDepth: 
    159281                currentDir = parentDir + '/' + wmsLayer.name 
     
    161283                print 'Created directory "%s"' % currentDir 
    162284                if not isinstance(wmsLayer, BottomWMSLayer): 
    163                     for child in wmsLayer.children: 
    164                         _convertToDirectory(child, currentDir, currentLevel+1) 
     285                    for childWmsLayer in wmsLayer.children: 
     286                        _convertToDirectory(childWmsLayer, currentDir, currentLevel+1) 
    165287            elif currentLevel == self.maxDirDepth: 
    166288                # Create a KML document with no styles 
Note: See TracChangeset for help on using the changeset viewer.