【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇
原发表于ESRI中国社区,转过来。我的社区帐号:jhlong
6周年征文
似乎就我这一篇ArcEngine开发的,搞开发的很少么?还是搞开发的都不善于言语?欢迎做ArcEngine开发的交流心得。
ArcGis里的注记,个人认为常用的有:
1.图层标注如arcmap里图层右键属性-labels内的label。这些有不少高级注记功能,其实ArcEngine都可以实现。看到论坛里也有不少没解决的于此相关的问题。具体看下面代码。
2.屏幕element。这类有文本、点、线、面等,且可设置各种样式和符号。这些不保存在数据库中,可以通过保存mxd的方式进行保存。
3.注记图层。就是IAnnoClass,存于数据库的注记。
以下代码也是针对以上3种注记类型进行说明。
个人文字描述不是太好,所以不多说,主要是代码加注释。需要时去找对应的大标题下的内容即可,总体不难理解。
文档看起来排版有点乱,附上下载,下载的可能不是最新的,以本帖为准。
http://pan.baidu.com/share/link?shareid=104303&uk=305065349
CSDN下载:
http://download.csdn.net/detail/jhlong12345/4768967
【初级】ArcEngine注记处理专题.zip (45.79 KB)
新浪爱问下载:http://ishare.iask.sina.com.cn/f/34753842.html
值此论坛6周年之际,发出来庆祝论坛生日快乐。
主要适合初学者,也有一些高级功能及不常用的。自行选择。
代码应该都可以直接复制粘贴使用,不能用时请自己思考,还不行可以pm我。
如果看的过程中有任何疑问,请pm我。回帖也可以,但有可能漏掉。
初次写这种文章,如果有不对的或需要补充或描述不清晰的地方,烦请大家告知。
如果觉得对你有用,请多多支持。
请勿用于商业用途,请免费转载和分享,转载请注明作者和出处。
目录
1.说明 1
2.准备工作 2
2.1创建一个自定义颜色IRgbColor 2
2.2添加Element到地图窗口 2
2.3字体不随地图缩放变化 2
3.为图层增加注记表达式 2
4.注记表达式转图层要素 5
5.创建注记图层要素 6
6.创建文本注记TextElement 7
6.1创建一个字体 7
6.2创建文本注记样式 8
6.3.创建文本注记 8
7.创建面注记PolygonElement 8
7.1根据4点创建一个面 8
7.2.创建面符号 9
7.3.创建PolygonElement 9
8.创建线注记LineElement 9
8.1根据2点创建一条线 9
8.2.创建线符号 10
8.3.创建线注记 10
9创建一个点状注记(MarkerElement) 10
9.1根据XY创建一个点 10
9.2.创建MarkerElement 11
10.符号 11
内容整理自我的博客:http://jhlong12345.blog.163.com/ ... 631292012625336861/ 。
2.准备工作
2.1创建一个自定义颜色IRgbColor
下面会用到,很简单的一个小函数
/// <summary> /// 自定义颜色,3个0是黑色,3个255是白色/// </summary>
/// <param name="r">红,取值0—255,下同</param>
/// <param name="g">绿.</param>
/// <param name="b">蓝</param>
/// <returns>IRgbColor</returns>
/// <remarks></remarks>
private static IRgbColor ColorCustom(int r, int g, int b)
{
IRgbColor myColor = new RgbColor();
myColor.Red = r;
myColor.Blue = b;
myColor.Green = g;
return myColor;
}
2.2添加Element到地图窗口
创建完Element后,用下面这句就可以加到当前地图窗口了。
(pMap as IGraphicsContainer) .AddElement(ele, 0);
2.3字体不随地图缩放变化
IMAP的ReferenceScale方法设置为0时,无论地图如何缩放,字体大小的屏幕距离不变,地图距离随比例尺变化。设置为其他比例尺如10000时,字体的地图距离就保持比例尺为10000时的高度,屏幕距离一直随比例尺变化。
3.为图层增加注记表达式
从注记表达式的文本文件中程序里读取并动态地增加注记。label expression 可以为简单的或vb脚本等。[]内的是字段名称。
例如:
Function FindLabel ([BH],[BM],[KD],[XZ])
FindLabel = FormatNumber([KD],,true,false,false)
if (IsNull([XZ]) or ([XZ] = "")) then FindLabel = [BH] &"/"& [BM]&chr()&chr()&FindLabel
if left([XZ],)= or left([XZ],)= then FindLabel = [BH]&"G/"&[BM]&chr()&chr()&FindLabel
if left([XZ],)= or left([XZ],)= then FindLabel = [BH] &"/"&[BM]&chr()&chr()&FindLabel
End Function
或者简单的一个字段:
[XZ]
这里的接口很少用,但是合理利用的话可以达到ArcMap里的高级效果
代码如下:
/// <summary>
/// 图层增加注记表达式
/// </summary>
/// <param name="map">The map.</param>
/// <param name="tcname">The tcname.</param>
public static void AddAnno(ILayer plyr , string tcname)
{
if (!System.IO.File.Exists(注记文件存储路径))
return;
IGeoFeatureLayer pGeoFeaLayer = (plyr as IFeatureLayer) as IGeoFeatureLayer;
IAnnotateLayerPropertiesCollection pAnoLayPC = pGeoFeaLayer.AnnotationProperties;
pAnoLayPC.Clear();
ILabelEngineLayerProperties pAnnoLayerProps = new LabelEngineLayerPropertiesClass();
(pAnnoLayerProps.BasicOverposterLayerProperties as IOverposterLayerProperties2).TagUnplaced = false; ////是否覆盖,对应Arcmap:layer properties-》labels-》placement properties-》conflict detection ::place overlapping labels
pAnnoLayerProps.SymbolID = ;
IBasicOverposterLayerProperties4 blp = pAnnoLayerProps.BasicOverposterLayerProperties asIBasicOverposterLayerProperties4;
//blp.PointPlacementMethod = esriOverposterPointPlacementMethod.esriAroundPoint;
blp.NumLabelsOption = esriBasicNumLabelsOption.esriOneLabelPerShape; ////每个图形只标注一个 label properties--palcement properties--duplicate labels
blp.PolygonPlacementMethod = esriOverposterPolygonPlacementMethod.esriAlwaysHorizontal; //// label properties--palcement properties--polygon settings
// blp.PlaceOnlyInsidePolygon = true; ////保证在图形内部 label properties--palcement properties--only place label inside polygon
string annoExpression = GetAnnoExpression(注记文件存储路径);
if (annoExpression.ToUpper().IndexOf("FUNCTION") >= )
pAnnoLayerProps.IsExpressionSimple = false;
else
pAnnoLayerProps.IsExpressionSimple = true; //对应aArcMap的advanced选项
pAnnoLayerProps.Expression = annoExpression;
pAnnoLayerProps.Symbol = CreateTextSymbol();
pGeoFeaLayer.DisplayAnnotation = true;
}
/// <summary>
/// 根据注记文件存储路径获取标注表达式
/// </summary>
/// <param name="tcname">The tcname.</param>
/// <returns></returns>
public static string GetAnnoExpression(string注记文件存储路径)
{
StreamReader sr = new StreamReader(注记文件存储路径);
string annoExpression = string.Empty;
while (!sr.EndOfStream)
{
string text = sr.ReadLine();
if (annoExpression == string.Empty)
annoExpression = text;
else
annoExpression = annoExpression + "\r\n" + text;
}
return annoExpression;
}
4.注记表达式转图层要素
这段代码参见帮助文档,无需多解释,根据需要使用。
public static void ConvertLabelsToGDBAnnotationSingleLayer(IMap pMap, int layerIndex, bool featureLinked, stringlogpath)
{
string tcmc = string.Empty;
try
{
IConvertLabelsToAnnotation pConvertLabelsToAnnotation = new ConvertLabelsToAnnotationClass();
ITrackCancel pTrackCancel = new CancelTrackerClass();
//Change global level options for the conversion by sending in different parameters to the next line.
pConvertLabelsToAnnotation.Initialize(pMap,
esriAnnotationStorageType.esriDatabaseAnnotation,
esriLabelWhichFeatures.esriAllFeatures, true, pTrackCancel, null);
ILayer pLayer = pMap.get_Layer(layerIndex);
IGeoFeatureLayer pGeoFeatureLayer = pLayer as IGeoFeatureLayer;
if (pGeoFeatureLayer != null)
{
IFeatureClass pFeatureClass = pGeoFeatureLayer.FeatureClass;
IDataset pDataset = pFeatureClass as IDataset;
tcmc = GtMap.JNCommon.Engine.GetPureName(pDataset.Name);
IFeatureWorkspace pFeatureWorkspace = pDataset.Workspace as
IFeatureWorkspace;
//Add the layer information to the converter object. Specify the parameters of the output annotation feature class here as well.
pConvertLabelsToAnnotation.AddFeatureLayer(pGeoFeatureLayer, tcmc + "ZJ", pFeatureWorkspace,
pFeatureClass.FeatureDataset, featureLinked, true, true, true, true, "");
//Do the conversion.
pConvertLabelsToAnnotation.ConvertLabels();
string errorInfo = pConvertLabelsToAnnotation.ErrorInfo;
if (!string.IsNullOrEmpty(errorInfo))
LogWrite(logpath, errorInfo);
//IEnumLayer pEnumLayer = pConvertLabelsToAnnotation.AnnoLayers;
//Turn off labeling for the layer converted.
pGeoFeatureLayer.DisplayAnnotation = false;
//Add the result annotation layer to the map.
//pMap.AddLayers(pEnumLayer, true);
//Refresh the map to update the display.
IActiveView pActiveView = pMap as IActiveView;
pActiveView.Refresh();
iConvertCnt++;
if (iConvertCnt < )
{
IFeatureClass cls = pFeatureWorkspace.OpenFeatureClass(tcmc + "ZJ");
if (cls != null)
{
if ((cls.FeatureCount(null) == ) && (pFeatureClass.FeatureCount(null) > ))
{
pGeoFeatureLayer.DisplayAnnotation = true;
ConvertLabelsToGDBAnnotationSingleLayer(pMap, layerIndex, featureLinked, logpath);
}
}
}
}
}
catch (System.Exception ex)
{
}
}
5.创建注记图层要素
创建完文本注记TextElement后,可以选择以屏幕注记的形式加到屏幕上,也可以通过下面的方式转为注记图层要素:
IFeatureClass annocls = 获取注记图层
IDataset pDataset = annocls as IDataset;
ITransactions pTransactions = pDataset.Workspace as ITransactions;
pTransactions.StartTransaction();
IFDOGraphicsLayerFactory pFDOGLFactory = new FDOGraphicsLayerFactoryClass();
ILayer tmpLayer = pFDOGLFactory.OpenGraphicsLayer(pDataset.Workspace as IFeatureWorkspace, annocls.FeatureDataset, pDataset.Name);
IFDOGraphicsLayer pFDOGLayer = tmpLayer as IFDOGraphicsLayer;
IElementCollection pElementColl = new ElementCollectionClass();
pFDOGLayer.BeginAddElements();
……………………………… //创建text element
pElementColl.Add(element, );
…………………………….
////每新增100个提交下,最后再提交下。防止过多转换失败
if ((pElementColl != null) && (pElementColl.Count == ))
{
pFDOGLayer.DoAddElements(pElementColl, );
pFDOGLayer.EndAddElements();
pElementColl.Clear();
pTransactions.CommitTransaction();
pTransactions.StartTransaction();
pFDOGLayer.BeginAddElements();
}
if (pElementColl.Count > )
pFDOGLayer.DoAddElements(pElementColl, );
pFDOGLayer.EndAddElements();
pElementColl.Clear();
pTransactions.CommitTransaction();
6.创建文本注记TextElement
6.1创建一个字体
/// <summary>
/// 字体设置
/// </summary>
/// <param name="size">The size.</param>
/// <param name="fontname">The fontname.</param>
/// <returns>
/// IFontDisp
/// </returns>
public static stdole.IFontDisp GetIFontDisp(float size, string fontname)
{
string fontFamilyName = fontname;
FontStyle fontStyle = FontStyle.Regular;
Font font = new Font(fontFamilyName, size, fontStyle);
return OLE.GetIFontDispFromFont(font) as stdole.IFontDisp;
}
6.2创建文本注记样式
/// <summary>
///文本注记样式
/// </summary>
/// <param name="geometry">标注点,一个点即可</param>
/// <param name="text">标注内容</param>
/// <returns>
/// IElement
/// </returns>
public static ITextSymbol GetTextElement(IGeometry geometry, string text)
{
ITextSymbol textSymbol = new TextSymbolClass();
textSymbol.Color = ColorCustom(, , );
////不可以直接修改textSymbol.Font.Bold等属性,无效
stdole.IFontDisp font = GetIFontDisp(9F, "宋体");
font .Bold = false;
font .Italic = false;
font .Strikethrough = false;
font .Underline = false;
textSymbol.Font = font;
textSymbol.HorizontalAlignment = esriTextHorizontalAlignment.esriTHALeft; ////水平
textSymbol.VerticalAlignment = esriTextVerticalAlignment.esriTVATop; ; ////垂直
return textSymbol ;
}
6.3.创建文本注记
ITextElement textElement = new TextElementClass();
textElement.ScaleText = true;
textElement.Symbol = GetTextElement();
textElement.Text = text;
IElement element = textElement as IElement;
element.Geometry = geometry;
7.创建面注记PolygonElement
7.1根据4点创建一个面
/// <summary>
/// 根据4个点创建图形,点序要顺时针
/// </summary>
/// <param name="pnt1">点1</param>
/// <param name="pnt2">点2</param>
/// <param name="pnt3">点3</param>
/// <param name="pnt4">点4</param>
/// <returns>IPolygon</returns>
public static IPolygon CreatePolygonBy4Points(IPoint pnt1, IPoint pnt2, IPoint pnt3, IPoint pnt4)
{
IPointCollection pPntCol = new PolygonClass();
object missing = Type.Missing; ////顺时针添加
pPntCol.AddPoint(pnt1, ref missing, ref missing);
pPntCol.AddPoint(pnt2, ref missing, ref missing);
pPntCol.AddPoint(pnt3, ref missing, ref missing);
pPntCol.AddPoint(pnt4, ref missing, ref missing);
pPntCol.AddPoint(pnt1, ref missing, ref missing); //// 为保持首尾相联,故将第一个点再添加一次
return pPntCol as IPolygon;
}
7.2.创建面符号
/// <summary>
/// 创建 面 符号
/// </summary>
/// <param name="r">The r.</param>
/// <param name="g">The g.</param>
/// <param name="b">The b.</param>
/// <returns>ISimpleFillSymbol</returns>
public static ISimpleFillSymbol CreateGeoSymbol(int r, int g, int b)
{
ISimpleFillSymbol psymbol = new SimpleFillSymbolClass() as ISimpleFillSymbol;
psymbol.Color = ColorCustom(r, g, b);
psymbol.Outline.Color = ColorCustom(r, g, b);
return psymbol;
}
7.3.创建PolygonElement
IElement pele = new PolygonElementClass();
pele.Geometry = pGeoCol as IGeometry;
(pele as IFillShapeElement).Symbol = CreateGeoSymbol(r, gD, b);
(pmap as IGraphicsContainer).AddElement(pele, );
8.创建线注记LineElement
8.1根据2点创建一条线
/// <summary>
/// 创建线
/// </summary>
/// <param name="pnt1">The PNT1.</param>
/// <param name="pnt2">The PNT2.</param>
public static void CreateLine( IPoint pnt1, IPoint pnt2)
{
IPolyline pline = new PolylineClass();
pline.FromPoint = pnt1;
pline.ToPoint = pnt2;
}
8.2.创建线符号
/// <summary>
/// 创建 线 符号
/// </summary>
/// <param name="r">The r.</param>
/// <param name="g">The g.</param>
/// <param name="b">The b.</param>
/// <returns>ILineSymbol</returns>
/// <remarks></remarks>
private static ILineSymbol CreateLineSymbol(int r, int g, int b)
{
ILineSymbol psymbol = new SimpleLineSymbolClass() as ILineSymbol;
psymbol.Color = ColorCustom(r, g, b);
psymbol.Width = ;
return psymbol;
}
8.3.创建线注记
IElement pele = new LineElementClass();
pele.Geometry = pPolyline as IGeometry;
(pele as ILineElement).Symbol = CreateLineSymbol(r, g, b);
(pmap as IGraphicsContainer).AddElement(pele, );
9创建一个点状注记(MarkerElement)
9.1根据XY创建一个点
/// <summary>
/// 根据x y创建新点
/// </summary>
/// <param name="dX">x坐标值</param>
/// <param name="dY">y坐标值</param>
/// <returns>返回点要素</returns>
/// <remarks></remarks>
public static IPoint GetPntFromXY(double dX, double dY)
{
IPoint pPnt = new PointClass();
pPnt.PutCoords(dX, dY);
return pPnt;
}
9.2.创建MarkerElement
IElement pele = new MarkerElementClass();
pele.Geometry = GetPntFromXY(x, y);
IMarkerSymbol sym = new SimpleMarkerSymbolClass();
sym.Color = ColorCustom(rDefault, gDefault, bDefault);
sym.Size = ;
(pele as IMarkerElement).Symbol = sym;
10.符号
Element创建好后可以赋个符号。这一块主要是符号化相关的东西,不做详细介绍,只简单创建一个。
ISimpleFillSymbol pSFillSym = new SimpleFillSymbolClass();
pSFillSym.Style = esriSimpleFillStyle.esriSFSHollow;
ISimpleLineSymbol pLineSymbol = new SimpleLineSymbolClass();
pLineSymbol.Style=esriSimpleLineStyle.esriSLSNull; pSFillSym.Outline = pLineSymbol; ISimpleMarkerSymbol Symbol = new SimpleMarkerSymbolClass();
(pSymbol as IMarkerSymbol).Size = ;
【ESRI论坛6周年征文】ArcEngine注记(Anno/ Label/Element等)处理专题 -入门篇的更多相关文章
- 【ArcEngine入门与提高】Element(元素)、Annotation(注记)旋转
因项目需要,需要做一个旋转注记的工具.因为注记这玩意用的比较少,网上资源也很少,所以做起来相当头疼.在经过一番研究之后,终于搞清楚注记的存储原理了,原来是和Element的类似,只不过注记是要把Ele ...
- ArcEngine标注和注记
转自原文 ArcEngine标注和注记 标注和注记是ArcEngine中提供的两种使用文字信息标注地图要素的方式.其中标注是作为图层的属性存在的,可以动态创建,注记作为地理要素被存储.需要注意的是Sh ...
- 用IFeatureWorkspaceAnno.CreateAnnotationClass 创建注记图层时报“The application is not licensed to modify or create schema”的错误的解决方案。
用IFeatureWorkspaceAnno.CreateAnnotationClass 的方法创建注记图层的时候报"The application is not licensed to m ...
- ArcGis 创建Annotation注记要素类、添加注记要素 并加载到Activeview AO C#
AO中一般有两种方式存储图面注记元素,一种使用TextElement,它是文档级的元素,编辑后要通过文档(mxd)保存:另一种是使用Annotation要素类,它是一个独立的要素类(featurecl ...
- arcgis10.2 打开CAD文件注记乱码
1.使用ARCGIS10.2打开CAD文件,图面显示的注记内容为乱码,属性表中的注记内容正常2.同样的CAD文件在ARCGIS9.3中打开正常出现此情况影响历史数据使用,请求ESRI技术支持注:系统添 ...
- CentOS-7.4(1708)release notes发行注记
Red Hat Enterprise Linux 当前的最新版本是 7.3. Red Hat Enterprise Linux 7 当前仅支持 64 位CPU:64-bit AMD.64-bit In ...
- ArcGIS中的标注和注记
在ArcMap中可以使用标注和注记来识别要素,选择标注或注记取决于你需要如何控制文本显示以及在ArcMap中如何存储文本. 1.标注只是临时显示相关数据或字段 2.标注用于长时间保存数据以及显示方式. ...
- 关于arcgis engine的注记显示与关闭问题
1.注记的添加需要拿到IGeoFeatureLayer接口下的AnnotationProperties属性,转为IAnnotationLayerPropertiesCollection接口,并创建一个 ...
- 创建文本注记TextElement
1.创建一个字体 /// <summary> /// 字体设置 /// </summary> /// <param name="size">Th ...
随机推荐
- 【ASM】ASMSNMP用户已存在
[ASM]ASMSNMP用户已存在 During Oracle Grid Infrastructure for a cluster installation, the ASMSNMP account ...
- Lucene索引文件学习
最近在做搜索,抽空看一下lucene,资料挺多的,不过大部分都是3.x了--在对着官方文档大概看一下. 优化后的lucene索引文件(4.9.0) 一.段文件 1.段文件:segments_5p和s ...
- YARN与MRv1的对比
YARN与MRv1的对比 转载请注明出处:http://www.cnblogs.com/BYRans/ Hadoop 1.0存在的问题 由于Hadoop 1.0的良好特性,Hadoop 1.0被应用到 ...
- eclipse下maven项目保持原有目录结构配置resin运行环境
maven项目用起来很方便,但是它的目录结构和eclipse的目录结构是有区别的,故而在eclipse下的maven项目,直接运行调试是有一些问题的. 为了方便maven项目的运行调试,因而也就有了像 ...
- insertion sort(插入排序)
#include<stdio.h> #include<time.h> int insertion_sort() { ; int a[max],i,j; srand((unsig ...
- Java Generics and Collections-2.2
2.2 Wildcards with extends 前面介绍过List<Integer>不是List<Number>的子类,即前者不能替换后者, java使用? extend ...
- Neutron 理解(14):Neutron ML2 + Linux bridge + VxLAN 组网
学习 Neutron 系列文章: (1)Neutron 所实现的虚拟化网络 (2)Neutron OpenvSwitch + VLAN 虚拟网络 (3)Neutron OpenvSwitch + GR ...
- 利用QMP和QEMU虚拟机交互的几种方式
QMP是一种基于JSON格式的传输协议,我们能利用它与一个QEMU虚拟机实例进行交互,例如查询,更改虚拟机的状态,获取设备信息等等.下面是几种创建QMP的方法以及对其它的一些基本命令的使用: 1.基于 ...
- OpenStack 企业私有云的若干需求(6):大规模扩展性支持
本系列会介绍OpenStack 企业私有云的几个需求: 自动扩展(Auto-scaling)支持 多租户和租户隔离 (multi-tenancy and tenancy isolation) 混合云( ...
- PHP的魔法方法__set() __get()
php的魔法方法__set()与__get() Tags: PHP 我们先来看看官方的文档如何定义他们的: public void __set(string $name, mixed $value); ...