码迷,mamicode.com
首页 > 其他好文 > 详细

Create a geoprocessing tool to buffer a layer and retrieve messages____sync

时间:2015-07-23 21:12:24      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

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!\r\n";
        return;
      }

      //scroll the textbox to the bottom
      ScrollToBottom();
      //add message to the messages box
      txtMessages.Text += "Buffering layer: " + layer.Name + "\r\n";

      txtMessages.Text += "\r\nGet the geoprocessor. This might take a few seconds...\r\n";
      txtMessages.Update();
      //get an instance of the geoprocessor
      Geoprocessor gp = new Geoprocessor();
      gp.OverwriteOutput = true;
      txtMessages.Text += "Buffering...\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 += "Failed to buffer layer: " + layer.Name + "\r\n";
      }
      txtMessages.Text += ReturnMessages(gp);
      //scroll the textbox to the bottom
      ScrollToBottom();

      txtMessages.Text += "\r\nDone.\r\n";
      txtMessages.Text += "-----------------------------------------------------------------------------------------\r\n";
      //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}\n", 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();
    }
  }
}

 

Create a geoprocessing tool to buffer a layer and retrieve messages____sync

标签:

原文地址:http://www.cnblogs.com/943711466qq/p/4671448.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!