Select to view content in your preferred language

How to use ArcGIS uniqueValues For two Fields in ArcGIS JavaScript API

684
2
Jump to solution
09-08-2022 09:42 AM
geoca
by
New Contributor

Using ArcGIS JavaScript API 4.24 and uniqueValues from esri/smartMapping/statistics/uniqueValues I am able to get Count of all features in a FeatureLayer (fcLayer in this code)

In this example respond is based on AppType field from fcLayer

            uniqueValues({
                layer: fcLayer,
                field: "AppType",
            }).then(function(response) {
                let infos = response.uniqueValueInfos;
                infos.forEach(function(info) {
                    console.log("Application Type: ", info.value, " are: ", info.count);
                });
            });
 

geoca_1-1662655253510.png

 

 

Now Can you please let me know how I can robust this code in a way that I can group and query two fields AppType and AppStat  instead of field: "AppType" only?

I tried to use sqlExpression and sqlWhere instead of field option like

sqlExpression = "AppType = 'RD' AND AppStat = 'active'"
sqlWhere = "AppType = 'RD' AND AppStat = 'active'"

in the code like

        uniqueValues({
            layer: fcLayer,
            sqlExpression = "AppType = 'RD' AND AppStat = 'active'"
        }).then(function(response) {

or

        uniqueValues({
            layer: fcLayer,
            sqlWhere= "AppType = 'RD' AND AppStat = 'active'"
        }).then(function(response) {

 

But none of them returning any thing!

0 Kudos
1 Solution

Accepted Solutions
KristianEkenes
Esri Regular Contributor

Getting statistics for two fields like this is not supported at the moment in this function. However, we are adding support for it in our next release.

In the meantime, you can use Query.outStatistics to get the count of features with values from two fields.

 

// query for the average of the population change for all features
// Notice that you can pass a SQL expression as a field name to calculate statistics
let statDef = {
  onStatisticField: "CASE WHEN AppType = ‘RD’ AND AppStat = ‘active’ THEN 1 ELSE 0 END", // service field for 2015 population
  outStatisticFieldName: "RD_active",
  statisticType: "sum"
}

let query = layer.createQuery();
query.outStatistics = [ statDef ];

const { features } = await layer.queryFeatures(query);
let stats = features[0].attributes;
console.log("Average change:", stats.RD_active);

 

View solution in original post

0 Kudos
2 Replies
KristianEkenes
Esri Regular Contributor

Getting statistics for two fields like this is not supported at the moment in this function. However, we are adding support for it in our next release.

In the meantime, you can use Query.outStatistics to get the count of features with values from two fields.

 

// query for the average of the population change for all features
// Notice that you can pass a SQL expression as a field name to calculate statistics
let statDef = {
  onStatisticField: "CASE WHEN AppType = ‘RD’ AND AppStat = ‘active’ THEN 1 ELSE 0 END", // service field for 2015 population
  outStatisticFieldName: "RD_active",
  statisticType: "sum"
}

let query = layer.createQuery();
query.outStatistics = [ statDef ];

const { features } = await layer.queryFeatures(query);
let stats = features[0].attributes;
console.log("Average change:", stats.RD_active);

 

0 Kudos
geoca
by
New Contributor

Thanks Kristian

This was quick and helpful~

0 Kudos