Search code examples
javascriptarcgisarcgis-js-api

How to display count of results when filter feature layout ArcGis with javascript


I have multiple layouts in one project defined in object like this:

const layersByYears = [ 
    { year: 1918, layers: [ 
        { layerID: "443709212b894e9297888b3194b51100", opacity: 1.0, name: "ORP", featureLayer: null },
        { layerID: "61a605dca69c4aa0ac1b3858ba328fd3", opacity: 1.0, name: "obceSouc", featureLayer: null }
    ] },
    { year: 1936, layers: [ 
        { layerID: "a2e007238e304f1ea58005fc69c921e9", opacity: 1.0, name: "ORP", featureLayer: null },
        { layerID: "61a605dca69c4aa0ac1b3858ba328fd3", opacity: 1.0, name: "obceSouc", featureLayer: null }
    ] },
    { year: 1950, layers: [ 
        { layerID: "23bcd52310854d6185e5f22a4eca0303", opacity: 1.0, name: "ORP", featureLayer: null },
        { layerID: "61a605dca69c4aa0ac1b3858ba328fd3", opacity: 1.0, name: "obceSouc", featureLayer: null }
    ] },
    { year: 1981, layers: [ 
        { layerID: "401e9530ba074936800a8f596fc49d70", opacity: 1.0, name: "ORP", featureLayer: null },
        { layerID: "61a605dca69c4aa0ac1b3858ba328fd3", opacity: 1.0, name: "obceSouc", featureLayer: null }
    ] },    
]

Then I create variable to setup all layers:

const createFL = (layer) => {
        if(layer.featureLayer !== null && layer.featureLayer !== undefined)
            return;

        layer.featureLayer = new FeatureLayer({ portalItem: {
            id: layer.layerID,
            opacity: layer.opacity !== undefined ? layer.opacity : 1.0,
            visible: false,
        } })

        layer.featureLayer.load().then(function() {
            $("loadingProgressbar").value = ++loadingProgress.value;
            $("loadingProgressbar").max = loadingProgress.max;
            if(loadingProgress.max === loadingProgress.value)
                $("loadingProgressbar").style.display = "none";
        });
    }

Finally I provide filter like this:

function doQuery() {
    var selModul = document.getElementById("jevSelect");
    var attributeName = selModul.options[selModul.selectedIndex].value;
    

    const expressionSign = document.getElementById("signSelect");
    
    var selJev = document.getElementById("jevSelect");
    var inputValue = selJev.options[selJev.selectedIndex].text;

    let expression;
        
    expression = `${attributeName}${expressionSign.value}'${inputValue}'`;
        document.getElementById("printResults").innerHTML = expression;

        layersByYears.forEach(layerByYear =>
            layerByYear.layers.forEach(layer => {
                layer.featureLayer.definitionExpression = expression;   
            })
        )
        
    }

What I need is to display count of results (number) on each filter as label on point in map. Project is here map project - the problem is in year 1936.


Solution

  • If you want the count of features that complied to the condition, you can use just a count query using for example queryFeatureCount method of FeatureLayer (ArcGIS JS API - FeatureLayer). In your case you could add an extra property to your structure, count for example and fill it each time you change the definition expression, something like this,

    const layerQueryFeatureCount = function (layer, where) {
        if (!where) {
            where = "1==1";
        }
        layer.featureLayer.queryFeatureCount(new Query({where})).then(
            function (results) {
                layer.count = results;
                console.log(`${layer.name}: ${where} (count=${results})`);
            }, function (error) {
                console.log(error);
                layer.count = "-";
                console.log(`${layer.name}: ${where} (count=${error})`);
            }
        );
    }