1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

时间:2020-06-26 05:32:36

相关推荐

ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

ArcGIS二次开发基础教程(08):在MapControl上画图(添加元素)

添加临时元素

0. 点元素

//临时画图的元素将不会保存在地图中//全局变量IPoint pt;IElement ele;public void drawPoint(object sender, IMapControlEvents2_OnMouseDownEvent e){pt = new PointClass();//获取单击位置地图坐标pt.PutCoords(e.mapX,e.mapY);//创建点要素ele = new MarkElementClass();//设置点要素的空间几何体为点ele.Geometry = pt as IGeometry;//图形容器IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;//添加元素graphicsConainer.AddElement(ele,0);axMapControl1.Refresh();}//菜单栏“点要素” 选项 添加事件开启画图状态private void 点要素ToolStripMenuItem_Click(object sender, EventArgs e){axMapControl1.OnMouseDown += drawPoint;}

1. 线元素

//全局变量//点集IPointCollection ptCollection;IPoint linePt;IElement ele;public void drawLine(object sender, IMapControlEvents2_OnMouseDownEvent e){linePt = new PointClass();linePt.PutCoords(e.mapX,e.mapY);ptCollection.AddPoint(linePt);//多段线要素ele = new PolylineElementClass();IPolyline polyline = new PolylineClass();//将点集转换为多段线polyline = ptCollection as IPolyline;ele.Geometry = polyline as IGeometry;//图形容器IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;//添加元素graphicsConainer.AddElement(ele,0);axMapControl1.Refresh();}private void 线要素ToolStripMenuItem_Click(object sender, EventArgs e){//每次开启画图状态都重新创建点集ptCollection = new PolylineClass();axMapControl1.OnMouseDown += drawLine;}

2. 面元素

//全局变量IPoint polygonPt;IElement ele;IPointCollection ptCollection;public void drawPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e){polygonPt = new PointClass();polygonPt.PutCoords(e.mapX,e.mapY);ptCollection.AddPoint(polyginPt);ele = new PolygonElementClass();IPolygon polygon = new PolygonClass();polygon = ptCollection as IPolygon;ele.Geometry = polygon as IGeometry;//图形容器IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;//添加元素graphicsConainer.AddElement(ele,0);axMapControl1.Refresh();}private void 面要素ToolStripMenuItem_Click(object sender, EventArgs e){polygonCollect = new PolygonClass();axMapControl1.OnMouseDown += drawPolygon;}

添加元素到要素中

0. 点元素

//此处添加的元素将保存在地图中//全局变量IPoint pt;public void vectorPoint(object sender, IMapControlEvents2_OnMouseDownEvent e){//获取图层及要素类IFeatureLayer featureLayer = GetLayerByName("图层名称");IFeatureClass featureClass = featureLayer.FeatrueClass;//创建点要素并赋值坐标信息pt = new PointClass();pt.PutCoords(e.mapX,e.mapY);//从要素类创建新要素IFeature feature = featureClass.CreateFeature();//将点元素设置为新建要素的Shapefeature.Shape = pt as IGeomerty;//保存feature.Store();axMapControl1.Refresh();}//菜单栏“矢量点” 选项 添加事件开启画图状态private void 矢量点ToolStripMenuItem_Click(object sender, EventArgs e){axMapControl1.OnMouseDown += vectorPoint;}

1. 线元素

//全局变量IPoint linePt;IPointCollection ptCollection;public void vectorLine(object sender, IMapControlEvents2_OnMouseDownEvent e){//获取图层及要素类IFeatureLayer featureLayer = GetLayerByName("图层名称");IFeatureClass featureClass = featureLayer.FeatrueClass;linePt = new PointClass();linePt.PutCoords(e.mapX,e.mapY);ptCollection.AddPoint(linePt);IPolyline polyline = new PolylineClass();polyline = ptCollection as IPolyline;IFeature feature = featureClass.CreateFeature();feature.Shape = polyline as IGeometry;feature.Store();axMapControl1.Refresh();}private void 矢量线ToolStripMenuItem_Click(object sender, EventArgs e){pointCollect = new PolylineClass();axMapControl2.OnMouseDown += vectorLine;}

2. 面元素

//全局要素IPoint pt;IPointCollection ptCollection;public void vectorPolygon(object sender, IMapControlEvents2_OnMouseDownEvent e){//获取图层及要素类IFeatureLayer featureLayer = GetLayerByName("图层名称");IFeatureClass featureClass = featureLayer.FeatrueClass;pt = new PointClass();pt.PutCoords(e.mapX,e.MapY);ptCollection.AddPoint(pt);IPolygon polygon = new PolygonClass();polygon = ptCollection as IPolygon;IFeature feature = featureClass.CreateFeature();feature.Shape = polygon as IGeometry;feature.Store();axMapControl1.Refresh();}private void 矢量面ToolStripMenuItem_Click(object sender, EventArgs e){polygonCollect = new PolygonClass();axMapControl2.OnMouseDown += vectorPolygon;}

3. 文本元素

//实现为区县地图的每个区县添加区县名IFeatureLayer layer = GetLayerByName("图层名称")IFeatureCursor cursor = layer.FeatureClass.Search(null,true);IFeature feature = cursor.NextFeature();IGraphicsContainer graphicsContainer = axMapControl1.Map as IGraphicsContainer;while(feature!=null){//获取区县名string str = feature.get_Value(feature.Fields.FindField("区县名")).ToString();//创建一个文本元素TextElement textElement = new TextElementClass();//字体IFontDisp font = new StdFontClass() as IFontDisp;font.Bold = true;font.Size = 10;font.Name = "宋体";//文本颜色IRgbColor color = new RgbColorClass();color.Red = 0;color.Green = 0;color.Blue = 0;ITextSymbol textSymbol = new TextSymbolClass();textSymbol.Font = font;textSymbol.Color = color;textElement.Symbol = textSymbol;textElement.Text = str;IElement element = textElement as IElement;//文本显示位置在区县要素中心IPoint point = new PointClass();IGeometry5 geo = feature.Shape as IGeometry5;point.X = geo.CentroidEx.X;point.Y = geo.CentroidEx.Y;element.Geometry = point as IGeometry;graphicsContainer.AddElement(element,0);feature = cursor.NextFeature();} axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics,null,null);

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。