ae指定字段转成注记

16 篇文章 0 订阅

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 , falsefalsefalsetrue, 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(nullfalse);
            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;
        }

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值