Changeset 3514 for DPPP


Ignore:
Timestamp:
25/02/08 16:52:25 (11 years ago)
Author:
mkochan
Message:

Mostly corrected the layer views code. Switched to using "non-naive" View objects (they know the timesteps, although they may not use them).

Location:
DPPP/kml/csml2kml
Files:
3 edited

Legend:

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

    r3509 r3514  
    77 
    88wmsXmlNamespace = 'http://www.opengis.net/wms' 
    9  
    109 
    1110def wmsLayerFactory(layerElement): 
     
    4746        raise NotImplementedError('Use the wmsLayerFactory() function instead.') 
    4847 
    49     def toKML(self, wmsRequestConfigElement, views): 
    50         # ignore wmsRequestConfigElement, views 
     48    def toKML(self, wmsRequestConfigElement, viewTypes): 
     49        # ignore wmsRequestConfigElement, viewTypess 
    5150        kmlLayerFolder = KMLFolder(self.name, [], opened = False, visible = False) 
    5251        for childWmsLayer in self.children: 
    53             kmlLayerFolder.children.append( childWmsLayer.toKML(wmsRequestConfigElement, views) ) 
     52            kmlLayerFolder.children.append( childWmsLayer.toKML(wmsRequestConfigElement, viewTypes) ) 
    5453        return kmlLayerFolder 
    5554 
     
    8887        return self._parseName()[2] 
    8988 
    90     def toKML(self, wmsRequestConfigElement, views): 
    91         ''' 
    92         @param views: A list of View objects, which define how we are going to look at the data. 
     89    def toKML(self, wmsRequestConfigElement, viewTypes): 
     90        ''' 
     91        @param viewTyps: A list of View classes (but not instances), which define how we are going  
     92        to look at the data. 
    9393        @return: a KML.KMLFolder object representing a <kml:Folder> element with lots of <kml:GroundOverlay> 
    9494        elements, each standing for a different time segment. 
     
    116116            timestepString = timestepString.replace('+00:00', 'Z') 
    117117 
    118             wmsRequest = '%s?request=GetMap&amp;SERVICE=%s&amp;FORMAT=%s&amp;LAYERS=%s&amp;BBOX=%s&amp;WIDTH=%s&amp;HEIGHT=%s&amp;CRS=%s&TIME=%s' % (url, serviceVersion, imageFormat, layerName, bBox, imageWidth, imageHeight, crs, timestepString) 
     118            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) 
    119119 
    120120            return wmsRequest 
     
    130130                ) 
    131131 
    132         def buildLayerViewFolder(view): 
    133  
     132        def buildLayerViewFolder(viewType): 
     133 
     134            # Create a view that is informed in advance of the timesteps involved in the following categorisation 
     135            # (most views actually no not need to see the timesteps in advance). 
     136            view = viewFactory(viewType, self.timesteps) 
     137 
     138            # Create a KML folder that represents the view of the layer 
    134139            kmlLayerViewFolder = KMLFolder(view.name, [], visible = False, opened = False)             
    135140 
     
    159164 
    160165        kmlLayerFolder = KMLFolder(self.name, [], visible = False, opened = False) 
    161         for view in views: 
    162             kmlLayerFolder.children.append( buildLayerViewFolder(view) ) 
     166        for viewType in viewTypes: 
     167            kmlLayerFolder.children.append( buildLayerViewFolder(viewType) ) 
    163168        return kmlLayerFolder 
    164      
     169 
    165170class View: 
    166171    ''' 
     
    169174    ''' 
    170175 
    171     def __init__(self): 
     176    def __init__(self, timesteps): 
     177        ''' 
     178        Initialize the view. 
     179        @param timesteps: Some views (not all) may need to "see" all the timesteps before they 
     180        are asked to provide a category for each; they are passed in here. Most views actually 
     181        ignore the timesteps. 
     182        ''' 
    172183        pass 
    173184 
     
    210221        return timestep.replace(year=timestep.year+10) 
    211222 
    212 class ViewWholeCentury(View): 
    213  
    214     def __init__(self): 
    215         self.name = 'Whole century' 
     223def viewFactory(viewType, timesteps): 
     224    newView = viewType(timesteps) 
     225    return newView 
     226 
     227class ViewWholeTimecourse(View): 
     228 
     229    def __init__(self, timesteps): 
     230        self.name = 'Whole timecourse' 
     231        yearSet = set() 
     232        for timestep in timesteps: 
     233            yearSet.add(timestep.year) 
     234        self.sortedYears = list(yearSet); self.sortedYears.sort() 
    216235 
    217236    def getLogicalTimespan(self, timestep): 
    218         timespanStart = self._get20thCenturyDecade(timestep) 
     237        category = self.getCategory(timestep) 
     238        timespanStart = self._getFirstDayOfMonth( timestep.replace(year = category) ) 
    219239        timespanEnd = self._getMonthHence(timespanStart) 
    220240        return (timespanStart, timespanEnd) 
    221241 
    222242    def getCategory(self, timestep): 
    223         return timestep.isoformat() 
     243        try: 
     244            return self.sortedYears.index(timestep.year) + 1 
     245        except ValueError: 
     246            raise ValueError("Timestep's year is not among years that define the categories.") 
    224247 
    225248    def getCategoryDescription(self, category): 
    226249        '''Get a human-readable description of the category (here, return category verbatim).''' 
    227         return category 
     250        return str(category) 
    228251 
    229252class ViewSplittedByMonth(View): 
    230253 
    231     def __init__(self): 
     254    def __init__(self, timesteps): 
    232255        self.name = 'Compare months' 
    233256 
     
    252275        return monthNames[month-1] 
    253276 
    254 class ViewSplittedByDecade(View): 
    255  
    256     def __init__(self): 
    257         self.name = 'Compare decades' 
     277class ViewSplittedByYear(View): 
     278 
     279    def __init__(self, timesteps): 
     280        self.name = 'Compare period' 
    258281 
    259282    def getLogicalTimespan(self, timestep): 
     
    268291        ''' 
    269292        Get a human-readable description of the category that timestep belongs to. 
    270         For instance, for 1990, the result would be 'Decade of 1990'. 
     293        For instance, for 1990, the result would be 'Period of 1990'. 
    271294        ''' 
    272295        if not isinstance(category, int): 
    273296            raise ValueError('Category not an integer (a year)') 
    274297        year = category 
    275         return 'Decade of ' + str(year) 
     298        return 'Period of ' + str(year) 
    276299 
    277300class WMSCapabilities: 
     
    294317class WMSLayersConvertor: 
    295318     
    296     def __init__(self, topWmsLayer, wmsRequestConfigElement, baseKmlOutputDirectory): 
     319    def __init__(self, topWmsLayer, wmsRequestConfigElement, baseKmlOutputDirectory, maxDirDepth): 
    297320        self.topWmsLayer = topWmsLayer 
    298321        self.wmsRequestConfigElement = wmsRequestConfigElement 
    299322        self.baseKmlOutputDirectory = baseKmlOutputDirectory 
    300         self.maxDirDepth = 1 
    301         # [a:D,r:DEBUG] self.maxDirDepth = self.config(...)  
     323        self.maxDirDepth = maxDirDepth 
    302324         
    303325    def convert(self): 
    304326 
    305327        def _convertToKML(wmsLayer): 
    306             views = [ViewWholeCentury(), ViewSplittedByMonth(), ViewSplittedByDecade()] 
    307             return wmsLayer.toKML(self.wmsRequestConfigElement, views) 
     328            viewTypes = [ViewWholeTimecourse, ViewSplittedByMonth, ViewSplittedByYear] 
     329            return wmsLayer.toKML(self.wmsRequestConfigElement, viewTypes) 
    308330         
    309331        def _convertToDirectory(wmsLayer, parentDir, currentLevel): 
  • DPPP/kml/csml2kml/python/csml2kml/csml2kml/tests/testWMSCapabilities.py

    r3508 r3514  
    1717wmsRequestConfigElement = etree2.getroot().find('WMSRequest') 
    1818 
    19 wmsLayersConvertor = WMSLayersConvertor(wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-ar4') 
     19wmsLayersConvertor = WMSLayersConvertor( 
     20    wmsCapabilities.topWmsLayer, wmsRequestConfigElement, '../../../../output/wms-ar4', 1 
     21    ) 
    2022wmsLayersConvertor.convert() 
  • DPPP/kml/csml2kml/testdata/ar4.conf.xml

    r3506 r3514  
    11<WMSLayers2KML> 
    22  <WMSRequest> 
    3     <URL>http://www-devel.ipcc-data.org/maps/wms/obs</URL> 
     3    <URL>http://www-devel.ipcc-data.org/maps/wms/ar4</URL> 
    44    <ServiceVersion>1.1.1</ServiceVersion> 
    55    <ImageFormat>image/png</ImageFormat> 
Note: See TracChangeset for help on using the changeset viewer.