Changeset 3204 for DPPP


Ignore:
Timestamp:
11/01/08 17:01:10 (12 years ago)
Author:
mkochan
Message:

Added config per CSML file

Location:
DPPP/kml/csml2kml
Files:
2 added
4 edited

Legend:

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

    r3198 r3204  
    11import os 
    2 from cElementTree import ElementTree, Element, SubElement, tostring 
     2from cElementTree import ElementTree, Element, SubElement, tostring, dump 
    33 
    44import csml.parser 
    55import kmlfeatures 
    66from csmlwrappers import GridSeriesFeatureWrapper 
     7from configuration import Configuration 
    78 
    89''' Defines a class which performs conversion of a CSML file to a KML file.''' 
     
    1011# [UNSAFE] No exception handling. 
    1112class Convertor: 
    12     def __init__(self, csmlFilename, kmlFilename): 
     13    def __init__(self, csmlFilename, configFilename, kmlFilename): 
    1314        self.csmlFilename = csmlFilename 
    1415        self.kmlFilename = kmlFilename 
    15         self.ds=csml.parser.Dataset()        # empty csml "Dataset" object 
    16         self.ds.parse(self.csmlFilename)     # parse the CSML file into this object 
     16        self.ds=csml.parser.Dataset()                # empty csml "Dataset" object 
     17        self.ds.parse(self.csmlFilename)             # parse the CSML file into this object 
     18        self.config = Configuration(configFilename)  # read in the config file (on per CSML file basis) 
    1719 
    1820    # Auxiliary function, indents XML 
     
    5557 
    5658            # Instantiate a GridSeriesKML object with this information 
    57             gsKML = kmlfeatures.GridSeriesKML(description, name, bBox, timeSteps, timeBounds) 
     59            gsKML = kmlfeatures.GridSeriesKML(self.config, self.csmlFilename, name, description, bBox, timeSteps, timeBounds) 
    5860 
    5961            # Call the outputKML method of this object to generate a populated KML Folder element 
    60             kml_folder = gsKML.outputKML(kmlfeatures.GridSeriesKML.getDecadeSince1900) 
     62            kml_folder = gsKML.outputKML(kmlfeatures.getDecadeSince1900) 
    6163 
    6264            # Append the folder to the kml document. 
  • DPPP/kml/csml2kml/python/csmlwrappers.py

    r3198 r3204  
    66         
    77    def getBBox(self): 
    8         north = self.feature.boundedBy.upperCorner.CONTENT.split()[1] 
    9         south = self.feature.boundedBy.lowerCorner.CONTENT.split()[1] 
    10         east = self.feature.boundedBy.upperCorner.CONTENT.split()[0]         
    11         west = self.feature.boundedBy.lowerCorner.CONTENT.split()[0] 
    12  
     8        north = int(float(self.feature.boundedBy.upperCorner.CONTENT.split()[1])) 
     9        south = int(float(self.feature.boundedBy.lowerCorner.CONTENT.split()[1])) 
     10        east  = int(float(self.feature.boundedBy.upperCorner.CONTENT.split()[0]))         
     11        west  = int(float(self.feature.boundedBy.lowerCorner.CONTENT.split()[0])) 
    1312        bbox = BBox(north,south,east,west) 
    14          
    1513        return bbox 
    1614         
     
    3230        self.east=east 
    3331        self.west=west 
     32    def str(self): 
     33        return "%d,%d,%d,%d" % (self.west, self.south, self.east, self.north) 
    3434 
    3535class PointSeriesFeatureWrapper(object): 
     36    # Example source: /usr/local/ceda_dev/data/csml 
    3637    pass 
  • DPPP/kml/csml2kml/python/kmlfeatures.py

    r3198 r3204  
     1'''KML representatitions of various CSML features''' 
     2 
     3import string 
     4import re 
    15from cElementTree import ElementTree, Element, SubElement 
    2 import re 
     6from configuration import Configuration 
    37 
    4 '''KML representatitions of various CSML features''' 
    58 
    69class GridSeriesKML(object): 
    710    ''' Represents a CSML GridSeriesFeature in KML (via WMS requests).''' 
    811     
    9     def __init__(self, parentFilename, name, description, bBox, timeSteps, timeBounds): 
     12    def __init__(self, config, parentFilename, name, description, bBox, timeSteps, timeBounds): 
    1013        ''' Initialise with the key information content''' 
    1114 
    12         #Name of the CSML file in which feature is contained 
     15        # Feature conversion configuration 
     16        self.config = config 
     17 
     18        # Name of the CSML file in which feature is contained 
    1319        self.parentFilename = parentFilename 
    1420 
    15         #A short name for the feature: 
     21        # A short name for the feature: 
    1622        self.name=name 
    1723 
    18         #A descriptive name for the feature: 
     24        # A descriptive name for the feature: 
    1925        self.description=description 
    2026 
    21         #The latitude/longitude bounding box as a csmlwrappers.BBox object  
     27        # The latitude/longitude bounding box as a csmlwrappers.BBox object  
    2228        self.bBox=bBox 
    2329 
    24         #The time steps  
     30        # The time steps  
    2531        self.timeSteps=timeSteps 
    2632 
    27         #Valid bounds of the individual timesteps 
     33        # Valid bounds of the individual timesteps 
    2834        self.timeBounds=timeBounds 
     35 
    2936     
    30     def getDecadeSince1900(self, year): 
    31         return (year - 1900) / 10  # get decade as a "logical" year 
    32  
    33     def getSameYear(self, year): 
    34         return year 
    35  
    36     def _getLogicalTimeSpan(self, timestep, getLogicalYear): 
     37    def _getLogicalTimeSpan(self, timestep, logicalYearTransform): 
    3738        '''Translate timestep to a timespan that covers a full month in a year 
    3839           corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8 
     
    4142        (sYear, sMonth, sDay) = matchObject.groups() 
    4243        year = int(sYear) 
    43         year = getLogicalYear(year) 
     44        year = logicalYearTransform(year) 
    4445        month = int(sMonth) 
    4546        startDate = "%04d-%02d-%02d" % (year, month, 1) 
     
    5051       
    5152        return (startDate, endDate) 
     53 
     54    def _coordWithin180(self, x): 
     55        ''' Put the x coordinate within bounds of the period (-180, 180)''' 
     56        #if x != 180: 
     57        #    x = ((x + 180) % 360) - 180 
     58        return x 
    5259     
    53     # [UNRESOLVED] bBox dependency 
    54     def _buildWMSRequest(self, timestep): 
     60    # [UNTESTED] 
     61    def _buildWMSRequest(self, timeStep): 
    5562        ''' Build a WMS request ''' 
    56         
    57         c = getFeatureWMSRequestConfig() 
    58         url = c.get('URL').CONTENT 
    59         serviceVersion = c.get('ServiceVersion').CONTENT 
    60         imageFormat = c.get('ImageFormat').CONTENT 
    61         imageWidth = c.get('ImageWidth').CONTENT 
    62         imageHeight = c.get('ImageHeight').CONTENT 
    63         layerName = c.get('LayerName').CONTENT 
    64         filenameExcludingSuffix = string.join(parentFilename.split('.')[:-1], '.') 
    65         layerName.replace('#FILENAME_EXCL_SUFFIX#', filenameExcludingSuffix) 
    66         layerName.replace('#FEATURE_NAME#', name) 
    67         crs = c.get('CRS').CONTENT 
    6863 
    69         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, timeStep+'Z') 
     64        # Set request configuration parameters 
     65        c = self.config 
     66        url = c.get('URL') 
     67        serviceVersion = c.get('ServiceVersion') 
     68        imageFormat = c.get('ImageFormat') 
     69        imageWidth = c.get('ImageWidth') 
     70        imageHeight = c.get('ImageHeight') 
     71        crs = c.get('CRS') 
     72        layerName = c.get('LayerName') 
     73        filenameWithoutPath = self.parentFilename.split('/')[-1] 
     74        filenameExcludingSuffix = string.join(filenameWithoutPath.split('.')[:-1], '.') 
     75        print '%', filenameExcludingSuffix, self.name 
     76        layerName = layerName.replace('#FILENAME_EXCL_SUFFIX#', filenameExcludingSuffix) 
     77        layerName = layerName.replace('#FEATURE_NAME#', self.name) 
     78 
     79        # Make sure that the bounding box is within (-180, 180) bounds for longitude and (-90, 90) for latitude 
     80        self.bBox.east = self._coordWithin180(self.bBox.east) 
     81        self.bBox.west = self._coordWithin180(self.bBox.west) 
     82         
     83 
     84        wmsRequest = '%s?request=GetMap&SERVICE=%s&FORMAT=%s&LAYERS=%s&BBOX=%s&WIDTH=%s&HEIGHT=%s&CRS=%s&TIME=%s' % (url, serviceVersion, imageFormat, layerName, self.bBox.str(), imageWidth, imageHeight, crs, timeStep+'Z') 
    7085 
    7186        return wmsRequest 
     
    8196        SubElement(folderElement, 'visibility').text='0' 
    8297         
    83         for timestep in self.timesteps.split(): 
     98        for timestep in self.timeSteps.split(): 
    8499 
    85             goElement=SubElement(folder, 'GroundOverlay')       
    86             SubElement(goElement, 'name').text=self.shortname + ' ' + timestep 
     100            goElement=SubElement(folderElement, 'GroundOverlay')       
     101            SubElement(goElement, 'name').text=self.name + ' ' + timestep 
    87102            SubElement(goElement, 'open').text='0' 
    88103            SubElement(goElement, 'visibility').text='0' 
     
    99114 
    100115            latlonboxElement=SubElement(goElement, 'LatLonBox') 
    101             # [HARDCODED] Need to fix 0,360 to -180,180, for now as follows 
    102             #             ** guess it should be fixed in the CSML package ** 
    103             east = self.bbox.east 
    104             west = self.bbok.west 
    105             if east=='360.0': east='180' 
    106             if west=='0.0': west='-180' 
    107             SubElement(latlonboxElement, 'north').text=self.bbox.north 
    108             SubElement(latlonboxElement, 'south').text=self.bbox.south 
    109             SubElement(latlonboxElement, 'east').text=east 
    110             SubElement(latlonboxElement, 'west').text=west 
     116            SubElement(latlonboxElement, 'north').text=str(self.bBox.north) 
     117            SubElement(latlonboxElement, 'south').text=str(self.bBox.south) 
     118            SubElement(latlonboxElement, 'east' ).text=str(self.bBox.east) 
     119            SubElement(latlonboxElement, 'west' ).text=str(self.bBox.west) 
    111120         
    112121        return folderElement 
     122 
     123# ---------- These are defined *outside* the class -- i.e. they are plain functions  
     124 
     125def getDecadeSince1900(year): 
     126    return (year - 1900) / 10  # get decade as a "logical" year 
     127 
     128def getSameYear(year): 
     129    return year 
     130 
     131# ----------------- 
  • DPPP/kml/csml2kml/python/testConvertor.py

    r3193 r3204  
    11from convertor import Convertor 
    22 
    3 c = Convertor('../testdata/clim_10.csml', '../output/clim_10.kml') 
     3c = Convertor('../testdata/clim_10.csml', '../testdata/clim_10.csml2kml.conf.xml', '../output/clim_10.kml') 
    44c.writeKML() 
Note: See TracChangeset for help on using the changeset viewer.