ArcEngine開發之緩衝區分析實現(C#)

1、界面如下:

2、實現步驟

a、創建新窗體,BufferAnalysisForm

b、添加控件ComboBox,TextBox,Button等,具體參數設置如下:

控件名

名稱

其他

ComboBox

cboLayers

 

TextBox

txtBufferDistance

 

ComboBox

cboUnits

 

TextBox

txtOutputPath

 

Button

btnOutputLayer

圖標是’>’

Button

btnBuffer

 

TextBox

txtMessages

屬性中MultiLinetrue

Button

btnCancel

 

 

 

 

 

 

 

 

 

 

 

 

 

 

c、框體的源代碼如下:

using System;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using ESRI.ArcGIS.ADF.BaseClasses;
using ESRI.ArcGIS.ADF.CATIDs;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geoprocessor;
using ESRI.ArcGIS.Geoprocessing;
using ESRI.ArcGIS.AnalysisTools;

namespace GIS
{
    public partial class BufferAnalysisForm : Form
    {
        //in order to scroll the messages textbox to the bottom we must import this Win32 call
        [DllImport("user32.dll")]
        private static extern int PostMessage(IntPtr wnd,
                                              uint Msg,
                                              IntPtr wParam,
                                              IntPtr lParam);

        private IHookHelper m_hookHelper = null;
        private const uint WM_VSCROLL = 0x0115;
        private const uint SB_BOTTOM = 7;

        public BufferAnalysisForm(IHookHelper hookHelper)
        {
            InitializeComponent();

            m_hookHelper = hookHelper;
        }

        /// <summary>
        /// 窗體加載響應
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BufferAnalysisForm_Load(object sender, EventArgs e)
        {
            if (null == m_hookHelper || null == m_hookHelper.Hook || 0 == m_hookHelper.FocusMap.LayerCount)
                return;

            //load all the feature layers in the map to the layers combo
            IEnumLayer layers = GetLayers();
            layers.Reset();
            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                cboLayers.Items.Add(layer.Name);
            }
            //select the first layer
            if (cboLayers.Items.Count > 0)
                cboLayers.SelectedIndex = 0;

            string tempDir = System.IO.Path.GetTempPath();
            txtOutputPath.Text = System.IO.Path.Combine(tempDir, ((string)cboLayers.SelectedItem + "_buffer.shp"));

            //set the default units of the buffer
            int units = Convert.ToInt32(m_hookHelper.FocusMap.MapUnits);
            cboUnits.SelectedIndex = units;
        }

        /// <summary>
        /// 輸出路徑按鈕響應函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOutputLayer_Click(object sender, EventArgs e)
        {
            //set the output layer
            SaveFileDialog saveDlg = new SaveFileDialog();
            saveDlg.CheckPathExists = true;
            saveDlg.Filter = "Shapefile (*.shp)|*.shp";
            saveDlg.OverwritePrompt = true;
            saveDlg.Title = "Output Layer";
            saveDlg.RestoreDirectory = true;
            saveDlg.FileName = (string)cboLayers.SelectedItem + "_buffer.shp";

            DialogResult dr = saveDlg.ShowDialog();
            if (dr == DialogResult.OK)
                txtOutputPath.Text = saveDlg.FileName;
        }

        /// <summary>
        /// 分析按鈕響應函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnBuffer_Click(object sender, EventArgs e)
        {
            //修改當前指針樣式
            this.Cursor = Cursors.WaitCursor;

            //make sure that all parameters are okay
            double bufferDistance;
            double.TryParse(txtBufferDistance.Text, out bufferDistance);
            if (0.0 == bufferDistance)
            {
                MessageBox.Show("無效的緩衝距離!");
                return;
            }

            if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(txtOutputPath.Text)) ||
              ".shp" != System.IO.Path.GetExtension(txtOutputPath.Text))
            {
                MessageBox.Show("無效的文件名!");
                return;
            }

            if (m_hookHelper.FocusMap.LayerCount == 0)
                return;

            //get the layer from the map
            IFeatureLayer layer = GetFeatureLayer((string)cboLayers.SelectedItem);
            if (null == layer)
            {
                txtMessages.Text += "圖層 " + (string)cboLayers.SelectedItem + "未被找到!\r\n";
                return;
            }

            //scroll the textbox to the bottom
            ScrollToBottom();
            //add message to the messages box
            txtMessages.Text += "進行緩衝區的圖層: " + layer.Name + "\r\n";

            txtMessages.Text += "\r\n正在獲取空間數據。這可能需要幾秒鐘時間...\r\n";
            txtMessages.Update();
            //get an instance of the geoprocessor
            Geoprocessor gp = new Geoprocessor();
            gp.OverwriteOutput = true;
            txtMessages.Text += "正在進行緩衝區分析...\r\n";
            txtMessages.Update();

            //create a new instance of a buffer tool
            ESRI.ArcGIS.AnalysisTools.Buffer buffer = new ESRI.ArcGIS.AnalysisTools.Buffer(layer, txtOutputPath.Text, Convert.ToString(bufferDistance) + " " + (string)cboUnits.SelectedItem);

            //execute the buffer tool (very easy :-))
            IGeoProcessorResult results = (IGeoProcessorResult)gp.Execute(buffer, null);
            if (results.Status != esriJobStatus.esriJobSucceeded)
            {
                txtMessages.Text += "緩衝區失敗的圖層: " + layer.Name + "\r\n";
            }
            txtMessages.Text += ReturnMessages(gp);
            //scroll the textbox to the bottom
            ScrollToBottom();

            txtMessages.Text += "\r\n完成!\r\n";
            txtMessages.Text += "------------------------------------------------------\r\n";
            //scroll the textbox to the bottom
            ScrollToBottom();
            
            //修改當前指針樣式
            this.Cursor = Cursors.Default;
        }

        /// <summary>
        /// 取消按鈕相應函數
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 返回消息
        /// </summary>
        /// <param name="gp"></param>
        /// <returns></returns>
        private string ReturnMessages(Geoprocessor gp)
        {
            StringBuilder sb = new StringBuilder();
            if (gp.MessageCount > 0)
            {
                for (int Count = 0; Count <= gp.MessageCount - 1; Count++)
                {
                    System.Diagnostics.Trace.WriteLine(gp.GetMessage(Count));
                    sb.AppendFormat("{0}\n", gp.GetMessage(Count));
                }
            }
            return sb.ToString();
        }

        /// <summary>
        /// 獲取圖層
        /// </summary>
        /// <param name="layerName"></param>
        /// <returns></returns>
        private IFeatureLayer GetFeatureLayer(string layerName)
        {
            //get the layers from the maps
            IEnumLayer layers = GetLayers();
            layers.Reset();

            ILayer layer = null;
            while ((layer = layers.Next()) != null)
            {
                if (layer.Name == layerName)
                    return layer as IFeatureLayer;
            }

            return null;
        }

        /// <summary>
        /// 獲取圖層
        /// </summary>
        /// <returns></returns>
        private IEnumLayer GetLayers()
        {
            UID uid = new UIDClass();
            uid.Value = "{40A9E885-5533-11d0-98BE-00805F7CED21}";
            IEnumLayer layers = m_hookHelper.FocusMap.get_Layers(uid, true);

            return layers;
        }

        private void ScrollToBottom()
        {
            PostMessage((IntPtr)txtMessages.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, (IntPtr)IntPtr.Zero);
        }
    }
}

d、框體的調用: 

//定義參數
         IHookHelper map_hookHelper = new HookHelperClass();

//參數賦值
 //Hook定義
         map_hookHelper.Hook = this.axMapControl1.Object;

 

//窗體調用           
         BufferAnalysisForm BAFrm = new BufferAnalysisForm(map_hookHelper);
         BAFrm.ShowDialog();

 

希望與大家分享下自己查找到的或者自己修改後的代碼,新手學習,若有錯誤歡迎批評指正!

發佈了27 篇原創文章 · 獲贊 13 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章