前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >ArcGIS二次开发基础教程(06):有关图层的基本操作

ArcGIS二次开发基础教程(06):有关图层的基本操作

作者头像
全栈程序员站长
发布2022-09-15 11:32:05
1.5K0
发布2022-09-15 11:32:05
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

ArcGIS二次开发基础教程(06):有关图层的基本操作

0. PageLayout和MapControl 的同步

代码语言:javascript
复制
void CopyToPage()
   {
       //对象拷贝,把mapcontrol的地图拷贝重写到pagelayout里
       IObjectCopy copy = new ObjectCopyClass();
       object fromMap = axMapControl1.Map as Object;
       object toMap = axPageLayoutControl1.ActiveView.FocusMap as object;
       object copyMap = copy.Copy(fromMap) as object;
       copy.OverWrite(toMap,copyMap);
   }

//mapdontrol的事件
   void OnAfterScreenDraw(object sender, IMapControlEvents2_OnAfterScreenDrawEvent e)
   {
       //mapcontrol每次刷新都将显示范围设置为mapcontrol的视图
       IActiveView activeView =IPageLayoutControl1.ActiveView.FocusMap;
       IDisplayTransformation displayTransformation = activeView.ScreenDisplay.DisplayTransformation;
       displayTransformation.VisibleBounds = axMapControl1.Extent;
       axPageLayoutControl1.Refresh();
       CopyToPage();
   }

//mapcontrol的事件
   void OnMapReplaced(object sender, IMapControlEvents2_OnMapRepalcedEvent e)
   {
       //增添数据时调用
        CopyToPage();   
   }

1. 基本图层操作(更名,可见性,比例),矢量图层透明度,栅格图层对比度、亮度和透明度

代码语言:javascript
复制
private IFeatureLayer GetLayerByName(string name)
{
    IFeatureLayer featureLayer = null;
    for(int i=0;i<axMapControl1.LayerCount;i++)
    {
        featureLayer = axMapControl1.get_Layer(i) as IFeatureLayer;
        if(featureLayer.Name.Equals(name))
        {
            return featureLayer;
		}
	}
    return featureLayer;
}

//此处只演示操作 故属性值写死
IFeaturLayer layer = GetLayerByName("图层名称") as IFeatureLayer;
//更名
layer.Name = "新图层名称";
//可见性
layer.Visible = false;
//比例
layer.MinimumScale = 1500000;
layer.MaximumScale = 3000000;
//透明度
IFeatureEffects featureEffects = layer as IFeatureEffects;
featureEffects.Transparency = 10;
//栅格对比度、亮度和透明度
IRasterLayer rasterLayer = GetLayerByName("图层名称") as IRasterLayer;
IFeatureEffects featureEffects = rasterLayer as IFeatureEffects;
featureEffects.Contrast = 30;//对比度
featureEffects.Brightness = 50;//亮度
featureEffects.Transparency = 100;//透明度

2. 在TOCControl中右击图层名显示菜单,单击属性栏显示图层属性表

代码语言:javascript
复制
ILayer layer = null;//定义图层为全局变量
//TOCControl的点击事件
void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
    if(e.Button == 2)//如果是右击
    {
        //TOCControl提供了HitTest方法用来检测控件上被击中的item,参数详情可查阅帮助文档
        IBasicMap basicMap = new MapClass();
        //用来存储被击中的图层
        layer = new FeatureLayerClass();
        object other = new object();
        object index = new object();
        esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;
        axTOCControl1.HitTest(e.x, e.y, ref utem, ref layer, ref other, ref index);
        if(item == esriTOCControlItem.esriTOCControlItemLayer)
        {
            //ContextMenuStrip控件 在右键对应位置显示
            contextMenuStrip1.Show(axTOCContorl,new System.Drawing.Point(e.x, e.y));
        }
	}
}
//图层菜单的属性栏点击事件
void attributeToolStripMenuItem_Click(object sender, EventArgs e)
{
    //Attribute是我新建的窗体,构造函数用点击的图层作为参数
    Attribute attribute = new Attribute(layer);
    attribute.Show();
}
//以下是Attribute窗体中的代码
private ILayer myLayer;
public Attribute(ILayer layer)
{
    InitializeComponent();
    myLayer = layer;
}
//Attribute窗体的load事件,当窗体加载的时候把属性表显示出来
private void Attribute_Load(object sender, EventArgs e)
{
    //建表
    DataTable dt = new DataTable();
    DataColumn dc;
    //按表属性字段添加列
    for(int i=0;i<feature.Fields.FieldCount;i++)
    {
     	string name = feature.Fields.get_Field(i).Name;
        dc = new DataColumn(name);
        dc.Columns.Add(dc);
    }
    //用要素类的查询方法,获取要素指示光标(要素的遍历是通过指示光标来完成的)
    //设置查询条件为null,第二个参数True表示要素属性不可更改
    IFeatureLayer featureLayer = myLayer as IFeatureLayer;
    IFeatureCursor cursor = featureLayer.FeatureClass.Search(null,true);
    IFeature feature = cursor.Next();
    while(feature!=null)
    {
        DataRow dr = dt.NewRow();//获取表样式的行
        for(int i=0;i<feature.Fields.FieldCount;i++)
        {
            //Shape属性需匹配类型再赋值
            if(feature.get_Value(i).Name.Equals("Shape"))
            {
                switch(feature.Shape.GeometryType)
                {
                    case esriGeometryType.esriGeometryPoint:
                        dr[i] = "Point";
                        break;
                    case esriGeometryType.esriGeometryLine:
                        dr[i] = "Line";
                        break;
                    case esriGeometryType.esriGeometryPolygon:
                        dr[i] = "Polygon";
                        break;
                    case esriGeometryType.esriGeometryPolyline:
                        dr[i] = "Polyline";
                        break;
                    default:
                        dr[i] = "Other";
                        break;
                }
                continue;
            }
            //其他属性直接赋值
            dr[i] = feature.get_Value(i).ToString();
        }
        dt.Rows.Add(dr);
        feature = cursor.Next();
    }
    //用一个DataGridView控件显示表,设置数据源为上面建的表
    dataGridView1.DataSource = dt;
}

历届GIS应用技能大赛开发题答案点这里,尚在不定期更新中

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163424.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • ArcGIS二次开发基础教程(06):有关图层的基本操作
    • 0. PageLayout和MapControl 的同步
      • 1. 基本图层操作(更名,可见性,比例),矢量图层透明度,栅格图层对比度、亮度和透明度
        • 2. 在TOCControl中右击图层名显示菜单,单击属性栏显示图层属性表
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档