AnsweredAssumed Answered

Problem using "Shape.STLength()" field in the CalculateStatistics method

Question asked by srbricker on Nov 6, 2019

I am writing a reporting application so the boss can quickly get the number of linear miles in a variety of polyline feature classes. I am using the .CalculateStatistics method based on the examples on Github. The following code works precisely as intended for all numeric fields except "Shape.STLength()", which unfortunately is the field I need. The code is as follows:

double GetLength(FeatureClass fc)
{
    try
    {
         using (FeatureClassDefinition fcd = fc.GetDefinition())
         {
             Field LengthField = fcd.GetFields().FirstOrDefault(x => x.Name.Equals("Shape.STLength()")); //"PIPEDIAM" and other numeric fields work fine, Shape.STLength() fails, as does fcd.GetLengthField();
             if (LengthField == null) return 0;
             Debug.WriteLine(LengthField.Name); // Output is "Shape.STLength()" as expected

             StatisticsDescription SumDesc = new StatisticsDescription(LengthField, new List<StatisticsFunction>() { StatisticsFunction.Sum });
             TableStatisticsDescription tsd = new TableStatisticsDescription(new List<StatisticsDescription>() { SumDesc });

             return fc.CalculateStatistics(tsd).FirstOrDefault().StatisticsResults.FirstOrDefault().Sum; // exception is thrown on this line
          }
     }
     catch (Exception ex)
     {
             ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString(), "Error");
             return 0;
     }
}

The exception that is thrown on line 14 is as follows:

 

Error image

Again, it is noteworthy that everything works fine when I set the fieldname on line 7 to any numeric (double) field other than Shape.STLength(). 

Outcomes