1
|
/* Copyright (c) 2006-2013 by OpenLayers Contributors (see authors.txt for
|
2
|
* full list of contributors). Published under the 2-clause BSD license.
|
3
|
* See license.txt in the OpenLayers distribution or repository 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.WMSGetFeatureInfo
|
17
|
* The WMSGetFeatureInfo control uses a WMS query to get information about a point on the map. The
|
18
|
* information may be in a display-friendly format such as HTML, or a machine-friendly format such
|
19
|
* as GML, depending on the server's capabilities and the client's configuration. This control
|
20
|
* handles click or hover events, attempts to parse the results using an OpenLayers.Format, and
|
21
|
* fires a 'getfeatureinfo' event with the click position, the raw body of the response, and an
|
22
|
* array of features if it successfully read the response.
|
23
|
*
|
24
|
* Inherits from:
|
25
|
* - <OpenLayers.Control>
|
26
|
*/
|
27
|
OpenLayers.Control.WMSGetFeatureInfo = OpenLayers.Class(OpenLayers.Control, {
|
28
|
|
29
|
/**
|
30
|
* APIProperty: hover
|
31
|
* {Boolean} Send GetFeatureInfo requests when mouse stops moving.
|
32
|
* Default is false.
|
33
|
*/
|
34
|
hover: false,
|
35
|
|
36
|
/**
|
37
|
* APIProperty: drillDown
|
38
|
* {Boolean} Drill down over all WMS layers in the map. When
|
39
|
* using drillDown mode, hover is not possible, and an infoFormat that
|
40
|
* returns parseable features is required. Default is false.
|
41
|
*/
|
42
|
drillDown: false,
|
43
|
|
44
|
/**
|
45
|
* APIProperty: maxFeatures
|
46
|
* {Integer} Maximum number of features to return from a WMS query. This
|
47
|
* sets the feature_count parameter on WMS GetFeatureInfo
|
48
|
* requests.
|
49
|
*/
|
50
|
maxFeatures: 10,
|
51
|
|
52
|
/**
|
53
|
* APIProperty: clickCallback
|
54
|
* {String} The click callback to register in the
|
55
|
* {<OpenLayers.Handler.Click>} object created when the hover
|
56
|
* option is set to false. Default is "click".
|
57
|
*/
|
58
|
clickCallback: "click",
|
59
|
|
60
|
/**
|
61
|
* APIProperty: output
|
62
|
* {String} Either "features" or "object". When triggering a getfeatureinfo
|
63
|
* request should we pass on an array of features or an object with with
|
64
|
* a "features" property and other properties (such as the url of the
|
65
|
* WMS). Default is "features".
|
66
|
*/
|
67
|
output: "features",
|
68
|
|
69
|
/**
|
70
|
* APIProperty: layers
|
71
|
* {Array(<OpenLayers.Layer.WMS>)} The layers to query for feature info.
|
72
|
* If omitted, all map WMS layers with a url that matches this <url> or
|
73
|
* <layerUrls> will be considered.
|
74
|
*/
|
75
|
layers: null,
|
76
|
|
77
|
/**
|
78
|
* APIProperty: queryVisible
|
79
|
* {Boolean} If true, filter out hidden layers when searching the map for
|
80
|
* layers to query. Default is false.
|
81
|
*/
|
82
|
queryVisible: false,
|
83
|
|
84
|
/**
|
85
|
* APIProperty: url
|
86
|
* {String} The URL of the WMS service to use. If not provided, the url
|
87
|
* of the first eligible layer will be used.
|
88
|
*/
|
89
|
url: null,
|
90
|
|
91
|
/**
|
92
|
* APIProperty: layerUrls
|
93
|
* {Array(String)} Optional list of urls for layers that should be queried.
|
94
|
* This can be used when the layer url differs from the url used for
|
95
|
* making GetFeatureInfo requests (in the case of a layer using cached
|
96
|
* tiles).
|
97
|
*/
|
98
|
layerUrls: null,
|
99
|
|
100
|
/**
|
101
|
* APIProperty: infoFormat
|
102
|
* {String} The mimetype to request from the server. If you are using
|
103
|
* drillDown mode and have multiple servers that do not share a common
|
104
|
* infoFormat, you can override the control's infoFormat by providing an
|
105
|
* INFO_FORMAT parameter in your <OpenLayers.Layer.WMS> instance(s).
|
106
|
*/
|
107
|
infoFormat: 'text/html',
|
108
|
|
109
|
/**
|
110
|
* APIProperty: vendorParams
|
111
|
* {Object} Additional parameters that will be added to the request, for
|
112
|
* WMS implementations that support them. This could e.g. look like
|
113
|
* (start code)
|
114
|
* {
|
115
|
* radius: 5
|
116
|
* }
|
117
|
* (end)
|
118
|
*/
|
119
|
vendorParams: {},
|
120
|
|
121
|
/**
|
122
|
* APIProperty: format
|
123
|
* {<OpenLayers.Format>} A format for parsing GetFeatureInfo responses.
|
124
|
* Default is <OpenLayers.Format.WMSGetFeatureInfo>.
|
125
|
*/
|
126
|
format: null,
|
127
|
|
128
|
/**
|
129
|
* APIProperty: formatOptions
|
130
|
* {Object} Optional properties to set on the format (if one is not provided
|
131
|
* in the <format> property.
|
132
|
*/
|
133
|
formatOptions: null,
|
134
|
|
135
|
/**
|
136
|
* APIProperty: handlerOptions
|
137
|
* {Object} Additional options for the handlers used by this control, e.g.
|
138
|
* (start code)
|
139
|
* {
|
140
|
* "click": {delay: 100},
|
141
|
* "hover": {delay: 300}
|
142
|
* }
|
143
|
* (end)
|
144
|
*/
|
145
|
|
146
|
/**
|
147
|
* Property: handler
|
148
|
* {Object} Reference to the <OpenLayers.Handler> for this control
|
149
|
*/
|
150
|
handler: null,
|
151
|
|
152
|
/**
|
153
|
* Property: hoverRequest
|
154
|
* {<OpenLayers.Request>} contains the currently running hover request
|
155
|
* (if any).
|
156
|
*/
|
157
|
hoverRequest: null,
|
158
|
|
159
|
/**
|
160
|
* APIProperty: events
|
161
|
* {<OpenLayers.Events>} Events instance for listeners and triggering
|
162
|
* control specific events.
|
163
|
*
|
164
|
* Register a listener for a particular event with the following syntax:
|
165
|
* (code)
|
166
|
* control.events.register(type, obj, listener);
|
167
|
* (end)
|
168
|
*
|
169
|
* Supported event types (in addition to those from <OpenLayers.Control.events>):
|
170
|
* beforegetfeatureinfo - Triggered before the request is sent.
|
171
|
* The event object has an *xy* property with the position of the
|
172
|
* mouse click or hover event that triggers the request.
|
173
|
* nogetfeatureinfo - no queryable layers were found.
|
174
|
* getfeatureinfo - Triggered when a GetFeatureInfo response is received.
|
175
|
* The event object has a *text* property with the body of the
|
176
|
* response (String), a *features* property with an array of the
|
177
|
* parsed features, an *xy* property with the position of the mouse
|
178
|
* click or hover event that triggered the request, and a *request*
|
179
|
* property with the request itself. If drillDown is set to true and
|
180
|
* multiple requests were issued to collect feature info from all
|
181
|
* layers, *text* and *request* will only contain the response body
|
182
|
* and request object of the last request.
|
183
|
*/
|
184
|
|
185
|
/**
|
186
|
* Constructor: <OpenLayers.Control.WMSGetFeatureInfo>
|
187
|
*
|
188
|
* Parameters:
|
189
|
* options - {Object}
|
190
|
*/
|
191
|
initialize: function(options) {
|
192
|
options = options || {};
|
193
|
options.handlerOptions = options.handlerOptions || {};
|
194
|
|
195
|
OpenLayers.Control.prototype.initialize.apply(this, [options]);
|
196
|
|
197
|
if(!this.format) {
|
198
|
this.format = new OpenLayers.Format.WMSGetFeatureInfo(
|
199
|
options.formatOptions
|
200
|
);
|
201
|
}
|
202
|
|
203
|
if(this.drillDown === true) {
|
204
|
this.hover = false;
|
205
|
}
|
206
|
|
207
|
if(this.hover) {
|
208
|
this.handler = new OpenLayers.Handler.Hover(
|
209
|
this, {
|
210
|
'move': this.cancelHover,
|
211
|
'pause': this.getInfoForHover
|
212
|
},
|
213
|
OpenLayers.Util.extend(this.handlerOptions.hover || {}, {
|
214
|
'delay': 250
|
215
|
}));
|
216
|
} else {
|
217
|
var callbacks = {};
|
218
|
callbacks[this.clickCallback] = this.getInfoForClick;
|
219
|
this.handler = new OpenLayers.Handler.Click(
|
220
|
this, callbacks, this.handlerOptions.click || {});
|
221
|
}
|
222
|
},
|
223
|
|
224
|
/**
|
225
|
* Method: getInfoForClick
|
226
|
* Called on click
|
227
|
*
|
228
|
* Parameters:
|
229
|
* evt - {<OpenLayers.Event>}
|
230
|
*/
|
231
|
getInfoForClick: function(evt) {
|
232
|
this.events.triggerEvent("beforegetfeatureinfo", {xy: evt.xy});
|
233
|
// Set the cursor to "wait" to tell the user we're working on their
|
234
|
// click.
|
235
|
OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait");
|
236
|
this.request(evt.xy, {});
|
237
|
},
|
238
|
|
239
|
/**
|
240
|
* Method: getInfoForHover
|
241
|
* Pause callback for the hover handler
|
242
|
*
|
243
|
* Parameters:
|
244
|
* evt - {Object}
|
245
|
*/
|
246
|
getInfoForHover: function(evt) {
|
247
|
this.events.triggerEvent("beforegetfeatureinfo", {xy: evt.xy});
|
248
|
this.request(evt.xy, {hover: true});
|
249
|
},
|
250
|
|
251
|
/**
|
252
|
* Method: cancelHover
|
253
|
* Cancel callback for the hover handler
|
254
|
*/
|
255
|
cancelHover: function() {
|
256
|
if (this.hoverRequest) {
|
257
|
this.hoverRequest.abort();
|
258
|
this.hoverRequest = null;
|
259
|
}
|
260
|
},
|
261
|
|
262
|
/**
|
263
|
* Method: findLayers
|
264
|
* Internal method to get the layers, independent of whether we are
|
265
|
* inspecting the map or using a client-provided array
|
266
|
*/
|
267
|
findLayers: function() {
|
268
|
|
269
|
var candidates = this.layers || this.map.layers;
|
270
|
var layers = [];
|
271
|
var layer, url;
|
272
|
for(var i = candidates.length - 1; i >= 0; --i) {
|
273
|
layer = candidates[i];
|
274
|
if(layer instanceof OpenLayers.Layer.WMS &&
|
275
|
(!this.queryVisible || layer.getVisibility())) {
|
276
|
url = OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url;
|
277
|
// if the control was not configured with a url, set it
|
278
|
// to the first layer url
|
279
|
if(this.drillDown === false && !this.url) {
|
280
|
this.url = url;
|
281
|
}
|
282
|
if(this.drillDown === true || this.urlMatches(url)) {
|
283
|
layers.push(layer);
|
284
|
}
|
285
|
}
|
286
|
}
|
287
|
return layers;
|
288
|
},
|
289
|
|
290
|
/**
|
291
|
* Method: urlMatches
|
292
|
* Test to see if the provided url matches either the control <url> or one
|
293
|
* of the <layerUrls>.
|
294
|
*
|
295
|
* Parameters:
|
296
|
* url - {String} The url to test.
|
297
|
*
|
298
|
* Returns:
|
299
|
* {Boolean} The provided url matches the control <url> or one of the
|
300
|
* <layerUrls>.
|
301
|
*/
|
302
|
urlMatches: function(url) {
|
303
|
var matches = OpenLayers.Util.isEquivalentUrl(this.url, url);
|
304
|
if(!matches && this.layerUrls) {
|
305
|
for(var i=0, len=this.layerUrls.length; i<len; ++i) {
|
306
|
if(OpenLayers.Util.isEquivalentUrl(this.layerUrls[i], url)) {
|
307
|
matches = true;
|
308
|
break;
|
309
|
}
|
310
|
}
|
311
|
}
|
312
|
return matches;
|
313
|
},
|
314
|
|
315
|
/**
|
316
|
* Method: buildWMSOptions
|
317
|
* Build an object with the relevant WMS options for the GetFeatureInfo request
|
318
|
*
|
319
|
* Parameters:
|
320
|
* url - {String} The url to be used for sending the request
|
321
|
* layers - {Array(<OpenLayers.Layer.WMS)} An array of layers
|
322
|
* clickPosition - {<OpenLayers.Pixel>} The position on the map where the mouse
|
323
|
* event occurred.
|
324
|
* format - {String} The format from the corresponding GetMap request
|
325
|
*/
|
326
|
buildWMSOptions: function(url, layers, clickPosition, format) {
|
327
|
var layerNames = [], styleNames = [];
|
328
|
for (var i = 0, len = layers.length; i < len; i++) {
|
329
|
if (layers[i].params.LAYERS != null) {
|
330
|
layerNames = layerNames.concat(layers[i].params.LAYERS);
|
331
|
styleNames = styleNames.concat(this.getStyleNames(layers[i]));
|
332
|
}
|
333
|
}
|
334
|
var firstLayer = layers[0];
|
335
|
// use the firstLayer's projection if it matches the map projection -
|
336
|
// this assumes that all layers will be available in this projection
|
337
|
var projection = this.map.getProjection();
|
338
|
var layerProj = firstLayer.projection;
|
339
|
if (layerProj && layerProj.equals(this.map.getProjectionObject())) {
|
340
|
projection = layerProj.getCode();
|
341
|
}
|
342
|
var params = OpenLayers.Util.extend({
|
343
|
service: "WMS",
|
344
|
version: firstLayer.params.VERSION,
|
345
|
request: "GetFeatureInfo",
|
346
|
exceptions: firstLayer.params.EXCEPTIONS,
|
347
|
bbox: this.map.getExtent().toBBOX(null,
|
348
|
firstLayer.reverseAxisOrder()),
|
349
|
feature_count: this.maxFeatures,
|
350
|
height: this.map.getSize().h,
|
351
|
width: this.map.getSize().w,
|
352
|
format: format,
|
353
|
info_format: firstLayer.params.INFO_FORMAT || this.infoFormat
|
354
|
}, (parseFloat(firstLayer.params.VERSION) >= 1.3) ?
|
355
|
{
|
356
|
crs: projection,
|
357
|
i: parseInt(clickPosition.x),
|
358
|
j: parseInt(clickPosition.y)
|
359
|
} :
|
360
|
{
|
361
|
srs: projection,
|
362
|
x: parseInt(clickPosition.x),
|
363
|
y: parseInt(clickPosition.y)
|
364
|
}
|
365
|
);
|
366
|
if (layerNames.length != 0) {
|
367
|
params = OpenLayers.Util.extend({
|
368
|
layers: layerNames,
|
369
|
query_layers: layerNames,
|
370
|
styles: styleNames
|
371
|
}, params);
|
372
|
}
|
373
|
OpenLayers.Util.applyDefaults(params, this.vendorParams);
|
374
|
return {
|
375
|
url: url,
|
376
|
params: OpenLayers.Util.upperCaseObject(params),
|
377
|
callback: function(request) {
|
378
|
this.handleResponse(clickPosition, request, url);
|
379
|
},
|
380
|
scope: this
|
381
|
};
|
382
|
},
|
383
|
|
384
|
/**
|
385
|
* Method: getStyleNames
|
386
|
* Gets the STYLES parameter for the layer. Make sure the STYLES parameter
|
387
|
* matches the LAYERS parameter
|
388
|
*
|
389
|
* Parameters:
|
390
|
* layer - {<OpenLayers.Layer.WMS>}
|
391
|
*
|
392
|
* Returns:
|
393
|
* {Array(String)} The STYLES parameter
|
394
|
*/
|
395
|
getStyleNames: function(layer) {
|
396
|
// in the event of a WMS layer bundling multiple layers but not
|
397
|
// specifying styles,we need the same number of commas to specify
|
398
|
// the default style for each of the layers. We can't just leave it
|
399
|
// blank as we may be including other layers that do specify styles.
|
400
|
var styleNames;
|
401
|
if (layer.params.STYLES) {
|
402
|
styleNames = layer.params.STYLES;
|
403
|
} else {
|
404
|
if (OpenLayers.Util.isArray(layer.params.LAYERS)) {
|
405
|
styleNames = new Array(layer.params.LAYERS.length);
|
406
|
} else { // Assume it's a String
|
407
|
styleNames = layer.params.LAYERS.replace(/[^,]/g, "");
|
408
|
}
|
409
|
}
|
410
|
return styleNames;
|
411
|
},
|
412
|
|
413
|
/**
|
414
|
* Method: request
|
415
|
* Sends a GetFeatureInfo request to the WMS
|
416
|
*
|
417
|
* Parameters:
|
418
|
* clickPosition - {<OpenLayers.Pixel>} The position on the map where the
|
419
|
* mouse event occurred.
|
420
|
* options - {Object} additional options for this method.
|
421
|
*
|
422
|
* Valid options:
|
423
|
* - *hover* {Boolean} true if we do the request for the hover handler
|
424
|
*/
|
425
|
request: function(clickPosition, options) {
|
426
|
var layers = this.findLayers();
|
427
|
if(layers.length == 0) {
|
428
|
this.events.triggerEvent("nogetfeatureinfo");
|
429
|
// Reset the cursor.
|
430
|
OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait");
|
431
|
return;
|
432
|
}
|
433
|
|
434
|
options = options || {};
|
435
|
if(this.drillDown === false) {
|
436
|
var wmsOptions = this.buildWMSOptions(this.url, layers,
|
437
|
clickPosition, layers[0].params.FORMAT);
|
438
|
var request = OpenLayers.Request.GET(wmsOptions);
|
439
|
|
440
|
if (options.hover === true) {
|
441
|
this.hoverRequest = request;
|
442
|
}
|
443
|
} else {
|
444
|
this._requestCount = 0;
|
445
|
this._numRequests = 0;
|
446
|
this.features = [];
|
447
|
// group according to service url to combine requests
|
448
|
var services = {}, url;
|
449
|
for(var i=0, len=layers.length; i<len; i++) {
|
450
|
var layer = layers[i];
|
451
|
var service, found = false;
|
452
|
url = OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url;
|
453
|
if(url in services) {
|
454
|
services[url].push(layer);
|
455
|
} else {
|
456
|
this._numRequests++;
|
457
|
services[url] = [layer];
|
458
|
}
|
459
|
}
|
460
|
var layers;
|
461
|
for (var url in services) {
|
462
|
layers = services[url];
|
463
|
var wmsOptions = this.buildWMSOptions(url, layers,
|
464
|
clickPosition, layers[0].params.FORMAT);
|
465
|
OpenLayers.Request.GET(wmsOptions);
|
466
|
}
|
467
|
}
|
468
|
},
|
469
|
|
470
|
/**
|
471
|
* Method: triggerGetFeatureInfo
|
472
|
* Trigger the getfeatureinfo event when all is done
|
473
|
*
|
474
|
* Parameters:
|
475
|
* request - {XMLHttpRequest} The request object
|
476
|
* xy - {<OpenLayers.Pixel>} The position on the map where the
|
477
|
* mouse event occurred.
|
478
|
* features - {Array(<OpenLayers.Feature.Vector>)} or
|
479
|
* {Array({Object}) when output is "object". The object has a url and a
|
480
|
* features property which contains an array of features.
|
481
|
*/
|
482
|
triggerGetFeatureInfo: function(request, xy, features) {
|
483
|
this.events.triggerEvent("getfeatureinfo", {
|
484
|
text: request.responseText,
|
485
|
features: features,
|
486
|
request: request,
|
487
|
xy: xy
|
488
|
});
|
489
|
|
490
|
// Reset the cursor.
|
491
|
OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait");
|
492
|
},
|
493
|
|
494
|
/**
|
495
|
* Method: handleResponse
|
496
|
* Handler for the GetFeatureInfo response.
|
497
|
*
|
498
|
* Parameters:
|
499
|
* xy - {<OpenLayers.Pixel>} The position on the map where the
|
500
|
* mouse event occurred.
|
501
|
* request - {XMLHttpRequest} The request object.
|
502
|
* url - {String} The url which was used for this request.
|
503
|
*/
|
504
|
handleResponse: function(xy, request, url) {
|
505
|
|
506
|
var doc = request.responseXML;
|
507
|
if(!doc || !doc.documentElement) {
|
508
|
doc = request.responseText;
|
509
|
}
|
510
|
var features = this.format.read(doc);
|
511
|
if (this.drillDown === false) {
|
512
|
this.triggerGetFeatureInfo(request, xy, features);
|
513
|
} else {
|
514
|
this._requestCount++;
|
515
|
if (this.output === "object") {
|
516
|
this._features = (this._features || []).concat(
|
517
|
{url: url, features: features}
|
518
|
);
|
519
|
} else {
|
520
|
this._features = (this._features || []).concat(features);
|
521
|
}
|
522
|
if (this._requestCount === this._numRequests) {
|
523
|
this.triggerGetFeatureInfo(request, xy, this._features.concat());
|
524
|
delete this._features;
|
525
|
delete this._requestCount;
|
526
|
delete this._numRequests;
|
527
|
}
|
528
|
}
|
529
|
},
|
530
|
|
531
|
CLASS_NAME: "OpenLayers.Control.WMSGetFeatureInfo"
|
532
|
});
|