||
上一节我们学习了缓冲区分析。
在Arcengine中,数据空间分析的方法一般有两种,一种是我们之前学习的gp方法(Geoprocessor),另外一种是利用ITopologicalOperator接口。前者比较简单,我们可以根据Arcmap中的工具箱,利用Arcengine开发出一系列的工具。
1、首先是创建自定义的工具,封装到一个自定义工具里,可用于Arcmap、MapControl等。
2、在自定义菜单的onclick事件中调用代码,打开一个新的窗体,这个窗体就是我们想要的处理工具界面。
3、在这个工具界面中设计一些控件,可以读取一些文件、设置一些参数
4、在这个工具界面的确定按钮中,输入核心参数,从而运行这个工具的功能。这里用到的就是gp方法,而且不同的工具所调用gp方法差别不大。
例如,Clip工具的gp方法如下:
Geoprocessor gp = new Geoprocessor(); gp.OverwriteOutput = true; ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip(); clip.clip_features = @" ";//被裁剪图像的路径 clip.in_features = @"";//裁剪图像的路径 clip.out_feature_class = @"";输出路径 gp.Execute(clip, null);
那么怎么设计这个工具的界面和代码呢?按照上面的步骤试验一下吧。
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 cliptool
{
public partial class ClipToolDlg : Form
{
[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 ClipToolDlg(IHookHelper hookHelper)
{
InitializeComponent();
m_hookHelper = hookHelper;
}
private void ClipToolDlg_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)
{
cboLayers1.Items.Add(layer.Name);
cboLayers2.Items.Add(layer.Name);
}
//select the first layer
if (cboLayers1.Items.Count > 0)
cboLayers1.SelectedIndex = 0;
string tempDir = System.IO.Path.GetTempPath();
txtOutputPath.Text = System.IO.Path.Combine(tempDir, ((string)cboLayers1.SelectedItem + "_Clip.shp"));
}
private void button3_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)cboLayers1.SelectedItem + "_Clip.shp";
DialogResult dr = saveDlg.ShowDialog();
if (dr == DialogResult.OK)
txtOutputPath.Text = saveDlg.FileName;
}
private void button1_Click(object sender, EventArgs e)
{
if (m_hookHelper.FocusMap.LayerCount == 0)
return;
//get the layer from the map
IFeatureLayer layer1 = GetFeatureLayer((string)cboLayers1.SelectedItem);
IFeatureLayer layer2 = GetFeatureLayer((string)cboLayers2.SelectedItem);
if (null == layer1)
{
txtMessages.Text += "Layer " + (string)cboLayers1.SelectedItem + "cannot be found!\r\n";
return;
}
if (null == layer2)
{
txtMessages.Text += "Layer " + (string)cboLayers2.SelectedItem + "cannot be found!\r\n";
return;
}
ScrollToBottom();
txtMessages.Text += "Clip layer: " + layer1.Name + "\r\n";
txtMessages.Text += "\r\nGet the geoprocessor. This might take a few seconds...\r\n";
txtMessages.Update();
Geoprocessor gp = new Geoprocessor();
gp.OverwriteOutput = true;
txtMessages.Text += "Clipping...\r\n";
txtMessages.Update();
ESRI.ArcGIS.AnalysisTools.Clip clip = new ESRI.ArcGIS.AnalysisTools.Clip();
clip.clip_features = layer1;//被裁剪图像的路径
clip.in_features = layer2;//裁剪图像的路径
clip.out_feature_class = txtOutputPath.Text;//输出路径
IGeoProcessorResult results = (IGeoProcessorResult)gp.Execute(clip, null);
if (results.Status != esriJobStatus.esriJobSucceeded)
{
txtMessages.Text += "Failed to Clip layer: " + layer1.Name + "\r\n";
}
txtMessages.Text += ReturnMessages(gp);
ScrollToBottom();
txtMessages.Text += "\r\nDone.\r\n";
txtMessages.Text += "-----------------------------------------------------------------------------------------\r\n";
//scroll the textbox to the bottom
ScrollToBottom();
}
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();
}
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;
}
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);
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Archiver|手机版|科学网 ( 京ICP备07017567号-12 )
GMT+8, 2024-4-25 04:19
Powered by ScienceNet.cn
Copyright © 2007- 中国科学报社