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!\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();
    }
  }
}
时间: 2024-10-07 15:58:48

Create a geoprocessing tool to buffer a layer and retrieve messages____sync的相关文章

How to run a geoprocessing tool

How to run a geoprocessing tool In this topic Running a geoprocessing tool Toolbox names and namespaces Running custom geoprocessing tools Executing a tool by name Running a geoprocessing tool Each geoprocessing tool has a fixed set of parameters tha

Caffe源码-Layer类

Layer类简介 Layer是caffe中搭建网络的基本单元,caffe代码中包含大量Layer基类派生出来的各种各样的层,各自通过虚函数 Forward() 和 Backward() 实现自己的功能. Forward() 函数用于前向计算过程,由 bottom blob 计算 top blob 和 loss ,实现数据由浅至深的传递.而 Backward() 函数用于反向传播过程,由 top blob 的计算 bottom blob 的梯度,将网络的预测误差向浅层网络传递,以便更新网络的参数.

Chromium网页Layer Tree同步为Pending Layer Tree的过程分析

CC Layer Tree绘制完成后,会同步到一个新的CC Pending Layer Tree去.同步过程由Compositor线程执行,并且Main线程处于等待状态.所谓同步,就是将CC Layer Tree的内容拷贝到CC Pending Layer Tree去.同步完毕,Main线程就会被唤醒.本文接下来分析CC Layer Tree同步为CC Pending Layer Tree的过程. 老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注! CC La

AE 打开工具箱工具的对话框

The best approach to run a system tool—or any tool or model created, and saved in a toolbox from a button—is to create an add-in control. Add-in controls are a new feature in ArcGIS 10. You can create an add-in control through the Customize dialog bo

Arcengine调用GP服务

注需 2个引用的区别 D:\Program Files (x86)\ArcGIS\DeveloperKit10.1\DotNet\ESRI.ArcGIS.Geoprocessor.dll D:\Program Files (x86)\ArcGIS\DeveloperKit10.1\DotNet\ESRI.ArcGIS.Geoprocessing.dll 官网代码: Each geoprocessing tool has a fixed set of parameters that provide

QGIS开发Python插件入门教程

Building Our First Plugin with 'Plugin Builder' It's time to get our feet with the Plugin Builder . 1. On the QGIS menu bar click on the Plugin Builder icon to launch the plugin: 2. The main Plugin Builder dialog will appear. This is where we fill ou

自定义使用AVCaptureSession 拍照,摄像,载图

转载自 http://blog.csdn.net/andy_jiangbin/article/details/19823333 拍照,摄像,载图总结 1 建立Session  2 添加 input  3 添加output  4 开始捕捉 5 为用户显示当前录制状态 6 捕捉 7 结束捕捉 8 参考  1 建立Session  1.1 声明session  AVCaptureSession *session = [[AVCaptureSession alloc] init]; // Add inp

Dr.memory

Run Dr.memory on visual c++ 2013 Title: Dr. Memory Command: C:\Program Files (x86)\Dr. Memory\bin\drmemory.exe Arguments: -visual_studio -- $(TargetPath) Initial Directory: $(TargetDir) set arguments-light -no_midchunk_inheritance_ok -no_check_gdi -n

关于arcgi s_api_for_flex的总结

1.flex 的简介 a) Flex是adobe开发的东西,主要特点就是开发一个swf格式的应用,flex可以做桌面的应用和web的应用,但本质差不多. b) Flex采用mxml的格式来进行应用的布局,然后用ActionScript进行脚本处理. 例子: 在application中放进一个eris的map控件,map的一个load函数 为loadHandler <s:Application> <fx:Script> <![CDATA[ private function lo