标签:statistic void lan method get static rtp shape row
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; } } } }
标签:statistic void lan method get static rtp shape row
原文地址:https://www.cnblogs.com/gisoracle/p/12462288.html