source: cows/branches/cows-vis/cowsclient/cowsclient/lib/wmc_util.py @ 5265

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/cows/branches/cows-vis/cowsclient/cowsclient/lib/wmc_util.py@5265
Revision 5265, 3.0 KB checked in by domlowe, 11 years ago (diff)

adding separate cowsclient pylons app

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
10"""
11from cowsclient.lib.base import *
12#from ndgUtils.DocumentRetrieve import genericHTTP
13#from ndgUtils.ETxmlView import loadET,et2text, nsdumb
14from cowsclient.model.WMC import WMC
15#from ows_common import exceptions
16import urllib2, urllib
17import logging
18log = logging.getLogger(__name__)
19
20def getConTerraDoc(wmcURLs):
21    """
22    Construct an aggregated XML file and display this as a temporary webpage; this will automatically be
23    POSTED so that the data is sent via POST to the Con Terra mapClient to visualise (as required by this client)
24    @param wmcURLS: An array of URLs pointing to WMC docs to visualise
25    """
26    # firstly, retrieve each WMC doc
27    wmcDocs = []
28    for wmcURL in wmcURLs:
29        wmcDocs.append(WMC(wmcURL))
30   
31    # now create a dictionary of WMS/layer from these docs - to avoid duplicate layers
32    c.wms = {}
33    for wmcDoc in wmcDocs:
34        for layer in wmcDoc.layers:
35            if layer.wmsURL not in c.wms:
36                c.wms[layer.wmsURL] = [layer.name]
37            else:
38                if layer.name not in c.wms[layer.wmsURL]:
39                    c.wms[layer.wmsURL].append(layer.name)
40   
41    # now create the form to post this data
42    c.redirectToConterra = True; #TODO, remove?
43    response.write(render('selectedItems'))
44   
45
46   
47def GetWebMapContext(self):
48    """
49    Lookup a WMC doc and return it in the response
50    """
51    # retrieve context data from the specifiled url
52    wmcDoc = RetrieveWebMapContext(self, self.inputs['ENDPOINT'])
53   
54    response.headers['Content-Type'] = 'text/xml'
55    response.write(wmcDoc)
56   
57
58def GetLegend(self):
59    """
60    Lookup a legend for a WMS map
61    NB, all required parameters are already included in the endpoint by this stage
62    """
63    endpoint = self.inputs['ENDPOINT']
64    if not endpoint:
65        raise exceptions.MissingParameterValue, "ENDPOINT parameter required"
66    log.info('Getting Legend from endpoint: ' + endpoint)
67
68    try:
69        filehandle = urllib2.urlopen(endpoint)
70    except IOError:
71        raise exceptions.URLUnavailable, "Could not access WMS endpoint: %s" % endpoint
72       
73    response.headers['Content-Type'] = 'text/xml'
74    response.write(filehandle.read())
75   
76def RetrieveWebMapContext(self, endpoint):
77    """
78    Get a WMC doc from a specified endpoint
79    @param endpoint: endpoint to retrieve WMC doc from
80    """
81    if not endpoint:
82        raise exceptions.MissingParameterValue, "ENDPOINT parameter required"
83    log.info('Getting WebMapContext from endpoint: ' + endpoint)
84
85    try:
86        filehandle = urllib2.urlopen(endpoint)
87    except IOError:
88        raise exceptions.URLUnavailable, "Could not access WMC endpoint: %s" % endpoint
89
90    return filehandle.read()
91       
Note: See TracBrowser for help on using the repository browser.