主要有三个需要学习的内容:
1.不同窗体间AxMapControl1的传递问题;
2.如何获取axTOCControl1选中的图层;
3.如何获取选中图层的路径;
4.如何对GDB空间中的点线面进行简单符号化
。
本文只讨论对GDB空间中的点线面要素类进行符号化,不讨论对.shp文件进行符号化。
两者的区别无非是工作空间Iworkspace的实例化方式不同而已。
一个是IWorkspaceFactory wkf = new FileGDBWorkspaceFactoryClass();
一个是IWorkspaceFactory wkf =new ShapefileWorkspaceFactoryClass();
学习开始:
1.不同窗体间AxMapControl1的传递问题;
需要引用AxMapControl1的窗体设置属性:
IMapControl2 axMapControl1;
public IMapControl2 AxMapControl1
{
set { axMapControl1 = value; }
}
父窗体实例化设置引用窗体属性:
Form3 form3 = new Form3();
form3.AxMapControl1 = axMapControl1.Object as IMapControl2;
2.如何获取axTOCControl1选中的图层;
ILayer selectLayer;//选中图层
//tocc点击
private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
{
if (axMapControl1.LayerCount == 0)//图层为空时,返回
return;
esriTOCControlItem item = esriTOCControlItem.esriTOCControlItemNone;//创建TOCControl中item的实力
IBasicMap map = null;
ILayer layer = null;
object other = null;
object index = null;
axTOCControl1.HitTest(e.x, e.y, ref item, ref map, ref layer, ref other, ref index);
if (e.button == 2) //弹出右键菜单
{
if (item == esriTOCControlItem.esriTOCControlItemLayer)//判断是否选中图层,因为该控件还可以选择map
{
contextMenuStrip1.Show(axTOCControl1, e.x, e.y);//在鼠标单击位置弹出快捷菜单(以TOCControl控件为边界)
selectLayer = layer;//selIlayer是一个Ilayer类的全局变量,表示已被选中的图层
}
}
}
将选中的图层selectLayer传给引用窗体Form3
Form3 form3 = new Form3();
form3.selectlayer = selectLayer;
form3.ShowDialog();
3.如何获取选中图层的路径
//获取图层路径
private static string getLayerPath(ILayer pLayer)
{
IDatasetName pDatasetName = (pLayer as IDataLayer2).DataSourceName as IDatasetName;
IWorkspaceName pWorkspaceName = pDatasetName.WorkspaceName;
return pWorkspaceName.PathName ;
}
//通过图层名得到图层
private ILayer GetLayerByName(string strLyrName)
{
ILayer pLayer = null;
bool bFindLayer = false; ;
for (int i = 0; i<axMapControl1.Map.LayerCount; i++)
{
pLayer = axMapControl1.Map.get_Layer(i);
if (pLayer is IGroupLayer || pLayer is ICompositeLayer)
{
ICompositeLayer pComLyr = pLayer as ICompositeLayer;
for (int j = 0; j<pComLyr.Count; j++)
{
pLayer = pComLyr.get_Layer(j);
if (pLayer.Name.Equals(strLyrName))
{
bFindLayer = true;
break;
}
else
{
pLayer = null;
}
}
}
else
{
if (pLayer.Name == strLyrName)
{
bFindLayer = true;
}
else
{
pLayer = null;
}
}
if (bFindLayer)
{
break;
}
}
return pLayer;
}
getLayerPath(GetLayerByName(selectlayer.Name));
4.如何对GDB空间中的点线面进行简单符号化
/// 辅助将.NET颜色转换为AE颜色
private IColor ConvertNETColorToAEColor(Color pColor)
{
IRgbColor rgbColor = new RgbColorClass();
rgbColor.Red = pColor.R;
rgbColor.Blue = pColor.B;
rgbColor.Green = pColor.G;
return rgbColor as IColor;
}
/// 辅助将AE颜色转换为.NET下的颜色
private Color ConvertAERgbColorToNETColor(IColor pColor)
{
return ColorTranslator.FromOle(pColor.RGB);
}
点:
public void pointsimplemarkstyle()
{
//获取要素类,装在到矢量图层并添加到当前地图
IFeatureClass fc;
IWorkspaceFactory wkf = new FileGDBWorkspaceFactoryClass();
IWorkspace gdbWP = wkf.OpenFromFile(getLayerPath(GetLayerByName(selectlayer.Name)), 0);
//IWorkspace gdbWP = wkf.OpenFromFile(appPath + "\\fileGDB.gdb", 0);
if (gdbWP == null) { MessageBox.Show("打开空间数据库失败!"); return; }
IFeatureWorkspace fWP = gdbWP as IFeatureWorkspace;
fc = fWP.OpenFeatureClass(selectlayer.Name);
IFeatureLayer fLyr = new FeatureLayerClass();
this.axMapControl1.Map.DeleteLayer(GetLayerByName(selectlayer.Name));
fLyr.Name = selectlayer.Name;
fLyr.FeatureClass = fc;
this.axMapControl1.Map.AddLayer(fLyr as ILayer);
//MessageBox.Show(selectlayer.Name);
//得到选中图层的所在的路径,不包含自己的名字
// getLayerPath(GetLayerByName(selectlayer.Name));
//得到选中图层的名字
//GetLayerByName(selectlayer.Name)
//创建简单风格对象设置当前图层显示风格
IGeoFeatureLayer geofLyr = fLyr as IGeoFeatureLayer;
ISimpleRenderer sR = new SimpleRendererClass();
创建COLOR对象,并使用其设置线风格
//ICmykColor cmykColor = new CmykColorClass();
//cmykColor.Cyan = 0; cmykColor.Magenta = 100; cmykColor.Yellow = 100; cmykColor.Black = 0;
//使用符号风格对象
IMarkerSymbol pSymbol = new SimpleMarkerSymbolClass();
ISimpleMarkerSymbol mkSymbol = pSymbol as ISimpleMarkerSymbol;
//mkSymbol.Style = esriSimpleMarkerStyle.esriSMSCross;
mkSymbol.Style = esriSimpleMarkerStyle.esriSMSCircle;
//mkSymbol.Angle =45;
// pSymbol.Color = cmykColor as IColor;
pSymbol.Color = ConvertNETColorToAEColor(label6.BackColor);
pSymbol.Size =Convert.ToInt32(textBox1.Text);
pSymbol.Angle = 45;
sR.Symbol = pSymbol as ISymbol;
geofLyr.Renderer = sR as IFeatureRenderer;
this.axMapControl1.Refresh();
}
线:
public void linesimplemarkstyle()
{
IFeatureClass fc;
IWorkspaceFactory wkf = new FileGDBWorkspaceFactoryClass();
IWorkspace gdbWP = wkf.OpenFromFile(getLayerPath(GetLayerByName(selectlayer.Name)), 0);
//IWorkspace gdbWP = wkf.OpenFromFile(appPath + "\\fileGDB.gdb", 0);
if (gdbWP == null) { MessageBox.Show("打开空间数据库失败!"); return; }
IFeatureWorkspace fWP = gdbWP as IFeatureWorkspace;
fc = fWP.OpenFeatureClass(selectlayer.Name);
IFeatureLayer fLyr = new FeatureLayerClass();
this.axMapControl1.Map.DeleteLayer(GetLayerByName(selectlayer.Name));
fLyr.Name = selectlayer.Name;
fLyr.FeatureClass = fc;
this.axMapControl1.Map.AddLayer(fLyr as ILayer);
this.axMapControl1.Refresh();
//创建简单风格对象设置当前图层显示风格
IGeoFeatureLayer geofLyr = fLyr as IGeoFeatureLayer;
ISimpleRenderer sR = new SimpleRendererClass();
//使用线风格
ISimpleLineSymbol splSymbol = new SimpleLineSymbolClass();
splSymbol.Style = esriSimpleLineStyle.esriSLSSolid;
ILineSymbol lSymbol = splSymbol as ILineSymbol;
lSymbol.Color = ConvertNETColorToAEColor(label6.BackColor);
lSymbol.Width = Convert.ToInt32(textBox2.Text);
sR.Symbol = lSymbol as ISymbol;
geofLyr.Renderer = sR as IFeatureRenderer;
this.axMapControl1.Refresh();
}
面:
public void polygonsimplemarkstyle()
{
//获取要素类,装在到矢量图层并添加到当前地图
IFeatureClass fc;
IWorkspaceFactory wkf = new FileGDBWorkspaceFactoryClass();
IWorkspace gdbWP = wkf.OpenFromFile(getLayerPath(GetLayerByName(selectlayer.Name)), 0);
//IWorkspace gdbWP = wkf.OpenFromFile(appPath + "\\fileGDB.gdb", 0);
if (gdbWP == null) { MessageBox.Show("打开空间数据库失败!"); return; }
IFeatureWorkspace fWP = gdbWP as IFeatureWorkspace;
fc = fWP.OpenFeatureClass(selectlayer.Name);
IFeatureLayer fLyr = new FeatureLayerClass();
this.axMapControl1.Map.DeleteLayer(GetLayerByName(selectlayer.Name));
fLyr.Name = selectlayer.Name;
fLyr.FeatureClass = fc;
this.axMapControl1.Map.AddLayer(fLyr as ILayer);
this.axMapControl1.Refresh();
//创建简单风格对象设置当前图层显示风格
IGeoFeatureLayer geofLyr = fLyr as IGeoFeatureLayer;
ISimpleRenderer sR = new SimpleRendererClass();
//使用填充风格
ISimpleFillSymbol splSymbol = new SimpleFillSymbolClass();
splSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
IFillSymbol fSymbol = splSymbol as IFillSymbol;
fSymbol.Color = ConvertNETColorToAEColor(label6.BackColor);
sR.Symbol = fSymbol as ISymbol;
geofLyr.Renderer = sR as IFeatureRenderer;
this.axMapControl1.Refresh();
}