QGIS2WEB - OpenLayers - Improvement Popup by adding "highlight feature"

151 Views Asked by At

QGIS2WEB exports a map that allows to query by click multiple layers and multiple features at the same time, showing all the result in the outgoing popup as in this example where 6 different layers are queried. I would like to add the ability to selectively highlight the query feature. Do you know how to do it? I thought of a graphic with a button (I did it with photoshop):

example of 6 features queried in different layers enter image description here

example whit highlight button for any feature enter image description here

The code for highlighting is already present in qgis2web as pointermove, could this be reused and applied in the popup? I leave it written as an example:

if (doHighlight) {
                if (currentFeature !== highlight) {
                    if (highlight) {
                        featureOverlay.getSource().removeFeature(highlight);
                    }
                    if (currentFeature) {
                        var styleDefinition = currentLayer.getStyle().toString();

                        if (currentFeature.getGeometry().getType() == 'Point') {
                            var radius = styleDefinition.split('radius')[1].split(' ')[1];

                            highlightStyle = new ol.style.Style({
                                image: new ol.style.Circle({
                                    fill: new ol.style.Fill({
                                        color: 'rgba(255,255,0,1)'
                                    }),
                                    radius: radius,
                                    stroke: new ol.style.Stroke({
                                      color: 'rgba(255, 255, 0, 1)',
                                      width: 3
                                    }),
                                })
                            })
                        } else if (currentFeature.getGeometry().getType() == 'LineString') {

                            var featureWidth = styleDefinition.split('width')[1].split(' ')[1].replace('})','');

                            highlightStyle = new ol.style.Style({
                                stroke: new ol.style.Stroke({
                                    color: 'rgba(255,255,0,1)',
                                    lineDash: null,
                                    width: featureWidth
                                })
                            });

                        } else {
                            highlightStyle = new ol.style.Style({
                                        
                                    fill: new ol.style.Fill({
                                      color: 'rgba(255, 255, 0, 0)'
                                    }),
                                    stroke: new ol.style.Stroke({
                                      color: 'rgba(255, 255, 0, 1)',
                                      lineDash: [10, 10],
                                      width: 3
                                    }),                         
                            })
                        }
                        featureOverlay.getSource().addFeature(currentFeature);
                        featureOverlay.setStyle(highlightStyle);
                    }
                    highlight = currentFeature;
                }
            }

This is the graphical output

enter image description here

The entire current popup code, which I ask you to change, is this:

var onSingleClick = function(evt) {
    if (doHover) {
        return;
    }
    if (sketch) {
        return;
    }
    var pixel = map.getEventPixel(evt.originalEvent);
    var coord = evt.coordinate;
    var popupField;
    var currentFeature;
    var currentFeatureKeys;
    var clusteredFeatures;
    var popupText = '<ul>';
    map.forEachFeatureAtPixel(pixel, function(feature, layer) {
        if (feature instanceof ol.Feature && (layer.get("interactive") || layer.get("interactive") == undefined)) {
            var doPopup = false;
            for (k in layer.get('fieldImages')) {
                if (layer.get('fieldImages')[k] != "Hidden") {
                    doPopup = true;
                }
            }
            currentFeature = feature;
            clusteredFeatures = feature.get("features");
            var clusterFeature;
            if (typeof clusteredFeatures !== "undefined") {
                if (doPopup) {
                    for(var n=0; n<clusteredFeatures.length; n++) {
                        clusterFeature = clusteredFeatures[n];
                        currentFeatureKeys = clusterFeature.getKeys();
                        popupText += '<li><table>'
                        for (var i=0; i<currentFeatureKeys.length; i++) {
                            if (currentFeatureKeys[i] != 'geometry') {
                                popupField = '';
                                if (layer.get('fieldLabels')[currentFeatureKeys[i]] == "inline label") {
                                popupField += '<th>' + layer.get('fieldAliases')[currentFeatureKeys[i]] + ':</th><td>';
                                } else {
                                    popupField += '<td colspan="2">';
                                }
                                if (layer.get('fieldLabels')[currentFeatureKeys[i]] == "header label") {
                                    popupField += '<strong>' + layer.get('fieldAliases')[currentFeatureKeys[i]] + ':</strong><br />';
                                }
                                if (layer.get('fieldImages')[currentFeatureKeys[i]] != "ExternalResource") {
                                    popupField += (clusterFeature.get(currentFeatureKeys[i]) != null ? autolinker.link(clusterFeature.get(currentFeatureKeys[i]).toLocaleString()) + '</td>' : '');
                                } else {
                                    popupField += (clusterFeature.get(currentFeatureKeys[i]) != null ? '<img src="images/' + clusterFeature.get(currentFeatureKeys[i]).replace(/[\\\/:]/g, '_').trim()  + '" /></td>' : '');
                                }
                                popupText += '<tr>' + popupField + '</tr>';
                            }
                        } 
                        popupText += '</table></li>';    
                    }
                }
            } else {
                currentFeatureKeys = currentFeature.getKeys();
                if (doPopup) {
                    popupText += '<li><table>';
                    for (var i=0; i<currentFeatureKeys.length; i++) {
                        if (currentFeatureKeys[i] != 'geometry') {
                            popupField = '';
                            if (layer.get('fieldLabels')[currentFeatureKeys[i]] == "inline label") {
                                popupField += '<th>' + layer.get('fieldAliases')[currentFeatureKeys[i]] + ':</th><td>';
                            } else {
                                popupField += '<td colspan="2">';
                            }
                            if (layer.get('fieldLabels')[currentFeatureKeys[i]] == "header label") {
                                popupField += '<strong>' + layer.get('fieldAliases')[currentFeatureKeys[i]] + ':</strong><br />';
                            }
                            if (layer.get('fieldImages')[currentFeatureKeys[i]] != "ExternalResource") {
                                popupField += (currentFeature.get(currentFeatureKeys[i]) != null ? autolinker.link(currentFeature.get(currentFeatureKeys[i]).toLocaleString()) + '</td>' : '');
                            } else {
                                popupField += (currentFeature.get(currentFeatureKeys[i]) != null ? '<img src="images/' + currentFeature.get(currentFeatureKeys[i]).replace(/[\\\/:]/g, '_').trim()  + '" /></td>' : '');
                            }
                            popupText += '<tr>' + popupField + '</tr>';
                        }
                    }
                    popupText += '</table>';
                }
            }
        }
    });
    if (popupText == '<ul>') {
        popupText = '';
    } else {
        popupText += '</ul>';
    }
    
    var viewProjection = map.getView().getProjection();
    var viewResolution = map.getView().getResolution();
    for (i = 0; i < wms_layers.length; i++) {
        if (wms_layers[i][1]) {
            var url = wms_layers[i][0].getSource().getGetFeatureInfoUrl(
                evt.coordinate, viewResolution, viewProjection,
                {
                    'INFO_FORMAT': 'text/html',
                });
            if (url) {
                popupText = popupText + '<iframe style="width:100%;height:110px;border:0px;" id="iframe" seamless src="' + url + '"></iframe>';
            }
        }
    }

    if (popupText) {
        overlayPopup.setPosition(coord);
        content.innerHTML = popupText;
        container.style.display = 'block';        
    } else {
        container.style.display = 'none';
        closer.blur();
    }
};

You can help me? Thank you

0

There are 0 best solutions below