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

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

adding kml code

Line 
1from cElementTree import ElementTree, Element, SubElement
2
3class GridSeries_KML(object):
4    ''' represents a CSML GridSeriesFeature in KML (via WMS requests).'''
5   
6    def __init__(self, description, shortname, wmsendpoint, wmslayer, bbox, timesteps,    timebounds):       
7        ''' initialise with the key information content'''
8       
9        #A descriptive name for the feature:
10        self.description=description       
11        #A short name for the feature:
12        self.shortname=shortname
13        #The WMS endpoint:   
14        self.wmsendpoint=wmsendpoint
15        #The WMS layer name:
16        self.wmslayer=wmslayer
17        #The latitude/longitude bounding box as a csmlwrappers.BBox object
18        self.bbox=bbox
19        #The timesteps
20        self.timesteps=timesteps
21        #The valid bounds of the individual timesteps
22        self.timebounds=timebounds
23       
24    #def _getWSMTime(self, timestep):
25        #1905-2-15T0:0:0.0Z
26   
27    def _buildWMSRequest(self, timestep):
28        ''' build wms request based on values of wmsendpoint, wmslayer, bbox '''
29        #hardcoded to ipcc development site for demo purposes - in NERC portals endpoint should be of form: http://host/wms/identifier
30        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')
31       
32        return wmsrequest
33
34    def outputKML(self):
35        ''' method to output KML as ElementTree instance - returns a 'Folder' element which can be put into a KML Document element '''
36       
37        #build XML document structure - call _buildWMSRequest to get individual request strings.
38        refreshMode='onExpire'
39       
40        folder=Element('Folder')
41        SubElement(folder, 'name').text=str(self.description)
42        SubElement(folder, 'open').text='0'
43       
44        tempcount=0
45       
46        for timestep in self.timesteps.split():
47            tempcount=tempcount+1
48            #add a ground overlay for each timestep
49            #...TODO       
50            go=SubElement(folder, 'GroundOverlay')     
51            SubElement(go, 'name').text=self.shortname + ' ' + timestep
52            SubElement(go, 'open').text='0'
53            timespan=SubElement(go, 'TimeSpan')
54            #TODO - need a method for retrieving time bounds
55            SubElement(timespan, 'begin').text=timestep
56            SubElement(timespan, 'end').text=timestep
57            icon=SubElement(go,'icon')
58            SubElement(icon, 'href').text=self._buildWMSRequest(timestep)
59            SubElement(icon, 'refreshMode').text=refreshMode
60            latlonbox=SubElement(go, 'LatLonBox')
61            SubElement(latlonbox, 'north').text=self.bbox.north
62            SubElement(latlonbox, 'south').text=self.bbox.south
63            SubElement(latlonbox, 'east').text=self.bbox.east
64            SubElement(latlonbox, 'west').text=self.bbox.west
65            #if tempcount==5:# just do a few timesteps for testing purposes
66                #break 
67       
68       
69        return folder
70
71
Note: See TracBrowser for help on using the repository browser.