AE指定字段转成注记

简介:

转自原文 ae指定字段转成注记

ArcMap中有一个功能是Label Features,就是可以将图层内指定字段值显示以Label形式显示在主窗口上,在Label Features后,用右键点击图层,发现可以出现一个原来灰色的功能名,ConvertLabelsToAnnotation。这个功能在AE中提供,可是自己找不到设定字段值的位置,无可奈何,只有另辟蹊径。

就是先创建一个注记层,然后按照图层里指定字段的值来生成注记。就是一个一个Feature点转换。可能会比AE提供的接口效率低,没有办法,自己不会用那个接口,也就只能这样勉强的用着了。这个方面最难点就是创建一个新的注记图层,有很多属性需要设置,比较麻烦,下面是代码

复制代码
private void ConvertToAnnotationLayer(IMap pMap, ILayer pLayer, string fieldname, esriGeometryType type)
{
    IFeatureLayer pFeatLayer = pLayer as IFeatureLayer;

    int i = pFeatLayer.FeatureClass.FindField("SHAPE");
    IField pShapeField = pFeatLayer.FeatureClass.Fields.get_Field(i);
    IDataset pDataSet = pFeatLayer.FeatureClass as IDataset;
    IWorkspace pWS = pDataSet.Workspace;
    IFeatureWorkspace pFeatWS = pWS as IFeatureWorkspace;


    IGeoFeatureLayer pGeoFeatLayer = pFeatLayer as IGeoFeatureLayer;
    IFields pFields = pGeoFeatLayer.FeatureClass.Fields;

    IAnnotationLayerFactory pAnnoLayerFact = new FDOGraphicsLayerFactoryClass();
    IGraphicsLayerScale pGraphyScale = new GraphicsLayerScaleClass();
    pGraphyScale.ReferenceScale = 200000;
    pGraphyScale.Units = esriUnits.esriMeters;
    IAnnotationLayer pAnnoLayer = null;

    ISymbolCollection2 pSymColl = new SymbolCollectionClass();
    IFormattedTextSymbol pTextSymbol = new TextSymbolClass();
    IRgbColor pRGB = new RgbColorClass();
    pRGB.Red = 0;
    pRGB.Blue = 0;
    pRGB.Green = 0;
    pTextSymbol.Color = pRGB;
    
      /*      pTextSymbol.Font=*/

 
    m_FontDisp.Size=8;           
    pTextSymbol.Font =m_FontDisp ;
    pTextSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHACenter;
    pTextSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVABaseline;
    pTextSymbol.CharacterSpacing = 100;
    pTextSymbol.CharacterWidth = 50;
    pTextSymbol.WordSpacing = 100;
    
   //         IBoundsProperties pBoundProp = pTextSymbol as IBoundsProperties;
   /* pBoundProp.FixedSize = false;*/
  //          pBoundProp.FixedAspectRatio = true;
    ISymbol pSymbol = pTextSymbol as ISymbol;
    ISymbolIdentifier2 pSymident2;
    pSymColl.AddSymbol(pSymbol, fieldname, out pSymident2);
    ISymbolCollection pSymColl1= pSymColl as ISymbolCollection;

    IOverposterProperties pOverpost = new BasicOverposterPropertiesClass();

    IAnnotateLayerPropertiesCollection pAnnoPropColl = new AnnotateLayerPropertiesCollectionClass();
    IAnnotateLayerProperties pAnnoProp;
    ILabelEngineLayerProperties2 pLabelEngine=new LabelEngineLayerPropertiesClass();
    pLabelEngine.AnnotationClassID = 0;
    pLabelEngine.Symbol = pTextSymbol;
    pLabelEngine.SymbolID = pSymident2.ID;
    pAnnoProp = pLabelEngine as IAnnotateLayerProperties;
    pAnnoProp.Class = pLayer.Name + fieldname;
    pAnnoProp.LabelWhichFeatures = esriLabelWhichFeatures.esriAllFeatures;
    pAnnoProp.Priority = 0;
    IActiveView pActView=pMap as  IActiveView;
    pAnnoProp.GraphicsContainer = pActView.GraphicsContainer;
    pAnnoProp.FeatureLayer = pFeatLayer;
    pAnnoProp.FeatureLinked = true;
    pAnnoProp.AnnotationMaximumScale = 10000000000000000000;
    pAnnoProp.AnnotationMinimumScale = 0.00000000001;
    pAnnoPropColl.Add(pAnnoProp);
    try
    {
       pAnnoLayer=pAnnoLayerFact.CreateAnnotationLayer(pFeatWS, pFeatLayer.FeatureClass.FeatureDataset, pLayer.Name + "_A_" + fieldname, pShapeField.GeometryDef,
         null, pAnnoPropColl,pGraphyScale,pSymColl1 , false, false, false, true, pOverpost, "");

    }
    catch (Exception s)
    {
        string mes = s.Message;
    }
    if (pAnnoLayer == null)
        return;
    int num=pFields.FindField(fieldname);
    ConvertToAnnotateByFeature(pLayer, num, ref pAnnoLayer);
    pMap.AddLayer(pAnnoLayer as ILayer);
}
   private bool ConvertToAnnotateByFeature(ILayer pLayer,int fieldnum,ref IAnnotationLayer pAnnoLayer)
{
    IFeatureLayer pFeatlayer = pLayer as IFeatureLayer;
    if (pFeatlayer == null)
        return false;
    IFeatureClass pFeatClass = pFeatlayer.FeatureClass;
    IFeatureCursor pFeatCursor = pFeatClass.Search(null, false);
    IFeature pFeat = pFeatCursor.NextFeature();

    IFeatureLayer pAnnoFeatLayer=pAnnoLayer as IFeatureLayer;
    IAnnotationClassExtension pAnnotateExten = pAnnoFeatLayer.FeatureClass.Extension as IAnnotationClassExtension;
    ISymbol pSymbol = pAnnotateExten.SymbolCollection.get_Symbol(0);
    IGeometry pGeo;
    double angle = 0;
    IRgbColor pRGB=new RgbColorClass();
    pRGB.Blue=255;
    pRGB.Green=255;
    pRGB.Red=0;
    /////判断是不是公路层的Width,特殊处理/////
    string temp_layername = pLayer.Name.ToUpper();
    bool bwidth=false;
    if(pFeatClass.Fields.get_Field(fieldnum).Name.ToUpper()=="WIDTH")
    {
        if (pLayer.Name.ToUpper().Contains("LRDL"))
            bwidth = true;
    }   
    IElementCollection pElementColl = new ElementCollectionClass();
    pAnnoLayer.BeginAddElements();
    while(pFeat!=null)
    {
        pGeo = pFeat.Shape;
        IPoint pLabelPoint = null;
        if(pGeo is IPolyline)
        {
            IPolyline pline=pGeo as IPolyline;
            pLabelPoint = GetLabelPoint(pline);
        }
        if(pGeo is IPoint)
        {
            pLabelPoint = pGeo as IPoint;
        }
        if(pGeo is IPolygon)
        {
            IPolygon pPolygon = pGeo as IPolygon;
            IArea pArea = pPolygon as IArea;
            pLabelPoint = pArea.LabelPoint;
        }
        object val=pFeat.get_Value(fieldnum);
        string s=val.ToString();
        if (s == null || s.Length == 0)
        {
            pFeat = pFeatCursor.NextFeature();
            continue;
        }
        ///对公路的宽度进行特殊处理
        if(bwidth)
        {
            
            double width = double.Parse(s);
            int rtegnum = pFeatClass.FindField("RTEG");
            string rteg = pFeat.get_Value(rtegnum).ToString();
            if(rteg=="高速"&&width>55.0)
            {
                IElement pElement = MakeTextElement(pGeo, angle, s, pRGB, pSymbol);
                pElementColl.Add(pElement, pFeat.OID);
            }
            else if(width>40.0)
            {
                IElement pElement = MakeTextElement(pGeo, angle, s, pRGB, pSymbol);
                pElementColl.Add(pElement, pFeat.OID);
            }
        }
        else if (/*pLabelPoint != null&&*/s.Length>0)
        {
            IElement pElement = MakeTextElement(pGeo, angle, s, pRGB,pSymbol);                    
            pElementColl.Add(pElement, pFeat.OID);
        }
        pFeat = pFeatCursor.NextFeature();
    }
    pAnnoLayer.EndAddElements();
    pAnnoLayer.BeginAddElements();
    if(pElementColl.Count<1)
    {
        pAnnoLayer.EndAddElements();
        return true;
    }
    pAnnoLayer.DoAddElements(pElementColl, 0);
 /*   pAnnoLayer.SetupAttributeConversion()*/
    pAnnoLayer.EndAddElements();
    return true;
}
复制代码

 

没有整理与归纳的知识,一文不值!高度概括与梳理的知识,才是自己真正的知识与技能。 永远不要让自己的自由、好奇、充满创造力的想法被现实的框架所束缚,让创造力自由成长吧! 多花时间,关心他(她)人,正如别人所关心你的。理想的腾飞与实现,没有别人的支持与帮助,是万万不能的。



    本文转自wenglabs博客园博客,原文链接:http://www.cnblogs.com/arxive/p/6262974.html ,如需转载请自行联系原作者

相关文章
|
5天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
303 116
|
20天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
7天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
494 45
Meta SAM3开源:让图像分割,听懂你的话
|
14天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
694 223
|
2天前
|
Windows
dll错误修复 ,可指定下载dll,regsvr32等
dll错误修复 ,可指定下载dll,regsvr32等
135 95
|
12天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1708 158
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
953 62