[76] | 1 | /* Copyright (c) 2006-2010 by OpenLayers Contributors (see authors.txt for |
---|
| 2 | * full list of contributors). Published under the Clear BSD license. |
---|
| 3 | * See http://svn.openlayers.org/trunk/openlayers/license.txt for the |
---|
| 4 | * full text of the license. */ |
---|
| 5 | |
---|
| 6 | |
---|
| 7 | /** |
---|
| 8 | * @requires OpenLayers/Control.js |
---|
| 9 | * @requires OpenLayers/Handler/Click.js |
---|
| 10 | * @requires OpenLayers/Handler/Hover.js |
---|
| 11 | * @requires OpenLayers/Request.js |
---|
| 12 | * @requires OpenLayers/Format/WMSGetFeatureInfo.js |
---|
| 13 | */ |
---|
| 14 | |
---|
| 15 | /** |
---|
| 16 | * Class: OpenLayers.Control.WMTSGetFeatureInfo |
---|
| 17 | * The WMTSGetFeatureInfo control uses a WMTS query to get information about a |
---|
| 18 | * point on the map. The information may be in a display-friendly format |
---|
| 19 | * such as HTML, or a machine-friendly format such as GML, depending on the |
---|
| 20 | * server's capabilities and the client's configuration. This control |
---|
| 21 | * handles click or hover events, attempts to parse the results using an |
---|
| 22 | * OpenLayers.Format, and fires a 'getfeatureinfo' event for each layer |
---|
| 23 | * queried. |
---|
| 24 | * |
---|
| 25 | * Inherits from: |
---|
| 26 | * - <OpenLayers.Control> |
---|
| 27 | */ |
---|
| 28 | OpenLayers.Control.WMTSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, { |
---|
| 29 | |
---|
| 30 | /** |
---|
| 31 | * APIProperty: hover |
---|
| 32 | * {Boolean} Send GetFeatureInfo requests when mouse stops moving. |
---|
| 33 | * Default is false. |
---|
| 34 | */ |
---|
| 35 | hover: false, |
---|
| 36 | |
---|
| 37 | /** |
---|
| 38 | * Property: requestEncoding |
---|
| 39 | * {String} One of "KVP" or "REST". Only KVP encoding is supported at this |
---|
| 40 | * time. |
---|
| 41 | */ |
---|
| 42 | requestEncoding: "KVP", |
---|
| 43 | |
---|
| 44 | /** |
---|
| 45 | * APIProperty: drillDown |
---|
| 46 | * {Boolean} Drill down over all WMTS layers in the map. When |
---|
| 47 | * using drillDown mode, hover is not possible. A getfeatureinfo event |
---|
| 48 | * will be fired for each layer queried. |
---|
| 49 | */ |
---|
| 50 | drillDown: false, |
---|
| 51 | |
---|
| 52 | /** |
---|
| 53 | * APIProperty: maxFeatures |
---|
| 54 | * {Integer} Maximum number of features to return from a WMTS query. This |
---|
| 55 | * sets the feature_count parameter on WMTS GetFeatureInfo |
---|
| 56 | * requests. |
---|
| 57 | */ |
---|
| 58 | maxFeatures: 10, |
---|
| 59 | |
---|
| 60 | /** APIProperty: clickCallback |
---|
| 61 | * {String} The click callback to register in the |
---|
| 62 | * {<OpenLayers.Handler.Click>} object created when the hover |
---|
| 63 | * option is set to false. Default is "click". |
---|
| 64 | */ |
---|
| 65 | clickCallback: "click", |
---|
| 66 | |
---|
| 67 | /** |
---|
| 68 | * Property: layers |
---|
| 69 | * {Array(<OpenLayers.Layer.WMTS>)} The layers to query for feature info. |
---|
| 70 | * If omitted, all map WMTS layers will be considered. |
---|
| 71 | */ |
---|
| 72 | layers: null, |
---|
| 73 | |
---|
| 74 | /** |
---|
| 75 | * APIProperty: queryVisible |
---|
| 76 | * {Boolean} Filter out hidden layers when searching the map for layers to |
---|
| 77 | * query. Default is true. |
---|
| 78 | */ |
---|
| 79 | queryVisible: true, |
---|
| 80 | |
---|
| 81 | /** |
---|
| 82 | * Property: infoFormat |
---|
| 83 | * {String} The mimetype to request from the server |
---|
| 84 | */ |
---|
| 85 | infoFormat: 'text/html', |
---|
| 86 | |
---|
| 87 | /** |
---|
| 88 | * Property: vendorParams |
---|
| 89 | * {Object} Additional parameters that will be added to the request, for |
---|
| 90 | * WMTS implementations that support them. This could e.g. look like |
---|
| 91 | * (start code) |
---|
| 92 | * { |
---|
| 93 | * radius: 5 |
---|
| 94 | * } |
---|
| 95 | * (end) |
---|
| 96 | */ |
---|
| 97 | vendorParams: {}, |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Property: format |
---|
| 101 | * {<OpenLayers.Format>} A format for parsing GetFeatureInfo responses. |
---|
| 102 | * Default is <OpenLayers.Format.WMSGetFeatureInfo>. |
---|
| 103 | */ |
---|
| 104 | format: null, |
---|
| 105 | |
---|
| 106 | /** |
---|
| 107 | * Property: formatOptions |
---|
| 108 | * {Object} Optional properties to set on the format (if one is not provided |
---|
| 109 | * in the <format> property. |
---|
| 110 | */ |
---|
| 111 | formatOptions: null, |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * APIProperty: handlerOptions |
---|
| 115 | * {Object} Additional options for the handlers used by this control, e.g. |
---|
| 116 | * (start code) |
---|
| 117 | * { |
---|
| 118 | * "click": {delay: 100}, |
---|
| 119 | * "hover": {delay: 300} |
---|
| 120 | * } |
---|
| 121 | * (end) |
---|
| 122 | */ |
---|
| 123 | handlerOptions: null, |
---|
| 124 | |
---|
| 125 | /** |
---|
| 126 | * Property: handler |
---|
| 127 | * {Object} Reference to the <OpenLayers.Handler> for this control |
---|
| 128 | */ |
---|
| 129 | handler: null, |
---|
| 130 | |
---|
| 131 | /** |
---|
| 132 | * Property: hoverRequest |
---|
| 133 | * {<OpenLayers.Request>} contains the currently running hover request |
---|
| 134 | * (if any). |
---|
| 135 | */ |
---|
| 136 | hoverRequest: null, |
---|
| 137 | |
---|
| 138 | /** |
---|
| 139 | * Constant: EVENT_TYPES |
---|
| 140 | * |
---|
| 141 | * Supported event types (in addition to those from <OpenLayers.Control>): |
---|
| 142 | * beforegetfeatureinfo - Triggered before each request is sent. |
---|
| 143 | * The event object has an *xy* property with the position of the |
---|
| 144 | * mouse click or hover event that triggers the request and a *layer* |
---|
| 145 | * property referencing the layer about to be queried. If a listener |
---|
| 146 | * returns false, the request will not be issued. |
---|
| 147 | * getfeatureinfo - Triggered when a GetFeatureInfo response is received. |
---|
| 148 | * The event object has a *text* property with the body of the |
---|
| 149 | * response (String), a *features* property with an array of the |
---|
| 150 | * parsed features, an *xy* property with the position of the mouse |
---|
| 151 | * click or hover event that triggered the request, a *layer* property |
---|
| 152 | * referencing the layer queried and a *request* property with the |
---|
| 153 | * request itself. If drillDown is set to true, one event will be fired |
---|
| 154 | * for each layer queried. |
---|
| 155 | * exception - Triggered when a GetFeatureInfo request fails (with a |
---|
| 156 | * status other than 200) or whenparsing fails. Listeners will receive |
---|
| 157 | * an event with *request*, *xy*, and *layer* properties. In the case |
---|
| 158 | * of a parsing error, the event will also contain an *error* property. |
---|
| 159 | */ |
---|
| 160 | EVENT_TYPES: ["beforegetfeatureinfo", "getfeatureinfo", "exception"], |
---|
| 161 | |
---|
| 162 | /** |
---|
| 163 | * Property: pending |
---|
| 164 | * {Number} The number of pending requests. |
---|
| 165 | */ |
---|
| 166 | pending: 0, |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Constructor: <OpenLayers.Control.WMTSGetFeatureInfo> |
---|
| 170 | * |
---|
| 171 | * Parameters: |
---|
| 172 | * options - {Object} |
---|
| 173 | */ |
---|
| 174 | initialize: function(options) { |
---|
| 175 | // concatenate events specific to vector with those from the base |
---|
| 176 | this.EVENT_TYPES = |
---|
| 177 | OpenLayers.Control.WMTSGetFeatureInfo.prototype.EVENT_TYPES.concat( |
---|
| 178 | OpenLayers.Control.prototype.EVENT_TYPES |
---|
| 179 | ); |
---|
| 180 | |
---|
| 181 | options = options || {}; |
---|
| 182 | options.handlerOptions = options.handlerOptions || {}; |
---|
| 183 | |
---|
| 184 | OpenLayers.Control.prototype.initialize.apply(this, [options]); |
---|
| 185 | |
---|
| 186 | if (!this.format) { |
---|
| 187 | this.format = new OpenLayers.Format.WMSGetFeatureInfo( |
---|
| 188 | options.formatOptions |
---|
| 189 | ); |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | if (this.drillDown === true) { |
---|
| 193 | this.hover = false; |
---|
| 194 | } |
---|
| 195 | |
---|
| 196 | if (this.hover) { |
---|
| 197 | this.handler = new OpenLayers.Handler.Hover( |
---|
| 198 | this, { |
---|
| 199 | move: this.cancelHover, |
---|
| 200 | pause: this.getInfoForHover |
---|
| 201 | }, |
---|
| 202 | OpenLayers.Util.extend( |
---|
| 203 | this.handlerOptions.hover || {}, {delay: 250} |
---|
| 204 | ) |
---|
| 205 | ); |
---|
| 206 | } else { |
---|
| 207 | var callbacks = {}; |
---|
| 208 | callbacks[this.clickCallback] = this.getInfoForClick; |
---|
| 209 | this.handler = new OpenLayers.Handler.Click( |
---|
| 210 | this, callbacks, this.handlerOptions.click || {} |
---|
| 211 | ); |
---|
| 212 | } |
---|
| 213 | }, |
---|
| 214 | |
---|
| 215 | /** |
---|
| 216 | * Method: activate |
---|
| 217 | * Activates the control. |
---|
| 218 | * |
---|
| 219 | * Returns: |
---|
| 220 | * {Boolean} The control was effectively activated. |
---|
| 221 | */ |
---|
| 222 | activate: function () { |
---|
| 223 | if (!this.active) { |
---|
| 224 | this.handler.activate(); |
---|
| 225 | } |
---|
| 226 | return OpenLayers.Control.prototype.activate.apply( |
---|
| 227 | this, arguments |
---|
| 228 | ); |
---|
| 229 | }, |
---|
| 230 | |
---|
| 231 | /** |
---|
| 232 | * Method: deactivate |
---|
| 233 | * Deactivates the control. |
---|
| 234 | * |
---|
| 235 | * Returns: |
---|
| 236 | * {Boolean} The control was effectively deactivated. |
---|
| 237 | */ |
---|
| 238 | deactivate: function () { |
---|
| 239 | return OpenLayers.Control.prototype.deactivate.apply( |
---|
| 240 | this, arguments |
---|
| 241 | ); |
---|
| 242 | }, |
---|
| 243 | |
---|
| 244 | /** |
---|
| 245 | * Method: getInfoForClick |
---|
| 246 | * Called on click |
---|
| 247 | * |
---|
| 248 | * Parameters: |
---|
| 249 | * evt - {<OpenLayers.Event>} |
---|
| 250 | */ |
---|
| 251 | getInfoForClick: function(evt) { |
---|
| 252 | this.request(evt.xy, {}); |
---|
| 253 | }, |
---|
| 254 | |
---|
| 255 | /** |
---|
| 256 | * Method: getInfoForHover |
---|
| 257 | * Pause callback for the hover handler |
---|
| 258 | * |
---|
| 259 | * Parameters: |
---|
| 260 | * evt - {Object} |
---|
| 261 | */ |
---|
| 262 | getInfoForHover: function(evt) { |
---|
| 263 | this.request(evt.xy, {hover: true}); |
---|
| 264 | }, |
---|
| 265 | |
---|
| 266 | /** |
---|
| 267 | * Method: cancelHover |
---|
| 268 | * Cancel callback for the hover handler |
---|
| 269 | */ |
---|
| 270 | cancelHover: function() { |
---|
| 271 | if (this.hoverRequest) { |
---|
| 272 | --this.pending; |
---|
| 273 | if (this.pending <= 0) { |
---|
| 274 | OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); |
---|
| 275 | this.pending = 0; |
---|
| 276 | } |
---|
| 277 | this.hoverRequest.abort(); |
---|
| 278 | this.hoverRequest = null; |
---|
| 279 | } |
---|
| 280 | }, |
---|
| 281 | |
---|
| 282 | /** |
---|
| 283 | * Method: findLayers |
---|
| 284 | * Internal method to get the layers, independent of whether we are |
---|
| 285 | * inspecting the map or using a client-provided array |
---|
| 286 | */ |
---|
| 287 | findLayers: function() { |
---|
| 288 | var candidates = this.layers || this.map.layers; |
---|
| 289 | var layers = []; |
---|
| 290 | var layer; |
---|
| 291 | for (var i=candidates.length-1; i>=0; --i) { |
---|
| 292 | layer = candidates[i]; |
---|
| 293 | if (layer instanceof OpenLayers.Layer.WMTS && |
---|
| 294 | layer.requestEncoding === this.requestEncoding && |
---|
| 295 | (!this.queryVisible || layer.getVisibility())) { |
---|
| 296 | layers.push(layer); |
---|
| 297 | if (!this.drillDown || this.hover) { |
---|
| 298 | break; |
---|
| 299 | } |
---|
| 300 | } |
---|
| 301 | } |
---|
| 302 | return layers; |
---|
| 303 | }, |
---|
| 304 | |
---|
| 305 | /** |
---|
| 306 | * Method: buildRequestOptions |
---|
| 307 | * Build an object with the relevant options for the GetFeatureInfo request. |
---|
| 308 | * |
---|
| 309 | * Parameters: |
---|
| 310 | * layer - {<OpenLayers.Layer.WMTS>} A WMTS layer. |
---|
| 311 | * xy - {<OpenLayers.Pixel>} The position on the map where the |
---|
| 312 | * mouse event occurred. |
---|
| 313 | */ |
---|
| 314 | buildRequestOptions: function(layer, xy) { |
---|
| 315 | var loc = this.map.getLonLatFromPixel(xy); |
---|
| 316 | var getTileUrl = layer.getURL( |
---|
| 317 | new OpenLayers.Bounds(loc.lon, loc.lat, loc.lon, loc.lat) |
---|
| 318 | ); |
---|
| 319 | var params = OpenLayers.Util.getParameters(getTileUrl); |
---|
| 320 | var tileInfo = layer.getTileInfo(loc); |
---|
| 321 | OpenLayers.Util.extend(params, { |
---|
| 322 | service: "WMTS", |
---|
| 323 | version: layer.version, |
---|
| 324 | request: "GetFeatureInfo", |
---|
| 325 | infoFormat: this.infoFormat, |
---|
| 326 | i: tileInfo.i, |
---|
| 327 | j: tileInfo.j |
---|
| 328 | }); |
---|
| 329 | OpenLayers.Util.applyDefaults(params, this.vendorParams); |
---|
| 330 | return { |
---|
| 331 | url: layer.url instanceof Array ? layer.url[0] : layer.url, |
---|
| 332 | params: OpenLayers.Util.upperCaseObject(params), |
---|
| 333 | callback: function(request) { |
---|
| 334 | this.handleResponse(xy, request, layer); |
---|
| 335 | }, |
---|
| 336 | scope: this |
---|
| 337 | }; |
---|
| 338 | }, |
---|
| 339 | |
---|
| 340 | /** |
---|
| 341 | * Method: request |
---|
| 342 | * Sends a GetFeatureInfo request to the WMTS |
---|
| 343 | * |
---|
| 344 | * Parameters: |
---|
| 345 | * xy - {<OpenLayers.Pixel>} The position on the map where the mouse event |
---|
| 346 | * occurred. |
---|
| 347 | * options - {Object} additional options for this method. |
---|
| 348 | * |
---|
| 349 | * Valid options: |
---|
| 350 | * - *hover* {Boolean} true if we do the request for the hover handler |
---|
| 351 | */ |
---|
| 352 | request: function(xy, options) { |
---|
| 353 | options = options || {}; |
---|
| 354 | var layers = this.findLayers(); |
---|
| 355 | if (layers.length > 0) { |
---|
| 356 | var issue, layer; |
---|
| 357 | for (var i=0, len=layers.length; i<len; i++) { |
---|
| 358 | layer = layers[i]; |
---|
| 359 | issue = this.events.triggerEvent("beforegetfeatureinfo", { |
---|
| 360 | xy: xy, |
---|
| 361 | layer: layer |
---|
| 362 | }); |
---|
| 363 | if (issue !== false) { |
---|
| 364 | ++this.pending; |
---|
| 365 | var requestOptions = this.buildRequestOptions(layer, xy); |
---|
| 366 | var request = OpenLayers.Request.GET(requestOptions); |
---|
| 367 | if (options.hover === true) { |
---|
| 368 | this.hoverRequest = request; |
---|
| 369 | } |
---|
| 370 | } |
---|
| 371 | } |
---|
| 372 | if (this.pending > 0) { |
---|
| 373 | OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait"); |
---|
| 374 | } |
---|
| 375 | } |
---|
| 376 | }, |
---|
| 377 | |
---|
| 378 | /** |
---|
| 379 | * Method: handleResponse |
---|
| 380 | * Handler for the GetFeatureInfo response. |
---|
| 381 | * |
---|
| 382 | * Parameters: |
---|
| 383 | * xy - {<OpenLayers.Pixel>} The position on the map where the mouse event |
---|
| 384 | * occurred. |
---|
| 385 | * request - {XMLHttpRequest} The request object. |
---|
| 386 | * layer - {<OpenLayers.Layer.WMTS>} The queried layer. |
---|
| 387 | */ |
---|
| 388 | handleResponse: function(xy, request, layer) { |
---|
| 389 | --this.pending; |
---|
| 390 | if (this.pending <= 0) { |
---|
| 391 | OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait"); |
---|
| 392 | this.pending = 0; |
---|
| 393 | } |
---|
| 394 | if (request.status && (request.status < 200 || request.status >= 300)) { |
---|
| 395 | this.events.triggerEvent("exception", { |
---|
| 396 | xy: xy, |
---|
| 397 | request: request, |
---|
| 398 | layer: layer |
---|
| 399 | }); |
---|
| 400 | } else { |
---|
| 401 | var doc = request.responseXML; |
---|
| 402 | if (!doc || !doc.documentElement) { |
---|
| 403 | doc = request.responseText; |
---|
| 404 | } |
---|
| 405 | var features, except; |
---|
| 406 | try { |
---|
| 407 | features = this.format.read(doc); |
---|
| 408 | } catch (error) { |
---|
| 409 | except = true; |
---|
| 410 | this.events.triggerEvent("exception", { |
---|
| 411 | xy: xy, |
---|
| 412 | request: request, |
---|
| 413 | error: error, |
---|
| 414 | layer: layer |
---|
| 415 | }); |
---|
| 416 | } |
---|
| 417 | if (!except) { |
---|
| 418 | this.events.triggerEvent("getfeatureinfo", { |
---|
| 419 | text: request.responseText, |
---|
| 420 | features: features, |
---|
| 421 | request: request, |
---|
| 422 | xy: xy, |
---|
| 423 | layer: layer |
---|
| 424 | }); |
---|
| 425 | } |
---|
| 426 | } |
---|
| 427 | }, |
---|
| 428 | |
---|
| 429 | /** |
---|
| 430 | * Method: setMap |
---|
| 431 | * Set the map property for the control. |
---|
| 432 | * |
---|
| 433 | * Parameters: |
---|
| 434 | * map - {<OpenLayers.Map>} |
---|
| 435 | */ |
---|
| 436 | setMap: function(map) { |
---|
| 437 | this.handler.setMap(map); |
---|
| 438 | OpenLayers.Control.prototype.setMap.apply(this, arguments); |
---|
| 439 | }, |
---|
| 440 | |
---|
| 441 | CLASS_NAME: "OpenLayers.Control.WMTSGetFeatureInfo" |
---|
| 442 | }); |
---|