Counting query results

345
3
Jump to solution
01-07-2022 12:35 PM
AnthonyDAlessio
New Contributor

I have a query that runs against a Feature layer to returns points in a SceneView  via a DefinitionExpression (just saw, maybe deprecated, will look into that, but it works just fine)

These points could be one of 100 or so different categories and I'm trying to show how many of each category were returned.  I have the initial count for each one using uniqueValues, but that works because at the start every item in every category is displayed.

Category1 (7)

Category2 (15)

Category3 (10)

After the filter is run, maybe Category1 only has 5 items now, Category2 has 10, etc.

If ArcGIS was a database I'd probably just query the fields I needed to calculate this when I initially pull in the data, store it in an array and just query that array to get my counts when things were updated so I wouldn't have to go back to the database, but ArcGIS doesn't let me do a straight up select statement that I'm aware of.

I'm looking for advice on how best to update these counts after a query is run, I can figure out how many total points were returned, but I need to be able to display how many in each category were returned from the query.

 

 

0 Kudos
1 Solution

Accepted Solutions
DanielLaumer_Esri
Esri Contributor

Hi @AnthonyDAlessio

Thank you for providing details about the issue! I think I now understand your problem. 

I think the easiest solution would be to put the code snippet which you provided into a function and then call that function right after each change of the definitionExpression (or use the watch() function of the featureLayer in order to listen to a change of the definitonExpression). The uniqueValues function adapts the results according to the definitionExpression. Maybe you will need to adjust the code a bit to not always re-generate the checkboxes but just change the count value. 

And on the side: It is possible to query all features of a featureLayer with a WHERE clause. You can use the layer.createQuery() or layer.queryFeatures() function. 

I hope this helps, if you have further questions just ask 😉

Daniel

View solution in original post

3 Replies
DanielLaumer_Esri
Esri Contributor

Hi @AnthonyDAlessio

I am not sure if I understand the problem correctly. What I understand is: You want to filter a FeatureLayer with the definitionExpression and after each filtering query, you want to know how many features of each category are being shown. And in order to get this information you use the uniqueValues function. Is this correct?

Can you maybe provide some code snippet in order for us to better understand the issue at hand?

Daniel

0 Kudos
AnthonyDAlessio
New Contributor

Thanks Daniel, that pretty much sums it up, (included a picture of the checkboxes I'm using to drive the filter) three categories across the top and 100 or so different indications.  When you check/uncheck one of the 3 boxes across the top I filter the sites shown using Layer.definitionExpression.   All this works well, but I'm trying to get a new count of how many sites perform that "Indication" to update the labels, for example: as you can see in the image there is 1 site that does "Acute Kidney Injury", if that site only happens to be "Pre-Clinical" and I deselect "Pre-Clinical" then after the filter runs I want to show (0) next to the label instead of (1)

 Screenshot 2022-01-10 143607.png

As I said, normally I'd just query the database directly and build an array of these to easily change the values as checkboxes are changed, no way to do this in ArcGIS that I have found (I could definitely be missing something though)

SELECT COUNT(column_name)
FROM table_name
WHERE condition; 

 Here is the uniqueValues code used to build the checkboxes and initially come up with the total.

 

uniqueValues({
  layer: conditionLayer,
  field: "Indication",

  view: map
  }).then(function (response) {
    // prints each unique value and the count of features containing that value
    this.infos = response.uniqueValueInfos;
    $.each(this.infos, function () {
      var currentValue = this.value;
        $("#feature-node").append('<input type="checkbox" id="' + currentValue +'" name="checkbox" checked="true" value="'+ currentValue + '" onClick="filter()">').append('<label for="' + currentValue +'"">' + currentValue + ' (' + this.count + ')</label></div><BR>');
        });
});

 

 

0 Kudos
DanielLaumer_Esri
Esri Contributor

Hi @AnthonyDAlessio

Thank you for providing details about the issue! I think I now understand your problem. 

I think the easiest solution would be to put the code snippet which you provided into a function and then call that function right after each change of the definitionExpression (or use the watch() function of the featureLayer in order to listen to a change of the definitonExpression). The uniqueValues function adapts the results according to the definitionExpression. Maybe you will need to adjust the code a bit to not always re-generate the checkboxes but just change the count value. 

And on the side: It is possible to query all features of a featureLayer with a WHERE clause. You can use the layer.createQuery() or layer.queryFeatures() function. 

I hope this helps, if you have further questions just ask 😉

Daniel