首页 > 解决方案 > 过滤特定属性并对值求和

问题描述

我有一个带有多边形的传单地图,您可以单击每个多边形来选择它们,并且有一个信息窗口“L.control”显示所选多边形的值。当您继续单击多边形时,信息窗口会为每个选定的多边形添加值,您将获得所有选定多边形的总值。这一切都很好,但我需要深入了解特定属性的更详细的总和,例如下面的区域示例。如果选择了十个多边形,我想区分具有属性“REGION SOUTH”和“REGION NORTH”的区域的总数以及所有区域的总数。这是我正在使用的代码,对不同属性的总数求和是没有问题的,但是你如何对定义的属性求和?

我如何以及在哪里可以添加一种过滤器解决方案,它只汇总我想要的属性?

$.each(statesData.features, function(index, feature) {
var name = `${feature.properties.ZIPCODE} ${feature.properties.Name}  ( ${feature.properties.average_time} -  ${feature.properties.CITY})`
placenames.push(name);
zipcodes[name] = feature.properties.ZIPCODE;
time = feature.properties.average_time
});

etc.... 

// Now get the totals of selected polygons
var detailshow = function() {
var result = ''
var total = 0
var total1 = 0
var total2 = 0
var total3 = 0
var total4 = 0
for (var i = 0; i < featuresSelected.length; i++) {

var properties = featuresSelected[i].feature.properties
    result +=
        `
    ${properties.CITY}<br>
    Zipcode: ${properties.ZIPCODE}
    <a href="#" onclick=dellayer(${properties.ZIPCODE})>Delete</a>
    <hr>`;
    total += properties.amount, // sum amount for all regions
    total1 += properties.average_time, // in seconds
    total2 += properties.distance,
    total3 += properties.amount, // amount for Region South only
    total4 += properties.amount, // amount for Region North only

    // Convert seconds to timeformat
    var convertTime = function (input, separator) {
    var pad = function(input) {return input < 10 ? "0" + input : input;};
        return [
        pad(Math.floor(input / 3600)),
        pad(Math.floor(input % 3600 / 60)),
        pad(Math.floor(input % 60)),
        ].join(typeof separator !== 'undefined' ?  separator : ':' );
    }

var resultTime = convertTime(total1);

}
return {
    result: result,
    total: total,
    resultTime: resultTime,
    total2: total2
    total3: total3
    total4: total4
};
}

detailsselected.update = function(arrayselected) {

var details = detailshow()
this._div.innerHTML =
'<b>Zipcodes</b><br>' + 
'Total time: <b>' + details.resultTime + ' hh:mm:ss</b><br>' +
'Total amount: <b>' + details.total + ' st</b><br>' +
'Region South amount: <b>' + details.total3 + ' st</b><br>' +
'Region North amount: <b>' + details.total4 + ' st</b><br>' +
'Distance: <b>' + details.total2.toFixed(1) + ' km</b><br>';
$('#suma', window.parent.document).val(details.resultTime, details.total, details.total2, details.total3, details.total4);


};

detailsselected.addTo(map);

特征选择:

function checkExistsLayers(feature) {
var result = false
for (var i = 0; i < featuresSelected.length; i++) {
    if (featuresSelected[i].ZIPCODE == feature.properties.ZIPCODE) {
        result = true;
        break;
    }
};
return result

}

这是 json 文件结构的一部分:

var statesData = new L.LayerGroup;
var statesData = {"type":"FeatureCollection","features":[{"type":"Feature","properties":{"ZIPCODE":12345,"CITY":"LONDON","REGION":"REGION SOUTH","amount":1088,"average_time":26150,"distance":2.2},"geometry":{"type":"MultiPolygon","coordinates":...

我确实尝试了以下方法,但没有奏效......

    function filt_north (feature){
        if (feature.properties.REGION === 'REGION NORTH' )
        return true;
    }

    total4 += filt_north.(properties.amount), // amount for Region North only

标签: javascriptfilterleaflet

解决方案


尝试了您的解决方案,似乎它破坏了代码并且根本没有加总=停止工作。我这样做了,应该以不同的方式完成吗?过滤功能:

    function filt_south (feature){
    if (feature.properties.REGION === 'REGION SOUTH')
    return true;
}

function filt_north (feature){
    if (feature.properties.REGION === 'REGION NORTH')
    return true;
}

然后改成这个(我一定是在这里做错了):

    // Now get the totals of selected polygons
    var detailshow = function() {
    var result = ''
    var total = 0
    var total1 = 0
    var total2 = 0
    var total3 = 0
    var total4 = 0

    let filteredResults = featuresSelected.filter(
        result => filt_south(result.feature) || filt_north(result.feature)
        );

    for (let result of filteredResults) {
        var properties = result.feature.properties;

    for (var i = 0; i < featuresSelected.length; i++) { 
        var properties = featuresSelected[i].feature.properties
        result +=
        `
    ${properties.CITY}<br>
    Zipcode: ${properties.ZIPCODE}
    <a href="#" onclick=dellayer(${properties.ZIPCODE})>Delete</a>
    <hr>`;
    total += properties.amount, // sum amount for all regions
    total1 += properties.average_time,
    total2 += properties.distance,
    total3 += filt_south (properties.amount), // amount for Region South only
    total4 += filt_north (properties.amount) // amount for Region North only

    // Convert seconds to timeformat
    var convertTime = function (input, separator) {
    var pad = function(input) {return input < 10 ? "0" + input : input;};
        return [
        pad(Math.floor(input / 3600)),
        pad(Math.floor(input % 3600 / 60)),
        pad(Math.floor(input % 60)),
        ].join(typeof separator !== 'undefined' ?  separator : ':' );
    }

var resultTime = convertTime(total1);
}
}
return {
    result: result,
    total: total,
    resultTime: resultTime,
    total2: total2
    total3: total3
    total4: total4
};
}

detailsselected.update = function(arrayselected) {

var details = detailshow()
this._div.innerHTML =
'<b>Zipcodes</b><br>' + 
'Total time: <b>' + details.resultTime + ' hh:mm:ss</b><br>' +
'Total amount: <b>' + details.total + ' st</b><br>' +
'Region South amount: <b>' + details.total3 + ' st</b><br>' +
'Region North amount: <b>' + details.total4 + ' st</b><br>' +
'Distance: <b>' + details.total2.toFixed(1) + ' km</b><br>';
$('#suma', window.parent.document).val(details.resultTime, details.total, details.total2, details.total3, details.total4);


};

detailsselected.addTo(map)

推荐阅读