ArcEngine添加注记

ArcEngine添加注记

标注和注记是ArcEngine中提供的两种使用文字信息标注地图要素的方式。其中标注是作为图层的属性存在的,可以动态创建,注记作为地理要素被存储。需要注意的是Shp文件不支持注记. 。绘制标注的方式有两种。
文章来源:CSDN jojojojo2002

使用TextElment绘制标注

这种方法的原理就是把属性表中的某个属性创建TextElment对象,然后使用IGraphicsContainer 的AddElement方法添加标注.实例代码。但这种方法存在不便于后期修改设计,重新渲染繁琐等问题。

//使用TextElment绘制标注, fieldName为要绘制的属性
using stdole;//字体命名空间
	public static void AddLable(AxMapControl axMapControl, ILayer layer, string fieldName)
    {
        IRgbColor pColor = new RgbColorClass()
        {
            Red = 255,
            Blue = 0,
            Green = 0
        };
        IFontDisp pFont = new StdFont()
        {
            Name = "宋体",
            Size = 5
        } as IFontDisp;  
        ITextSymbol pTextSymbol = new TextSymbolClass()
        {
            Color = pColor,
            Font = pFont,
            Size = 11
        };
        IGraphicsContainer pGraContainer = axMapControl.Map as IGraphicsContainer;
        //遍历要标注的要素
        IFeatureLayer pFeaLayer = layer as IFeatureLayer;
        IFeatureClass pFeaClass = pFeaLayer.FeatureClass;
        IFeatureCursor pFeatCur = pFeaClass.Search(null, false);
        IFeature pFeature = pFeatCur.NextFeature();
        int index = pFeature.Fields.FindField(fieldName);//要标注的字段的索引
        IEnvelope pEnv = null;
        ITextElement pTextElment = null;
        IElement pEle = null;
        while (pFeature != null)
        {
            //使用地理对象的中心作为标注的位置
            pEnv = pFeature.Extent;
            IPoint pPoint = new PointClass();
            pPoint.PutCoords(pEnv.XMin + pEnv.Width * 0.5, pEnv.YMin + pEnv.Height * 0.5);
            pTextElment = new TextElementClass()
            {
                Symbol = pTextSymbol,
                ScaleText = true,
                Text = pFeature.get_Value(index).ToString()
            };
            pEle = pTextElment as IElement;
            pEle.Geometry = pPoint;
            //添加标注
            pGraContainer.AddElement(pEle, 0);
            pFeature = pFeatCur.NextFeature();
        }
        (axMapControl.Map as IActiveView).PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, axMapControl.Extent);
    }

使用ArcEngine中的标注对象口.LabelEngineLayerProperties来标注要素

IGeoFeatureLayer中的AnnotationProperties是一个包含LabelEngineLayerProperties对象的标注集合。而 LabelEngineLayerProperties实现了:

	IAnnotateProperties,  //
    IAnnotateLayerProperties, //可以控制标注的显示比例尺,过滤条件等
    ILabelEngineLayerProperties, 
    IAnnotateLayerTransformationProperties //控制标注的参考比例尺,单位,标注边界和缩放比率等

等几个主要的接口。LabelEngineLayerProperties可以操作标注要素的多个属性和行为,如设置文本的标注位置,标注尺寸,设置脚本,文字符号等。该类实现了大量操作标注的属性和方法,对于复杂的标注非常有用,而TextElment适合简单的标注
ILabelEngineLayerPropertiesLabelEngineLayerPropertiesClass 的主接口。他的ExpressionIsExpressionSimple用法如下:
IsExpressionSimple=true //Expression为简单表达式,其形式为: "["+属性字段名+"]"+其他
IsExpressionSimple=true=false //Expression为复杂表达式,其内容也为一个字符串,但是一个完整的VBScript or JScript 函数或者表达式。
ExpressionParser属性是一个Expression解析器,它支持更复杂的JS和Vbs代码。

using stdole;//字体命名空间
	public static void AddAnnotate(ILayer layer, string fieldName)
    {
        IGeoFeatureLayer pGeoLayer = layer as IGeoFeatureLayer;
        IAnnotateLayerPropertiesCollection IPALPColl = pGeoLayer.AnnotationProperties;
        IPALPColl.Clear();
        //IRgbColor pColor = GetColor(255, 0, 0, 255);
        IFontDisp pFont = new StdFont()
        {
            Name = "宋体",
            Bold = true
        } as IFontDisp;
        ITextSymbol pTextSymbol = new TextSymbolClass()
        {
            //Color = pColor,
            //Font = pFont,
            Size = 12
        };
        //用来控制标注和要素的相对位置关系
        ILineLabelPosition pLineLpos = new LineLabelPositionClass()
        {
            Parallel = false,  //修改标注的属性
            Perpendicular = true,
            InLine = true
        };
        //用来控制标注冲突
        ILineLabelPlacementPriorities pLinePlace = new LineLabelPlacementPrioritiesClass()
        {
            AboveStart = 5, //让above 和start的优先级为5
            BelowAfter = 4
        };
        //用来实现对ILineLabelPosition 和 ILineLabelPlacementPriorities以及更高级属性的控制
        IBasicOverposterLayerProperties pBOLP = new BasicOverposterLayerPropertiesClass()
        {
            FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon,
            LineLabelPlacementPriorities = pLinePlace,
            LineLabelPosition = pLineLpos
        };
        //创建标注对象
        ILabelEngineLayerProperties pLableEngine = new LabelEngineLayerPropertiesClass()
        {
            Symbol = pTextSymbol,
            BasicOverposterLayerProperties = pBOLP,
            IsExpressionSimple = true,
            Expression = "[" + fieldName + "]"
      	 };
         //设置标注的参考比例尺
	     IAnnotateLayerTransformationProperties pAnnoLyrPros = pLableEngine as IAnnotateLayerTransformationProperties;
        pAnnoLyrPros.ReferenceScale = 2500;
        //设置标注可见的最大最小比例尺
        IAnnotateLayerProperties pAnnoPros = pLableEngine as IAnnotateLayerProperties;
        pAnnoPros.AnnotationMaximumScale = 0;
        pAnnoPros.AnnotationMinimumScale = 250000;
        //pAnnoPros.WhereClause属性  设置过滤条件
        IPALPColl.Add(pAnnoPros);
        pGeoLayer.DisplayAnnotation = true;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值