过滤getFeatureInfo结果(leaflet wms插件)

时间:2017-09-17 20:49:14

标签: javascript leaflet wms getfeatureinfo

问题

使用leaflet.wms.js插件,我已经设法通过单击它们来显示有关WMS图层的信息(使用GetFeatureInfo)。问题是地理服务器只以纯文本形式提供数据,如下图所示,这是一团糟。

Yep, it is a mess indeed

因此,我想过滤GetFeatureInfo查询的结果,以便仅显示有用的信息。我写了一堆JavaScript行,过滤了包含GetFeatureInfo请求结果的<div>

var GetFeatureInfo = document.getElementsByClassName("leaflet-popup-content")[0].innerHTML;
tipo = GetFeatureInfo.split(/'/)[21];
legenda = GetFeatureInfo.split(/'/)[27];
document.getElementsByClassName("leaflet-popup-content")[0].innerHTML = tipo + ":<br/>PERICOLOSITÀ " + legenda;

我试图在脚本底部添加这些行,调用并配置地图,但它不起作用。我想这些线路不会在合适的时刻执行。

解决方案

感谢Sebastian Schulz,我设法过滤了GetFeatureInfo查询的结果。我们需要扩展L.WMS.Source类并使用钩子showFeatureInfo编辑类在弹出窗口中显示GetFEatureInfo的方式。像这样:

var CleanInfoSource = L.WMS.Source.extend({
    'showFeatureInfo': function(latlng, info){
        if (!this._map){return;}
        tipo = info.split(/'/)[21];
        legenda = info.split(/'/)[27];
        info = tipo + ":<br/>PERICOLOSITÀ " + legenda;
        this._map.openPopup(info, latlng);
    }
});

var minambPAI = new CleanInfoSource("http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/WMS_v1.3/Vettoriali/PAI_pericolosita.map",{
        format: "image/png",
        transparent: true,
        attribution: "<a href='http://www.pcn.minambiente.it'>Ministero dell&#8217;Ambiente</a>",
        info_format: "text/plain"
    }
);

正如塞巴斯蒂安所说,这种方法(以及其他方法)在documentation。我还发现钩子语法在leaflet.wms.js脚本中。 RTFM我猜......:)

1 个答案:

答案 0 :(得分:0)

根据Leaflet WMS documentation,您需要扩展类L.WMS.Source并覆盖挂钩(例如 showFeatureInfo )。检查此代码段并编辑 info 变量以进行自定义弹出窗口。

var map = L.map('map').setView([43.53, 10.32], 13);
var openTopoMap = L.tileLayer(
  'http://{s}.tile.opentopomap.org/{z}/{x}/{y}.png',
  {attribution: '<a href="https://opentopomap.org/copyright">OpenTopoMap</a>'}).addTo(map);
var MySource = L.WMS.Source.extend({
    'showFeatureInfo': function(latlng, info) {
        if (!this._map) {
            return;
        }
        // do whatever you like with info
        console.log(info)
        this._map.openPopup(info, latlng);
    }
});
var minambPAI = new MySource("http://wms.pcn.minambiente.it/ogc?map=/ms_ogc/WMS_v1.3/Vettoriali/PAI_pericolosita.map",
    {
        format: "image/png",
        transparent: true,
        attribution: "<a href='http://www.pcn.minambiente.it'>Ministero dell&#8217;Ambiente</a>",
        info_format: "text/plain"
    }
);
var periAlluvioneMME = minambPAI.getLayer('RN.PAI.PERICOLOSITA.ALLUVIONE').addTo(map);
var periFranaMME = minambPAI.getLayer('RN.PAI.PERICOLOSITA.FRANA_01');
var control = L.control.layers({}, {
    'Pericolosità  Alluvioni: moderata a molto elevata': periAlluvioneMME,
    'Pericolosità  Frane: moderata a molto elevata': periFranaMME
})
control.addTo(map);