Getting Statistics on a field

345
7
11-24-2017 05:48 AM
Highlighted
Regular Contributor

In ArcObjects I would use Geodatabase.IDataStatistics to get stats on a field.  Does anyone know if an equivalent exists in ArcPro SDK??

Thanks,

Reply
0 Kudos
7 Replies
Highlighted
New Contributor III

Brian,

ArcGIS Pro Core.Data has not exposed this functionality yet. We will look to do so in a future release.

Do you utilize the simple stats (count, min\max etc) or standard deviation?

Thanks,

Colin

Reply
0 Kudos
Highlighted
Regular Contributor

Hi Colin,

Just simple stats is all I am looking for.

Thanks!

Reply
0 Kudos
Highlighted
Regular Contributor

Hi Colin,

We are using UniqueValues, min, max. Please accelerate exposing of this functionality

Reply
0 Kudos
Highlighted
by Anonymous User
Not applicable

Hi Colin any word on when this might be exposed? Could do with an easy way of getting unique values from a field.

Reply
0 Kudos
Highlighted
New Contributor III

Good timing

With the release of 2.2 check out:

ProConcepts Geodatabase · Esri/arcgis-pro-sdk Wiki · GitHub 

Edit: totally missed your unique value statement, let me check with the team.

Thanks,

Colin

Reply
0 Kudos
Highlighted
by Anonymous User
Not applicable

Thanks Colin, yes please confirm if unique vals can be retrieved

Cheers

Reply
0 Kudos
Highlighted
Esri Regular Contributor

Hi Luke,

Yes, it is possible to get the unique values of a field, but the way to do so isn't obvious.

As Colin pointed out, the key is to use the Table.CalculateStatistics() routine. This will allow you to generate statistics on one or more fields and includes the ability to execute a group by on these statistics. To get the unique values, you execute this code, passing in a "dummy" statistic to calculate.

In the example below, I build a list of unique values for the SUB_REGION field of a States table.

// Get field for Region


Field regionField = featureClassDefinition.GetFields().First(x => x.Name.Equals("SUB_REGION"));

// Create a "fake" StatisticsDescription object


StatisticsDescription statisticsDescription = new StatisticsDescription(regionField, new List<StatisticsFunction>() { StatisticsFunction.Count });

// Create a TableStatisticsDescription that will return unique Region values (and a

// count of how many rows have that value)


TableStatisticsDescription tableStatisticsDescription = new TableStatisticsDescription(new List<StatisticsDescription>() { statisticsDescription });


tableStatisticsDescription.GroupBy = new List<Field>(){ regionField };

// Generate the statistics (and list of unique values)


IReadOnlyList<TableStatisticsResult> results = statesFeatureClass.CalculateStatistics(tableStatisticsDescription);

// Table.CalculateStatistics() returns one TableStatisticsResult object for each unique

// GroupBy tuple.
// If you pass one field into the TableStatisticsDescription.GroupBy object, you'll get

// one TableStatisticsResult object for each unique value of that field


foreach(TableStatisticsResult result in results)
{
  // TableStatisticsResult.GroupBy returns one KeyValuePair<Field,object> for each field

  // in the TableStatisticsDescription.GroupBy list
  // If you only pass one field into that list, we only get one

  // KeyValuePair<Field,object>


  KeyValuePair<Field, object> groupByPair = result.GroupBy.First();

  // groupByPair.Field is the Field you passed into TableStatisticsDescription.GroupBy
  // groupByPair.Value is a unique value of that field


  string myFieldValue = (string) groupByPair.Value;
  Console.WriteLine(myFieldValue);
}

We'll take a look at providing a more obvious way this to do in a future version of ArcGIS Pro.

Let me know if this helps,

--Rich