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);
            }
        }
Пример #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);
            }
        }
Пример #3
0
        public static async Task <TableStatisticsResult> GetRasterStats(Uri rasterUri, string field)
        {
            // parse the uri for the folder and file
            string strFileName   = null;
            string strFolderPath = null;

            if (rasterUri.IsFile)
            {
                strFileName   = System.IO.Path.GetFileName(rasterUri.LocalPath);
                strFolderPath = System.IO.Path.GetDirectoryName(rasterUri.LocalPath);
            }

            RasterDataset rDataset = null;
            await QueuedTask.Run(() =>
            {
                // Opens a file geodatabase. This will open the geodatabase if the folder exists and contains a valid geodatabase.
                using (Geodatabase geodatabase =
                           new Geodatabase(new FileGeodatabaseConnectionPath(new Uri(strFolderPath))))
                {
                    // Use the geodatabase.
                    try
                    {
                        rDataset = geodatabase.OpenDataset <RasterDataset>(strFileName);
                    }
                    catch (GeodatabaseTableException e)
                    {
                        Module1.Current.ModuleLogManager.LogError(nameof(GetRasterStats),
                                                                  "Unable to open raster " + strFileName);
                        Module1.Current.ModuleLogManager.LogError(nameof(GetRasterStats),
                                                                  "Exception: " + e.Message);
                        return;
                    }
                }
            });

            TableStatisticsResult tableStatisticsResult = null;

            if (rDataset != null)
            {
                await QueuedTask.Run(() =>
                {
                    Raster raster = rDataset.CreateRaster(new int[] { 0 });
                    if (raster != null)
                    {
                        var table = raster.GetAttributeTable();
                        if (table != null)
                        {
                            Field statField = table.GetDefinition().GetFields().First(x => x.Name.Equals(field));

                            StatisticsDescription statisticsDescription = new StatisticsDescription(statField, new List <StatisticsFunction>()
                            {
                                StatisticsFunction.Min, StatisticsFunction.Max
                            });
                            TableStatisticsDescription tableStatisticsDescription = new TableStatisticsDescription(new List <StatisticsDescription>()
                            {
                                statisticsDescription
                            });
                            IReadOnlyList <TableStatisticsResult> statResult = table.CalculateStatistics(tableStatisticsDescription);
                            tableStatisticsResult = statResult[0];
                        }
                    }
                });
            }
            return(tableStatisticsResult);
        }