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 | /** |
---|
10 | * @include GeoExt/data/LayerRecord.js |
---|
11 | */ |
---|
12 | |
---|
13 | /** api: (define) |
---|
14 | * module = GeoExt.data |
---|
15 | * class = WMSCapabilitiesReader |
---|
16 | * base_link = `Ext.data.DataReader <http://dev.sencha.com/deploy/dev/docs/?class=Ext.data.DataReader>`_ |
---|
17 | */ |
---|
18 | Ext.namespace("GeoExt.data"); |
---|
19 | |
---|
20 | /** api: constructor |
---|
21 | * .. class:: WMSCapabilitiesReader(meta, recordType) |
---|
22 | * |
---|
23 | * :param meta: ``Object`` Reader configuration from which: |
---|
24 | * ``layerOptions`` is an optional object passed as default options |
---|
25 | * to the ``OpenLayers.Layer.WMS`` constructor. |
---|
26 | * ``layerParams`` is an optional set of parameters to pass into the |
---|
27 | * ``OpenLayers.Layer.WMS`` constructor. |
---|
28 | * :param recordType: ``Array | Ext.data.Record`` An array of field |
---|
29 | * configuration objects or a record object. Default is |
---|
30 | * :class:`GeoExt.data.LayerRecord` with the following fields: |
---|
31 | * name, title, abstract, queryable, opaque, noSubsets, cascaded, |
---|
32 | * fixedWidth, fixedHeight, minScale, maxScale, prefix, formats, |
---|
33 | * styles, srs, dimensions, bbox, llbbox, attribution, keywords, |
---|
34 | * identifiers, authorityURLs, metadataURLs. |
---|
35 | * The type of these fields is the same as for the matching fields in |
---|
36 | * the object returned from |
---|
37 | * ``OpenLayers.Format.WMSCapabilities::read()``. |
---|
38 | * |
---|
39 | * Data reader class to create an array of |
---|
40 | * :class:`GeoExt.data.LayerRecord` objects from a WMS GetCapabilities |
---|
41 | * response. |
---|
42 | */ |
---|
43 | GeoExt.data.WMSCapabilitiesReader = function(meta, recordType) { |
---|
44 | meta = meta || {}; |
---|
45 | if(!meta.format) { |
---|
46 | meta.format = new OpenLayers.Format.WMSCapabilities(); |
---|
47 | } |
---|
48 | if(typeof recordType !== "function") { |
---|
49 | recordType = GeoExt.data.LayerRecord.create( |
---|
50 | recordType || meta.fields || [ |
---|
51 | {name: "name", type: "string"}, |
---|
52 | {name: "title", type: "string"}, |
---|
53 | {name: "abstract", type: "string"}, |
---|
54 | {name: "queryable", type: "boolean"}, |
---|
55 | {name: "opaque", type: "boolean"}, |
---|
56 | {name: "noSubsets", type: "boolean"}, |
---|
57 | {name: "cascaded", type: "int"}, |
---|
58 | {name: "fixedWidth", type: "int"}, |
---|
59 | {name: "fixedHeight", type: "int"}, |
---|
60 | {name: "minScale", type: "float"}, |
---|
61 | {name: "maxScale", type: "float"}, |
---|
62 | {name: "prefix", type: "string"}, |
---|
63 | {name: "formats"}, // array |
---|
64 | {name: "styles"}, // array |
---|
65 | {name: "srs"}, // object |
---|
66 | {name: "dimensions"}, // object |
---|
67 | {name: "bbox"}, // object |
---|
68 | {name: "llbbox"}, // array |
---|
69 | {name: "attribution"}, // object |
---|
70 | {name: "keywords"}, // array |
---|
71 | {name: "identifiers"}, // object |
---|
72 | {name: "authorityURLs"}, // object |
---|
73 | {name: "metadataURLs"} // array |
---|
74 | ] |
---|
75 | ); |
---|
76 | } |
---|
77 | GeoExt.data.WMSCapabilitiesReader.superclass.constructor.call( |
---|
78 | this, meta, recordType |
---|
79 | ); |
---|
80 | }; |
---|
81 | |
---|
82 | Ext.extend(GeoExt.data.WMSCapabilitiesReader, Ext.data.DataReader, { |
---|
83 | |
---|
84 | |
---|
85 | /** api: config[attributionCls] |
---|
86 | * ``String`` CSS class name for the attribution DOM elements. |
---|
87 | * Element class names append "-link", "-image", and "-title" as |
---|
88 | * appropriate. Default is "gx-attribution". |
---|
89 | */ |
---|
90 | attributionCls: "gx-attribution", |
---|
91 | |
---|
92 | /** private: method[read] |
---|
93 | * :param request: ``Object`` The XHR object which contains the parsed XML |
---|
94 | * document. |
---|
95 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
96 | * as a cache of ``Ext.data.Record`` objects. |
---|
97 | */ |
---|
98 | read: function(request) { |
---|
99 | var data = request.responseXML; |
---|
100 | if(!data || !data.documentElement) { |
---|
101 | data = request.responseText; |
---|
102 | } |
---|
103 | return this.readRecords(data); |
---|
104 | }, |
---|
105 | |
---|
106 | /** private: method[serviceExceptionFormat] |
---|
107 | * :param formats: ``Array`` An array of service exception format strings. |
---|
108 | * :return: ``String`` The (supposedly) best service exception format. |
---|
109 | */ |
---|
110 | serviceExceptionFormat: function(formats) { |
---|
111 | if (OpenLayers.Util.indexOf(formats, |
---|
112 | "application/vnd.ogc.se_inimage")>-1) { |
---|
113 | return "application/vnd.ogc.se_inimage"; |
---|
114 | } |
---|
115 | if (OpenLayers.Util.indexOf(formats, |
---|
116 | "application/vnd.ogc.se_xml")>-1) { |
---|
117 | return "application/vnd.ogc.se_xml"; |
---|
118 | } |
---|
119 | return formats[0]; |
---|
120 | }, |
---|
121 | |
---|
122 | /** private: method[imageFormat] |
---|
123 | * :param layer: ``Object`` The layer's capabilities object. |
---|
124 | * :return: ``String`` The (supposedly) best mime type for requesting |
---|
125 | * tiles. |
---|
126 | */ |
---|
127 | imageFormat: function(layer) { |
---|
128 | var formats = layer.formats; |
---|
129 | if (layer.opaque && |
---|
130 | OpenLayers.Util.indexOf(formats, "image/jpeg")>-1) { |
---|
131 | return "image/jpeg"; |
---|
132 | } |
---|
133 | if (OpenLayers.Util.indexOf(formats, "image/png")>-1) { |
---|
134 | return "image/png"; |
---|
135 | } |
---|
136 | if (OpenLayers.Util.indexOf(formats, "image/png; mode=24bit")>-1) { |
---|
137 | return "image/png; mode=24bit"; |
---|
138 | } |
---|
139 | if (OpenLayers.Util.indexOf(formats, "image/gif")>-1) { |
---|
140 | return "image/gif"; |
---|
141 | } |
---|
142 | return formats[0]; |
---|
143 | }, |
---|
144 | |
---|
145 | /** private: method[imageTransparent] |
---|
146 | * :param layer: ``Object`` The layer's capabilities object. |
---|
147 | * :return: ``Boolean`` The TRANSPARENT param. |
---|
148 | */ |
---|
149 | imageTransparent: function(layer) { |
---|
150 | return layer.opaque == undefined || !layer.opaque; |
---|
151 | }, |
---|
152 | |
---|
153 | /** private: method[readRecords] |
---|
154 | * :param data: ``DOMElement | String | Object`` A document element or XHR |
---|
155 | * response string. As an alternative to fetching capabilities data |
---|
156 | * from a remote source, an object representing the capabilities can |
---|
157 | * be provided given that the structure mirrors that returned from the |
---|
158 | * capabilities parser. |
---|
159 | * :return: ``Object`` A data block which is used by an ``Ext.data.Store`` |
---|
160 | * as a cache of ``Ext.data.Record`` objects. |
---|
161 | * |
---|
162 | * Create a data block containing Ext.data.Records from an XML document. |
---|
163 | */ |
---|
164 | readRecords: function(data) { |
---|
165 | if(typeof data === "string" || data.nodeType) { |
---|
166 | data = this.meta.format.read(data); |
---|
167 | } |
---|
168 | var version = data.version; |
---|
169 | var capability = data.capability || {}; |
---|
170 | var url = capability.request && capability.request.getmap && |
---|
171 | capability.request.getmap.href; |
---|
172 | var layers = capability.layers; |
---|
173 | var formats = capability.exception ? capability.exception.formats : []; |
---|
174 | var exceptions = this.serviceExceptionFormat(formats); |
---|
175 | var records = []; |
---|
176 | |
---|
177 | if(url && layers) { |
---|
178 | var fields = this.recordType.prototype.fields; |
---|
179 | var layer, values, options, params, field, v; |
---|
180 | |
---|
181 | for(var i=0, lenI=layers.length; i<lenI; i++){ |
---|
182 | layer = layers[i]; |
---|
183 | if(layer.name) { |
---|
184 | values = {}; |
---|
185 | for(var j=0, lenJ=fields.length; j<lenJ; j++) { |
---|
186 | field = fields.items[j]; |
---|
187 | v = layer[field.mapping || field.name] || |
---|
188 | field.defaultValue; |
---|
189 | v = field.convert(v); |
---|
190 | values[field.name] = v; |
---|
191 | } |
---|
192 | options = { |
---|
193 | attribution: layer.attribution ? |
---|
194 | this.attributionMarkup(layer.attribution) : |
---|
195 | undefined, |
---|
196 | minScale: layer.minScale, |
---|
197 | maxScale: layer.maxScale |
---|
198 | }; |
---|
199 | if(this.meta.layerOptions) { |
---|
200 | Ext.apply(options, this.meta.layerOptions); |
---|
201 | } |
---|
202 | params = { |
---|
203 | layers: layer.name, |
---|
204 | exceptions: exceptions, |
---|
205 | format: this.imageFormat(layer), |
---|
206 | transparent: this.imageTransparent(layer), |
---|
207 | version: version |
---|
208 | }; |
---|
209 | if (this.meta.layerParams) { |
---|
210 | Ext.apply(params, this.meta.layerParams); |
---|
211 | } |
---|
212 | values.layer = new OpenLayers.Layer.WMS( |
---|
213 | layer.title || layer.name, url, params, options |
---|
214 | ); |
---|
215 | records.push(new this.recordType(values, values.layer.id)); |
---|
216 | } |
---|
217 | } |
---|
218 | } |
---|
219 | |
---|
220 | return { |
---|
221 | totalRecords: records.length, |
---|
222 | success: true, |
---|
223 | records: records |
---|
224 | }; |
---|
225 | |
---|
226 | }, |
---|
227 | |
---|
228 | /** private: method[attributionMarkup] |
---|
229 | * :param attribution: ``Object`` The attribution property of the layer |
---|
230 | * object as parsed from a WMS Capabilities document |
---|
231 | * :return: ``String`` HTML markup to display attribution |
---|
232 | * information. |
---|
233 | * |
---|
234 | * Generates attribution markup using the Attribution metadata |
---|
235 | * from WMS Capabilities |
---|
236 | */ |
---|
237 | attributionMarkup : function(attribution){ |
---|
238 | var markup = []; |
---|
239 | |
---|
240 | if (attribution.logo){ |
---|
241 | markup.push("<img class='"+this.attributionCls+"-image' " |
---|
242 | + "src='" + attribution.logo.href + "' />"); |
---|
243 | } |
---|
244 | |
---|
245 | if (attribution.title) { |
---|
246 | markup.push("<span class='"+ this.attributionCls + "-title'>" |
---|
247 | + attribution.title |
---|
248 | + "</span>"); |
---|
249 | } |
---|
250 | |
---|
251 | if(attribution.href){ |
---|
252 | for(var i = 0; i < markup.length; i++){ |
---|
253 | markup[i] = "<a class='" |
---|
254 | + this.attributionCls + "-link' " |
---|
255 | + "href=" |
---|
256 | + attribution.href |
---|
257 | + ">" |
---|
258 | + markup[i] |
---|
259 | + "</a>"; |
---|
260 | } |
---|
261 | } |
---|
262 | |
---|
263 | return markup.join(" "); |
---|
264 | } |
---|
265 | }); |
---|