1、前言
在ArcEngine
中,图层标注用的还是很多的,下面就来介绍一下ArcEngine
中的标注功能。首先准备一份面要素文件,其属性表如下图所示:
2、图层标注
2.1、基本标注
基本标注可以利用ITextSymbol
接口实现,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 创建符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 删除已有文本元素
IActiveView pActiveView = axMapControl1.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.DeleteAllElements();
// 获取要素游标
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
// 遍历要素游标
int fieldIndex = pFeatureClass.Fields.FindField("Name");
while (pFeature != null)
{
// 获取重心
IArea pArea = pFeature.ShapeCopy as IArea;
IPoint pPoint = pArea.Centroid;
// 创建文本元素
ITextElement pTextElement = new TextElement() as ITextElement;
pTextElement.Symbol = pTextSymbol;
pTextElement.Text = pFeature.get_Value(fieldIndex).ToString();
// 添加文本元素
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
pGraphicsContainer.AddElement(pElement, 0);
pFeature = pFeatureCursor.NextFeature();
}
// 刷新地图
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
}
}
除了ITextSymbol
接口,也可以利用标注引擎实现,代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 创建符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 开启图层标注
IGeoFeatureLayer pGeoFeatureLayer = axMapControl1.get_Layer(0) as IGeoFeatureLayer;
pGeoFeatureLayer.DisplayAnnotation = true;
IBasicOverposterLayerProperties pBasicOverposterLayerProprties = new BasicOverposterLayerProperties();
pBasicOverposterLayerProprties.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
// 设置标注属性
ILabelEngineLayerProperties pLabelEngineLayerProperties = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
pLabelEngineLayerProperties.Expression = "[" + "Name" + "]";
pLabelEngineLayerProperties.Symbol = pTextSymbol;
pLabelEngineLayerProperties.BasicOverposterLayerProperties = pBasicOverposterLayerProprties;
// 刷新地图
IAnnotateLayerProperties pAnnotateLayerProperties = pLabelEngineLayerProperties as IAnnotateLayerProperties;
IAnnotateLayerPropertiesCollection pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties;
pAnnotateLayerPropertiesCollection.Clear();
pAnnotateLayerPropertiesCollection.Add(pAnnotateLayerProperties);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
}
}
}
运行结果如下图所示:
2.2、设置显示标注的比例尺
有时候我们只需要在比例尺为1:8000
到1:12000
时显示标注,其余比例尺下不显示标注,你可以这么做:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 创建符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 标注位置
ILineLabelPosition pLineLabelPosition = new LineLabelPosition();
pLineLabelPosition.Parallel = true;
pLineLabelPosition.Perpendicular = true;
pLineLabelPosition.InLine = true;
// 标注冲突
ILineLabelPlacementPriorities pLineLabelPlacementPriorities = new LineLabelPlacementPriorities();
pLineLabelPlacementPriorities.AboveStart = 5;
pLineLabelPlacementPriorities.BelowAfter = 4;
// 开启标注
IGeoFeatureLayer pGeoFeatureLayer = axMapControl1.get_Layer(0) as IGeoFeatureLayer;
pGeoFeatureLayer.DisplayAnnotation = true;
IBasicOverposterLayerProperties pBasicOverposterLayerProperties = new BasicOverposterLayerProperties();
pBasicOverposterLayerProperties.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
pBasicOverposterLayerProperties.LineLabelPlacementPriorities = pLineLabelPlacementPriorities;
pBasicOverposterLayerProperties.LineLabelPosition = pLineLabelPosition;
// 创建标注对象
ILabelEngineLayerProperties pLabelEngineLayerProperties = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
pLabelEngineLayerProperties.Symbol = pTextSymbol;
pLabelEngineLayerProperties.BasicOverposterLayerProperties = pBasicOverposterLayerProperties;
pLabelEngineLayerProperties.IsExpressionSimple = true;
pLabelEngineLayerProperties.Expression = "[Name]";
// 设置标注的参考比例尺
IAnnotateLayerTransformationProperties pAnnotateLayerTransformationProperties = pLabelEngineLayerProperties as IAnnotateLayerTransformationProperties;
pAnnotateLayerTransformationProperties.ReferenceScale = 2500000;
// 设置标注的最大最小比例尺
IAnnotateLayerProperties pAnnotateLayerProperties = pLabelEngineLayerProperties as IAnnotateLayerProperties;
pAnnotateLayerProperties.AnnotationMaximumScale = 8000;
pAnnotateLayerProperties.AnnotationMinimumScale = 12000;
// 刷新地图
IAnnotateLayerPropertiesCollection pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties;
pAnnotateLayerPropertiesCollection.Clear();
pAnnotateLayerPropertiesCollection.Add(pAnnotateLayerProperties);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
}
}
}
2.3、水平分子式标注
在ArcMap
中,我们可以利用VBScript
或JScript
或Python
实现这一功能,ArcEngine
的实现代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 创建符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 标注位置
ILineLabelPosition pLineLabelPosition = new LineLabelPosition();
pLineLabelPosition.Parallel = true;
pLineLabelPosition.Perpendicular = true;
pLineLabelPosition.InLine = true;
// 标注冲突
ILineLabelPlacementPriorities pLineLabelPlacementPriorities = new LineLabelPlacementPriorities();
pLineLabelPlacementPriorities.AboveStart = 5;
pLineLabelPlacementPriorities.BelowAfter = 4;
// 开启标注
IGeoFeatureLayer pGeoFeatureLayer = axMapControl1.get_Layer(0) as IGeoFeatureLayer;
pGeoFeatureLayer.DisplayAnnotation = true;
IBasicOverposterLayerProperties pBasicOverposterLayerProperties = new BasicOverposterLayerProperties();
pBasicOverposterLayerProperties.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
pBasicOverposterLayerProperties.LineLabelPlacementPriorities = pLineLabelPlacementPriorities;
pBasicOverposterLayerProperties.LineLabelPosition = pLineLabelPosition;
// 创建标注对象
ILabelEngineLayerProperties pLabelEngineLayerProperties = new LabelEngineLayerProperties() as ILabelEngineLayerProperties;
pLabelEngineLayerProperties.Symbol = pTextSymbol;
pLabelEngineLayerProperties.BasicOverposterLayerProperties = pBasicOverposterLayerProperties;
pLabelEngineLayerProperties.IsExpressionSimple = true;
pLabelEngineLayerProperties.Expression = "\"姓名:\" & [Name] &vbnewline&\"-----------------\" &vbnewline &\"编号:\"& [Code]";
// 设置标注的参考比例尺
IAnnotateLayerTransformationProperties pAnnotateLayerTransformationProperties = pLabelEngineLayerProperties as IAnnotateLayerTransformationProperties;
pAnnotateLayerTransformationProperties.ReferenceScale = 2500000;
// 设置标注的最大最小比例尺
IAnnotateLayerProperties pAnnotateLayerProperties = pLabelEngineLayerProperties as IAnnotateLayerProperties;
pAnnotateLayerProperties.AnnotationMaximumScale = 8000;
pAnnotateLayerProperties.AnnotationMinimumScale = 12000;
// 刷新地图
IAnnotateLayerPropertiesCollection pAnnotateLayerPropertiesCollection = pGeoFeatureLayer.AnnotationProperties;
pAnnotateLayerPropertiesCollection.Clear();
pAnnotateLayerPropertiesCollection.Add(pAnnotateLayerProperties);
axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewBackground, null, null);
}
}
}
运行结果如下图所示:
2.4、气泡标注
气泡标注的实现代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 删除已有文本元素
IActiveView pActiveView = axMapControl1.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.DeleteAllElements();
// 获取要素游标
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
// 遍历要素游标
int fieldIndex = pFeatureClass.Fields.FindField("Name");
while (pFeature != null)
{
// 获取重心
IArea pArea = pFeature.ShapeCopy as IArea;
IPoint pLabelPoint = pArea.LabelPoint;
// 文本符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 设置气泡框
IFormattedTextSymbol pFormattedTextSymbol = pTextSymbol as IFormattedTextSymbol;
pFormattedTextSymbol.Background = CreateBalloonCallout(pLabelPoint.X, pLabelPoint.Y) as ITextBackground;
pFormattedTextSymbol.Direction = esriTextDirection.esriTDAngle;
// 创建文本元素
ITextElement pTextElement = new TextElement() as ITextElement;
pTextElement.Symbol = pTextSymbol;
pTextElement.Text = pFeature.get_Value(fieldIndex).ToString();
// 元素位置
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.SpatialReference = pLabelPoint.SpatialReference;
pPoint.X = pLabelPoint.X - 50;
pPoint.Y = pLabelPoint.Y + 100;
// 添加文本元素
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
pGraphicsContainer.AddElement(pElement, 0);
pFeature = pFeatureCursor.NextFeature();
}
// 刷新地图
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
// 创建气泡框
private IBalloonCallout CreateBalloonCallout(double x, double y)
{
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 225;
pRgbColor.Blue = 225;
pRgbColor.Green = 225;
// 气泡框填充符号
ISimpleFillSymbol pSimpleFillSymbol = new SimpleFillSymbol();
pSimpleFillSymbol.Color = pRgbColor;
pSimpleFillSymbol.Style = esriSimpleFillStyle.esriSFSSolid;
// 锚点
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.X = x;
pPoint.Y = y;
// 气泡框
IBalloonCallout pBalloonCallout = new BalloonCallout();
pBalloonCallout.Style = esriBalloonCalloutStyle.esriBCSRoundedRectangle;
pBalloonCallout.Symbol = pSimpleFillSymbol;
pBalloonCallout.LeaderTolerance = 5;
pBalloonCallout.AnchorPoint = pPoint;
return pBalloonCallout;
}
}
}
运行结果如下图所示:
2.5、牵引线标注
牵引线标注的实现代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.DataSourcesFile;
using ESRI.ArcGIS.DataSourcesGDB;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.esriSystem;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Output;
using ESRI.ArcGIS.SystemUI;
using stdole;
namespace WinApp
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
axMapControl1.LoadMxFile(@"C:\Users\DSF\Desktop\data\无标题.mxd");
axMapControl1.Extent = axMapControl1.FullExtent;
}
// 标注
private void btnLabel_Click(object sender, EventArgs e)
{
// 创建颜色
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 255;
pRgbColor.Green = 0;
pRgbColor.Blue = 0;
// 创建字体
IFontDisp pFontDisp = new StdFont() as IFontDisp;
pFontDisp.Bold = true;
pFontDisp.Name = "楷体";
pFontDisp.Size = 20;
// 删除已有文本元素
IActiveView pActiveView = axMapControl1.ActiveView;
IGraphicsContainer pGraphicsContainer = pActiveView.GraphicsContainer;
pGraphicsContainer.DeleteAllElements();
// 获取要素游标
IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
IFeatureClass pFeatureClass = pFeatureLayer.FeatureClass;
IFeatureCursor pFeatureCursor = pFeatureClass.Search(null, true);
IFeature pFeature = pFeatureCursor.NextFeature();
// 遍历要素游标
int fieldIndex = pFeatureClass.Fields.FindField("Name");
while (pFeature != null)
{
// 获取重心
IArea pArea = pFeature.ShapeCopy as IArea;
IPoint pLabelPoint = pArea.LabelPoint;
// 创建文本符号
ITextSymbol pTextSymbol = new TextSymbol();
pTextSymbol.Angle = 0;
pTextSymbol.Color = pRgbColor;
pTextSymbol.Font = pFontDisp;
// 设置牵引线
IFormattedTextSymbol pFormattedTextSymbol = pTextSymbol as IFormattedTextSymbol;
pFormattedTextSymbol.Background = CreateLineCallout(pLabelPoint.X, pLabelPoint.Y) as ITextBackground;
pFormattedTextSymbol.Direction = esriTextDirection.esriTDAngle;
// 创建文本元素
ITextElement pTextElement = new TextElement() as ITextElement;
pTextElement.Symbol = pTextSymbol;
pTextElement.Text = pFeature.get_Value(fieldIndex).ToString();
// 元素位置
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.SpatialReference = pLabelPoint.SpatialReference;
pPoint.X = pLabelPoint.X - 50;
pPoint.Y = pLabelPoint.Y + 200;
// 添加文本元素
IElement pElement = pTextElement as IElement;
pElement.Geometry = pPoint;
pGraphicsContainer.AddElement(pElement, 0);
pFeature = pFeatureCursor.NextFeature();
}
// 刷新地图
System.Runtime.InteropServices.Marshal.ReleaseComObject(pFeatureCursor);
pActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
}
// 创建牵引线
private ILineCallout CreateLineCallout(double x, double y)
{
IRgbColor pRgbColor = new RgbColor();
pRgbColor.Red = 0;
pRgbColor.Blue = 0;
pRgbColor.Green = 0;
// 牵引线样式
ISimpleLineSymbol pLeaderLine = new SimpleLineSymbol();
pLeaderLine.Color = pRgbColor;
pLeaderLine.Width = 1;
// 竖线
ISimpleLineSymbol pAccentBar = new SimpleLineSymbol();
pAccentBar.Color = pRgbColor;
// 锚点
IPoint pPoint = new ESRI.ArcGIS.Geometry.Point();
pPoint.X = x;
pPoint.Y = y;
// 创建牵引线
ILineCallout pLineCallout = new LineCallout();
pLineCallout.Style = esriLineCalloutStyle.esriLCSUnderline;
pLineCallout.Border = null;
pLineCallout.AccentBar = pAccentBar;
pLineCallout.LeaderLine = pLeaderLine;
pLineCallout.AnchorPoint = pPoint;
return pLineCallout;
}
}
}
运行结果如下图所示:
3、结语
其实ArcEngine
中的标注功能是很强大的,有兴趣的同志可以继续深入了解。