《GS软件设计》实习报告
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 _bxwMap {
public partial class BufferAnalysisForm : Form {
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; }
private void BufferAnalysisForm_Load(object sender, EventArgs e)
{
if (null == m_hookHelper || null == m_hookHelper.Hook || 0 == m_hookHelper.FocusMap.LayerCount)
return;
IEnumLayer layers = GetLayers(); layers.Reset();
ILayer layer = null;
while ((layer = layers.Next()) != null) {
cboLayers.Items.Add(layer.Name); }
if (cboLayers.Items.Count > 0) cboLayers.SelectedIndex = 0;
string tempDir = System.IO.Path.GetTempPath();
txtOutputPath.Text = System.IO.Path.Combine(tempDir, ((string)cboLayers.SelectedItem + \
35
《GS软件设计》实习报告
//set the default units of the buffer int units = Convert.ToInt32(m_hookHelper.FocusMap.MapUnits);
cboUnits.SelectedItem = units; }
private void btnOutputLayer_Click(object sender, EventArgs e)
{
//set the output layer
SaveFileDialog saveDlg = new SaveFileDialog(); saveDlg.CheckPathExists = true;
saveDlg.Filter = \ saveDlg.OverwritePrompt = true; saveDlg.Title = \ saveDlg.RestoreDirectory = true;
saveDlg.FileName = (string)cboLayers.SelectedItem + \
DialogResult dr = saveDlg.ShowDialog(); if (dr == DialogResult.OK)
txtOutputPath.Text = saveDlg.FileName; }
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)) ||
\!= System.IO.Path.GetExtension(txtOutputPath.Text))
36
《GS软件设计》实习报告
{
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 + \
txtMessages.Text += \正在获取空间数据。这可能需要几秒钟时间...\\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) {
37
《GS软件设计》实习报告
txtMessages.Text += \缓冲区失败的图层: \+ layer.Name + \
}
txtMessages.Text += ReturnMessages(gp); //scroll the textbox to the bottom ScrollToBottom();
txtMessages.Text += \完成!\\r\\n\ txtMessages.Text += \
//scroll the textbox to the bottom ScrollToBottom();
//修改当前指针样式
this.Cursor = Cursors.Default; }
private void btnCancel_Click(object sender, EventArgs e) {
this.Close(); }
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(\gp.GetMessage(Count));
} }
return sb.ToString(); }
private IFeatureLayer GetFeatureLayer(string layerName) {
IEnumLayer layers = GetLayers(); layers.Reset();
ILayer layer = null;
while ((layer = layers.Next()) != null) {
38
《GS软件设计》实习报告
if (layer.Name == layerName)
return layer as IFeatureLayer; }
return null; }
private IEnumLayer GetLayers() {
UID uid = new UIDClass();
uid.Value = \ 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);
} } }
五、GIS软件设计小结
本学期的GIS软件设计的实习步入了尾声。在这两周的时间里,从无到有,从0开始构建一个程序的框架、逐步的完善它的功能。这是一个令人欣喜的过程,在这个过程中我也收获良多。
本次实习要用C#语言来完成一个小型的GIS平台系统,主要是对ArcAgine的二次开发。虽然完成的功能不是很多,但毕竟是本专业领域的内容,要用到很多以前所学的GIS基础知识,是对所学知识一次很好的总结。在完成的过程中也遇到了很多困难。这个程序比以前碰到的都要复杂,从课本上能得到的帮助很少,可以拿来用的现成方法更少。需要自己查阅大量资料,归纳总结得出最好的实施步骤。在实际操作中由于对代码不熟悉经常出现报错,报错之后要很久才能找到错误原因,又需要很久才能排除错误。还有对VS2005软件某些功能不熟悉,操作时用了比较笨的、费时间的方法。在实习过程中,老师、同学都给了我很多帮助,在这些帮助下我才能最终完成了这个程序,在此对他们的热情帮助表示感谢。
本次完成的系统中还有一些不足,比如界面不够友好和美观。代码写的比较乱,维护和改正错误的时候比较困难。实现的功能还不够完善等。这些不足还需要提高。经过这两周的实习我对GIS软件开发产生了浓厚的兴趣,在接下来的课程学习中希望可以学到更多相关知识。
39
《GS软件设计》实习报告
40