forked from qgis/QGIS-Web-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FeatureInfoDisplay.js
executable file
·378 lines (359 loc) · 16.3 KB
/
FeatureInfoDisplay.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
*
* FeatureInfoDisplay.js -- part of Quantum GIS Web Client
*
* Copyright (2010-2012), The QGIS Project All rights reserved.
* Quantum GIS Web Client is released under a BSD license. Please see
* https://github.com/qgis/qgis-web-client/blob/master/README
* for the full text of the license and the list of contributors.
*
*/
/* FeatureInfos are presented to the user in two ways using OpenLayers.Popup classes:
* If the mouse stops and GetFeatureInfo has results for this mouse position
* a small box presents the contents of the info field (GetProjectSettings) or the
* field named "toolbox" (GetCapabilities), this is called hoverPopup throughout this script.
* If the user clicks in the map the contents of all visible fields (and if activated the wkt geometry)
* is presented in a popup called clickPopup throughout this script.
* hoverPopups are disabled when a clickPopup is open, however clicking at another position in the map
* closes the currently opened clickPopup and opens a new one (if there is GetFeatureInfo response).
* If the cursor is at a position where there is GetFeatureInfo response it indicates the possibility
* to click by changing to "hand".
*/
var featureInfoPopupContents;
var closePopupClick = false; // stores if the click results from closing a clickPopup
function showFeatureInfo(evt) {
if (identifyToolActive) {
if (!closePopupClick) {
var map = geoExtMap.map; // gets OL map object
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(evt.text, "text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(evt.text);
}
// open AttributeTree panel
featureInfoResultLayers = new Array();
highLightGeometry = new Array();
parseFIResult(xmlDoc);
featureInfoResultLayers.reverse();
highLightGeometry.reverse();
if (featureInfoResultLayers.length > 0) {
if (hoverPopup) {removeHoverPopup();}
if (clickPopup) {removeClickPopup();}
var text = "";
if (identificationMode == 'topMostHit') {
text += featureInfoResultLayers[0];
featureInfoHighlightLayer.addFeatures(highLightGeometry[0]);
//feature.geometry.getBounds().getCenterLonLat()
} else {
for (var i = 0; i < featureInfoResultLayers.length; i++) {
text += featureInfoResultLayers[i];
featureInfoHighlightLayer.addFeatures(highLightGeometry[i]);
}
}
clickPopup = new OpenLayers.Popup.FramedCloud(
null, // id
map.getLonLatFromPixel(evt.xy), // lonlat
null, //new OpenLayers.Size(1,1), // contentSize
text, //contentHTML
null, // anchor
true, // closeBox
onClickPopupClosed // closeBoxCallBackFunction
);
clickPopup.autoSize = true;
clickPopup.events.fallThrough = false;
map.addPopup(clickPopup); //*/
changeCursorInMap("default");
}
} else {
closePopupClick = false;
}
activateGetFeatureInfo(true);
}
}
function showFeatureInfoHover(evt) {
var map = geoExtMap.map; // gets OL map object
if (identifyToolActive) {
if (hoverPopup) {removeHoverPopup();}
if (window.DOMParser) {
var parser = new DOMParser();
xmlDoc = parser.parseFromString(evt.text, "text/xml");
} else {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = "false";
xmlDoc.loadXML(evt.text);
}
var layerNodes = xmlDoc.getElementsByTagName("Layer");
var text = '';
var result = false;
for (var i = layerNodes.length - 1; i > -1; --i) {
//case vector layers
var featureNodes = layerNodes[i].getElementsByTagName("Feature");
// show layer display field or if missing, the attribute 'tooltip'
var tooltipAttributeName = wmsLoader.layerProperties[layerNodes[i].getAttribute("name")].displayField || "tooltip";
for (var j = 0; j < featureNodes.length; ++j) {
if (j == 0) {
if (showHoverLayerTitle) {
text += '<h2 class="hoverLayerTitle">' + wmsLoader.layerProperties[layerNodes[i].getAttribute("name")].title + '</h2>';
}
result = true;
}
var attribNodes = featureNodes[j].getElementsByTagName("Attribute");
var attributesDict = {};
for (var k = 0; k < attribNodes.length; ++k) {
attributesDict[attribNodes[k].getAttribute("name")] = attribNodes[k].getAttribute("value");
}
var tooltipFieldAvailable = attributesDict.hasOwnProperty(tooltipAttributeName);
var geometryFieldAvailable = attributesDict.hasOwnProperty('geometry');
if (tooltipFieldAvailable) {
var aValue = attributesDict[tooltipAttributeName]
if (aValue.match(/</)) {
text += aValue;
}
else {
attribText = '<p>' + aValue.replace(/\n/, "<br/>");
attribText = attribText.replace("\n", "<br/>");
text += attribText + '</p>';
}
text += '<hr class="hrHoverLayer"/>';
}
else if (tooltipTemplates && tooltipTemplates.hasOwnProperty(layerNodes[i].getAttribute("name"))){
templateText = tooltipTemplates[layerNodes[i].getAttribute("name")].template;
tooltipText = templateText.replace(/<%(\w*)%>/g,function(m,key){
var value = attributesDict.hasOwnProperty(key) ? attributesDict[key] : "";
return value.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
})
text += tooltipText+"<br/>";
} else if (tooltipAttributeName.indexOf('[%') !== -1){ // Look into displayField for template tags...
var tooltipText = tooltipAttributeName;
var re = new RegExp(/\[%[^"]*"(.*?)"[^"]*%\]/g);
var ttmatch;
while(ttmatch = re.exec(tooltipAttributeName)){
var key = ttmatch[1];
var val = attributesDict.hasOwnProperty(key) ? attributesDict[key] : "";
tooltipText = tooltipText.replace(ttmatch[0], val);
}
text += tooltipText+"<br/>";
}
if (geometryFieldAvailable) {
var feature = new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT(attributesDict["geometry"]));
featureInfoHighlightLayer.addFeatures([feature]);
}
}
//case raster layers
var rasterAttributeNodes = Array();
var rasterLayerChildNode = layerNodes[i].firstChild;
while (rasterLayerChildNode) {
if (rasterLayerChildNode.nodeName == "Attribute") {
rasterAttributeNodes.push(rasterLayerChildNode);
}
rasterLayerChildNode = rasterLayerChildNode.nextSibling;
}
for (var j = 0; j < rasterAttributeNodes.length; ++j) {
if (j == 0) {
if (showHoverLayerTitle) {
text += '<h2 class="hoverLayerTitle">' + wmsLoader.layerProperties[layerNodes[i].getAttribute("name")].title + '</h2>';
}
result = true;
}
text += '<p>'+rasterAttributeNodes[j].getAttribute("name")+": "+rasterAttributeNodes[j].getAttribute("value")+'</p>';
text += '<hr class="hrHoverLayer"/>';
}
if (identificationMode == 'topMostHit' && result) {
break;
}
}
if (result) {
changeCursorInMap("pointer");
if (!clickPopup) {
// only show hoverPopup if no clickPopup is open
//get rid of last <hr/>
text = text.replace(/<hr class="hrHoverLayer"\/>$/,'');
hoverPopup = new OpenLayers.Popup.FramedCloud(
null, // id
map.getLonLatFromPixel(evt.xy), // lonlat
null, // new OpenLayers.Size(1,1), // contentSize
text , //contentHTML
null, // anchor
false, // closeBox
null // closeBoxCallback
);
hoverPopup.autoSize = true;
hoverPopup.keepInMap = true;
hoverPopup.panMapIfOutOfView = false;
hoverPopup.events.on({"click": onHoverPopupClick});
map.addPopup(hoverPopup);
}
} else {
changeCursorInMap("default");
}
}
}
// disable all GetFeatureInfoRequest until we have a reponse
function onBeforeGetFeatureInfoClick(evt){
activateGetFeatureInfo(false);
}
// reenable GetFeatureInfo
function noFeatureInfoClick(evt){
activateGetFeatureInfo(true);
}
/* we need this function in order to pass through the click to the map events
* */
function onHoverPopupClick(evt){
if (hoverPopup) {removeHoverPopup();}
var map = geoExtMap.map; // gets OL map object
evt.xy = map.events.getMousePosition(evt); // non api function of OpenLayers.Events
map.events.triggerEvent("click", evt);
}
function onClickPopupClosed(evt) {
removeClickPopup();
// enable the hover popup for the curent mosue position
WMSGetFInfoHover.activate();
var map = geoExtMap.map; // gets OL map object
evt.xy = map.events.getMousePosition(evt); // non api function of OpenLayers.Events
map.events.triggerEvent("mousemove", evt);
closePopupClick = true; // indicate to not open a new clickPopup
}
function removeClickPopup() {
var map = geoExtMap.map; // gets OL map object
map.removePopup(clickPopup);
clickPopup.destroy();
clickPopup = null;
featureInfoHighlightLayer.removeAllFeatures();
}
function removeHoverPopup(){
var map = geoExtMap.map; // gets OL map object
map.removePopup(hoverPopup);
hoverPopup.destroy();
hoverPopup = null;
featureInfoHighlightLayer.removeAllFeatures();
}
function showFeatureSelected(args) {
// select feature in layer
thematicLayer.mergeNewParams({
"SELECTION": args["layer"] + ":" + args["id"]
});
if (args["doZoomToExtent"]){
geoExtMap.map.zoomToExtent(args["bbox"]);
}
else{
geoExtMap.map.setCenter(new OpenLayers.LonLat(args["x"], args["y"]), args["zoom"]);
}
}
function clearFeatureSelected() {
// clear selection
thematicLayer.mergeNewParams({
"SELECTION": null
});
}
function parseFIResult(node) {
if (node.hasChildNodes()) {
if (node.hasChildNodes() && node.nodeName == "Layer") {
var hasAttributes = false;
var rasterData = false;
var htmlText = "<h2>" + wmsLoader.layerProperties[node.getAttribute("name")].title + "</h2>";
var geoms = new Array();
var layerChildNode = node.firstChild;
while (layerChildNode) {
if (layerChildNode.hasChildNodes() && layerChildNode.nodeName === "Feature") {
htmlText += '\n <p></p>\n <table>\n <tbody>';
//case vector data
var attributeNode = layerChildNode.firstChild;
while (attributeNode) {
if (attributeNode.nodeName == "Attribute") {
var attName = attributeNode.getAttribute("name");
var attValue = attributeNode.getAttribute("value");
if ((attName !== mapInfoFieldName) && ((suppressEmptyValues == true && attValue.replace(/^\s\s*/, '').replace(/\s\s*$/, '') !== "") || suppressEmptyValues == false)) {
if (attName === "geometry") {
var feature = new OpenLayers.Feature.Vector(OpenLayers.Geometry.fromWKT(attValue));
geoms.push(feature);
if (! suppressInfoGeometry) {
htmlText += "\n <tr>";
if (showFieldNamesInClickPopup) {
htmlText += "<td>" + attName + ":</td>";
}
htmlText += "<td>" + attValue + "</td></tr>";
hasAttributes = true;
}
} else {
htmlText += "\n <tr>";
if (showFieldNamesInClickPopup) {
htmlText += "<td>" + attName + ":</td>";
}
// add hyperlinks for URLs in attribute values
if (attValue != '' && /http:\/\/.+\..+/i.test(attValue)) {
if (! /\<a./i.test(attValue)) {
//do not reformat already formated tags
attValue = "<a class=\"popupLink\" href=\"" + attValue + "\" target=\"_blank\">" + attValue + "</a>";
}
}
htmlText += "<td>" + attValue + "</td></tr>";
hasAttributes = true;
}
}
}
attributeNode = attributeNode.nextSibling;
}
htmlText += "\n </tbody>\n </table>";
}
else if (layerChildNode.nodeName === "Attribute") {
//case raster data
if (rasterData == false) {
htmlText += "\n <p></p>\n <table>\n <tbody>";
}
htmlText += '\n<tr><td>'+layerChildNode.getAttribute("name") + '</td><td>' + layerChildNode.getAttribute("value") + '</td></tr>';
hasAttributes = true;
rasterData = true;
}
layerChildNode = layerChildNode.nextSibling;
}
//htmlText += "\n</ul>";
if (hasAttributes) {
if (rasterData) {
htmlText += "\n </tbody>\n </table>";
}
//alert(htmlText);
featureInfoResultLayers.push(htmlText);
highLightGeometry.push(geoms);
}
} else {
var child = node.firstChild;
while (child) {
parseFIResult(child);
child = child.nextSibling;
}
}
}
}
function listLayersWithFeatures(node) {
if (node.hasChildNodes()) {
if (node.nodeName == "Layer") {
featureInfoResultLayers.push(node.getAttribute("name"));
} else {
var child = node.firstChild;
while (child) {
listLayersWithFeatures(child);
child = child.nextSibling;
}
}
}
}
function getFeatures(layerName, node) {
if (node.hasChildNodes()) {
if (node.nodeName == "Layer" && node.getAttribute("name") == layerName) {
return node.firstChild;
} else {
var child = node.firstChild;
while (child) {
getFeatures(layerName, child);
child = child.nextSibling;
}
}
}
}