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

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

Prototype: Changed dataset folders in KML to be closed & unloaded by default. Added multiple datasets to extraction.

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        if month+1 <= 12:
37            endDate = "%04d-%02d-%02d" % (year, month+1, 1)
38        else:
39            endDate = "%04d-%02d-%02d" % (year+1, 1, 1)
40     
41        return (startDate, endDate)
42   
43    def _buildWMSRequest(self, timestep):
44        ''' build wms request based on values of wmsendpoint, wmslayer, bbox '''
45        #hardcoded to ipcc development site for demo purposes - in NERC portals endpoint should be of form: http://host/wms/identifier
46        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')
47
48        return wmsrequest
49
50    def outputKML(self):
51        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element '''
52       
53        #build XML document structure - call _buildWMSRequest to get individual request strings.
54        refreshMode='onExpire'
55       
56        folder=Element('Folder')
57        SubElement(folder, 'name').text=str(self.description)
58        SubElement(folder, 'open').text='0'
59        SubElement(folder, 'visibility').text='0'
60       
61        tempcount=0
62       
63        for timestep in self.timesteps.split():
64            tempcount=tempcount+1
65            #add a ground overlay for each timestep
66            #...TODO       
67            go=SubElement(folder, 'GroundOverlay')     
68            SubElement(go, 'name').text=self.shortname + ' ' + timestep
69            SubElement(go, 'open').text='0'
70            SubElement(go, 'visibility').text='0'
71            timespan=SubElement(go, 'TimeSpan')
72            #TODO - need a method for retrieving time bounds
73            (startTime, endTime) = self._getLogicalTimeSpan(timestep)
74            SubElement(timespan, 'begin').text=startTime
75            SubElement(timespan, 'end').text=endTime
76            print "%s..%s" % (startTime, endTime)
77            icon=SubElement(go,'icon')
78            SubElement(icon, 'href').text=self._buildWMSRequest(timestep)
79            SubElement(icon, 'refreshMode').text=refreshMode
80            latlonbox=SubElement(go, 'LatLonBox')
81            SubElement(latlonbox, 'north').text=self.bbox.north
82            SubElement(latlonbox, 'south').text=self.bbox.south
83            SubElement(latlonbox, 'east').text=self.bbox.east
84            SubElement(latlonbox, 'west').text=self.bbox.west
85
86        return folder
87
88
Note: See TracBrowser for help on using the repository browser.