source: TI02-CSML/trunk/services/3rdParty/OWSLib-0.2.0/owslib/wmc.py @ 2194

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI02-CSML/trunk/services/3rdParty/OWSLib-0.2.0/owslib/wmc.py@2194
Revision 2194, 6.5 KB checked in by lawrence, 13 years ago (diff)

Adding various specs and 3rd party code of interest for the CSML
services development.

Line 
1# -*- coding: ISO-8859-15 -*-
2# =============================================================================
3# Copyright (c) 2004 Sean C. Gillies
4# Copyright (c) 2005 Nuxeo SARL <http://nuxeo.com>
5#
6# Authors : Sean Gillies <sgillies@frii.com>
7#           Julien Anguenot <ja@nuxeo.com>
8#
9# Contact email: sgillies@frii.com
10# =============================================================================
11
12"""Web Map Context (WMC)
13
14Specification can be found over there :
15https://portal.opengeospatial.org/files/?artifact_id=8618
16
17"""
18
19from etree import etree
20
21context_ns_uri = 'http://www.opengis.net/context'
22context_schemas_uri = 'http://schemas.opengis.net/context/1.0.0/context.xsd'
23
24def WMCElement(tag):
25    """WMC based element
26    """
27    return etree.Element("{%s}"%context_ns_uri + tag)
28
29class MapContext:
30    """ Map Context abstraction
31
32    It uses a Map representation as input and export it as as map
33    context
34    """
35
36    def __init__(self, map_):
37        self._map = map_
38
39    def _getRootElement(self):
40        root = WMCElement('ViewContext')
41        attrs = {
42            '{http://www.w3.org/2001/XMLSchema-instance}schemaLocation':
43            context_ns_uri + ' ' + context_schemas_uri,
44            'id' : self._map.id,
45            'version' : '1.0.0',
46            }
47        for k, v in attrs.items():
48            root.attrib[k] = v
49        return root
50
51    def _getGeneralElement(self):
52        general = WMCElement('General')
53        general.append(self._getWindowElement())
54        general.append(self._getBoundingBoxElement())
55        return general
56
57    def _getWindowElement(self):
58        window = WMCElement('Window')
59        window.attrib['width'] = str(self._map.size[0])
60        window.attrib['height'] = str(self._map.size[1])
61        return window
62
63    def _getBoundingBoxElement(self):
64        bbox = WMCElement('BoundingBox')
65        bbox.attrib['SRS'] = str(self._map.srs.split()[0])
66        bbox.attrib['minx'] = str(self._map.bounds[0])
67        bbox.attrib['miny'] = str(self._map.bounds[1])
68        bbox.attrib['maxx'] = str(self._map.bounds[2])
69        bbox.attrib['maxy'] = str(self._map.bounds[3])
70        return bbox
71
72    def _getLayerListElement(self):
73        layerlist = WMCElement('LayerList')
74        layering = zip(self._map.layernames, self._map.layertitles)
75        layer_infos = self._map.getLayerInfos()
76
77        # mapbuilder draws layers in bottom-top order
78        for name, title in layering:
79
80            # Layer
81            layer = WMCElement('Layer')
82            layer.attrib['queryable'] = '0'
83            layer.attrib['hidden'] = str(
84                int(name not in self._map.visible_layers))
85
86            # Layer styles
87            if layer_infos and layer_infos.get(title):
88                stylelist = WMCElement('StyleList')
89                # Get wms `Style` nodes for a given layer
90                for e_style in layer_infos.get(title):
91                    e_style.attrib['current'] = '1'
92                    # Change namespace to wmc
93                    for node in e_style.getiterator():
94                        tag_name = node.tag[node.tag.rfind('}')+1:]
95                        node.tag = "{%s}"%context_ns_uri + tag_name
96                    stylelist.append(e_style)
97                layer.append(stylelist)
98
99            # Server
100            server = WMCElement('Server')
101            server.attrib['service'] = 'OGC:WMS'
102            server.attrib['version'] = '1.1.1'
103            server.attrib['title'] = 'OGC:WMS'
104
105            # OnlineRessource
106            oressource = WMCElement('OnlineResource')
107            oressource.attrib[
108                '{http://www.w3.org/1999/xlink}type'] = 'simple'
109            oressource.attrib[
110                '{http://www.w3.org/1999/xlink}href'] = self._map.url
111            server.append(oressource)
112            layer.append(server)
113
114            # Name
115            e_name = WMCElement('Name')
116            e_name.text = name
117            layer.append(e_name)
118
119            # Title
120            e_title = WMCElement('Title')
121            e_title.text = title
122            layer.append(e_title)
123
124            # Format
125            formatlist = WMCElement('FormatList')
126            format = WMCElement('Format')
127            format.attrib['current'] = '1'
128            format.text = self._map.format
129            formatlist.append(format)
130            layer.append(formatlist)
131            layerlist.append(layer)
132
133        return layerlist
134
135    def __call__(self):
136        """Export self._map to WMC
137        """
138        wmc_doc_tree = self._getRootElement()
139        wmc_doc_tree.append(self._getGeneralElement())
140        wmc_doc_tree.append(self._getLayerListElement())
141        return etree.tostring(wmc_doc_tree)
142
143
144class AggregateMapContext(MapContext):
145    """ Map Context abstraction
146
147    It uses a Map representation as input and export it as as map
148    context -- with aggregation of all layers accomplished through
149    overload of the Layer/Name property
150    """
151
152    def _getLayerListElement(self):
153        layerlist = WMCElement('LayerList')
154        #layering = zip(self._map.layernames, self._map.layertitles)
155        layer_infos = self._map.getLayerInfos()
156
157        # Layer
158        layer = WMCElement('Layer')
159        layer.attrib['queryable'] = '0'
160        layer.attrib['hidden'] = '0'
161
162        # Server
163        server = WMCElement('Server')
164        server.attrib['service'] = 'OGC:WMS'
165        server.attrib['version'] = '1.1.1'
166        server.attrib['title'] = 'OGC:WMS'
167
168        # OnlineRessource
169        oressource = WMCElement('OnlineResource')
170        oressource.attrib['{http://www.w3.org/1999/xlink}type'] = 'simple'
171        oressource.attrib['{http://www.w3.org/1999/xlink}href'] = self._map.url
172        server.append(oressource)
173        layer.append(server)
174
175        # Name
176        e_name = WMCElement('Name')
177        e_name.text = ','.join(self._map.layernames)
178        layer.append(e_name)
179
180        # Title
181        e_title = WMCElement('Title')
182        e_title.text = 'Aggregate Layers'
183        layer.append(e_title)
184
185        # Format
186        formatlist = WMCElement('FormatList')
187        format = WMCElement('Format')
188        format.attrib['current'] = '1'
189        format.text = self._map.format
190        formatlist.append(format)
191        layer.append(formatlist)
192        layerlist.append(layer)
193       
194        return layerlist
195
196
197def mapToWebMapContext(map, aggregate_layers=False):
198    """Helper
199
200    if the second argument evaluates to True, then all map layers are
201    aggregated into a single map context layer.
202    """
203    if aggregate_layers:
204        return AggregateMapContext(map)()
205    else:
206        return MapContext(map)()
207
Note: See TracBrowser for help on using the repository browser.