中国矿业大学2012级1班实验报告课程名称GIS设计与开发上机时间2015年6月18日学生姓名任金波学号07123176任课教师张海荣中国矿业大学环境与测绘学院成绩基于ArcObjects组件的常用GIS功能实现【上机目的】熟悉VisualStudio开发环境,掌握ArcGISEngine的组件式开发方式,掌握GIS基本功能的开发方法。【上机内容】以ArcObjectsfor.NET为开发包,用C#高级程序设计语言二次开发一个包含GIS基本操作的应用程序,实现地图文档的打开、保存、地图缩放/漫游,属性查询,空间查询,建立并访问地理空间数据库,TOC控件的右键菜单,添加shape数据,栅格数据等。【上机要求】1.完成代码的编写工作,通过对示例代码的学习,把这些分散的代码相互粘合起来,形成一个能实现常用GIS功能的应用程序。学会VisualStudio2010代码常用调试方法,学会QueryInterface。2.了解利用ComGIS进行应用型地理信息系统建设工作的基本思路。3.独立实验,认真、按时完成实验报告。代码编写过程中,同时思考以下问题:(1)数据源如何被加载访问的?ArcGIS数据组织方式?(2)ArcGISEngine开发中所涉及的面向对象知识中诸如类,继承,对象,接口等如何表达的?【详细步骤】一.使用控件创建桌面应用程序1.打开VS2010,新建一个Windows窗体应用程序2.添加控件TOC,Toolbar,Map,License,设置属性,调整界面:3.添加地图文档4.将Toolbar控件和TOC控件与Map控件关联5.添加工具命令6.启动调试按钮,运行程序7.添加语句,重新运行程序二.地图的打开与保存1.新建文件打开菜单2.添加代码privatevoidopenmxd_Click(objectsender,EventArgse){OpenFileDialogOpenFileDialog=newOpenFileDialog();OpenFileDialog.Title=打开amxd文档;OpenFileDialog.Multiselect=false;OpenFileDialog.Filter=*.mxd|*.mxd;OpenFileDialog.RestoreDirectory=true;if(OpenFileDialog.ShowDialog()==DialogResult.OK){try{IMapDocumentmapdocument=newMapDocumentClass();boolispasswordprotected=mapdocument.get_IsPasswordProtected(OpenFileDialog.FileName);if(!ispasswordprotected){axMapControl1.LoadMxFile(OpenFileDialog.FileName,0,string.Empty);}}catch{MessageBox.Show(打开地图文档失败+OpenFileDialog.FileName)}}3.运行程序4新建保存菜单,添加代码privatevoidsavemxd_Click(objectsender,EventArgse){if(axMapControl1.Map.LayerCount=0){}else{IMxdContentsmxdcontents=axMapControl1.MapasIMxdContents;IMapDocumentmapdocument=newMapDocumentClass();mapdocument.Open(axMapControl1.DocumentFilename,);IActiveViewactiveiew=axMapControl1.MapasIActiveView;mapdocument.ReplaceContents(mxdcontents);if(mapdocument.get_IsReadOnly(mapdocument.DocumentFilename)==true){MessageBox.Show(本地图文档只读,不可保存);}else{try{mapdocument.Save(true,true);MessageBox.Show(地Ì?图ª?文?档̦Ì保À¡ê存ä?成¨¦功|);}catch{}}}5.运行程序注:地图所在文件夹名称为中文时出错,修改文件夹名称为英文后运行成功。三.地图的缩放与漫游1.在菜单栏添加放大,缩小,漫游,全局菜单2.添加代码intflag;privatevoidzoomin_Click(objectsender,EventArgse){flag=1;axMapControl1.MousePointer=esriControlsMousePointer.esriPointerZoomIn;}privatevoidzoomout_Click(objectsender,EventArgse){flag=2;axMapControl1.MousePointer=esriControlsMousePointer.esriPointerZoomOut;}privatevoidpan_Click(objectsender,EventArgse){flag=3;axMapControl1.MousePointer=esriControlsMousePointer.esriPointerPan;}privatevoidextent_Click(objectsender,EventArgse){axMapControl1.Extent=axMapControl1.FullExtent;}注:先添加引用usingESRI.ArcGIS.Controls3.添加onmousedown事件处理函数privatevoidaxMapControl1_onmousedown(objectsender,ESRI.ArcGIS.Controls.IMapControlEvents2_OnMouseDownEvente){ESRI.ArcGIS.Geometry.IEnvelopeenvelope=null;switch(flag){case1:{envelope=axMapControl1.TrackRectangle();axMapControl1.Extent=envelope;flag=0;axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;break;}case2:{envelope=axMapControl1.Extent;envelope.Expand(2,2,true);axMapControl1.Extent=envelope;flag=0;axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;break;}case3:{envelope=axMapControl1.Extent;axMapControl1.Pan();flag=0;axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerDefault;break;}}4.运行程序,点击图标实现地图的缩放与漫游四.地图的属性查询1.在菜单栏添加属性查询2.添加Windows窗体:.从工具箱中添加datagridview,设置属性dock为fill:在类中添加代码:usingSystem.Data;//datatable类型在其中,故需在namespace里添加。publicpartialclassattributequeryresult;Form{privatedatatabledatatable;publicattributequeryresult(datatabledatatable){this.datatable=datatable;initializecomponent();}}privatevoidattributequeryresult_load(objectsender,evenargse){if(datatable!=null){datagridview1.datasource=datatable;}}3.在attributequery_click中添加代码:privatevoidattributequery_Click(objectsender,EventArgse){try{ILayerlayer=axMapControl1.get_Layer(0);IFeatureLayerfeaturelayer=layerasFeatureLayer;IFeatureClassfeatureclass=featurelayer.FeatureClass;ILayerFieldslayerfilds=featurelayerasILayerFields;DataSetdataset=newDataSet(dstest);DataTabledatatable=newDataTable(featurelayer.Name);DataColumndatacolumn=null;for(inti=0;ilayerfilds.FieldCount;i++){datacolumn=newDataColumn(layerfilds.get_Field(i).Name);datatable.Columns.Add(datacolumn);datacolumn=null;}IFeaturefeature=null;IQueryFilterqueryfilter=newQueryFilterClass();IFeatureCursorfeaturecursor;queryfilter.WhereClause=POP_CLASS=+'5,000,000andgreater';featurecursor=featureclass.Search(queryfilter,true);feature=featurecursor.NextFeature();while(feature!=null){DataRowdatarow=datatable.NewRow();for(intj=0;jlayerfilds.FieldCount;j++){if(layerfilds.FindField(featureclass.ShapeFieldName)==j){datarow[j]=featureclass.ShapeFieldName.ToString();}else{datarow[j]=feature.get_Value(j);}}datatable.Rows.Add(datarow);feature=featurecursor.NextFeature();}if(datatable.Rows.Count0){attributequeryresultattributequeryresult=newattributequeryresult(datatable);attributequeryresult.ShowDialog();}}catch{}}}}4.查看运行结果五.空间查询1.在菜单栏空间查询里新建点选,矩形选取等菜单,添加click处理函数,设置flag标记当前操作。privatevoid点?选?ToolStripMenuItem_Click(objectsender,EventArgse){flag=4;}2.在axMapControl1_OnMouseDown里添加主要代码