Return feature ID for query buffer with StatisticDefinition

269
5
12-28-2020 11:52 AM
JeffKropelnicki
New Contributor II

Edits to remove what I have found will not work and try to better explain what I am looking to do. 

I have a feature polygon layer that is queried with a buffer. I use StatisticDefinition to sum one field shown in the code below. Using the 4.17 JS API https://developers.arcgis.com/javascript/latest/api-reference/esri-tasks-support-StatisticDefinition... 

I would like to return the polygon ID which is a string in this case I am using block groups. The problem is I cant use StatisticDefinition on a string field as it must be a sum, max, min or so on.

When I call console.log(result.features[0].attributes) I see one attribute from the query hh_202006. So this is the summed value for hh_202006 inside the buffer. I would like to add the ID for all block groups. 

 

function queryStatistics() {
const statDefinitions = [ onStatisticField: "CASE WHEN 202006 > 0 THEN 202006 ELSE 0 END", outStatisticFieldName: "hh_202006", statisticType: "sum", }] const query = featureLayerView.layer.createQuery() query.geometry = sketchGeometry; query.distance = bufferSize; query.units = 'miles' query.outStatistics = statDefinitions; //Only seeing results for this return featureLayerView.queryFeatures(query).then(function (result) { console.log(result.features[0].attributes)
}
}

 

0 Kudos
5 Replies
BlakeTerhune
MVP Regular Contributor

Reading the documentation, it says "If outStatistics is specified the only other query parameters that should be used are groupByFieldsForStatistics, orderByFields, text, and where." This indicates that the outFields parameter will be ignored. If you want to sum the length field and display the name of all the features used to calculate this sum, you'll need separate queries. Or just query all the features like normal and sum the length yourself (no extra outStatistics query needed).

0 Kudos
JeffKropelnicki
New Contributor II
That is what I thought about the outFields 
Can you tell me more about summing the length field to return a string? 

{
        onStatisticField: ??
        outStatisticFieldName: "name",
        statisticType: "sum",
      },
0 Kudos
BlakeTerhune
MVP Regular Contributor

 

var lengthSum = 0;
layer.queryFeatures(query).then(function(featureSet) {
  featureSet.features.forEach(function(feature) {
    lengthSum += feature.attributes.lengthFieldName
  });
});
console.log(lengthSum);

 

JeffKropelnicki
New Contributor II

Thank you for the reply I have been looking for a work around on this for a long time. Is there are working example of this out there? this code does not work for me and I dont understand how I can get all the names of the polygons inside a buffer which are strings from summing the length column. 

0 Kudos
BlakeTerhune
MVP Regular Contributor

You may need to clarify what you're trying to do with an example of what you want the output to look like. You have access to all output fields in the featureSet features so you can get the length (to sum) and the name values all in the same spot.

 

var lengthSum = 0;
var names = [];
layer.queryFeatures(query).then(function(featureSet) {
  featureSet.features.forEach(function(feature) {
    lengthSum += feature.attributes.lengthFieldName;
    names.push(feature.attributes.nameFieldName);
  });
});

console.log(`Total length of queried features is ${lengthSum}`);
console.log(`Names of queried features ${names}`);

 

 

EDIT:

Just realized I made a slight error on how to construct the forEach loop. I updated the code to be featureSet.features.forEach and feature.attributes

 

0 Kudos