简单标签 简单标签
简单标签指定了特征必须如何标记。
支持的选项有:
属性
描述
LabelAttribute
指定用作标签来源的属性名称。
LabelExpression
提供了一种自定义和格式化标签文本的方式。覆盖 LabelAttribute
FontFamily
指定用于渲染文本的字体系列。默认值取决于系统。
FontStyle
应用于文本的样式。
- FontStyle.Regular - 常规文本。
- FontStyle.Bold - 粗体文本。
- FontStyle.Italic - 斜体文本。
- FontStyle.Underine - 带下划线的文本。
- FontStyle.StrikeOut - 中间有一条线的文本。
FontSize
指定文本的大小。
FontColor
确定文本的颜色。
HaloSize
确定文本周围光晕(或轮廓)的大小。
HaloColor
确定文本周围光晕的颜色。
GeometryExpression
用于变换几何体的几何表达式,然后再将其传递给标签引擎。
MultipartMode
指定多部件几何体的渲染行为。
- MultipartMode.All - 在几何体的每个部分附近放置标签。
- MultipartMode.Any - 在几何体的任何一部分附近放置一个标签。
- MultipartMode.Largest - 在几何体的最大部分附近放置标签。
Placement
指定标签相对于几何体的位置方式。
- PointLabelPlacement - 将标签放置在几何体的中心附近。
- LineLabelPlacement - 将标签放置在线或其周长上。
Priority
指定标签的优先级,以防它与其他标签重叠。 优先级较低的标签不会被渲染。默认值为 1000。
示例
点标签示例
默认情况下,SimpleLabeling 在点上绘制文本:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(500, 200))
{
var symbol = new SimpleMarker
{
FillColor = Color.LightGray,
StrokeStyle = StrokeStyle.None
};
var labeling = new SimpleLabeling(labelAttribute: "name");
map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol, labeling);
map.Padding = 50;
map.Render(dataDir + "points_labeling_out.svg", Renderers.Svg);
}
以下是如何设置字体样式:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(500, 200))
{
var symbol = new SimpleMarker
{
FillColor = Color.LightGray,
StrokeStyle = StrokeStyle.None
};
var labeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 2,
HaloColor = Color.LightGray,
FontSize = 15,
FontStyle = FontStyle.Italic,
};
map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol, labeling);
map.Padding = 50;
map.Render(dataDir + "points_labeling_styled_out.svg", Renderers.Svg);
}
为了控制文本相对于点特征的位置,必须设置 placement 属性:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(500, 200))
{
var symbol = new SimpleMarker
{
FillColor = Color.LightGray,
StrokeStyle = StrokeStyle.None
};
var labeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 1,
Placement = new PointLabelPlacement
{
VerticalAnchorPoint = VerticalAnchor.Bottom,
HorizontalAnchorPoint = HorizontalAnchor.Left,
HorizontalOffset = 2,
VerticalOffset = 2,
Rotation = 10,
}
};
map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbol, labeling);
map.Padding = 50;
map.Render(dataDir + "points_labeling_placed_out.svg", Renderers.Svg);
}
对于更高级的场景,您可能希望为特征选择不同的标签。以下是如何做到:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(500, 200))
{
var symbolizer = new SimpleMarker {FillColor = Color.LightGray, StrokeStyle = StrokeStyle.None};
var labeling = new RuleBasedLabeling();
// Set labeling to be used for small cities.
labeling.Add(f => f.GetValue<int>("population") <= 2500, new SimpleLabeling("name")
{
FontStyle = FontStyle.Italic,
HaloSize = 1,
FontSize = 10,
FontColor = Color.Green,
Priority = 1,
Placement = new PointLabelPlacement
{
VerticalAnchorPoint = VerticalAnchor.Bottom,
HorizontalAnchorPoint = HorizontalAnchor.Center,
}
});
// Set labeling to be used for all other cities.
labeling.AddElseRule(new SimpleLabeling("name")
{
HaloSize = 1,
FontSize = 15,
FontColor = Color.Red,
Priority = 2,
Placement = new PointLabelPlacement
{
VerticalAnchorPoint = VerticalAnchor.Bottom,
HorizontalAnchorPoint = HorizontalAnchor.Center,
}
});
map.Add(VectorLayer.Open(dataDir + "points.geojson", Drivers.GeoJson), symbolizer, labeling);
map.Padding = 40;
map.Render(dataDir + "rule_based_labeling_out.svg", Renderers.Svg);
}
线标签示例
默认情况下,SimpleLabeling 在线的中心附近绘制标签:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(1000, 634))
{
var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) };
var labeling = new SimpleLabeling(labelAttribute: "name");
map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer, labeling);
map.Padding = 50;
map.Render(dataDir + "lines_labeling_out.svg", Renderers.Svg);
}
为了使标签平行于线,可以使用 LineLabelPlacement 和 LineLabelAlignment.Parallel:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(1000, 634))
{
var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) };
var labeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 1,
Placement = new LineLabelPlacement
{
Alignment = LineLabelAlignment.Parallel,
}
};
map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer, labeling);
map.Padding = 50;
map.Render(dataDir + "lines_labeling_parallel_out.svg", Renderers.Svg);
}
如果您希望文本精确地跟随线,可以使用 LineLabelPlacement 和 LineLabelAlignment.Curved:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(1000, 634))
{
var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) };
var labeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 1,
Placement = new LineLabelPlacement
{
Alignment = LineLabelAlignment.Curved,
}
};
map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer, labeling);
map.Padding = 50;
map.Render(dataDir + "lines_labeling_curved_out.svg", Renderers.Svg);
}
如果您不希望文本与线重叠,请使用 LineLabelPlacement.Offset:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(1000, 634))
{
var symbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xAE, 0xD9, 0xFD) };
var labeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 1,
Placement = new LineLabelPlacement
{
Alignment = LineLabelAlignment.Curved,
Offset = 5,
}
};
map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), symbolizer, labeling);
map.Padding = 50;
map.Render(dataDir + "lines_labeling_curved_offset_out.svg", Renderers.Svg);
}
对于更高级的场景,您可能希望根据特征属性值动态调整标签样式。以下是如何做到:
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-gis/Aspose.GIS-for-.NET
using (var map = new Map(1000, 634))
{
var lineSymbolizer = new SimpleLine { Width = 1.5, Color = Color.FromArgb(0xae, 0xd9, 0xfd) };
lineSymbolizer.FeatureBasedConfiguration = (feature, symbolizer) =>
{
if (feature.GetValue<string>("NAM") == "UNK")
{
symbolizer.Width = 1;
symbolizer.Style = StrokeStyle.Dash;
}
};
var lineLabeling = new SimpleLabeling(labelAttribute: "name")
{
HaloSize = 1,
Placement = new LineLabelPlacement
{
Alignment = LineLabelAlignment.Parallel,
},
FontSize = 20,
FeatureBasedConfiguration = (feature, labeling) =>
{
if (feature.GetValue<string>("NAM") == "UNK")
{
// change labeling properties for some features.
labeling.FontStyle = FontStyle.Italic;
labeling.FontSize = 10;
labeling.Priority = -1;
var placement = (LineLabelPlacement) labeling.Placement;
placement.Alignment = LineLabelAlignment.Curved;
placement.Offset = 5;
}
}
};
map.Add(VectorLayer.Open(dataDir + "lines.geojson", Drivers.GeoJson), lineSymbolizer, lineLabeling);
map.Padding = 50;
map.Render(dataDir + "lines_labeling_feature_based_out.svg", Renderers.Svg);
}