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.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
|
|
124
|
/**
|
125
|
* Property: handler
|
126
|
* {Object} Reference to the <OpenLayers.Handler> for this control
|
127
|
*/
|
128
|
handler: null,
|
129
|
|
130
|
/**
|
131
|
* Property: hoverRequest
|
132
|
* {<OpenLayers.Request>} contains the currently running hover request
|
133
|
* (if any).
|
134
|
*/
|
135
|
hoverRequest: null,
|
136
|
|
137
|
/**
|
138
|
* APIProperty: events
|
139
|
* {<OpenLayers.Events>} Events instance for listeners and triggering
|
140
|
* control specific events.
|
141
|
*
|
142
|
* Register a listener for a particular event with the following syntax:
|
143
|
* (code)
|
144
|
* control.events.register(type, obj, listener);
|
145
|
* (end)
|
146
|
*
|
147
|
* Supported event types (in addition to those from <OpenLayers.Control.events>):
|
148
|
* beforegetfeatureinfo - Triggered before each request is sent.
|
149
|
* The event object has an *xy* property with the position of the
|
150
|
* mouse click or hover event that triggers the request and a *layer*
|
151
|
* property referencing the layer about to be queried. If a listener
|
152
|
* returns false, the request will not be issued.
|
153
|
* getfeatureinfo - Triggered when a GetFeatureInfo response is received.
|
154
|
* The event object has a *text* property with the body of the
|
155
|
* response (String), a *features* property with an array of the
|
156
|
* parsed features, an *xy* property with the position of the mouse
|
157
|
* click or hover event that triggered the request, a *layer* property
|
158
|
* referencing the layer queried and a *request* property with the
|
159
|
* request itself. If drillDown is set to true, one event will be fired
|
160
|
* for each layer queried.
|
161
|
* exception - Triggered when a GetFeatureInfo request fails (with a
|
162
|
* status other than 200) or whenparsing fails. Listeners will receive
|
163
|
* an event with *request*, *xy*, and *layer* properties. In the case
|
164
|
* of a parsing error, the event will also contain an *error* property.
|
165
|
*/
|
166
|
|
167
|
/**
|
168
|
* Property: pending
|
169
|
* {Number} The number of pending requests.
|
170
|
*/
|
171
|
pending: 0,
|
172
|
|
173
|
/**
|
174
|
* Constructor: <OpenLayers.Control.WMTSGetFeatureInfo>
|
175
|
*
|
176
|
* Parameters:
|
177
|
* options - {Object}
|
178
|
*/
|
179
|
initialize: function(options) {
|
180
|
options = options || {};
|
181
|
options.handlerOptions = options.handlerOptions || {};
|
182
|
|
183
|
OpenLayers.Control.prototype.initialize.apply(this, [options]);
|
184
|
|
185
|
if (!this.format) {
|
186
|
this.format = new OpenLayers.Format.WMSGetFeatureInfo(
|
187
|
options.formatOptions
|
188
|
);
|
189
|
}
|
190
|
|
191
|
if (this.drillDown === true) {
|
192
|
this.hover = false;
|
193
|
}
|
194
|
|
195
|
if (this.hover) {
|
196
|
this.handler = new OpenLayers.Handler.Hover(
|
197
|
this, {
|
198
|
move: this.cancelHover,
|
199
|
pause: this.getInfoForHover
|
200
|
},
|
201
|
OpenLayers.Util.extend(
|
202
|
this.handlerOptions.hover || {}, {delay: 250}
|
203
|
)
|
204
|
);
|
205
|
} else {
|
206
|
var callbacks = {};
|
207
|
callbacks[this.clickCallback] = this.getInfoForClick;
|
208
|
this.handler = new OpenLayers.Handler.Click(
|
209
|
this, callbacks, this.handlerOptions.click || {}
|
210
|
);
|
211
|
}
|
212
|
},
|
213
|
|
214
|
/**
|
215
|
* Method: getInfoForClick
|
216
|
* Called on click
|
217
|
*
|
218
|
* Parameters:
|
219
|
* evt - {<OpenLayers.Event>}
|
220
|
*/
|
221
|
getInfoForClick: function(evt) {
|
222
|
this.request(evt.xy, {});
|
223
|
},
|
224
|
|
225
|
/**
|
226
|
* Method: getInfoForHover
|
227
|
* Pause callback for the hover handler
|
228
|
*
|
229
|
* Parameters:
|
230
|
* evt - {Object}
|
231
|
*/
|
232
|
getInfoForHover: function(evt) {
|
233
|
this.request(evt.xy, {hover: true});
|
234
|
},
|
235
|
|
236
|
/**
|
237
|
* Method: cancelHover
|
238
|
* Cancel callback for the hover handler
|
239
|
*/
|
240
|
cancelHover: function() {
|
241
|
if (this.hoverRequest) {
|
242
|
--this.pending;
|
243
|
if (this.pending <= 0) {
|
244
|
OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait");
|
245
|
this.pending = 0;
|
246
|
}
|
247
|
this.hoverRequest.abort();
|
248
|
this.hoverRequest = null;
|
249
|
}
|
250
|
},
|
251
|
|
252
|
/**
|
253
|
* Method: findLayers
|
254
|
* Internal method to get the layers, independent of whether we are
|
255
|
* inspecting the map or using a client-provided array
|
256
|
*/
|
257
|
findLayers: function() {
|
258
|
var candidates = this.layers || this.map.layers;
|
259
|
var layers = [];
|
260
|
var layer;
|
261
|
for (var i=candidates.length-1; i>=0; --i) {
|
262
|
layer = candidates[i];
|
263
|
if (layer instanceof OpenLayers.Layer.WMTS &&
|
264
|
layer.requestEncoding === this.requestEncoding &&
|
265
|
(!this.queryVisible || layer.getVisibility())) {
|
266
|
layers.push(layer);
|
267
|
if (!this.drillDown || this.hover) {
|
268
|
break;
|
269
|
}
|
270
|
}
|
271
|
}
|
272
|
return layers;
|
273
|
},
|
274
|
|
275
|
/**
|
276
|
* Method: buildRequestOptions
|
277
|
* Build an object with the relevant options for the GetFeatureInfo request.
|
278
|
*
|
279
|
* Parameters:
|
280
|
* layer - {<OpenLayers.Layer.WMTS>} A WMTS layer.
|
281
|
* xy - {<OpenLayers.Pixel>} The position on the map where the
|
282
|
* mouse event occurred.
|
283
|
*/
|
284
|
buildRequestOptions: function(layer, xy) {
|
285
|
var loc = this.map.getLonLatFromPixel(xy);
|
286
|
var getTileUrl = layer.getURL(
|
287
|
new OpenLayers.Bounds(loc.lon, loc.lat, loc.lon, loc.lat)
|
288
|
);
|
289
|
var params = OpenLayers.Util.getParameters(getTileUrl);
|
290
|
var tileInfo = layer.getTileInfo(loc);
|
291
|
OpenLayers.Util.extend(params, {
|
292
|
service: "WMTS",
|
293
|
version: layer.version,
|
294
|
request: "GetFeatureInfo",
|
295
|
infoFormat: this.infoFormat,
|
296
|
i: tileInfo.i,
|
297
|
j: tileInfo.j
|
298
|
});
|
299
|
OpenLayers.Util.applyDefaults(params, this.vendorParams);
|
300
|
return {
|
301
|
url: OpenLayers.Util.isArray(layer.url) ? layer.url[0] : layer.url,
|
302
|
params: OpenLayers.Util.upperCaseObject(params),
|
303
|
callback: function(request) {
|
304
|
this.handleResponse(xy, request, layer);
|
305
|
},
|
306
|
scope: this
|
307
|
};
|
308
|
},
|
309
|
|
310
|
/**
|
311
|
* Method: request
|
312
|
* Sends a GetFeatureInfo request to the WMTS
|
313
|
*
|
314
|
* Parameters:
|
315
|
* xy - {<OpenLayers.Pixel>} The position on the map where the mouse event
|
316
|
* occurred.
|
317
|
* options - {Object} additional options for this method.
|
318
|
*
|
319
|
* Valid options:
|
320
|
* - *hover* {Boolean} true if we do the request for the hover handler
|
321
|
*/
|
322
|
request: function(xy, options) {
|
323
|
options = options || {};
|
324
|
var layers = this.findLayers();
|
325
|
if (layers.length > 0) {
|
326
|
var issue, layer;
|
327
|
for (var i=0, len=layers.length; i<len; i++) {
|
328
|
layer = layers[i];
|
329
|
issue = this.events.triggerEvent("beforegetfeatureinfo", {
|
330
|
xy: xy,
|
331
|
layer: layer
|
332
|
});
|
333
|
if (issue !== false) {
|
334
|
++this.pending;
|
335
|
var requestOptions = this.buildRequestOptions(layer, xy);
|
336
|
var request = OpenLayers.Request.GET(requestOptions);
|
337
|
if (options.hover === true) {
|
338
|
this.hoverRequest = request;
|
339
|
}
|
340
|
}
|
341
|
}
|
342
|
if (this.pending > 0) {
|
343
|
OpenLayers.Element.addClass(this.map.viewPortDiv, "olCursorWait");
|
344
|
}
|
345
|
}
|
346
|
},
|
347
|
|
348
|
/**
|
349
|
* Method: handleResponse
|
350
|
* Handler for the GetFeatureInfo response.
|
351
|
*
|
352
|
* Parameters:
|
353
|
* xy - {<OpenLayers.Pixel>} The position on the map where the mouse event
|
354
|
* occurred.
|
355
|
* request - {XMLHttpRequest} The request object.
|
356
|
* layer - {<OpenLayers.Layer.WMTS>} The queried layer.
|
357
|
*/
|
358
|
handleResponse: function(xy, request, layer) {
|
359
|
--this.pending;
|
360
|
if (this.pending <= 0) {
|
361
|
OpenLayers.Element.removeClass(this.map.viewPortDiv, "olCursorWait");
|
362
|
this.pending = 0;
|
363
|
}
|
364
|
if (request.status && (request.status < 200 || request.status >= 300)) {
|
365
|
this.events.triggerEvent("exception", {
|
366
|
xy: xy,
|
367
|
request: request,
|
368
|
layer: layer
|
369
|
});
|
370
|
} else {
|
371
|
var doc = request.responseXML;
|
372
|
if (!doc || !doc.documentElement) {
|
373
|
doc = request.responseText;
|
374
|
}
|
375
|
var features, except;
|
376
|
try {
|
377
|
features = this.format.read(doc);
|
378
|
} catch (error) {
|
379
|
except = true;
|
380
|
this.events.triggerEvent("exception", {
|
381
|
xy: xy,
|
382
|
request: request,
|
383
|
error: error,
|
384
|
layer: layer
|
385
|
});
|
386
|
}
|
387
|
if (!except) {
|
388
|
this.events.triggerEvent("getfeatureinfo", {
|
389
|
text: request.responseText,
|
390
|
features: features,
|
391
|
request: request,
|
392
|
xy: xy,
|
393
|
layer: layer
|
394
|
});
|
395
|
}
|
396
|
}
|
397
|
},
|
398
|
|
399
|
CLASS_NAME: "OpenLayers.Control.WMTSGetFeatureInfo"
|
400
|
});
|