Select to view content in your preferred language

How to Calculate the sum area of a polygon featurelayer by QueryStatisticsAsync ?

273
2
03-13-2025 02:06 AM
happygis
Emerging Contributor

Instead of looping through features, I need a way to calculate the sum area of a polygon featurelayer efficiently. How to use QueryStatisticsAsync to calculate the total area of all graphics in a FeatureLayer?

0 Kudos
2 Replies
pnarkhede
Esri Contributor

Hi there,

Thank you for the question!

To efficiently calculate the total area of all polygons in a FeatureLayer without looping through individual features, you can use the QueryStatisticsAsync method. This approach is much faster and cleaner, especially for large datasets.

Here's how you can achieve this:

// Define statistic definition for summing polygon areas
var areaStatisticDefinition = new StatisticDefinition("Shape_Area", StatisticType.Sum, "TotalArea");

// Set up statistics query parameters
var queryParameters = new StatisticsQueryParameters(new List<StatisticDefinition> { areaStatisticDefinition });

// Execute statistics query asynchronously on your FeatureLayer
var statisticsResult = await yourFeatureLayer
                                 .FeatureTable
                                 .QueryStatisticsAsync(queryParameters);

// Retrieve total area result
double totalArea = 0;
if (statisticsResult.Any())
{
    totalArea = Convert.ToDouble(statisticsResult.First().Statistics["TotalArea"]);
}

// Output or use the totalArea as needed
Console.WriteLine($"Total Area: {totalArea}");

 

Let me know if you have any questions or run into issues while implementing this! 

0 Kudos
happygis
Emerging Contributor

is "Shape_Area" a preserve fieldName? there is no Shape_Area in my table.

0 Kudos