AE中Add-In外掛開發例項
阿新 • • 發佈:2019-01-24
作為一個小白,在做Add-In開發時,網上資源比較少,最好的學習Add-In開發方法就是多看例項,在此分享我自己查詢資料拼湊而成的一個例項,話不多說,精品如下:
該外掛實現的功能是計算省會城市的空間可達性,計算公式為:
具體引數的意義如下:
Ai為節點i的可達性水平,Ai的值越小,節點的可達性越好,反之越差。
Tij節點i通過某種交通方式到達節點j 的最短旅行時間;
Mj為節點城市的某種社會經濟要素流的流量,表示該經濟中心對周圍地區的輻射和吸引力的高低,可採用人口規模、GDP總量等指標,文中採用各節點區域的GDP總量;
n為交通網路中除i點以外的節點總數。
視窗設計為:
具體實現程式碼(只分享核心部分):
using System; using System.IO; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Editor; using ESRI.ArcGIS.ArcMapUI; using ESRI.ArcGIS.Desktop; using ESRI.ArcGIS.Framework; using ESRI.ArcGIS.Controls; using ESRI.ArcGIS.Catalog; using ESRI.ArcGIS.DataManagementTools; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.Display; using ESRI.ArcGIS.Search; using ESRI.ArcGIS.Maplex; using ESRI.ArcGIS.Carto; namespace space_accessiblity { public partial class Form1 : Form { private IApplication m_application; public Form1() { InitializeComponent(); } //開啟form2對話方塊,顯示關於按鈕的內容 private void button4_Click(object sender, EventArgs e) { Form2 fm = new Form2(); fm.ShowDialog(); } //幫助按鈕 private void button3_Click(object sender, EventArgs e) { System.Diagnostics.Process.Start(@"G:\kaifa\space_accessiblity\space_accessiblity\使用說明.docx"); //System.Diagnostics.Process.Start("text1.txt", path1); /*string path1 = Application.StartupPath + "\\text1.txt"; string content; using (FileStream stream = File.OpenRead(path1)) { using (StreamReader reader = new StreamReader(stream)) { content = reader.ReadToEnd(); } } MessageBox.Show(content);*/ } //實現單個城市空間可達性計算 private void button1_Click(object sender, EventArgs e) { double sum = 0; double t = 0; double m = 0; double tm = 0; double access = 0; try { //設定地圖物件 m_application = ArcMap.Application; IDocument document = m_application.Document; IMxDocument mapDocument = document as IMxDocument; IMap map = mapDocument.FocusMap; //選擇圖層 IFeatureLayer pFeatureLayer = map.get_Layer(1) as IFeatureLayer; IFeatureClass FeatureClass = pFeatureLayer.FeatureClass; //獲取表中要素個數 //要素層轉為要素選擇 IFeatureSelection pFeatureSelection = (IFeatureSelection)pFeatureLayer; //新建查詢過濾器 IQueryFilter pQueryFilter = new QueryFilterClass(); //設定查詢條件 pQueryFilter.WhereClause = "start='" + comboBox1.Text + "'"; pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); //獲取查詢結果 ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet; //MessageBox.Show("一共查詢到"+pSelectionSet.Count.ToString()+"個要素"); ICursor pCursor; pSelectionSet.Search(null, false, out pCursor); IFeatureCursor pFeatureCursor = (IFeatureCursor)pCursor; IFeature ft = pFeatureCursor.NextFeature(); sum = Double.Parse(ft.get_Value(ft.Fields.FindField("GDP")).ToString()); t = Double.Parse(ft.get_Value(ft.Fields.FindField("time")).ToString()); m = Double.Parse(ft.get_Value(ft.Fields.FindField("GDP")).ToString()); tm = t * m; //MessageBox.Show(ft.get_Value(ft.Fields.FindField("GDP")).ToString()); while (ft != null) { t = 0; m = 0; IFeature ft1 = pFeatureCursor.NextFeature(); sum = sum + Double.Parse(ft1.get_Value(ft1.Fields.FindField("GDP")).ToString()); t = Double.Parse(ft1.get_Value(ft1.Fields.FindField("time")).ToString()); m = Double.Parse(ft1.get_Value(ft1.Fields.FindField("GDP")).ToString()); tm = tm + t * m; } } catch { access = tm / sum; textBox1.Text = comboBox1.Text+"的交通可達性為"+Convert.ToString(access); return; } } private void button2_Click(object sender, EventArgs e) { double sum2 = 0; double t2 = 0; double m2 = 0; double tm2 = 0; double access2 = 0; double sum3 = 0; double t3 = 0; double m3 = 0; double tm3 = 0; double access3 = 0; m_application = ArcMap.Application; IDocument document = m_application.Document; IMxDocument mapDocument = document as IMxDocument; IMap map = mapDocument.FocusMap; //選擇圖層 IFeatureLayer pFeatureLayer = map.get_Layer(1) as IFeatureLayer; IFeatureClass FeatureClass = pFeatureLayer.FeatureClass; //comboBox2空間可達性 try { //要素層轉為要素選擇 IFeatureSelection pFeatureSelection = (IFeatureSelection)pFeatureLayer; //新建查詢過濾器 IQueryFilter pQueryFilter = new QueryFilterClass(); //設定查詢條件 pQueryFilter.WhereClause = "start='" + comboBox2.Text + "'"; pFeatureSelection.SelectFeatures(pQueryFilter, esriSelectionResultEnum.esriSelectionResultNew, false); //獲取查詢結果 ISelectionSet pSelectionSet = pFeatureSelection.SelectionSet; ICursor pCursor; pSelectionSet.Search(null, false, out pCursor); IFeatureCursor pFeatureCursor = (IFeatureCursor)pCursor; IFeature ft = pFeatureCursor.NextFeature(); sum2 = Double.Parse(ft.get_Value(ft.Fields.FindField("GDP")).ToString()); t2 = Double.Parse(ft.get_Value(ft.Fields.FindField("time")).ToString()); m2 = Double.Parse(ft.get_Value(ft.Fields.FindField("GDP")).ToString()); tm2 = t2 * m2; while (ft != null ) { IFeature ft1 = pFeatureCursor.NextFeature(); sum2 = sum2 + Double.Parse(ft1.get_Value(ft1.Fields.FindField("GDP")).ToString()); t2 = Double.Parse(ft1.get_Value(ft1.Fields.FindField("time")).ToString()); m2 = Double.Parse(ft1.get_Value(ft1.Fields.FindField("GDP")).ToString()); tm2 = tm2 + t2 * m2; } } catch { access2 = tm2 / sum2; return; } finally { try { IFeatureSelection pFeatureSelection3 = (IFeatureSelection)pFeatureLayer; IQueryFilter pQueryFilter3 = new QueryFilterClass(); //設定查詢條件 pQueryFilter3.WhereClause = "start='" + comboBox3.Text + "'"; pFeatureSelection3.SelectFeatures(pQueryFilter3, esriSelectionResultEnum.esriSelectionResultNew, false); //獲取查詢結果 ISelectionSet pSelectionSet3 = pFeatureSelection3.SelectionSet; //MessageBox.Show("一共查詢到"+pSelectionSet.Count.ToString()+"個要素"); ICursor pCursor3; pSelectionSet3.Search(null, false, out pCursor3); IFeatureCursor pFeatureCursor3 = (IFeatureCursor)pCursor3; IFeature ft3 = pFeatureCursor3.NextFeature(); sum3 = Double.Parse(ft3.get_Value(ft3.Fields.FindField("GDP")).ToString()); // MessageBox.Show(sum3.ToString()); t3 = Double.Parse(ft3.get_Value(ft3.Fields.FindField("time")).ToString()); m3 = Double.Parse(ft3.get_Value(ft3.Fields.FindField("GDP")).ToString()); tm3 = t3 * m3; while (ft3 != null) { t3 = 0; m3 = 0; IFeature ft4 = pFeatureCursor3.NextFeature(); sum3 = sum3 + Double.Parse(ft4.get_Value(ft4.Fields.FindField("GDP")).ToString()); t3 = Double.Parse(ft4.get_Value(ft4.Fields.FindField("time")).ToString()); m3 = Double.Parse(ft4.get_Value(ft4.Fields.FindField("GDP")).ToString()); tm3 = tm3 + t3 * m3; } } catch { access3 = tm3 / sum3; if (access2 > access3) { textBox1.Text = comboBox2.Text + "的交通可達性為" + Convert.ToString(access2) + "\r\n\n" + comboBox3.Text + "的交通可達性為" + Convert.ToString(access3) + "\r\n" + comboBox3.Text + "的交通可達性比" + comboBox2.Text + "大"; } else { textBox1.Text = comboBox2.Text + "的交通可達性為" + Convert.ToString(access2) + "\r\n\n" + comboBox3.Text + "的交通可達性為" + Convert.ToString(access3) + "\r\n" + comboBox3.Text + "的交通可達性比" + comboBox2.Text + "小"; } } } } } }