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

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

Adjust code to allow multiple WMC docs to be selected in the selections
tab and displayed in the view tab. Also rename the display tab to 'details'
and add new exceptions to deal with URL lookup failures.

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
7
8@author: Calum Byrom
9"""
10from ows_server.lib.base import *
11from ows_server.lib.ndgInterface import interface
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
17
18def getWMCDocLayer(url):
19    """
20    Retrieves a WMC document, then strips out and returns the layer info
21    - this is used in the conterra_aggregate to produce the input file for
22    Con Terra
23    """
24    wmcTree = getWMCDoc(url)
25    helper = nsdumb(wmcTree)
26
27    layersTree = helper.find(wmcTree, 'LayerList')
28    if layersTree is None:
29        return ''
30
31    # add each of the layer elements
32    layers = layersTree.getchildren()
33    layersData = []
34    for child in layers:
35        layersData.append(ET.tostring(child))
36       
37    return ''.join(layersData)
38
39
40def getWMCDoc(url):
41    """
42    Gets a wmc from the specified url
43    """
44    x=genericHTTP(proxyServer='http://wwwcache3.rl.ac.uk:8080/')
45    wmcDoc = x.get(url)
46    wmcTree = loadET(wmcDoc) 
47
48    return wmcTree
49
50def getConTerraDoc(wmcURLs):
51    """
52    Uses an array of WMC URLs to construct an aggregation to allow the data
53    to be plotted in Con Terra
54    """
55    c.wmcDocs = []
56    for wmcURL in wmcURLs:
57        c.wmcDocs.append(WMC(wmcURL)) 
58    session.save()
59   
60    response.headers['Content-Type'] = 'text/xml'
61    response.headers['Content-Disposition'] = 'attachment;filename=wmcAggregate.xml'
62    return render('conterra_aggregate', format='xml')
63
64
65   
66def GetWebMapContext(self):
67    """
68    Lookup a WMC doc and return it in the response
69    """
70    # retrieve context data from the specifiled url
71    endpoint = self.inputs['ENDPOINT']
72    if not endpoint:
73        raise exceptions.MissingParameterValue, "ENDPOINT parameter required"
74    logger.info('Getting WebMapContext from endpoint: ' + endpoint)
75
76    try:
77        filehandle = urllib2.urlopen(endpoint)
78    except IOError:
79        raise exceptions.URLUnavailable, "Could not access WMC endpoint: %s" % endpoint
80       
81    response.headers['Content-Type'] = 'text/xml'
82    response.write(filehandle.read())
83   
Note: See TracBrowser for help on using the repository browser.