功能说明:

针对基础部分和三维数据库建设部分实习内容,进行校园二、三维地理信息系统设计,完成校园地理信息系统二、三维数据库设计和实现,并完成简单的查询、浏览和分析功能。本次实习开发语言为C#,开发平台为vs2010。

界面设计:

在本次实习中,新建的程序项目为窗体型,在窗体中拖入控件MenuStrip、ToolbarControl、StatusStrip、SplitContainer、TabControl、MapControl、PageLayoutControl 等控件,并设置其相关的Dock 属性、Name 和Text属性等,然后进行页面绑定,并添加相应的ArcGISD的工具,本次开发整体页面布局如下:

arcgis 二次开发python 版本 gis二次开发语言_二次开发


1.菜单的添加及其实现

添加菜单,并在其属性面板中设置 Name 属性,然后实现相关菜单,双击事件按钮打到“ Load ”事件并双击,添加此事件,其中新建文件、打开文件、加载数据、关闭文件等部分代码如下:

private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
 { //文件路径名称,包含文件名称和路径名称
 string strName = null;
 //定义OpenFileDialog,获取并打开地图文档
 OpenFileDialog openFileDialog = new OpenFileDialog();
 openFileDialog.Title = “打开MXD”;
 openFileDialog.Filter = “MXD文件(.mxd)|.mxd”;
 if (openFileDialog.ShowDialog() == DialogResult.OK)
 { strName = openFileDialog.FileName;
 if (strName != “”)
 { this.axMapControl1.LoadMxFile(strName); } }
 this.axMapControl1.Extent = this.axMapControl1.FullExtent;
 }
 private void AddData_Click(object sender, EventArgs e)
 { //添加数据
 int currentLayerCount = this.axMapControl1.LayerCount;
 ICommand pCommand = new ControlsAddDataCommandClass();
 pCommand.OnCreate(this.axMapControl1.Object);
 pCommand.OnClick(); }
 private void Save_Click(object sender, EventArgs e)
 { if (null != m_pageLayoutControl.DocumentFilename&& m_mapControl.CheckMxFile(m_page
 LayoutControl.DocumentFilename))
 { // 创建一个新的地图文档实例
 IMapDocument mapDoc = new MapDocumentClass();
 // 打开当前地图文档
 mapDoc.Open(m_pageLayoutControl.DocumentFilename, string.Empty);
 // 用 PageLayout 中的文档替换当前文档中的 PageLayout 部分
 mapDoc.ReplaceContents((IMxdContents)m_pageLayoutControl.PageLayout);
 // 保存地图文档
 mapDoc.Save(mapDoc.UsesRelativePaths, false);
 mapDoc.Close(); } }
 private void SaveAs_Click(object sender, EventArgs e)
 { // 调用另存为命令
 ICommand command = new ControlsSaveAsDocCommandClass();
 command.OnCreate(m_controlsSynchronizer.ActiveControl);
 command.OnClick(); }

添加完成后运行时的界面如下图所示:

arcgis 二次开发python 版本 gis二次开发语言_C#_02


2、MapControl 与PageLayoutControl 同步

为了能够很方面地进行MapView 和Layout View 两种视图的切换,以及二者之间的数据是同步显示,比较简单的方法莫过于二者共享一份地图了,这也是最常用的方法。首先要新建同步类ControlsSynchronizer,新建Maps 类、新建打开文档类OpenNewMapDocument,并在OnClick 函数中添加相应代码,进而实现两种视图的同步。

3、状态栏信息的添加与实现

应用程序的状态栏一般用来显示程序的当前状态,当前所使用的工具。GIS 应用程序一般也在状态栏显示当前光标的坐标、比例尺等信息。为了实现此功能,首先我们要添加状态栏项目、然后显示当前所用工具信息、显示当前比例尺、显示当前坐标,。运行界面截图如下图所示:

arcgis 二次开发python 版本 gis二次开发语言_GIS_03


4、按照属性查询

按照属性查询时,要对待查询图选择相应的字段、图层,然后设置属性进行查询,查询是设计界面如下:

arcgis 二次开发python 版本 gis二次开发语言_GIS_04


按照属性查询时代码如下:

