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 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
{
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二次开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
2019-03-11 arcgis 获得工具箱工具的个数
2018-03-11 excel 鼠标上下左右移动
2018-03-11 excel 批注