source: DPPP/kml/csml2kml/python/kmlfeatures.py @ 3210

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/DPPP/kml/csml2kml/python/kmlfeatures.py@3210
Revision 3210, 5.3 KB checked in by mkochan, 13 years ago (diff)

Removed code obsolete in last commit

Line 
1'''KML representatitions of various CSML features'''
2
3import string
4import re
5from cElementTree import ElementTree, Element, SubElement
6from configuration import Configuration
7
8
9class GridSeriesKML(object):
10    ''' Represents a CSML GridSeriesFeature in KML (via WMS requests).'''
11   
12    def __init__(self, config, parentFilename, name, description, bBox, timeSteps, timeBounds):
13        ''' Initialise with the key information content'''
14
15        # Feature conversion configuration
16        self.config = config
17
18        # Name of the CSML file in which feature is contained
19        self.parentFilename = parentFilename
20
21        # A short name for the feature:
22        self.name=name
23
24        # A descriptive name for the feature:
25        self.description=description
26
27        # The latitude/longitude bounding box as a csmlwrappers.BBox object
28        self.bBox=bBox
29
30        # The time steps
31        self.timeSteps=timeSteps
32
33        # Valid bounds of the individual timesteps
34        self.timeBounds=timeBounds
35
36   
37    def _getLogicalTimeSpan(self, timestep, logicalYearTransform):
38        '''Translate timestep to a timespan that covers a full month in a year
39           corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8
40        '''
41        matchObject = re.match("(\d+)\-(\d+)\-(\d+)T", timestep)
42        (sYear, sMonth, sDay) = matchObject.groups()
43        year = int(sYear)
44        year = logicalYearTransform(year)
45        month = int(sMonth)
46        startDate = "%04d-%02d-%02d" % (year, month, 1)
47        if month+1 <= 12:
48            endDate = "%04d-%02d-%02d" % (year, month+1, 1)
49        else:
50            endDate = "%04d-%02d-%02d" % (year+1, 1, 1)
51     
52        return (startDate, endDate)
53
54    def _longitudeWithinMinusPlus180(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
59   
60    def _buildWMSRequest(self, timeStep):
61        ''' Build a WMS request '''
62
63        # Set request configuration parameters
64        c = self.config
65        url = c.get('URL')
66        serviceVersion = c.get('ServiceVersion')
67        imageFormat = c.get('ImageFormat')
68        imageWidth = c.get('ImageWidth')
69        imageHeight = c.get('ImageHeight')
70        crs = c.get('CRS')
71        layerName = c.get('LayerName')
72        filenameWithoutPath = self.parentFilename.split('/')[-1]
73        filenameExcludingSuffix = string.join(filenameWithoutPath.split('.')[:-1], '.')
74        layerName = layerName.replace('#FILENAME_EXCL_SUFFIX#', filenameExcludingSuffix)
75        layerName = layerName.replace('#FEATURE_NAME#', self.name)
76
77        # If timeStep contains a time part, make sure that the "Z" for "Zulu" is appended, as required by WMS
78        if re.search("T\d+\:\d+:\d+(\.\d+)$", timeStep):
79            timeStep = timeStep + 'Z'
80
81        # If required, make sure the longitude part of the bounding box is within (-180, 180)
82        if c.get('LongitudeBounds') == 'MINUS_180_TO_PLUS_180':
83            self.bBox.east = self._longitudeWithinMinusPlus180(self.bBox.east)
84            self.bBox.west = self._longitudeWithinMinusPlus180(self.bBox.west)
85       
86        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, self.bBox.str(), imageWidth, imageHeight, crs, timeStep)
87
88        return wmsRequest
89
90    def outputKML(self, logicalYearTransform):
91        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element '''
92       
93        # Build XML document structure -- call _buildWMSRequest to get individual request strings.
94     
95        folderElement=Element('Folder')
96        SubElement(folderElement, 'name').text=str(self.description)
97        SubElement(folderElement, 'open').text='0'
98        SubElement(folderElement, 'visibility').text='0'
99       
100        for timestep in self.timeSteps.split():
101
102            goElement=SubElement(folderElement, 'GroundOverlay')     
103            SubElement(goElement, 'name').text=self.name + ' ' + timestep
104            SubElement(goElement, 'open').text='0'
105            SubElement(goElement, 'visibility').text='0'
106
107            timespanElement=SubElement(goElement, 'TimeSpan')
108            (startTime, endTime) = self._getLogicalTimeSpan(timestep, logicalYearTransform)
109            SubElement(timespanElement, 'begin').text=startTime
110            SubElement(timespanElement, 'end').text=endTime
111
112            # Include the WMS service call address
113            iconElement=SubElement(goElement,'icon')
114            SubElement(iconElement, 'href').text=self._buildWMSRequest(timestep)
115            SubElement(iconElement, 'refreshMode').text='onExpire'
116
117            latlonboxElement=SubElement(goElement, 'LatLonBox')
118            SubElement(latlonboxElement, 'north').text=str(self.bBox.north)
119            SubElement(latlonboxElement, 'south').text=str(self.bBox.south)
120            SubElement(latlonboxElement, 'east' ).text=str(self.bBox.east)
121            SubElement(latlonboxElement, 'west' ).text=str(self.bBox.west)
122       
123        return folderElement
124
125# ---------- These are defined *outside* the class -- i.e. they are plain functions
126
127def getDecadeSince1900(year):
128    return (year - 1900) / 10  # get decade as a "logical" year
129
130def getSameYear(year):
131    return year
132
133# -----------------
Note: See TracBrowser for help on using the repository browser.