private void 属性ToolStripMenuItem_Click(object sender, EventArgs e)
 { //初始化属性查询窗体
 AttributeQueryForm attributeQueryForm = new AttributeQueryForm(this.axMapControl1);
 attributeQueryForm.Show(); }
 private DataTable LoadQueryResult(AxMapControl mapControl, IFeatureLayer featureLayer, IGeometry geometry)
 {IFeatureClass pFeatureClass = featureLayer.FeatureClass;//根据图层属性字段初始化DataTable
 IFields pFields = pFeatureClass.Fields;
 DataTable pDataTable = new DataTable();
 for (int i = 0; i < pFields.FieldCount; i++)
 { string strFldName;
 strFldName = pFields.get_Field(i).AliasName;
 pDataTable.Columns.Add(strFldName); }
 ISpatialFilter pSpatialFilter = new SpatialFilterClass();
 pSpatialFilter.Geometry = geometry;
 //根据图层类型选择缓冲方式
 switch (pFeatureClass.ShapeType)
 { case esriGeometryType.esriGeometryPoint:
 pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelContains; break;
 case esriGeometryType.esriGeometryPolyline:
 pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelCrosses; break;
 case esriGeometryType.esriGeometryPolygon:
 pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects; break; }
 //定义空间过滤器的空间字段
 pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
 IQueryFilter pQueryFilter;
 IFeatureCursor pFeatureCursor;
 IFeature pFeature;
 //利用要素过滤器查询要素
 pQueryFilter = pSpatialFilter as IQueryFilter;
 pFeatureCursor = featureLayer.Search(pQueryFilter, true);
 pFeature = pFeatureCursor.NextFeature();
 while (pFeature != null)
 { string strFldValue = null;
 DataRow dr = pDataTable.NewRow();//遍历图层属性表字段值,并加入pDataTable
 for (int j = 0; j < pFields.FieldCount; j++)
 { string strFldName1 = pFields.get_Field(j).Name;
 if (strFldName1 == “Shape”)
 { strFldValue = Convert.ToString(pFeature.Shape.GeometryType); }
 else strFldValue = Convert.ToString(pFeature.get_Value(j));
 dr[j] = strFldValue; }
 pDataTable.Rows.Add(dr);//高亮选择要素
 mapControl.Map.SelectFeature((ILayer)featureLayer, pFeature);
 mapControl.ActiveView.Refresh();
 pFeature = pFeatureCursor.NextFeature(); }
 return pDataTable; }

程序运行时截图如下:

arcgis 二次开发python 版本 gis二次开发语言_位置查询_05


arcgis 二次开发python 版本 gis二次开发语言_位置查询_06


arcgis 二次开发python 版本 gis二次开发语言_GIS_07


5、按照空间位置查询

按照空间位置进行查询时,首先要选择图层,然后选择查询方式,查询方式包括点、线、圆、矩形4种方式,按照位置查询代码如下:

private void 位置ToolStripMenuItem_Click(object sender, EventArgs e)
 { SpatialQueryForm spatialQueryForm = new SpatialQueryForm(this.axMapControl1);
 if (spatialQueryForm.ShowDialog() == DialogResult.OK)
 { //标记为“空间查询”,获取查询方式和图层
 this.mTool = “SpaceQuery”;
 this.mQueryMode = spatialQueryForm.mQueryMode;
 this.mLayerIndex = spatialQueryForm.mLayerIndex;
 this.axMapControl1.MousePointer=ESRI.ArcGIS.Controls.esriControlsMousePointer.esriPointerCrosshair; }
 }
 Private void axMapControl1 _OnMouseDown (objectsender, IMapControl Events2_OnMouseDownEvent e)
 { //清空上次选择的结果
 this.axMapControl1.Map.ClearSelection();
 switch (mTool)
 { case “SpaceQuery”: //获取当前视图
 ESRI.ArcGIS.Carto.IActiveView pActiveView = this.axMapControl1.ActiveView;//获取鼠标点
 ESRI.ArcGIS.Geometry.IPoint pPoint = pActiveView.ScreenDisplay.DisplayTransformation.ToMapPoint(e.x, e.y);
 ESRI.ArcGIS.Geometry.IGeometry pGeometry = null;
 switch (this.mQueryMode)
 {case 0: //矩形查询
 pGeometry = this.axMapControl1.TrackRectangle(); break;
 case 1: //线查询
 pGeometry = this.axMapControl1.TrackLine(); break;
 case 2: //点查询
 ESRI.ArcGIS.Geometry.ITopologicalOperator pTopo;
 ESRI.ArcGIS.Geometry.IGeometry pBuffer;
 pGeometry = pPoint;
 pTopo = pGeometry as ESRI.ArcGIS.Geometry.ITopologicalOperator;
 //根据点位创建缓冲区,缓冲半径设为0.1,可自行修改
 pBuffer = pTopo.Buffer(0.1);
 pGeometry = pBuffer.Envelope; break;
 case 3: //圆查询
 pGeometry = this.axMapControl1.TrackCircle();
 break; }
 ESRI.ArcGIS.Carto.IFeatureLayer pFeatureLayer = this.axMapControl1.Map.get_Layer(this.mLayerIndex) as ESRI.ArcGIS.Carto.IFeatureLayer;
 DataTable pDataTable = this.LoadQueryResult(axMapControl1, pFeatureLayer, pGeometry);
 this.dataGridView1.DataSource = pDataTable.DefaultView;
 this.dataGridView1.Refresh(); break; default: break; } }

程序运行时,选择好图层与查询方式后如下图所示,然后在图上用矩形框选一定区域,如下图所示,查询结果亦见下图。

arcgis 二次开发python 版本 gis二次开发语言_GIS_08


arcgis 二次开发python 版本 gis二次开发语言_属性查询_09