怎么在指定图层添加线元素(C#)
我是初学arcGis,请大家及时伸出援助之手啊 --------------------编程问答-------------------- 画线,首先得到线对象,要用到 IPointCollection 和 IPolyline 对象,然后加载线元素IElement。
/// <summary>
/// 画线
/// <param name="arrPointAll">点坐标数组</param>
///<returns></returns>
private void DrawLine(ArrayList arrPointAll)
{
if (arrPointAll.Count <= 0)//点坐标数组不能为空
{
return;
}
if (activeView == null)
{
activeView = this.MapControl.ActiveView.FocusMap as IActiveView;
}
//删除以前的element
DeleteOldElement(activeView.GraphicsContainer);
// 获取IRGBColor接口
IRgbColor color = new RgbColor();
// 设置颜色属性
color.Red = 255;
color.Transparency = 255;
//点
IPoint pPoint = new PointClass();
//线样式
ISimpleLineSymbol lineSymbol = new SimpleLineSymbolClass();
lineSymbol.Color = color;
lineSymbol.Style = esriSimpleLineStyle.esriSLSInsideFrame;
lineSymbol.Width = 1;
//线元素
ILineElement lineElement = new LineElementClass();
lineElement.Symbol = lineSymbol;
//创建线
IPolyline m_Polyline = new PolylineClass();
//点集合
IPointCollection m_PointCollection = new PolylineClass();
//点数组
ArrayList arrPoint = new ArrayList();
object missing = Type.Missing;
foreach (object o in arrPointAll)
{
arrPoint = (ArrayList)o;
pPoint.PutCoords(int.Parse(arrPoint[0].ToString()), int.Parse(arrPoint[1].ToString()));
m_PointCollection.AddPoint(pPoint, ref missing, ref missing);
}
//QI for IPolyline
m_Polyline = m_PointCollection as IPolyline;
//放大地图
//折线范围
IEnvelope pEnvelope = m_Polyline.Envelope;
//折线区域
IArea pArea = pEnvelope as IArea;
pPoint = pArea.Centroid;
this.ChangeEnvelope(pPoint, 0.06, 0.06);
//QI for IElement
IElement element = lineElement as IElement;
element.Geometry = m_Polyline;
//加载线元素到地图
activeView.GraphicsContainer.AddElement(element, 0);
//Refresh the graphics
activeView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
--------------------编程问答-------------------- 我试过了,没反应,地图上没有增加线 --------------------编程问答-------------------- IFeatureLayer pLayer = (IFeatureLayer)axMapControl1.get_Layer(0);//所要加的层
IFeatureLayer pFeatureLyr = pLayer as IFeatureLayer;//将ILayer转换为IFeaturelayer,为了对图层上的要素进行编辑
IFeatureClass pFeatCls = pFeatureLyr.FeatureClass;//定义一个要素集合,并获取图层的要素集合
IFeatureClassWrite fr = (IFeatureClassWrite)pFeatCls;//定义一个实现新增要素的接口实例,并该实例作用于当前图层的要素集
IWorkspaceEdit w = (pFeatCls as IDataset).Workspace as IWorkspaceEdit;//定义一个工作编辑工作空间,用于开启前图层的编辑状态
IFeature f;//定义一个IFeature实例,用于添加到当前图层上 +
w.StartEditing(true);//开启编辑状态
w.StartEditOperation();//开启编辑操作
IPoint point = new PointClass();
point.SpatialReference = this.axMapControl1.SpatialReference;
IGeometry geometry;
point.X = 200;
point.Y = 300;
geometry = (IGeometry)point;
IPoint point2 = new PointClass();
point2.SpatialReference = this.axMapControl1.SpatialReference;
IGeometry g2;
point2.X = 400;
point2.Y = 600;
g2 = (IGeometry)point2;
IPolyline line = new PolylineClass();
line.SpatialReference = this.axMapControl1.SpatialReference;
IGeometry gLine;
line.FromPoint = point;
line.ToPoint = point2;
gLine = (IGeometry)line;
f = pFeatCls.CreateFeature();//实例化IFeature对象, 这样IFeature对象就具有当前图层上要素的字段信息
IPolygon pPolygon = gLine as IPolygon;
f.Shape = pPolygon;
f.set_Value(4, "100010");//设置IFeature对象的索引是3的字段值
f.Store();//保存IFeature对象
fr.WriteFeature(f);//将IFeature对象,添加到当前图层上
w.StopEditOperation();//停止编辑操作
w.StopEditing(true);//关闭编辑状态,并保存修改
this.axMapControl1.Refresh();//刷新地图
为什么不能在图层上显示线???
补充:.NET技术 , C#