source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/wmc_util.py @ 4169

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/lib/wmc_util.py@4169
Revision 4169, 3.1 KB checked in by cbyrom, 12 years ago (diff)

Fix small typo bug + add extra checking and logging + remove unused import.

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