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

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

Added logical years to the prototype. Now all first decade of the 20th Century is in year 1900, the next in year 1901 etc.

Line 
1from cElementTree import ElementTree, Element, SubElement
2import re
3
4class GridSeries_KML(object):
5    ''' represents a CSML GridSeriesFeature in KML (via WMS requests).'''
6   
7    def __init__(self, description, shortname, wmsendpoint, wmslayer, bbox, timesteps,    timebounds):       
8        ''' initialise with the key information content'''
9       
10        #A descriptive name for the feature:
11        self.description=description       
12        #A short name for the feature:
13        self.shortname=shortname
14        #The WMS endpoint:   
15        self.wmsendpoint=wmsendpoint
16        #The WMS layer name:
17        self.wmslayer=wmslayer
18        #The latitude/longitude bounding box as a csmlwrappers.BBox object
19        self.bbox=bbox
20        #The timesteps
21        self.timesteps=timesteps
22        #The valid bounds of the individual timesteps
23        self.timebounds=timebounds
24       
25    def _getLogicalTimeSpan(self, timestep):
26        ''' Translate timestep to a timespan that cover a full month in a year corresponding to the decade (e.g. 1900s -> 0, 1980s -> 8'''
27        matchObject = re.match("(\d+)\-(\d+)\-(\d+)T", timestep)
28        (sYear, sMonth, sDay) = matchObject.groups()
29        year = (int(sYear) - 1900) / 10  # get decade ("logical" year)
30        month = int(sMonth)
31        daysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
32        isLeapYear = (year % 4) == 0  # even logical years can be leap years :)
33        if isLeapYear:
34            daysInMonth[1] = 29
35        startDate = "%04d-%02d-%02d" % (year, month, 1)
36        endDate = "%04d-%02d-%02d" % (year, month, daysInMonth[month-1])
37       
38        return (startDate, endDate)
39   
40    def _buildWMSRequest(self, timestep):
41        ''' build wms request based on values of wmsendpoint, wmslayer, bbox '''
42        #hardcoded to ipcc development site for demo purposes - in NERC portals endpoint should be of form: http://host/wms/identifier
43        wmsrequest='       http://www-devel.ipcc-data.org/maps/wms/obs?request=GetMap&SERVICE=1.1.1&FORMAT=image/png&LAYERS=%s&BBOX=-180,-90,180,90&WIDTH=640&HEIGHT=480&SRS=EPSG:4326&TIME=%s'%(self.wmslayer, timestep+'Z')
44
45        return wmsrequest
46
47    def outputKML(self):
48        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element '''
49       
50        #build XML document structure - call _buildWMSRequest to get individual request strings.
51        refreshMode='onExpire'
52       
53        folder=Element('Folder')
54        SubElement(folder, 'name').text=str(self.description)
55        SubElement(folder, 'open').text='0'
56       
57        tempcount=0
58       
59        for timestep in self.timesteps.split():
60            tempcount=tempcount+1
61            #add a ground overlay for each timestep
62            #...TODO       
63            go=SubElement(folder, 'GroundOverlay')     
64            SubElement(go, 'name').text=self.shortname + ' ' + timestep
65            SubElement(go, 'open').text='0'
66            timespan=SubElement(go, 'TimeSpan')
67            #TODO - need a method for retrieving time bounds
68            (startTime, endTime) = self._getLogicalTimeSpan(timestep)
69            SubElement(timespan, 'begin').text=startTime
70            SubElement(timespan, 'end').text=endTime
71            print "%s..%s" % (startTime, endTime)
72            icon=SubElement(go,'icon')
73            SubElement(icon, 'href').text=self._buildWMSRequest(timestep)
74            SubElement(icon, 'refreshMode').text=refreshMode
75            latlonbox=SubElement(go, 'LatLonBox')
76            SubElement(latlonbox, 'north').text=self.bbox.north
77            SubElement(latlonbox, 'south').text=self.bbox.south
78            SubElement(latlonbox, 'east').text=self.bbox.east
79            SubElement(latlonbox, 'west').text=self.bbox.west
80
81        return folder
82
83
Note: See TracBrowser for help on using the repository browser.