当前位置:   article > 正文

ArcEngine教程(三)——图层的基本操作_arcengine 加载图层

arcengine 加载图层

本篇主要介绍图层的基本操作,包括打开、删除。

首先说下图层,地理数据是以图层的形式加载到地图对象(Map)上的,图层是作为一个中介链接地图对象和数据,图层中不存储地理数据,只添加了地理数据的引用。地理数据始终保存在地理数据文件或GeoDatabase中。

一、打开图层
打开图层的思路:

  1. 打开地理数据文件
  2. 新建图层,将图层对应的地理数据指向第1步打开的地理数据文件
  3. 将第2部新建的图层添加到Map对象中,刷新MapControl控件,显示地理数据

先上代码:

/// <summary>
/// 点击 添加图层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_addLayer_Click(object sender, EventArgs e)
{
    //一、打开shape文件
    OpenFileDialog ofd = new OpenFileDialog();
    ofd.Title = "打开图层文件";
    ofd.Filter = "map documents(*.shp)|*.shp";
    if (DialogResult.OK != ofd.ShowDialog())
    {
        return;
    }
    FileInfo shpFile = new FileInfo(ofd.FileName);
    //文件所在目录
    string folder = shpFile.DirectoryName;
    //文件的名称
    string fileName = shpFile.Name;
    //使用Feature工作空间对象,打开刚刚选择的shapefile文件
    IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactory();
    IFeatureWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(folder, 0) as IFeatureWorkspace;
    IFeatureClass pFeatureClass = pWorkspace.OpenFeatureClass(fileName);

    //二、新建图层对象,将图层对象内容设置为打开的文件
    IFeatureLayer pLayer = new FeatureLayer();
    //将上面一步打开的IFeatureClass,作为这个图层的内容
    pLayer.FeatureClass = pFeatureClass;
    pLayer.Name = pFeatureClass.AliasName;

    //三、将图层添加到MapControl中,并刷新
    axMapControl1.Map.AddLayer(pLayer);
    axMapControl1.ActiveView.Refresh();
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35

需要添加三个引用:

using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.Geodatabase;
  • 1
  • 2
  • 3

关于ArcEngine添加引用,详见:https://blog.csdn.net/qq_42956227/article/details/82592653

二、 删除图层
删除图层的逻辑:

  1. 获取选中的图层
  2. 删除这个图层

然后麻烦的地方就是获取选中的图层,由于TOCControl控件中是无法直接返回选中图层的索引的,所以调用HitTest方法获取图层。
先介绍下HitTest方法:

public void HitTest ( 
    int X, int Y, 
    ref esriTOCControlItem ItemType, 
    ref IBasicMap BasicMap, 
    ref ILayer Layer, 
    ref object Unk, 
    ref object Data 
);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

参数的含义如下:

  1. X,Y :鼠标点击的坐标;
  2. ITemType: esriTOCControlItem枚举常量
  3. BasicMap:绑定MapControl的IBasicMap接口
  4. Layer:被点击的图层
  5. Unk:TOCControl的LegendGroup对象
  6. Data:LegendClass在LegendGroup中的Index

esriTOCControlItem枚举常量:

nameindexremarks
esriTOCControlItemNone0没有对象
esriTOCControlItemMap1Map对象
esriTOCControlItemLayer2Layer对象
esriTOCControlItemHeading3对象的标题
esriTOCControlItemLegendClass4LegendClass对象

附上所有代码:

/// <summary>
/// 删除当前选中图层
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btn_deleteLayer_Click(object sender, EventArgs e)
{
    //一、判断选中的图层是否为空
    if (m_layer == null)
    {
        MessageBox.Show("请选择图层");
        return;
    }

    //二、删除图层
    IMap pMap = axMapControl1.Map;
    pMap.DeleteLayer(m_layer);
    m_layer = null;
}

//被选中的图层
ILayer m_layer = null;

/// <summary>
/// 获取鼠标点击的图层。由于TOCControl控件中是无法直接返回选中图层的索引的,所以调用HitTest方法获取图层。
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
    if (axMapControl1.LayerCount == 0)
    {
        return;
    }
    esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
    IBasicMap map = null;
    object other = null;
    object index = null;
    //根据单击的x、y坐标返回相应的参数,包括选中图层、地图、索引等
    axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref m_layer, ref other, ref index);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/341053
推荐阅读
  

闽ICP备14008679号