ArcGIS Pro二次开发计算一个面层的总面积

Posted gisoracle

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ArcGIS Pro二次开发计算一个面层的总面积相关的知识,希望对你有一定的参考价值。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;

namespace ProAppModule1
{
    public class ylpub
    {
        public static string InputBox(string Caption, string Hint, string Default)
        {            //by 闫磊 Email:Landgis@126.com,yanleigis@21cn.com 2007.10.10 
            Form InputForm = new Form();
            InputForm.MinimizeBox = false;
            InputForm.MaximizeBox = false;
            InputForm.StartPosition = FormStartPosition.CenterScreen;
            InputForm.Width = 220;
            InputForm.Height = 150;
            //InputForm.Font.Name = "宋体";
            //InputForm.Font.Size = 10;
            InputForm.Text = Caption;
            Label lbl = new Label();
            lbl.Text = Hint;
            lbl.Left = 10;
            lbl.Top = 20;
            lbl.Parent = InputForm;
            lbl.AutoSize = true;
            TextBox tb = new TextBox();
            tb.Left = 30;
            tb.Top = 45;
            tb.Width = 160;
            tb.Parent = InputForm;
            tb.Text = Default;
            tb.SelectAll();
            Button btnok = new Button();
            btnok.Left = 30;
            btnok.Top = 80;
            btnok.Parent = InputForm;
            btnok.Text = "确定";
            InputForm.AcceptButton = btnok;//回车响应  
            btnok.DialogResult = DialogResult.OK;
            Button btncancal = new Button();
            btncancal.Left = 120;
            btncancal.Top = 80;
            btncancal.Parent = InputForm;
            btncancal.Text = "取消";
            btncancal.DialogResult = DialogResult.Cancel;
            try
            {
                if (InputForm.ShowDialog() == DialogResult.OK)
                {
                    return tb.Text;
                }
                else
                {
                    return null;
                }
            }
            finally
            { InputForm.Dispose(); }
        }

    }
}

botton1

using System;
using System.Collections.Generic;
using System.Linq;
//using System.Windows;
//using System.Windows.Forms;

using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.CIM;
using ArcGIS.Core.Data;
using ArcGIS.Core.Geometry;
using ArcGIS.Desktop.Catalog;
using ArcGIS.Desktop.Core;
using ArcGIS.Desktop.Editing;
using ArcGIS.Desktop.Extensions;
using ArcGIS.Desktop.Framework;
using ArcGIS.Desktop.Framework.Contracts;
using ArcGIS.Desktop.Framework.Dialogs;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;

namespace ProAppModule1
{
    internal class Button1 : ArcGIS.Desktop.Framework.Contracts.Button
    {
       
        protected override void OnClick()

        {

            try

            {
                string LayerName = ylpub.InputBox("图层名:", "my", "");

                var featureLayer = MapView.Active.Map.GetLayersAsFlattenedList().OfType<FeatureLayer>().Where(fl => fl.Name.Contains(LayerName)).FirstOrDefault();

                var area = QueuedTask.Run(() =>

                {
                    if (featureLayer == null)
                    {
                        MessageBox.Show("图层:" + LayerName + "不存在");
                    }



                    var fc = featureLayer.GetFeatureClass();

                    return GetArea(fc);

                });

                MessageBox.Show($@"Len: {area.Result}");

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.ToString(), "Error");

            }

        }

        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;

            }

        }


        double GetArea(FeatureClass fc, EnterpriseDatabaseType enterpriseDbType)

        {

            try

            {

                using (FeatureClassDefinition fcd = fc.GetDefinition())

                {

                    // the name of the area field changes depending on what enterprise geodatabase is used

                    var areaFieldName = "Shape_Area";

                    switch (enterpriseDbType)

                    {

                        case EnterpriseDatabaseType.SQLServer:

                            areaFieldName = "STArea";

                            break;

                    }

                    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;

            }

        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ArcGIS.Core.Data;
using ArcGIS.Desktop.Framework.Threading.Tasks;
using ArcGIS.Desktop.Mapping;
 
namespace ProAppModule1
{
    public static class Utilities
    {
        /// <summary>
        /// returns the enterprise gdb type for a given feature layer
        /// </summary>
        /// <param name="lyr"></param>
        /// <returns>EnterpriseDatabaseType enum of database or .Unknown</returns>
        public static EnterpriseDatabaseType GetDatabaseType(FeatureLayer lyr)
        {
            EnterpriseDatabaseType enterpriseDatabaseType = EnterpriseDatabaseType.Unknown;
            using (Table table = lyr.GetTable())
            {
                try
                {
                    var geodatabase = table.GetDatastore() as Geodatabase;
                    enterpriseDatabaseType = (geodatabase.GetConnector() as DatabaseConnectionProperties).DBMS;
                }
                catch (InvalidOperationException)
                {
                }
            }
            return enterpriseDatabaseType;
        }
 
        /// <summary>
        /// workaround to get sum from enterprise gdb lenght/area fields
        /// see https://community.esri.com/message/889796-problem-using-shapestlength-field-in-the-calculatestatistics-method
        /// </summary>
        /// <param name="fc">feature class to get sum from</param>
        /// <param name="fieldName">fieldname to sum up</param>
        /// <returns>sum</returns>
        public static double GetSumWorkAround(FeatureClass fc, string fieldName)
        {
            try
            {
                using (FeatureClassDefinition fcd = fc.GetDefinition())
                {
                    double totalLen = 0.0;
                    var cur = fc.Search();
                    while (cur.MoveNext())
                    {
                        var feat = cur.Current;
                        totalLen += Convert.ToDouble(feat[fieldName]);
                    }
                    return totalLen;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}

以上是关于ArcGIS Pro二次开发计算一个面层的总面积的主要内容,如果未能解决你的问题,请参考以下文章

ArcGIS Pro二次开发-添加字段

ArcGIS Pro二次开发画注记

ArcGIS Pro二次开发执行GP工具

ArcGIS Pro二次开发获得符号库的符号

ArcGIS Pro二次开发(19):创建要素类(FeatureClass)

ArcGIS二次开发综述