大家好,又见面了,我是你们的朋友全栈君。
ArcGIS二次开发基础教程(06):有关图层的基本操作
0. PageLayout和MapControl 的同步
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. 基本图层操作(更名,可见性,比例),矢量图层透明度,栅格图层对比度、亮度和透明度
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中右击图层名显示菜单,单击属性栏显示图层属性表
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;
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163424.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...