User Tools

Site Tools


source_code_serviceseditionsample_labelingbasedoncolumns_cs_090807.zip

Source Code ServicesEditionSample LabelingBasedOnColumns CS 090807.zip

ColumnBasedTextStyle.cs

 using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace LabelingBasedOnColumns  
 {  
     class ColumnBasedTextStyle : TextStyle  
     {  
         private string sizeColumnName;  
         private string angleColumnName;  
         private string colorColumnName;  
         private string fontColumnName;  
 
         public ColumnBasedTextStyle()  
             : this(string.Empty, string.Empty, string.Empty, string.Empty, string.Empty)  
         { }  
 
         public ColumnBasedTextStyle( string TextColumnName, string SizeColumnName, string AngleColumnName, string ColorColumnName, string FontColumnName)  
         {  
             this.TextColumnName = TextColumnName;  
             this.sizeColumnName = SizeColumnName;  
             this.angleColumnName = AngleColumnName;  
             this.colorColumnName = ColorColumnName;  
             this.fontColumnName = FontColumnName;  
         }  
 
        public string SizeColumnName  
         {  
             get { return sizeColumnName; }  
             set { sizeColumnName = value; }  
         }  
 
         public string AngleColumnName  
         {  
             get { return angleColumnName; }  
             set { angleColumnName = value; }  
         }  
 
         public string ColorColumnName  
         {  
             get { return colorColumnName; }  
             set { colorColumnName = value; }  
         }  
 
         public string FontColumnName  
         {  
             get { return fontColumnName; }  
             set { fontColumnName = value; }  
         }  
 
         protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas, Collection<SimpleCandidate> labelsInThisLayer, Collection<SimpleCandidate> labelsInAllLayers)  
         {  
             // Loop through each feature and get the values for the different columns to label.  
             foreach (Feature feature in features)  
             {  
                 //Get the values for Text, Angle, Size and Font of each feature.  
                 string Text = feature.ColumnValues[TextColumnName];  
                 float Angle = Convert.ToSingle(feature.ColumnValues[AngleColumnName]);  
                 float Size = Convert.ToSingle(feature.ColumnValues[SizeColumnName]);  
                 int Color = Convert.ToInt32(feature.ColumnValues[ColorColumnName]);  
                 string Font = feature.ColumnValues[FontColumnName];  
 
                 //Convert from world coordinate to screen coordinate the location of the feature.  
                 ScreenPointF textScreenPoint = ExtentHelper.ToScreenCoordinate (canvas .CurrentWorldExtent ,feature ,canvas. Width,canvas .Height );  
 
                 //Use DrawText method of canvas to label according to the values previously set.  
                 canvas.DrawText(Text,new GeoFont(Font,Size),new GeoSolidBrush(GeoColor.FromWin32(Color)), null, new ScreenPointF[] { textScreenPoint }, DrawingLevel.LevelFour, 0, 0,Angle );  
             }  
         }  
 
         protected override Collection<string> GetRequiredColumnNamesCore()  
         {  
             // Here we grab the columns 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);  
             }  
             if (!columns.Contains(sizeColumnName))  
             {  
                 columns.Add(sizeColumnName);  
             }  
             if (!columns.Contains(angleColumnName))  
             {  
                 columns.Add(angleColumnName);  
             }  
             if (!columns.Contains(colorColumnName))  
             {  
                 columns.Add(colorColumnName);  
             }  
             if (!columns.Contains(fontColumnName))  
             {  
                 columns.Add(fontColumnName);  
             }  
             return columns;  
         }  
 
     }  
 }  
 

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace LabelingBasedOnColumns  
 {  
     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());  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Drawing;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace LabelingBasedOnColumns  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
         ShapeFileFeatureLayer worldLayer = null;  
         ShapeFileFeatureLayer labelLayer = 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(-14.00,54.92,41.10,29.31), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             // Add the static layers to the MapEngine  
             worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\Countries02.shp", ShapeFileReadWriteMode.ReadOnly);  
             worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;  
             worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("WorldLayer", worldLayer);  
 
             // Add the static layers to the MapEngine  
             labelLayer = new ShapeFileFeatureLayer(@"..\..\Data\LabelPoint.shp", ShapeFileReadWriteMode.ReadOnly);  
 
             //Set the custom textstyle with the appropriate column names  
             ColumnBasedTextStyle columnBasedTextStyle = new ColumnBasedTextStyle("Name","Size","Angle","Color","Font");  
             //Add the custom textstyle to the customStyles collection.  
             labelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(columnBasedTextStyle);  
             labelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("labelLayer", labelLayer);  
 
             DrawImage();  
         }  
 
 
         private void DrawImage()  
         {  
             if (bitmap != null) { bitmap.Dispose(); }  
             bitmap = new Bitmap(Map.Width, Map.Height);  
             mapEngine.OpenAllLayers();  
             mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
             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();  
         }  
     }  
 }  
 
source_code_serviceseditionsample_labelingbasedoncolumns_cs_090807.zip.txt · Last modified: 2015/09/08 05:14 by admin