• 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;
                }
            }
        }
    }
  • 相关阅读:
    概要设计
    JNI内存泄露
    Flash 与 JS 交互
    Lucene.net 实现全文搜索(转)
    自动打开登录游戏的代码
    Ajax,网站改版的一种方法
    IE6/IE7和Firefox对Div处理的差异(转)
    搜VC里找到的
    C# 尝试读取或写入受保护的内存。这通常指示其他内存已损坏
    HTML特殊转义字符列表
  • 原文地址:https://www.cnblogs.com/gisoracle/p/12462288.html
Copyright © 2020-2023  润新知