Changeset 3193


Ignore:
Timestamp:
09/01/08 16:28:41 (12 years ago)
Author:
mkochan
Message:

Code polishing

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

Legend:

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

    r3191 r3193  
    1313        self.csmlFilename = csmlFilename 
    1414        self.kmlFilename = kmlFilename 
    15         ds=csml.parser.Dataset()   # empty csml "Dataset" object 
    16         ds.parse(csmlFilename)     # parse the CSML file into this object 
     15        self.ds=csml.parser.Dataset()        # empty csml "Dataset" object 
     16        self.ds.parse(self.csmlFilename)     # parse the CSML file into this object 
    1717 
    1818    # Auxiliary function, indents XML 
     
    2323                elem.text = i + "  " 
    2424            for child in elem: 
    25                 indent(child, level+1) 
     25                self._indentXML(child, level+1) 
    2626            if not child.tail or not child.tail.strip(): 
    2727                child.tail = i 
     
    4141 
    4242        # Create a separate KML folder for each feature 
    43         for feature in ds.featureCollection.featureMembers: 
     43        for feature in self.ds.featureCollection.featureMembers: 
    4444            # GridSeriesFeatureWrapper allows extended querying of a GridSeriesFeature object 
    4545            gsFeature=GridSeriesFeatureWrapper(feature)  
     
    5656            #wmsendpoint e.g. 'clim_10/cld' 
    5757            #is this consistent for all WMSs -- perhaps we need to provide this information as input parameter 
    58             wmslayer=os.path.split(filename)[1].split('.')[0]+ '/' + feature.name.CONTENT 
     58            # [NOTE] Uses csmlFilename -- un-nice way of passing endpoint 
     59            wmslayer=os.path.split(self.csmlFilename)[1].split('.')[0]+ '/' + feature.name.CONTENT 
    5960 
    60             # Now instantiate a GridSeries_KML object with this information 
    61             gsKML=kmlfeatures.GridSeries_KML(description, shortname, wmsendpoint, wmslayer, bbox, timesteps, timebounds) 
     61            # Instantiate a GridSeriesKML object with this information 
     62            gsKML=kmlfeatures.GridSeriesKML(description, shortname, wmsendpoint, wmslayer, bbox, timesteps, timebounds) 
    6263 
    6364            # Call the outputKML method of this object to generate a populated KML Folder element 
    64             kml_folder=gsKML.outputKML()     
     65            kml_folder=gsKML.outputKML(kmlfeatures.GridSeriesKML.getDecadeSince1990) 
    6566 
    6667            # Append the folder to the kml document. 
     
    7071        kml_root=Element('kml', xmlns='http://earth.google.com/kml/2.2') 
    7172        kml_root.append(kml_document) 
    72         indentXML(kml_root, 0) 
     73        self._indentXML(kml_root, 0) 
    7374        tree = ElementTree(kml_root) 
    7475         
    75         f = open(kmlFilename, 'w') 
     76        f = open(self.kmlFilename, 'w') 
    7677        tree.write(f) 
    7778        f.close() 
  • DPPP/kml/csml2kml/python/csmlwrappers.py

    r3190 r3193  
    1111        west = self.feature.boundedBy.lowerCorner.CONTENT.split()[0] 
    1212 
    13         # [EXTEND] Need to fix 0,360 to -180,180, for now: 
    14         if east=='360.0': east='180' 
    15         if west=='0.0': west='-180' 
    16          
    17         bbox = BBox(north,south,east,west)       
     13        bbox = BBox(north,south,east,west) 
    1814         
    1915        return bbox 
     
    2521        return timesteps 
    2622     
     23    # [TO_DO]  Requires a look into the specs. 
    2724    def getTimeBounds(self): 
    2825        timebounds='' 
     
    3532        self.east=east 
    3633        self.west=west 
     34 
     35class PointSeriesFeatureWrapper(object): 
     36    pass 
  • DPPP/kml/csml2kml/python/kmlfeatures.py

    r3188 r3193  
    22import re 
    33 
    4 class GridSeries_KML(object): 
     4'''KML representatitions of various CSML features''' 
     5 
     6class GridSeriesKML(object): 
    57    ''' represents a CSML GridSeriesFeature in KML (via WMS requests).''' 
    68     
    7     def __init__(self, description, shortname, wmsendpoint, wmslayer, bbox, timesteps,    timebounds):         
     9    def __init__(self, description, shortname, wmsendpoint, wmslayer, bbox, timesteps, timebounds): 
    810        ''' initialise with the key information content''' 
    9          
     11 
    1012        #A descriptive name for the feature: 
    1113        self.description=description         
     
    2224        #The valid bounds of the individual timesteps 
    2325        self.timebounds=timebounds 
    24          
    25     def _getDecadeSince1900(self, year): 
     26     
     27    def getDecadeSince1900(self, year): 
    2628        return (year - 1900) / 10  # get decade as a "logical" year 
     29 
     30    def getSameYear(self, year): 
     31        return year 
    2732 
    2833    #--------------------------------------------------------------------------------     
     
    3237    #-------------------------------------------------------------------------------- 
    3338    def _getLogicalTimeSpan(self, timestep, getLogicalYear): 
    34         ''' Translate timestep to a timespan that cover a full month in a year corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8''' 
     39        '''Translate timestep to a timespan that covers a full month in a year 
     40        corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8 
     41        ''' 
    3542        matchObject = re.match("(\d+)\-(\d+)\-(\d+)T", timestep) 
    3643        (sYear, sMonth, sDay) = matchObject.groups() 
     
    4653        return (startDate, endDate) 
    4754     
     55    # [HARDCODED] 
    4856    def _buildWMSRequest(self, timestep): 
    4957        ''' build wms request based on values of wmsendpoint, wmslayer, bbox ''' 
     
    5361        return wmsrequest 
    5462 
    55     def outputKML(self): 
     63    def outputKML(self, logicalYearTransform): 
    5664        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element ''' 
    5765         
    58         #build XML document structure - call _buildWMSRequest to get individual request strings. 
    59         refreshMode='onExpire' 
    60          
    61         folder=Element('Folder') 
    62         SubElement(folder, 'name').text=str(self.description) 
    63         SubElement(folder, 'open').text='0' 
    64         SubElement(folder, 'visibility').text='0' 
    65          
    66         tempcount=0 
     66        # Build XML document structure -- call _buildWMSRequest to get individual request strings. 
     67       
     68        folderElement=Element('Folder') 
     69        SubElement(folderElement, 'name').text=str(self.description) 
     70        SubElement(folderElement, 'open').text='0' 
     71        SubElement(folderElement, 'visibility').text='0' 
    6772         
    6873        for timestep in self.timesteps.split(): 
    69             tempcount=tempcount+1 
    70             #add a ground overlay for each timestep 
    71             #...TODO        
    72             go=SubElement(folder, 'GroundOverlay')       
    73             SubElement(go, 'name').text=self.shortname + ' ' + timestep 
    74             SubElement(go, 'open').text='0' 
    75             SubElement(go, 'visibility').text='0' 
    76             timespan=SubElement(go, 'TimeSpan') 
    77             #(startTime, endTime) = self._getLogicalTimeSpan(timestep, lambda x: x) 
    78             (startTime, endTime) = self._getLogicalTimeSpan(timestep, self._getDecadeSince1900) 
    79             SubElement(timespan, 'begin').text=startTime 
    80             SubElement(timespan, 'end').text=endTime 
    81             print "%s..%s" % (startTime, endTime) 
    82             icon=SubElement(go,'icon') 
    83             SubElement(icon, 'href').text=self._buildWMSRequest(timestep) 
    84             SubElement(icon, 'refreshMode').text=refreshMode 
    85             latlonbox=SubElement(go, 'LatLonBox') 
    86             SubElement(latlonbox, 'north').text=self.bbox.north 
    87             SubElement(latlonbox, 'south').text=self.bbox.south 
    88             SubElement(latlonbox, 'east').text=self.bbox.east 
    89             SubElement(latlonbox, 'west').text=self.bbox.west 
    9074 
    91         return folder 
     75            goElement=SubElement(folder, 'GroundOverlay')       
     76            SubElement(goElement, 'name').text=self.shortname + ' ' + timestep 
     77            SubElement(goElement, 'open').text='0' 
     78            SubElement(goElement, 'visibility').text='0' 
    9279 
     80            timespanElement=SubElement(goElement, 'TimeSpan') 
     81            (startTime, endTime) = self._getLogicalTimeSpan(timestep, logicalYearTransform) 
     82            SubElement(timespanElement, 'begin').text=startTime 
     83            SubElement(timespanElement, 'end').text=endTime 
    9384 
     85            # Include the WMS service call address 
     86            iconElement=SubElement(goElement,'icon') 
     87            SubElement(iconElement, 'href').text=self._buildWMSRequest(timestep) 
     88            SubElement(iconElement, 'refreshMode').text='onExpire' 
     89 
     90            latlonboxElement=SubElement(goElement, 'LatLonBox') 
     91            # [HARDCODED] Need to fix 0,360 to -180,180, for now as follows 
     92            #             ** guess it should be fixed in the CSML package ** 
     93            east = self.bbox.east 
     94            west = self.bbok.west 
     95            if east=='360.0': east='180' 
     96            if west=='0.0': west='-180' 
     97            SubElement(latlonboxElement, 'north').text=self.bbox.north 
     98            SubElement(latlonboxElement, 'south').text=self.bbox.south 
     99            SubElement(latlonboxElement, 'east').text=east 
     100            SubElement(latlonboxElement, 'west').text=west 
     101         
     102        return folderElement 
  • DPPP/kml/csml2kml/python/testConvertor.py

    r3191 r3193  
    22 
    33c = Convertor('../testdata/clim_10.csml', '../output/clim_10.kml') 
    4 c.writeXML() 
     4c.writeKML() 
Note: See TracChangeset for help on using the changeset viewer.