source: cowsclient/cowsclient/lib/wmc_util.py @ 5364

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cowsclient/cowsclient/lib/wmc_util.py@5364
Revision 5364, 3.2 KB checked in by domlowe, 10 years ago (diff)

Legend added to cows client

Line 
1# Copyright (C) 2007 STFC & NERC (Science and Technology Facilities Council).
2# This software may be distributed under the terms of the
3# Q Public License, version 1.0 or later.
4# http://ndg.nerc.ac.uk/public_docs/QPublic_license.txt
5"""
6Utils to aid use of wmc docs - including interfacing these with the con terra client
7and getting associated legend data
8
9@author: Calum Byrom, modified for NDG Security by Dominic Lowe
10"""
11from cowsclient.lib.base import *
12from cowsclient.model.WMC import WMC
13from cowsclient.lib import exceptions 
14import urllib2, urllib
15import logging
16log = logging.getLogger(__name__)
17
18
19def getConTerraDoc(wmcURLs):
20    """
21    Construct an aggregated XML file and display this as a temporary webpage; this will automatically be
22    POSTED so that the data is sent via POST to the Con Terra mapClient to visualise (as required by this client)
23    @param wmcURLS: An array of URLs pointing to WMC docs to visualise
24    """
25    # firstly, retrieve each WMC doc
26    wmcDocs = []
27    for wmcURL in wmcURLs:
28        wmcDocs.append(WMC(wmcURL))
29   
30    # now create a dictionary of WMS/layer from these docs - to avoid duplicate layers
31    c.wms = {}
32    for wmcDoc in wmcDocs:
33        for layer in wmcDoc.layers:
34            if layer.wmsURL not in c.wms:
35                c.wms[layer.wmsURL] = [layer.name]
36            else:
37                if layer.name not in c.wms[layer.wmsURL]:
38                    c.wms[layer.wmsURL].append(layer.name)
39   
40    # now create the form to post this data
41    c.redirectToConterra = True; #TODO, remove?
42    response.write(render('selectedItems'))
43   
44
45   
46def GetWebMapContext(self):
47    """
48    Lookup a WMC doc and return it in the response
49    """
50    # retrieve context data from the specifiled url
51    wmcDoc = RetrieveWebMapContext(self, self.inputs['ENDPOINT'])
52    log.info('WMC RESPONSE %s'%response)
53    response.headers['Content-Type'] = 'text/xml'
54    response.write(wmcDoc)
55
56def GetLegend(self):
57    """
58    Lookup a legend for a WMS map
59    NB, all required parameters are already included in the endpoint by this stage
60    """
61    endpoint = self.inputs['ENDPOINT']
62    if not endpoint:
63        raise exceptions.MissingParameterValue, "ENDPOINT parameter required"
64    try:
65        req = urllib2.Request(endpoint,urllib.urlencode(request.params),{'Cookie': request.headers.get('Cookie', '')})
66        filehandle = urllib2.urlopen(req)
67    except IOError:
68        raise exceptions.URLUnavailable, "Could not access WMC endpoint: %s" % endpoint
69    response.headers['Content-Type'] = 'image/gif'
70    c.legendurl = endpoint
71    response.write(render('legendRender'))
72   
73   
74def RetrieveWebMapContext(self, endpoint):
75    """
76    Get a WMC doc from a specified endpoint
77    @param endpoint: endpoint to retrieve WMC doc from
78    """
79    if not endpoint:
80        raise exceptions.MissingParameterValue, "ENDPOINT parameter required"
81    log.info('Getting WebMapContext from endpoint: ' + endpoint)   
82    urlstring=('%s&request=GetContext'%(str(endpoint)))
83    #cookies are passed to enable authorisation mechanisms e.g. ndg security
84    #try:
85
86    req = urllib2.Request(urlstring)
87    req.add_header('Cookie', request.headers.get('Cookie', ''))
88    filehandle = urllib2.urlopen(req)
89    return filehandle.read()
Note: See TracBrowser for help on using the repository browser.