• Create a geoprocessing tool to buffer a layer and retrieve messages____sync


    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 GpBufferLayer
    {
      publicpartialclass BufferDlg : Form
      {
        //in order to scroll the messages textbox to the bottom we must import this Win32 call
        [DllImport("user32.dll")]
        privatestaticexternint PostMessage(IntPtr wnd,
                                              uint Msg,
                                              IntPtr wParam,
                                              IntPtr lParam);
    
        private IHookHelper m_hookHelper = null;
        privateconstuint WM_VSCROLL = 0x0115;
        privateconstuint SB_BOTTOM = 7;
    
        public BufferDlg(IHookHelper hookHelper)
        {
          InitializeComponent();
    
          m_hookHelper = hookHelper;
        }
    
        privatevoid bufferDlg_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 layerif (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 bufferint units = Convert.ToInt32(m_hookHelper.FocusMap.MapUnits);
          cboUnits.SelectedIndex = units;
        }
    
        privatevoid 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;
        }
    
        privatevoid btnBuffer_Click(object sender, EventArgs e)
        {
          //make sure that all parameters are okaydouble bufferDistance;
          double.TryParse(txtBufferDistance.Text, out bufferDistance);
          if (0.0 == bufferDistance)
          {
            MessageBox.Show("Bad buffer distance!");
            return;
          }
    
          if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(txtOutputPath.Text)) ||
            ".shp" != System.IO.Path.GetExtension(txtOutputPath.Text))
          {
            MessageBox.Show("Bad output filename!");
            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 += "Layer " + (string)cboLayers.SelectedItem + "cannot be found!
    ";
            return;
          }
    
          //scroll the textbox to the bottom
          ScrollToBottom();
          //add message to the messages box
          txtMessages.Text += "Buffering layer: " + layer.Name + "
    ";
    
          txtMessages.Text += "
    Get the geoprocessor. This might take a few seconds...
    ";
          txtMessages.Update();
          //get an instance of the geoprocessor
          Geoprocessor gp = new Geoprocessor();
          gp.OverwriteOutput = true;
          txtMessages.Text += "Buffering...
    ";
          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 += "Failed to buffer layer: " + layer.Name + "
    ";
          }
          txtMessages.Text += ReturnMessages(gp);
          //scroll the textbox to the bottom
          ScrollToBottom();
    
          txtMessages.Text += "
    Done.
    ";
          txtMessages.Text += "-----------------------------------------------------------------------------------------
    ";
          //scroll the textbox to the bottom
          ScrollToBottom();
    
        }
    
        privatestring 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}
    ", gp.GetMessage(Count));
            }
          }
          return sb.ToString();
        }
    
        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;
          }
    
          returnnull;
        }
    
        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;
        }
    
        privatevoid ScrollToBottom()
        {
          PostMessage((IntPtr)txtMessages.Handle, WM_VSCROLL, (IntPtr)SB_BOTTOM, (IntPtr)IntPtr.Zero);
        }
        privatevoid btnCancel_Click(object sender, EventArgs e)
        {
          this.Close();
        }
      }
    }
  • 相关阅读:
    Django搭建环境
    python切片
    python数据类型
    jquery 淡入淡出属性
    Jquery Tab切换
    jQuery Clone方法
    jQuery属性操作
    python 变量以及循环
    获取网站目录
    posting-jsonobject-with-httpclient-from-web-api
  • 原文地址:https://www.cnblogs.com/943711466qq/p/4671448.html
Copyright © 2020-2023  润新知