ArcGIS Engine图层标注

按指定一个或多个字段,指定分隔符进行标注,可设置字体、字号、颜色、是否加粗、是否斜体等。

代码如下:

/// <summary>
/// 要素图层标注
/// </summary>
public class FeatureLayerAnnotationHelper
{
	/// <summary>
	/// 要素图层设置标注
	/// </summary>
	/// <param name="pFeaturelayer">要素图层</param>
	/// <param name="nColorR">标注颜色R分量</param>
	/// <param name="nColorG">标注颜色G分量</param>
	/// <param name="ColorB">标注颜色B分量</param>
	/// <param name="lstLableField">标注字段(可为多个)</param>
	/// <param name="sSeparator">标注字段分隔符</param>
	/// <param name="sFontName">字体名称</param>
	/// <param name="nSize">字号</param>
	/// <param name="bBold">是否加粗</param>
	/// <param name="bItalic">是否斜体</param>
	/// <param name="dMinScale">最小显示比例(分母)</param>
	/// <param name="dMaxScale">最大显示比例(分母)</param>
	public static void SetLabel(IFeatureLayer pFeaturelayer, int nColorR, int nColorG, int ColorB, List<string> lstLableField,
		string sSeparator = " ", string sFontName = "宋体", int nSize = 8, bool bBold = false, bool bItalic = false, double dMinScale = 10000, double dMaxScale = -1)
	{
		SetLabel(pFeaturelayer, new RgbColorClass() { Red = nColorG, Green = nColorG, Blue = ColorB }, lstLableField, sSeparator, sFontName, nSize, bBold, bItalic, dMinScale, dMaxScale);
	}

	/// <summary>
	/// 要素图层设置标注
	/// </summary>
	/// <param name="pFeaturelayer">要素图层</param>
	/// <param name="pRGB">标注颜色</param>
	/// <param name="lstLableField">标注字段(可为多个)</param>
	/// <param name="sSeparator">标注字段分隔符</param>
	/// <param name="sFontName">字体名称</param>
	/// <param name="nSize">字号</param>
	/// <param name="bBold">是否加粗</param>
	/// <param name="bItalic">是否斜体</param>
	/// <param name="dMinScale">最小显示比例(分母)</param>
	/// <param name="dMaxScale">最大显示比例(分母)</param>
	public static void SetLabel(IFeatureLayer pFeaturelayer, IRgbColor pRGB, List<string> lstLableField,
		string sSeparator = " ", string sFontName = "宋体", int nSize = 8, bool bBold = false, bool bItalic = false,
		double dMinScale = 10000, double dMaxScale = -1)
	{
		if (null == pFeaturelayer)
		{
			LogServices.WriteDebugLog("FeatureLayer is NULL when SetLabel");
			return;
		}
		var pGeoFeaturelayer = pFeaturelayer as IGeoFeatureLayer;
		IAnnotateLayerPropertiesCollection pAnnoLayerPropsCollection = pGeoFeaturelayer.AnnotationProperties;
		pAnnoLayerPropsCollection.Clear();

		stdole.IFontDisp pFontDisp = null;
		try
		{
			var dotNetFont = new System.Drawing.Font(sFontName, nSize);
			pFontDisp = ESRI.ArcGIS.ADF.COMSupport.OLE.GetIFontDispFromFont(dotNetFont) as stdole.IFontDisp;
		}
		catch
		{
			pFontDisp.Name = "宋体";
		}
		pFontDisp.Size = nSize;
		pFontDisp.Bold = bBold;
		pFontDisp.Italic = bItalic;

		if (pRGB == null)
		{
			pRGB = new RgbColorClass { Red = 0, Green = 0, Blue = 0 };
		}
		var pTextSymbol = new TextSymbolClass { Font = pFontDisp, Size = (int)pFontDisp.Size, Color = pRGB };

		IBasicOverposterLayerProperties4 pBasicOverposterlayerProps4 = new BasicOverposterLayerPropertiesClass();
		switch (pFeaturelayer.FeatureClass.ShapeType)
		{
			case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolygon:
				pBasicOverposterlayerProps4.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolygon;
				break;
			case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPolyline:
				pBasicOverposterlayerProps4.FeatureType = esriBasicOverposterFeatureType.esriOverposterPolyline;
				break;
			case ESRI.ArcGIS.Geometry.esriGeometryType.esriGeometryPoint:
				pBasicOverposterlayerProps4.FeatureType = esriBasicOverposterFeatureType.esriOverposterPoint;
				break;
		}
		pBasicOverposterlayerProps4.PointPlacementMethod = esriOverposterPointPlacementMethod.esriRotationField;
		//pBasicOverposterlayerProps4.RotationField = angleField;

		ILabelEngineLayerProperties pLabelEnginelayerProps = new LabelEngineLayerPropertiesClass
		{
			ExpressionParser = new AnnotationJScriptEngineClass(), //使用JScript引擎
			Expression = UnionLabelField(lstLableField, sSeparator), // [BSM] +  " | "  +  [XZQDM] +  " | "  +  [XZQMC]
			Symbol = pTextSymbol,
			BasicOverposterLayerProperties = pBasicOverposterlayerProps4 as IBasicOverposterLayerProperties,
			AnnotationMinimumScale = (dMinScale > 0) ? dMinScale : -1,
			AnnotationMaximumScale = (dMaxScale > 0 && dMinScale > dMaxScale) ? dMaxScale : -1
		};
		pAnnoLayerPropsCollection.Add((IAnnotateLayerProperties)pLabelEnginelayerProps);
		pGeoFeaturelayer.DisplayAnnotation = true;
	}

	/// <summary>
	/// 将多个标注字段转为表达式
	/// </summary>
	/// <param name="lstLableField">标注字段</param>
	/// <param name="sSeparator">分隔符</param>
	/// <returns>表达式</returns>
	private static string UnionLabelField(List<string> lstLableField, string sSeparator = " ")
	{
		if (null == lstLableField || 0 >= lstLableField.Count)
			return "";
		for (int i = lstLableField.Count - 1; i >= 0; i--)
		{
			if (string.IsNullOrWhiteSpace(lstLableField[i].Trim()))
			{
				lstLableField.RemoveAt(i);
				continue;
			}
			lstLableField[i] = "[" + lstLableField[i] + "]";
		}
		var sSep = $" + \"{sSeparator}\" + ";
		return string.Join(sSep, lstLableField);
	}
}

 

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值