📄 labellayer.cs
字号:
// Copyright 2005, 2006 - Morten Nielsen (www.iter.dk)
//
// This file is part of SharpMap.
// SharpMap is free software; you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// SharpMap is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
// You should have received a copy of the GNU Lesser General Public License
// along with SharpMap; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Collections.Generic;
using System.Text;
namespace SharpMap.Layers
{
/// <summary>
/// Label layer class
/// </summary>
/// <example>
/// Creates a new label layer and sets the label text to the "Name" column in the FeatureDataTable of the datasource
/// <code lang="C#">
/// //Set up a label layer
/// SharpMap.Layers.LabelLayer layLabel = new SharpMap.Layers.LabelLayer("Country labels");
/// layLabel.DataSource = layCountries.DataSource;
/// layLabel.Enabled = true;
/// layLabel.LabelColumn = "Name";
/// layLabel.Style = new SharpMap.Styles.LabelStyle();
/// layLabel.Style.CollisionDetection = true;
/// layLabel.Style.CollisionBuffer = new SizeF(20, 20);
/// layLabel.Style.ForeColor = Color.White;
/// layLabel.Style.Font = new Font(FontFamily.GenericSerif, 8);
/// layLabel.MaxVisible = 90;
/// layLabel.Style.HorizontalAlignment = SharpMap.Styles.LabelStyle.HorizontalAlignmentEnum.Center;
/// </code>
/// </example>
public class LabelLayer : Layer, IDisposable
{
/// <summary>
/// Labelling behaviour for Multipart geometry collections
/// </summary>
public enum MultipartGeometryBehaviourEnum
{
/// <summary>
/// Place label on all parts (default)
/// </summary>
All,
/// <summary>
/// Place label on object which the greatest length or area.
/// </summary>
/// <remarks>
/// Multipoint geometries will default to <see cref="First"/>
/// </remarks>
Largest,
/// <summary>
/// The center of the combined geometries
/// </summary>
CommonCenter,
/// <summary>
/// Center of the first geometry in the collection (fastest method)
/// </summary>
First
}
/// <summary>
/// Delegate method for creating advanced label texts
/// </summary>
/// <param name="fdr"></param>
/// <returns></returns>
public delegate string GetLabelMethod(SharpMap.Data.FeatureDataRow fdr);
/// <summary>
/// Creates a new instance of a LabelLayer
/// </summary>
public LabelLayer(string layername)
{
_Style = new SharpMap.Styles.LabelStyle();
this.LayerName = layername;
this.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
this.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
_MultipartGeometryBehaviour = MultipartGeometryBehaviourEnum.All;
_LabelFilter = SharpMap.Rendering.LabelCollisionDetection.SimpleCollisionDetection;
}
private MultipartGeometryBehaviourEnum _MultipartGeometryBehaviour;
/// <summary>
/// Gets or sets labelling behavior on multipart geometries
/// </summary>
/// <remarks>Default value is <see cref="MultipartGeometryBehaviourEnum.All"/></remarks>
public MultipartGeometryBehaviourEnum MultipartGeometryBehaviour
{
get { return _MultipartGeometryBehaviour; }
set { _MultipartGeometryBehaviour = value; }
}
private SharpMap.Rendering.LabelCollisionDetection.LabelFilterMethod _LabelFilter;
/// <summary>
/// Filtermethod delegate for performing filtering
/// </summary>
/// <remarks>
/// Default method is <see cref="SharpMap.Rendering.LabelCollisionDetection.SimpleCollisionDetection"/>
/// </remarks>
public SharpMap.Rendering.LabelCollisionDetection.LabelFilterMethod LabelFilter
{
get { return _LabelFilter; }
set { _LabelFilter = value; }
}
private System.Drawing.Drawing2D.SmoothingMode _SmoothingMode;
/// <summary>
/// Render whether smoothing (antialiasing) is applied to lines and curves and the edges of filled areas
/// </summary>
public System.Drawing.Drawing2D.SmoothingMode SmoothingMode
{
get { return _SmoothingMode; }
set { _SmoothingMode = value; }
}
private System.Drawing.Text.TextRenderingHint _TextRenderingHint;
/// <summary>
/// Specifies the quality of text rendering
/// </summary>
public System.Drawing.Text.TextRenderingHint TextRenderingHint
{
get { return _TextRenderingHint; }
set { _TextRenderingHint = value; }
}
private SharpMap.Data.Providers.IProvider _DataSource;
/// <summary>
/// Gets or sets the datasource
/// </summary>
public SharpMap.Data.Providers.IProvider DataSource
{
get { return _DataSource; }
set { _DataSource = value; }
}
private SharpMap.Styles.LabelStyle _Style;
/// <summary>
/// Gets or sets the rendering style of the label layer.
/// </summary>
public SharpMap.Styles.LabelStyle Style
{
get { return _Style; }
set { _Style = value; }
}
private SharpMap.Rendering.Thematics.ITheme _theme;
/// <summary>
/// Gets or sets thematic settings for the layer. Set to null to ignore thematics
/// </summary>
public SharpMap.Rendering.Thematics.ITheme Theme
{
get { return _theme; }
set { _theme = value; }
}
private string _LabelColumn;
/// <summary>
/// Data column or expression where label text is extracted from.
/// </summary>
/// <remarks>
/// This property is overriden by the <see cref="LabelStringDelegate"/>.
/// </remarks>
public string LabelColumn
{
get { return _LabelColumn; }
set { _LabelColumn = value; }
}
private GetLabelMethod _getLabelMethod;
/// <summary>
/// Gets or sets the method for creating a custom label string based on a feature.
/// </summary>
/// <remarks>
/// <para>If this method is not null, it will override the <see cref="LabelColumn"/> value.</para>
/// <para>The label delegate must take a <see cref="SharpMap.Data.FeatureDataRow"/> and return a string.</para>
/// <example>
/// Creating a label-text by combining attributes "ROADNAME" and "STATE" into one string, using
/// an anonymous delegate:
/// <code lang="C#">
/// myLabelLayer.LabelStringDelegate = delegate(SharpMap.Data.FeatureDataRow fdr)
/// { return fdr["ROADNAME"].ToString() + ", " + fdr["STATE"].ToString(); };
/// </code>
/// </example>
/// </remarks>
public GetLabelMethod LabelStringDelegate
{
get { return _getLabelMethod; }
set { _getLabelMethod = value; }
}
private string _RotationColumn;
/// <summary>
/// Data column from where the label rotation is derived.
/// If this is empty, rotation will be zero, or aligned to a linestring.
/// Rotation are in degrees (positive = clockwise).
/// </summary>
public string RotationColumn
{
get { return _RotationColumn; }
set { _RotationColumn = value; }
}
private int _Priority;
/// <summary>
/// A value indication the priority of the label in cases of label-collision detection
/// </summary>
public int Priority
{
get { return _Priority; }
set { _Priority = value; }
}
/// <summary>
/// Renders the layer
/// </summary>
/// <param name="g">Graphics object reference</param>
/// <param name="map">Map which is rendered</param>
public override void Render(System.Drawing.Graphics g, Map map)
{
if (this.Style.Enabled && this.Style.MaxVisible >= map.Zoom && this.Style.MinVisible < map.Zoom)
{
if (this.DataSource == null)
throw (new ApplicationException("DataSource property not set on layer '" + this.LayerName + "'"));
g.TextRenderingHint = this.TextRenderingHint;
g.SmoothingMode = this.SmoothingMode;
SharpMap.Geometries.BoundingBox envelope = map.Envelope; //View to render
if (this.CoordinateTransformation != null)
envelope = SharpMap.CoordinateSystems.Transformations.GeometryTransform.TransformBox(envelope, this.CoordinateTransformation.MathTransform.Inverse());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -