Changeset 3520 for DPPP


Ignore:
Timestamp:
26/02/08 16:47:04 (11 years ago)
Author:
mkochan
Message:

Passed testing using testWMSCapabilities.py for the IPCC "obs" dataset.

File:
1 edited

Legend:

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

    r3516 r3520  
    4949    def toKML(self, wmsRequestConfigElement, viewTypes): 
    5050        # ignore wmsRequestConfigElement, viewTypess 
    51         kmlLayerFolder = KMLFolder(self.name, [], opened = False, visible = False) 
     51        kmlLayerFolder = KMLFolder(self.title, [], opened = False, visible = False) 
    5252        for childWmsLayer in self.children: 
    5353            kmlLayerFolder.children.append( childWmsLayer.toKML(wmsRequestConfigElement, viewTypes) ) 
     
    100100    def toKML(self, wmsRequestConfigElement, viewTypes): 
    101101        ''' 
    102         @param viewTyps: A list of View classes (but not instances), which define how we are going  
    103         to look at the data. 
    104         @return: a KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> 
    105         elements, each standing for a different time segment. 
    106         ''' 
    107  
    108         def buildWMSRequest(layerName, timestep): 
    109             ''' Build a WMS request ''' 
    110  
    111             # We will be using configuration for WMS request 
    112             c = wmsRequestConfigElement 
    113  
    114             # Set request configuration parameters 
    115             url = c.find('URL').text 
    116             serviceVersion = c.find('ServiceVersion').text 
    117             imageFormat = c.find('ImageFormat').text 
    118             imageWidth = c.find('ImageWidth').text 
    119             imageHeight = c.find('ImageHeight').text 
    120             crs = c.find('CRS').text 
    121  
    122             bBox = '-180,-90,180,90' 
    123  
    124             # If the timezone is UTC (which in ISO form would look like 'yyyy-mm-ddThh:mm:ss+00:00'), 
    125             # then replace it with 'Z'. 
    126             timestepString = timestep.isoformat() 
    127             timestepString = timestepString.replace('+00:00', 'Z') 
    128  
    129             wmsRequest = '%s?request=GetMap&SERVICE=%s&FORMAT=%s&LAYERS=%s&BBOX=%s&WIDTH=%s&HEIGHT=%s&CRS=%s&TIME=%s' % (url, serviceVersion, imageFormat, layerName, bBox, imageWidth, imageHeight, crs, timestepString) 
    130  
    131             return wmsRequest 
    132  
    133         def buildKmlGroundOverlay(view, timestep): 
    134             (timespanStart, timespanEnd) = view.getLogicalTimespan(timestep) 
    135             return KMLGroundOverlay( 
    136                 self.name + ' ' + timestep.isoformat(), 
    137                 buildWMSRequest(self.name, timestep), 
    138                 timespanStart, timespanEnd, 
    139                 -180, -90, 180, 90, 
    140                 visible = False 
    141                 ) 
    142  
    143         def buildLayerViewFolder(viewType): 
    144  
    145             # Create a view that is informed in advance of the timesteps involved in the following categorisation 
    146             # (most views actually no not need to see the timesteps in advance). 
    147             view = viewFactory(viewType, self) 
    148  
    149             # Create a KML folder that represents the view of the layer 
    150             kmlLayerViewFolder = KMLFolder(view.name, [], visible = False, opened = False) 
    151  
    152             # Create a categorisation dictionary, dict, which will contain categories (as returned by 
    153             # view.getCategory()) as keys, and timesteps belonging into those categories as values. 
    154             dict = {} 
    155             for timestep in self.timesteps: 
    156                 category = view.getCategory(timestep) 
    157                 if not dict.has_key(category): 
    158                     dict[category] = [] 
    159                 dict[category].append(timestep) 
    160             categories = dict.keys() 
    161             categories.sort() 
    162  
    163             for category in categories: 
    164                 categoryDescription = view.getCategoryDescription(category) 
    165                 categoryTimesteps = dict[category] 
    166                 kmlCategoryFolder = KMLFolder(categoryDescription, [], visible = False, opened = False) 
    167                 for timestep in categoryTimesteps: 
    168                     kmlGroundOverlay = buildKmlGroundOverlay(view, timestep) 
    169                     kmlCategoryFolder.children.append(kmlGroundOverlay) 
    170                 kmlLayerViewFolder.children.append(kmlCategoryFolder) 
    171  
    172             return kmlLayerViewFolder 
    173  
    174         # ------------ 
    175  
    176         kmlLayerFolder = KMLFolder(self.name, [], visible = False, opened = False) 
     102        @param viewTyps: A list of View classes (but not instances), which define what kinds of view we are going  
     103                         to use to look at the data. 
     104        @return:         A KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> 
     105                         elements, each standing for a different time segment. 
     106        ''' 
     107        kmlLayerFolder = KMLFolder(self.title, [], visible = False, opened = False) 
    177108        for viewType in viewTypes: 
    178             kmlLayerFolder.children.append( buildLayerViewFolder(viewType) ) 
     109            view = viewType(self, wmsRequestConfigElement) 
     110            kmlLayerFolder.children.append( view.toKML() ) 
    179111        return kmlLayerFolder 
    180112 
     
    185117    ''' 
    186118 
    187     def __init__(self, layer): 
     119    def __init__(self, layer, wmsRequestConfigElement): 
    188120        ''' 
    189121        Initialize the view. 
     
    191123        ''' 
    192124        self.layer = layer 
     125        self.wmsRequestConfigElement = wmsRequestConfigElement 
     126 
     127    def areCategoriesListedExplicitly(self): 
     128        ''' 
     129        @returns: A boolean value that signifies whether the self.toKML() method should list the categories 
     130                  explicitly (in separate KMLFolder's). Must be implemented by all derived classes. 
     131        ''' 
     132        raise NotImplementedError() 
    193133 
    194134    def getLogicalTimespan(self, timestep): 
     
    213153    def _getFirstDayOfMonth(self, timestep): 
    214154        return timestep.replace(day=1) 
    215  
    216     ##def _get20thCenturyDecade(self, timestep): 
    217     ##    replYear = (timestep.year - 1900) / 10 + 1       # get decade as a "logical" year 
    218     ##    return timestep.replace(year=replYear, day=1)    # start a month on the 1st of the month 
    219155 
    220156    def _getMonthHence(self, timestep): 
     
    227163        return timestep.replace(year=timestep.year+1) 
    228164 
    229     ##def _getDecadeHence(self, timestep): 
    230     ##    return timestep.replace(year=timestep.year+10) 
    231  
    232165    def _getHalfPeriodEarlier(self, timestep): 
    233166        return timestep.replace(year = timestep.year-self.layer.getPeriod()/2) 
     
    236169        return timestep.replace(year = timestep.year+self.layer.getPeriod()/2) 
    237170 
    238 def viewFactory(viewType, layer): 
    239     newView = viewType(layer) 
    240     return newView 
     171    def toKML(self): 
     172 
     173        def buildWMSRequest(timestep): 
     174            ''' Build a WMS request ''' 
     175 
     176            # We will be using configuration for WMS request 
     177            c = self.wmsRequestConfigElement 
     178 
     179            # Set request configuration parameters 
     180            url = c.find('URL').text 
     181            serviceVersion = c.find('ServiceVersion').text 
     182            imageFormat = c.find('ImageFormat').text 
     183            imageWidth = c.find('ImageWidth').text 
     184            imageHeight = c.find('ImageHeight').text 
     185            crs = c.find('CRS').text 
     186 
     187            bBox = '-180,-90,180,90' 
     188 
     189            # If the timezone is UTC (which in ISO form would look like 'yyyy-mm-ddThh:mm:ss+00:00'), 
     190            # then replace it with 'Z'. 
     191            timestepString = timestep.isoformat() 
     192            timestepString = timestepString.replace('+00:00', 'Z') 
     193 
     194            wmsRequest = '%s?request=GetMap&SERVICE=%s&FORMAT=%s&LAYERS=%s&BBOX=%s&WIDTH=%s&HEIGHT=%s&CRS=%s&TIME=%s' % (url, serviceVersion, imageFormat, self.layer.name, bBox, imageWidth, imageHeight, crs, timestepString) 
     195 
     196            return wmsRequest 
     197 
     198        def buildKmlGroundOverlay(timestep): 
     199            (timespanStart, timespanEnd) = self.getLogicalTimespan(timestep) 
     200            return KMLGroundOverlay( 
     201                timestep.isoformat(), 
     202                buildWMSRequest(timestep), 
     203                timespanStart, timespanEnd, 
     204                -180, -90, 180, 90, 
     205                visible = False 
     206                ) 
     207 
     208        # Create a KML folder that represents the view of the layer 
     209        kmlLayerViewFolder = KMLFolder(self.name, [], visible = False, opened = False) 
     210 
     211        # Create a categorisation dictionary, dict, which will contain categories (as returned by 
     212        # self.getCategory()) as keys, and timesteps belonging into those categories as values. 
     213        dict = {} 
     214        for timestep in self.layer.timesteps: 
     215            category = self.getCategory(timestep) 
     216            if not dict.has_key(category): 
     217                dict[category] = [] 
     218            dict[category].append(timestep) 
     219        categories = dict.keys() 
     220        categories.sort() 
     221 
     222        for category in categories: 
     223            categoryDescription = self.getCategoryDescription(category) 
     224            categoryTimesteps = dict[category] 
     225            kmlCategoryFolder = KMLFolder(categoryDescription, [], visible = False, opened = False) 
     226            for timestep in categoryTimesteps: 
     227                kmlGroundOverlay = buildKmlGroundOverlay(timestep) 
     228                if self.areCategoriesListedExplicitly(): 
     229                    kmlCategoryFolder.children.append(kmlGroundOverlay) 
     230                else: 
     231                    kmlLayerViewFolder.children.append(kmlGroundOverlay) 
     232            if self.areCategoriesListedExplicitly(): 
     233                kmlLayerViewFolder.children.append(kmlCategoryFolder) 
     234 
     235        return kmlLayerViewFolder 
    241236 
    242237class ViewWholeTimecourse(View): 
    243238 
    244     def __init__(self, layer): 
     239    def __init__(self, layer, wmsRequestConfigElement): 
    245240        self.name = 'Whole timecourse' 
    246         self.layer = layer 
     241        View.__init__(self, layer, wmsRequestConfigElement) 
    247242        yearSet = set() 
    248243        for timestep in self.layer.timesteps: 
    249244            yearSet.add(timestep.year) 
    250245        self.sortedYears = list(yearSet); self.sortedYears.sort() 
     246 
     247    def areCategoriesListedExplicitly(self): 
     248        return False 
    251249 
    252250    def getLogicalTimespan(self, timestep): 
     
    268266class ViewSplittedByMonth(View): 
    269267 
    270     def __init__(self, layer): 
     268    def __init__(self, layer, wmsRequestConfigElement): 
    271269        self.name = 'Compare months' 
    272         self.layer = layer 
     270        View.__init__(self, layer, wmsRequestConfigElement) 
     271 
     272    def areCategoriesListedExplicitly(self): 
     273        return True 
    273274 
    274275    def getLogicalTimespan(self, timestep): 
     
    294295class ViewSplittedByPeriod(View): 
    295296 
    296     def __init__(self, layer): 
     297    def __init__(self, layer, wmsRequestConfigElement): 
    297298        self.name = 'Compare period' 
    298         self.layer = layer 
     299        View.__init__(self, layer, wmsRequestConfigElement) 
     300 
     301    def areCategoriesListedExplicitly(self): 
     302        return True 
    299303 
    300304    def getLogicalTimespan(self, timestep): 
     
    346350            viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByPeriod] 
    347351            return wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
    348          
     352 
     353        def _convertToFile(wmsLayer, parentDir): 
     354            kmlDocument = KMLDocument(wmsLayer.title, [])            # Create a KML document with no styles 
     355            kmlDocument.elements = _convertToKML(wmsLayer).children 
     356            filename = parentDir + '/' + wmsLayer.title + '.kml' 
     357            kmlDocument.save(filename) 
     358            print 'Saved file "%s"' % filename 
     359 
    349360        def _convertToDirectory(wmsLayer, parentDir, currentLevel): 
    350361            '''recursive''' 
    351             if currentLevel < self.maxDirDepth: 
     362            if currentLevel < self.maxDirDepth and not isinstance(wmsLayer, BottomWMSLayer): 
    352363                currentDir = parentDir + '/' + wmsLayer.title 
    353364                os.mkdir(currentDir) 
    354365                print 'Created directory "%s"' % currentDir 
    355                 if not isinstance(wmsLayer, BottomWMSLayer): 
    356                     for childWmsLayer in wmsLayer.children: 
    357                         _convertToDirectory(childWmsLayer, currentDir, currentLevel+1) 
    358             elif currentLevel == self.maxDirDepth: 
    359                 # Create a KML document with no styles 
    360                 kmlDocument = KMLDocument(wmsLayer.name, [])  
    361                 kmlDocument.elements.append( _convertToKML(wmsLayer) ) 
    362                 filename = parentDir + '/' + wmsLayer.title + '.kml' 
    363                 kmlDocument.save(filename) 
    364                 print 'Saved file "%s"' % filename 
     366                for childWmsLayer in wmsLayer.children: 
     367                    _convertToDirectory(childWmsLayer, currentDir, currentLevel+1) 
     368            elif currentLevel == self.maxDirDepth or isinstance(wmsLayer, BottomWMSLayer): 
     369                _convertToFile(wmsLayer, parentDir) 
    365370            else: 
    366371                pass 
Note: See TracChangeset for help on using the changeset viewer.