1 | /** |
---|
2 | * Copyright (c) 2008-2010 The Open Source Geospatial Foundation |
---|
3 | * |
---|
4 | * Published under the BSD license. |
---|
5 | * See http://svn.geoext.org/core/trunk/geoext/license.txt for the full text |
---|
6 | * of the license. |
---|
7 | */ |
---|
8 | |
---|
9 | Ext.namespace("GeoExt.tree"); |
---|
10 | |
---|
11 | /** api: (define) |
---|
12 | * module = GeoExt.tree |
---|
13 | * class = WMSCapabilitiesLoader |
---|
14 | * base_link = `Ext.tree.TreeLoader <http://www.dev.sencha.com/deploy/dev/docs/?class=Ext.tree.TreeLoader>`_ |
---|
15 | */ |
---|
16 | |
---|
17 | /** api: constructor |
---|
18 | * .. class:: WMSCapabilitiesLoader |
---|
19 | * |
---|
20 | * A loader that will load create a tree of all layers of a Web Map |
---|
21 | * Service (WMS), maintaining its tree structure. Nodes created by this |
---|
22 | * loader are instances of ``Ext.tree.TreeNode``. If the WMS Capabilities |
---|
23 | * advertise a name for a layer, an OpenLayers.Layer.WMS instance will |
---|
24 | * be set on the node in its ``layer`` attribute. |
---|
25 | */ |
---|
26 | GeoExt.tree.WMSCapabilitiesLoader = function(config) { |
---|
27 | Ext.apply(this, config); |
---|
28 | GeoExt.tree.WMSCapabilitiesLoader.superclass.constructor.call(this); |
---|
29 | }; |
---|
30 | |
---|
31 | Ext.extend(GeoExt.tree.WMSCapabilitiesLoader, Ext.tree.TreeLoader, { |
---|
32 | |
---|
33 | /** api: config[url] |
---|
34 | * ``String`` |
---|
35 | * The online resource of the Web Map Service. |
---|
36 | */ |
---|
37 | url: null, |
---|
38 | |
---|
39 | /** api: config[layerOptions] |
---|
40 | * ``Object`` |
---|
41 | * Optional options to set on the WMS layers which will be created by |
---|
42 | * this loader. |
---|
43 | */ |
---|
44 | layerOptions: null, |
---|
45 | |
---|
46 | /** api: config[layerParams] |
---|
47 | * ``Object`` |
---|
48 | * Optional parameters to set on the WMS layers which will be created by |
---|
49 | * this loader. |
---|
50 | */ |
---|
51 | layerParams: null, |
---|
52 | |
---|
53 | /** private: property[requestMethod] |
---|
54 | * ``String`` WMS GetCapabilities request needs to be done using HTTP GET |
---|
55 | */ |
---|
56 | requestMethod: 'GET', |
---|
57 | |
---|
58 | /** private: method[getParams] |
---|
59 | * Private getParams override. |
---|
60 | */ |
---|
61 | getParams: function(node) { |
---|
62 | return {'service': 'WMS', 'request': 'GetCapabilities'}; |
---|
63 | }, |
---|
64 | |
---|
65 | /** private: method[processResponse] |
---|
66 | * :param response: ``Object`` The XHR object |
---|
67 | * :param node: ``Ext.tree.TreeNode`` |
---|
68 | * :param callback: ``Function`` |
---|
69 | * :param scope: ``Object`` |
---|
70 | * |
---|
71 | * Private processResponse override. |
---|
72 | */ |
---|
73 | processResponse : function(response, node, callback, scope){ |
---|
74 | var capabilities = new OpenLayers.Format.WMSCapabilities().read( |
---|
75 | response.responseXML || response.responseText); |
---|
76 | this.processLayer(capabilities.capability, |
---|
77 | capabilities.capability.request.getmap.href, node); |
---|
78 | if (typeof callback == "function") { |
---|
79 | callback.apply(scope || node, [node]); |
---|
80 | } |
---|
81 | }, |
---|
82 | |
---|
83 | /** private: method[createWMSLayer] |
---|
84 | * :param layer: ``Object`` The layer object from the WMS GetCapabilities |
---|
85 | * parser |
---|
86 | * :param url: ``String`` The online resource of the WMS |
---|
87 | * :return: ``OpenLayers.Layer.WMS`` or ``null`` The WMS layer created or |
---|
88 | * null. |
---|
89 | * |
---|
90 | * Create a WMS layer which will be attached as an attribute to the |
---|
91 | * node. |
---|
92 | */ |
---|
93 | createWMSLayer: function(layer, url) { |
---|
94 | if (layer.name) { |
---|
95 | return new OpenLayers.Layer.WMS( layer.title, url, |
---|
96 | OpenLayers.Util.extend({formats: layer.formats[0], |
---|
97 | layers: layer.name}, this.layerParams), |
---|
98 | OpenLayers.Util.extend({minScale: layer.minScale, |
---|
99 | queryable: layer.queryable, maxScale: layer.maxScale, |
---|
100 | metadata: layer |
---|
101 | }, this.layerOptions)); |
---|
102 | } else { |
---|
103 | return null; |
---|
104 | } |
---|
105 | }, |
---|
106 | |
---|
107 | /** private: method[processLayer] |
---|
108 | * :param layer: ``Object`` The layer object from the WMS GetCapabilities |
---|
109 | * parser |
---|
110 | * :param url: ``String`` The online resource of the WMS |
---|
111 | * :param node: ``Ext.tree.TreeNode`` |
---|
112 | * |
---|
113 | * Recursive function to create the tree nodes for the layer structure |
---|
114 | * of a WMS GetCapabilities response. |
---|
115 | */ |
---|
116 | processLayer: function(layer, url, node) { |
---|
117 | Ext.each(layer.nestedLayers, function(el) { |
---|
118 | var n = this.createNode({text: el.title || el.name, |
---|
119 | // use nodeType 'node' so no AsyncTreeNodes are created |
---|
120 | nodeType: 'node', |
---|
121 | layer: this.createWMSLayer(el, url), |
---|
122 | leaf: (el.nestedLayers.length === 0)}); |
---|
123 | if(n){ |
---|
124 | node.appendChild(n); |
---|
125 | } |
---|
126 | if (el.nestedLayers) { |
---|
127 | this.processLayer(el, url, n); |
---|
128 | } |
---|
129 | }, this); |
---|
130 | } |
---|
131 | |
---|
132 | }); |
---|