ArcGis开发实验报告.doc
本科生实验(实习)报告学 院: 资源与环境学院 课程名称: ArcGIS应用与开发 班 级: 09级地理信息系统 姓 名: 张通 学 号: 指导教师: 闫培洁 教务处印制ArcGis开发实验报告1. 实验名称:ArcEngine应用及开发2. 实验目的:熟练并掌握用vs2005开发Gis应用软件。3. 实验要求:根据笔记步骤进行操作,对每一实验内容做好记录;实验后,写出实验报告。认真上机操作,建立感性认识。 实验报告内容包括:实验名称、目的、内容、记录、分析总结。4. 实验步骤及内容: a添加shp文件。b添加lyr文件。c显示洲地图部分属性。d综合以上上三步,做一个综合的显示实验。添加shp文件依次添加ArcGis Engine Toolbarcontrol ArcGis Engine Toccontrol ArcGis Engine Mapcontrol license comtrol这四个控件,调整布局并且关联它们。添加menustrip控件,命名为“添加shp”,并修改属性名称去掉中文。给ArcGis Engine Toolbarcontrol添加“放大”“缩小”“查看属性”三个功能控件。添加openfiledialog1控件。双击menustrip控件,进入代码编写器,编写代码。添加引用:using System.IO;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.DataSourcesFile;using ESRI.ArcGIS.Carto;输入代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.DataSourcesFile;using ESRI.ArcGIS.Carto;namespace zt public partial class Form1 : Form public Form1() InitializeComponent(); private void shpToolStripMenuItem_Click(object sender, EventArgs e) IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); openFileDialog1.Filter = "shapfile文件(*.shp)|*.shp" openFileDialog1.InitialDirectory = "D:新建文件夹ArcGISArcGlobeData" openFileDialog1.Multiselect = false; DialogResult pDialogResult = openFileDialog1.ShowDialog(); if (pDialogResult != DialogResult.OK) return; string pPath = openFileDialog1.FileName; string pFolder = Path.GetDirectoryName(pPath); string pFileName = Path.GetFileName(pPath); IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0); IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName); IFeatureLayer pFlayer = new FeatureLayerClass(); pFlayer.FeatureClass = pFC; pFlayer.Name = pFC.AliasName; ILayer player = pFlayer as ILayer; IMap pMap = axMapControl1.Map; pMap.AddLayer(player); axMapControl1.ActiveView.Refresh(); 添加lyr在原有的基础上添加,新建menustrip控件,命名为“添加lyr”,双击进入代码编写器编写代码。代码如下:openFileDialog1.Filter = "lyr文件(*.lyr)|*.lyr" openFileDialog1.InitialDirectory = "D:新建文件夹ArcGISArcGlobeData" openFileDialog1.Multiselect = false; DialogResult pDialogResult = openFileDialog1.ShowDialog(); if (pDialogResult != DialogResult.OK) return; string pFileName = openFileDialog1.FileName; axMapControl1.AddLayerFromFile(pFileName); axMapControl1.ActiveView.Refresh();图层属性在原有的基础上添加,新建menustrip控件,命名为“图层属性”,双击进入代码编写器编写代码。创建windons窗体,命名为FrmAttributeTable.cs从工具箱-数据中往新窗体上添加DataGridView控件把DataGridView的Dock属性设置为Fill在窗体上点击右键,选中“查看代码”为窗体添加“Load”事件处理在FrmAttributeTable.cs源代码顶部添加如下三行代码:using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;在窗体类中添加如下代码 private AxMapControl m_MapCtrl; public FrmAttributeTable(AxMapControl pMapCtrl) InitializeComponent(); m_MapCtrl = pMapCtrl; 为窗体的Load事件处理方法添加代码ILayer pLayer = m_MapCtrl.get_Layer(0); IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFC = pFLayer.FeatureClass; IFeatureCursor pFCursor = pFC.Search(null, false); IFeature pFeature = pFCursor.NextFeature(); DataTable pTable = new DataTable(); DataColumn colName = new DataColumn("洲名"); colName.DataType = System.Type.GetType("System.String"); pTable.Columns.Add(colName); DataColumn colArea = new DataColumn("面积"); colArea.DataType = System.Type.GetType("System.String"); pTable.Columns.Add(colArea); int indexOfName = pFC.FindField("CONTINENT"); int indexOfArea = pFC.FindField("Area"); while (pFeature != null) string name = pFeature.get_Value(indexOfName).ToString(); string area = pFeature.get_Value(indexOfArea).ToString(); DataRow pRow = pTable.NewRow(); pRow0 = name; pRow1 = area; pTable.Rows.Add(pRow); pFeature = pFCursor.NextFeature(); dataGridView1.DataSource = pTable; 在Form1.cs文件中加入“图层属性”菜单的Click事件处理FrmAttributeTable frm = new FrmAttributeTable(axMapControl1); frm.ShowDialog();完整代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using ESRI.ArcGIS.Controls;using ESRI.ArcGIS.Carto;using ESRI.ArcGIS.Geodatabase;namespace zt public partial class FrmAttributeTable : Form private AxMapControl m_MapCtrl; public FrmAttributeTable(AxMapControl pMapCtrl) InitializeComponent(); m_MapCtrl = pMapCtrl; private void FrmAttributeTable_Load(object sender, EventArgs e) ILayer pLayer = m_MapCtrl.get_Layer(0); IFeatureLayer pFLayer = pLayer as IFeatureLayer; IFeatureClass pFC = pFLayer.FeatureClass; IFeatureCursor pFCursor = pFC.Search(null, false); IFeature pFeature = pFCursor.NextFeature(); DataTable pTable = new DataTable(); DataColumn colName = new DataColumn("洲名"); colName.DataType = System.Type.GetType("System.String"); pTable.Columns.Add(colName); DataColumn colArea = new DataColumn("面积"); colArea.DataType = System.Type.GetType("System.String"); pTable.Columns.Add(colArea); int indexOfName = pFC.FindField("CONTINENT"); int indexOfArea = pFC.FindField("Area"); while (pFeature != null) string name = pFeature.get_Value(indexOfName).ToString(); string area = pFeature.get_Value(indexOfArea).ToString(); DataRow pRow = pTable.NewRow(); pRow0 = name; pRow1 = area; pTable.Rows.Add(pRow); pFeature = pFCursor.NextFeature(); dataGridView1.DataSource = pTable; Form1的完整代码如下:using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.IO;using ESRI.ArcGIS.Geodatabase;using ESRI.ArcGIS.DataSourcesFile;using ESRI.ArcGIS.Carto;namespace zt public partial class Form1 : Form public Form1() InitializeComponent(); private void shpToolStripMenuItem_Click(object sender, EventArgs e) IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory(); openFileDialog1.Filter = "shapfile文件(*.shp)|*.shp" openFileDialog1.InitialDirectory = "D:新建文件夹ArcGISArcGlobeData" openFileDialog1.Multiselect = false; DialogResult pDialogResult = openFileDialog1.ShowDialog(); if (pDialogResult != DialogResult.OK) return; string pPath = openFileDialog1.FileName; string pFolder = Path.GetDirectoryName(pPath); string pFileName = Path.GetFileName(pPath); IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(pFolder, 0); IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace; IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName); IFeatureLayer pFlayer = new FeatureLayerClass(); pFlayer.FeatureClass = pFC; pFlayer.Name = pFC.AliasName; ILayer player = pFlayer as ILayer; IMap pMap = axMapControl1.Map; pMap.AddLayer(player); axMapControl1.ActiveView.Refresh(); private void lyrToolStripMenuItem_Click(object sender, EventArgs e) openFileDialog1.Filter = "lyr文件(*.lyr)|*.lyr" openFileDialog1.InitialDirectory = "D:新建文件夹ArcGISArcGlobeData" openFileDialog1.Multiselect = false; DialogResult pDialogResult = openFileDialog1.ShowDialog(); if (pDialogResult != DialogResult.OK) return; string pFileName = openFileDialog1.FileName; axMapControl1.AddLayerFromFile(pFileName); axMapControl1.ActiveView.Refresh(); private void ToolStripMenuItem_Click(object sender, EventArgs e) FrmAttributeTable frm = new FrmAttributeTable(axMapControl1); frm.ShowDialog(); 实验总结:此次实验中遇到了许多的困难,通过自己核对代码,寻求别人的帮助,最终做出了结果。遇到的问题有:1. 找不到ArcGlobeData文件夹,老出问题。2. 没有添加引用,不识别代码。3. 在做属性图层的时候,不知道如何添加windons窗体4. 不会添加load事件5. 编写代码的时候没有定义变量,导致变量不可用,找不到原因6. 没有编写click事件的代码,程序运行部出来。7. 还有许多代码编写过成遇到的问题,诸如大小写问题等。这个问题是在添加shp、lyr文件后,点击图层属性显示的,就是不弹出属性框,在普燚的帮助下我终于弄明白了,只能在添加shp的时候才能点击属性图层,结果出来了。