按指定一个或多个字段,指定分隔符进行标注,可设置字体、字号、颜色、是否加粗、是否斜体等。
代码如下:
/// <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);
}
}
最后
以上就是粗心流沙最近收集整理的关于ArcGIS Engine图层标注的全部内容,更多相关ArcGIS内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复