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

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

Prototype: Added support for customized logical year determination

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 _getDecadeSince1900(self, year):
26        return (year - 1900) / 10  # get decade as a "logical" year
27
28    #--------------------------------------------------------------------------------   
29    # _getLogicalTimeSpan()
30    # [UNCLEAR] Am not sure about difference between asking WMS for data at some date
31    #           and at midnight of that day -- which is correct?
32    #--------------------------------------------------------------------------------
33    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'''
35        matchObject = re.match("(\d+)\-(\d+)\-(\d+)T", timestep)
36        (sYear, sMonth, sDay) = matchObject.groups()
37        year = int(sYear)
38        year = getLogicalYear(year)
39        month = int(sMonth)
40        startDate = "%04d-%02d-%02d" % (year, month, 1)
41        if month+1 <= 12:
42            endDate = "%04d-%02d-%02d" % (year, month+1, 1)
43        else:
44            endDate = "%04d-%02d-%02d" % (year+1, 1, 1)
45     
46        return (startDate, endDate)
47   
48    def _buildWMSRequest(self, timestep):
49        ''' build wms request based on values of wmsendpoint, wmslayer, bbox '''
50        #hardcoded to ipcc development site for demo purposes - in NERC portals endpoint should be of form: http://host/wms/identifier
51        wmsrequest='       http://www-devel.ipcc-data.org/maps/wms/obs?request=GetMap&amp;SERVICE=1.1.1&amp;FORMAT=image/png&amp;LAYERS=%s&amp;BBOX=-180,-90,180,90&amp;WIDTH=640&amp;HEIGHT=480&amp;SRS=EPSG:4326&TIME=%s'%(self.wmslayer, timestep+'Z')
52
53        return wmsrequest
54
55    def outputKML(self):
56        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element '''
57       
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
67       
68        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
90
91        return folder
92
93
Note: See TracBrowser for help on using the repository browser.