double GetLength(FeatureClass fc, EnterpriseDatabaseType enterpriseDbType)
        {
            try
            {
                using (FeatureClassDefinition fcd = fc.GetDefinition())
                {
                    // the name of the length field changes depending on what enterprise geodatabase is used
                    var areaFieldName = "Shape_Length";
                    switch (enterpriseDbType)
                    {
                    case EnterpriseDatabaseType.SQLServer:
                        areaFieldName = "STLength";
                        break;
                    }
                    Field lengthField = fcd.GetFields().FirstOrDefault(x => x.Name.Contains(areaFieldName));
                    if (lengthField == null)
                    {
                        return(0);
                    }
                    System.Diagnostics.Debug.WriteLine(lengthField.Name);

                    StatisticsDescription SumDesc = new StatisticsDescription(lengthField, new List <StatisticsFunction>()
                    {
                        StatisticsFunction.Sum
                    });
                    TableStatisticsDescription tsd = new TableStatisticsDescription(new List <StatisticsDescription>()
                    {
                        SumDesc
                    });
                    double sum = 0;
                    try
                    {
                        sum = fc.CalculateStatistics(tsd).FirstOrDefault().StatisticsResults.FirstOrDefault().Sum; // exception is thrown on this line
                    }
                    catch
                    {
                        sum = Utilities.GetSumWorkAround(fc, lengthField.Name);
                    }
                    return(sum);
                }
            }
            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString(), "Error");
                return(0);
            }
        }
Esempio n. 2
0
        double GetArea(FeatureClass fc)
        {
            try
            {
                using (FeatureClassDefinition fcd = fc.GetDefinition())
                {
                    // the name of the area field changes depending on what enterprise geodatabase is used
                    var   areaFieldName = "Shape_Area";
                    Field areaField     = fcd.GetFields().FirstOrDefault(x => x.Name.Contains(areaFieldName));
                    if (areaField == null)
                    {
                        return(0);
                    }
                    System.Diagnostics.Debug.WriteLine(areaField.Name); // Output is "Shape.STArea()" as expected

                    StatisticsDescription SumDesc = new StatisticsDescription(areaField, new List <StatisticsFunction>()
                    {
                        StatisticsFunction.Sum
                    });
                    TableStatisticsDescription tsd = new TableStatisticsDescription(new List <StatisticsDescription>()
                    {
                        SumDesc
                    });
                    double sum = 0;
                    try
                    {
                        sum = fc.CalculateStatistics(tsd).FirstOrDefault().StatisticsResults.FirstOrDefault().Sum; // exception is thrown on this line
                    }
                    catch
                    {
                        sum = Utilities.GetSumWorkAround(fc, areaField.Name);
                    }
                    return(sum);
                }
            }
            catch (Exception ex)
            {
                ArcGIS.Desktop.Framework.Dialogs.MessageBox.Show(ex.ToString(), "Error");
                return(0);
            }
        }