Source Code ServicesEditionSample WorldSizedTextStyle CS 090924.zip
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace WorldSizedTextStyle
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new TestForm());
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
using ThinkGeo.MapSuite.Core;
namespace WorldSizedTextStyle
{
public partial class TestForm : Form
{
private MapEngine mapEngine = new MapEngine();
private Bitmap bitmap = null;
ShapeFileFeatureLayer worldLayer = null;
ShapeFileFeatureLayer citiesLayer = null;
public TestForm()
{
InitializeComponent();
}
private void TestForm_Load(object sender, EventArgs e)
{
// Set the full extent and the background color
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(799469,6331325,2070180,5592939), Map.Width, Map.Height);
mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);
// Adds the static layers to the MapEngine
//Adds the country background layer.
worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\europecountries.shp", ShapeFileReadWriteMode.ReadOnly);
worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;
worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("WorldLayer", worldLayer);
//Adds the city layer for labeling.
citiesLayer = new ShapeFileFeatureLayer(@"..\..\Data\europecapitals.shp", ShapeFileReadWriteMode.ReadOnly);
citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();
//Sets the custom TextStyle WorldSizedTextStyle to display the city name with a label of a size of 50 kilometers
WorldSizedTextStyle worldSizedTextStyle = new WorldSizedTextStyle(GeographyUnit.Meter,"City_name",50,DistanceUnit.Kilometer);
citiesLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(worldSizedTextStyle);
citiesLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;
mapEngine.StaticLayers.Add("citiesLayer", citiesLayer);
//Adds a ScaleBarAdornmentLayer to the map to have a distance reference.
ScaleBarAdornmentLayer scaleBarAdornmentLayer = new ScaleBarAdornmentLayer();
scaleBarAdornmentLayer.Location = AdornmentLocation.LowerLeft;
scaleBarAdornmentLayer.UnitFamily = UnitSystem.Metric;
mapEngine.AdornmentLayers.Add(scaleBarAdornmentLayer);
DrawImage();
}
private void DrawImage()
{
if (bitmap != null) { bitmap.Dispose(); }
bitmap = new Bitmap(Map.Width, Map.Height);
mapEngine.OpenAllLayers();
mapEngine.DrawStaticLayers(bitmap, GeographyUnit.Meter);
mapEngine.DrawAdornmentLayers(bitmap, GeographyUnit.Meter);
mapEngine.CloseAllLayers();
Map.Image = bitmap;
}
private void ToolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
{
switch (e.Button.Tag.ToString())
{
case "Zoom In":
mapEngine.CurrentExtent.ScaleDown(50);
break;
case "Zoom Out":
mapEngine.CurrentExtent.ScaleUp(50);
break;
case "Full Extent":
mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-180.0, 83.0, 180.0, -90.0), Map.Width, Map.Height);
break;
case "Pan Left":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Left, 20);
break;
case "Pan Right":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Right, 20);
break;
case "Pan Up":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Up, 20);
break;
case "Pan Down":
mapEngine.CurrentExtent = ExtentHelper.Pan(mapEngine.CurrentExtent, PanDirection.Down, 20);
break;
default:
break;
}
DrawImage();
}
private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
WorldSizedTextStyle.cs
using System.Collections.Generic;
using System.Collections.ObjectModel;
using ThinkGeo.MapSuite.Core;
namespace WorldSizedTextStyle
{
class WorldSizedTextStyle : TextStyle
{
private GeographyUnit mapUnit;
private DistanceUnit textUnit;
private double textWorldSize;
public WorldSizedTextStyle()
: this(GeographyUnit.DecimalDegree, string.Empty, 50,DistanceUnit.Kilometer)
{ }
public WorldSizedTextStyle(GeographyUnit MapUnit, string TextColumnName, double TextWorldSize, DistanceUnit TextUnit)
{
this.mapUnit = MapUnit;
this.TextColumnName = TextColumnName;
this.textWorldSize = TextWorldSize;
this.textUnit = TextUnit;
}
public GeographyUnit MapUnit
{
get { return mapUnit; }
set { mapUnit = value; }
}
public double TextWorldSize
{
get { return textWorldSize; }
set { textWorldSize = value; }
}
public DistanceUnit TextUnit
{
get { return textUnit; }
set { textUnit = value; }
}
protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas,
System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInThisLayer,
System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)
{
//Gets the proper font size according to the current extent height. (meter is used as base unit)
double canvasHeightMeter;
double textSizeMeter = Conversion.ConvertMeasureUnits(textWorldSize,textUnit,DistanceUnit.Meter);
//if the mapunit is in Decimal Degrees, it uses a different logic than with other mapunits such as meters and feet.
if (mapUnit == GeographyUnit.DecimalDegree)
{
try
{
canvasHeightMeter = DecimalDegreesHelper.GetDistanceFromDecimalDegrees(canvas.CurrentWorldExtent.UpperLeftPoint.X,
canvas.CurrentWorldExtent.UpperLeftPoint.Y, canvas.CurrentWorldExtent.LowerLeftPoint.X, canvas.CurrentWorldExtent.LowerLeftPoint.Y, DistanceUnit.Meter);
}
catch { canvasHeightMeter = DecimalDegreesHelper.GetDistanceFromDecimalDegrees(180,90,180,-90,DistanceUnit.Meter); }
finally { }
}
else
{
DistanceUnit fromUnit = DistanceUnit.Meter;
if (mapUnit == GeographyUnit.Feet) fromUnit = DistanceUnit.Feet;
canvasHeightMeter = Conversion.ConvertMeasureUnits(canvas.CurrentWorldExtent.Height, fromUnit, DistanceUnit.Meter);
}
float fontSize = (float)((textSizeMeter * canvas.Height) / canvasHeightMeter);
// Loop through all of the features being passed in to draw.
foreach (Feature feature in features)
{
Collection<LabelingCandidate> labelingCandidates = GetLabelingCandidates(feature, canvas);
foreach (LabelingCandidate labelingCandidate in labelingCandidates)
{
if (CheckOverlapping(labelingCandidate, canvas, labelsInThisLayer, labelsInAllLayers)) { continue; }
SimpleCandidate simpleCandidate = new SimpleCandidate(labelingCandidate.OriginalText, labelingCandidate.ScreenArea);
if (labelsInAllLayers != null) { labelsInAllLayers.Add(simpleCandidate); }
if (labelsInThisLayer != null) { labelsInThisLayer.Add(simpleCandidate); }
string Text = feature.ColumnValues[TextColumnName];
//Draws the label according to the calculated font size.
PointShape centerPointShape = feature.GetBoundingBox().GetCenterPoint();
canvas.DrawTextWithWorldCoordinate(Text, new GeoFont("Arial", fontSize), new GeoSolidBrush(GeoColor.StandardColors.Black),
centerPointShape.X, centerPointShape.Y,DrawingLevel.LabelLevel);
}
}
}
protected override System.Collections.ObjectModel.Collection<string> GetRequiredColumnNamesCore()
{
// Here we grab the column from the textStyle and then add
// the required columns to make sure we pull back the column
// that we need for labeling.
Collection<string> columns = new Collection<string>();
if (!columns.Contains(TextColumnName))
{
columns.Add(TextColumnName);
}
return columns;
}
}
}