C#开发实验--卫星星历的计算、空间直角坐标和大地坐标的转换、ArcGIS Engine开发的简单实例

前不久在公众号GIS研发看到了暑假公益C#开发教程,教大家ArcGIS Engine开发。

想到了自己本科阶段也学习了C#开发和AE开发,学习了使用C#添加空间等,进行卫星星历的计算、空间直角坐标和大地坐标的转换、ArcGIS Engine开发的简单实例。

下面的ArcGIS Engine实验主要的内容包括:为程序加载工具栏,添加相应的控件,运用treeview控件显示图形要素的基本属性;编程实现打开、保存、另存地图文档的方法;运用工具栏工具进行地图要素的添加;运用状态栏控件实现地图坐标信息的显示。

总体步骤:

1、 新建项目,添加相应的控件、窗体、类库、命名空间,并按照实验指导修改属性;

2、 添加初始化代码,为窗体添加事件,并为事件添加代码;

3、 为初始化工具栏添加按钮代码;

4、 为第二个窗体添加命名空间与程序代码;

5、 为第一个窗体的axMapControl1与axMapControl1添加相应的事件;

6、 添加代码到如图1所示位置;

7、 为工具栏添加按钮;

8、 按要求添加菜单控件与statusStrip控件;

9、 添加类库或修改类库的嵌入式操作类型为false,添加命名空间;

10、具体代码与实验结果。

详细步骤:

1、新建项目,添加相应的控件、窗体、类库、命名空间,并按照实验指导修改属性。

(1)新建一个C#工程项目,并为默认建立的窗体分别添加“ToolBarControl”“TOCControl”“MapControl”“LicenseControl”控件。执行与MapControl控件的绑定操作,并通过“dock”属性排序;

(2)添加菜单控件,分别修改其text属性为:“图查属性”,其name属性为“PropertyViaFeature”。

(3)为工程添加windows类型的窗体,设其name属性为“formProperty”;在工具箱中“windows所有窗体”中找到“treeview”控件,双击添加到窗体中,同时设其dock属性为fill。

(4)添加ArcGIS的引用ESRI.ArcGIS.Geodatabase类库,并且在第一个窗体的代码窗口导入命名空间:

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

并且把引用ESRI.ArcGIS.Geodatabase的嵌入式操作类型改为False。

2、添加初始化代码,为窗体添加事件,并为事件添加代码。

(1)在public partial class Form1 : Form代码下添加如下的初始化语句:

public IMapControl2 pMapControl;
public IToolbarControl2 pToolBarControl;
public ITOCControl2 pTocControl;
public bool toolSelected = false;   

(2)为窗体添加load事件,并添加以下代码:

private void Form1_Load(object sender, EventArgs e)
{
pMapControl = (IMapControl2)axMapControl1.Object;
pTocControl = (ITOCControl2)axTOCControl1.Object;
pToolBarControl = (IToolbarControl2)axToolbarControl1.Object;
pToolBarControl.SetBuddyControl(pMapControl);
pTocControl.SetBuddyControl(pMapControl);
CreateToolBarItem();
}

3、 为初始化工具栏添加按钮代码。

为初始化工具栏建立添加按钮的过程:

private void CreateToolBarItem() {

pToolBarControl.AddItem("esriControls.ControlsOpenDocCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsAddDataCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomInTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomOutTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomInFixedCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomOutFixedCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapPanTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapFullExtentCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomToLastExtentBackCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapZoomToLastExtentForwardCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsSelectFeaturesTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsClearSelectionCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsSelectTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapIdentifyTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapFindCommand", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

pToolBarControl.AddItem("esriControls.ControlsMapMeasureTool", -1, -1, false, 0, esriCommandStyles.esriCommandStyleIconOnly);

4、 为第二个窗体添加命名空间与程序代码。

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase; 
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;

在 public partial class frmProperty : Form下添加如下代码:

public IMapControl2 pMapControl;
public IEnvelope pEnvelop;
public frmProperty(IMapControl2 pFMapControl, IEnvelope pFEnvelop)
{
    InitializeComponent();
    pMapControl = pFMapControl;
    pEnvelop = pFEnvelop;
}
public void SelectPropertyViaFeature()
{
   treeView1.Nodes.Clear();
   for (int i = 0; i < pMapControl.Map.LayerCount; i++)
      {
        IFeatureLayer pFeatureLayer = (IFeatureLayer)pMapControl.Map.get_Layer(i);
        IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
        ISpatialFilter pSpatialFilter = new SpatialFilterClass();
        pSpatialFilter.Geometry = pEnvelop;
        pSpatialFilter.GeometryField = pFeatureClass.ShapeFieldName;
        pSpatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
        IFields pFields = pFeatureClass.Fields;
        IFeatureCursor pFeatureCursor = pFeatureClass.Search(pSpatialFilter, false);
        TreeNode nodeParent;
        IFeature pFeature;
        pFeature = pFeatureCursor.NextFeature();        
        nodeParent = treeView1.Nodes.Add(pFeatureLayer.Name.ToString());
        while (pFeature != null)
         {
            TreeNode nodeSon;
           for (int j = 0; j < pFields.FieldCount; j++)
           {
              string fldValue;
              string fldName;
              fldName = pFields.get_Field(j).Name;
              if (fldName == "Shape")
              {
                fldValue = Convert.ToString(pFeature.Shape.GeometryType);
              }
              else
              fldValue = Convert.ToString(pFeature.get_Value(j));
              nodeSon = nodeParent.Nodes.Add(fldValue);
            }
            pMapControl.Map.SelectFeature(pFeatureLayer, pFeature);
            pFeature = pFeatureCursor.NextFeature();
          }
      }

      IActiveView pActiveView;
      pActiveView = (IActiveView)pMapControl.Map;
      pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeoSelection, null, null);
    }
    private void frmProperty_Load(object sender, EventArgs e)
    {
      SelectPropertyViaFeature();
    }

5、 为第一个窗体的axMapControl1与axMapControl1添加相应的事件。

Private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
    {
      if (e.button == 1 && toolSelected == true)
      {
        IEnvelope pEnvelop = pMapControl.TrackRectangle();
        pMapControl.Map.ClearSelection();
        frmProperty fProperty = new frmProperty(pMapControl, pEnvelop);
        fProperty.Show();
      }
    }

6、 添加代码到如图1所示位置。

在这里插入图片描述
**关注公众号后,回复:C#实验**

但是老师教程将代码添加到了图2位置中,我一开始是添加错了,后来也能运行。再看下图,没有相应的语句产生。所以也可以当有错误提示时,将该句复制过去。

7、 为工具栏添加按钮。

为工具栏添加如下控件

8、 按要求添加菜单控件与statusStrip控件。

(1)添加菜单控件,依次添加菜单栏的主菜单text属性为“文件”,其name属性为“menuFile”,依次添加子菜单,其text属性分别为“打开文档”,“保存文档”,“另存为….”,“退出”,其name属性分别为“menuOpenDoc”,“menuSaveDoc”,“menuSaveAsDoc”,“menuExitApp”。

(2) 打开工具箱,通过添加选项,添加statusBar控件,并双击添加到窗体上

9、 添加类库或修改类库的嵌入式操作类型为false,添加命名空间。

添加ArcGIS的引用ESRI.ArcGIS.GeodatabaseESRI.ArcGIS.ADF类库,修改嵌入式操作类型,在第一个窗体的代码窗口导入命名空间:

using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.SystemUI;

代码:

Form1.cs

Program.cs

结果图:

打开地图文档
图层属性查询
工具条的使用
文档另存

  • 18
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值