User Tools

Site Tools


source_code_serviceseditionsample_scalingtextstyle_cs_090918.zip

Source Code ServicesEditionSample ScalingTextStyle CS 090918.zip

Program.cs

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

ScalingImageStyle.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 
 
 using ThinkGeo.MapSuite.Core;  
 
 namespace ScalingTextStyle  
 {  
     class ScalingImageStyle : Style  
     {  
 
       private GeoImage image = null;  
             private GeographyUnit mapUnit;  
             private double maximumScale;  
             private double minimumScale;  
             private double maximumSize;  
             private double minimumSize;  
             private float xOffsetInPixel;  
             private float yOffsetInPixel;  
             private float rotateAngle;  
 
             // A default constructor that just calls the main constructor  
             public ScalingImageStyle()  
                 : this(GeographyUnit.DecimalDegree,null,double.MaxValue, 0,20,10 )  
             {  
             }  
 
             // A constructor with fewer parameters that calls the main constructor  
             public ScalingImageStyle(GeographyUnit mapUnit, GeoImage image, double MaximumScale, double MinimumScale, double MaximumSize, double MinimumSize)  
 
             {  
                 this.image = image;  
                 this.maximumScale = MaximumScale;  
                 this.minimumScale = MinimumScale;  
                 this.maximumSize = MaximumSize;  
                 this.minimumSize = MinimumSize;  
                 this.mapUnit = mapUnit;  
             }  
 
 
             // This is the map unit to determine the scale.  
             public GeographyUnit MapUnit  
             {  
                 get { return mapUnit; }  
                 set { mapUnit = value; }  
             }  
 
             // This is the image that will be scaled.  
             public GeoImage Image  
             {  
                 get { return image; }  
                 set { image = value; }  
             }  
 
             // The maximum scale will be the largest scale used to calculate the size of the image.  
             // If the scale gets larger than the maximum, then we compute the scaling based on  
             // this number instead.  This means that after this scale, the image will not get  
             // any smaller no matter how much more you zoom out.  
             public double MaximumScale  
             {  
                 get { return maximumScale; }  
                 set { maximumScale = value; }  
             }  
 
             // The minimum scale will be the smallest scale used to calculate the size of the image.  
             // If the scale gets smaller than the minimum, then we compute the scaling based on  
             // this number instead.  This means that after this scale, the image will not get  
             // any larger no matter how much more you zoom in.  
             public double MinimumScale  
             {  
                 get { return minimumScale; }  
                 set { minimumScale = value; }  
             }  
 
             // The MaximumSize is the size of the image at MinimumScale and lower.  
             public double MaximumSize  
             {  
                 get { return maximumSize; }  
                 set { maximumSize = value; }  
             }  
 
             // The MinimumSize is the size of the image at MaximumScale and higher.  
             public double MinimumSize  
             {  
                 get { return minimumSize; }  
                 set { minimumSize = value; }  
             }  
 
             // This is the X offset in pixels for the image.  
             public float XOffsetInPixel  
             {  
                 get { return xOffsetInPixel; }  
                 set { xOffsetInPixel = value; }  
             }  
 
             // This is the Y offset in pixels for the image.  
             public float YOffsetInPixel  
             {  
                 get { return yOffsetInPixel; }  
                 set { yOffsetInPixel = value; }  
             }  
 
             // This is the angle you would like to rotate the image.  
             public float RotationAngle  
             {  
                 get { return rotateAngle; }  
                 set { rotateAngle = value; }  
             }  
 
             // This is the method we needed to override.  
             protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInThisLayer, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)  
             {  
                 // First let's make sure the image isn't null.  
                 if (image != null)  
                 {  
                     // Loop through all of the features being passed in to draw.  
                     foreach (Feature feature in features)  
                     {  
                         // Let's make sure the features being passed in are points or multi points.  
                         WellKnownType shapeWellKnownType = feature.GetWellKnownType();  
                         if (shapeWellKnownType == WellKnownType.Point || shapeWellKnownType == WellKnownType.Multipoint)  
                         {  
                             double currentScale = ExtentHelper.GetScale(canvas.CurrentWorldExtent, canvas.Width, mapUnit);  
 
                             // Enforce the minimum and maximum scale properties  
                             if (currentScale > maximumScale) { currentScale = maximumScale; }  
                             if (currentScale < minimumScale) { currentScale = minimumScale; }  
 
                             // Calculate the size of the image based on the scale.  
                             //If the scale is between minimum scale and maximum scale, the size of the image will be calculated according  
                             //to the maximum scale/minimum scale ratio.  
                             //For example:  
                             //Minimum Scale:100,000  
                             //Maximum Scale:200,000  
                             //Minimum Size:10  
                             //Maximum Size:50  
                             //If the scale is 150,000 (mid value between 100,000 and 200,000), the size of the image will be  
                             //30 (mid value between 10 and 50).  
 
                             float imageSize = (float) (maximumSize - ((currentScale - minimumScale) * (maximumSize - minimumSize)) /  
                                                 (maximumScale - minimumScale));  
 
                             // Call the canvas method to draw the image scaled.  
                             PointShape point = new PointShape(feature.GetWellKnownBinary());  
                             canvas.DrawWorldImage(image, point.X, point.Y, imageSize, imageSize, DrawingLevel.LevelOne, xOffsetInPixel, yOffsetInPixel, rotateAngle);  
                         }  
                     }  
                 }  
             }  
 
 
 
     }  
 }  
 

ScalingTextStyle.cs

 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace ScalingTextStyle  
 {  
     class ScalingTextStyle : TextStyle  
     {  
         private GeographyUnit mapUnit;  
         private double maximumScale;  
         private double minimumScale;  
         private float maximumLabelSize;  
         private float minimumLabelSize;  
 
         public ScalingTextStyle()  
             : this(GeographyUnit.DecimalDegree, string.Empty, double.MaxValue, 0, 8, 16)  
         { }  
 
         public ScalingTextStyle(GeographyUnit MapUnit, string TextColumnName, double MaximumScale, double MinimumScale, float MaximumLabelSize, float MinimumLabelSize)  
         {  
             this.mapUnit = MapUnit;  
             this.TextColumnName = TextColumnName;  
             this.maximumScale = MaximumScale;  
             this.minimumScale = MinimumScale;  
             this.maximumLabelSize = MaximumLabelSize;  
             this.minimumLabelSize = MinimumLabelSize;  
         }  
 
         // This is the map unit to determine the scale.  
         public GeographyUnit MapUnit  
         {  
             get { return mapUnit; }  
             set { mapUnit = value; }  
         }  
 
         // The maximum scale will be the largest scale used to calculate the size of the label.  
         // If the scale gets larger than the maximum, then we compute the scaling based on  
         // this number instead.  This means that after this scale, the size of the label will not get  
         // any smaller no matter how much more you zoom out.  
         public double MaximumScale  
         {  
             get { return maximumScale; }  
             set { maximumScale = value; }  
         }  
 
         // The minimum scale will be the smallest scale used to calculate the size of the label.  
         // If the scale gets smaller than the minimum, then we compute the scaling based on  
         // this number instead.  This means that after this scale, the size of the label will not get  
         // any larger no matter how much more you zoom in.  
         public double MinimumScale  
         {  
             get { return minimumScale; }  
             set { minimumScale = value; }  
         }  
 
         // The MaximumSize is the size of the label at MinimumScale and lower.  
         public float MaximumLabelSize  
         {  
             get { return maximumLabelSize; }  
             set { maximumLabelSize = value; }  
         }  
 
         // The MinimumSize is the size of the label at MaximumScale and higher.  
         public float MinimumLabelSize  
         {  
             get { return minimumLabelSize; }  
             set { minimumLabelSize = value; }  
         }  
 
         protected override void DrawCore(IEnumerable<Feature> features, GeoCanvas canvas,  
             System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInThisLayer,  
             System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)  
         {  
 
             // 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)  
                 {  
                     // Let's make sure the features being passed in are points or multi points.  
                     WellKnownType shapeWellKnownType = feature.GetWellKnownType();  
                     if (shapeWellKnownType == WellKnownType.Point || shapeWellKnownType == WellKnownType.Multipoint)  
                     {  
                         double currentScale = ExtentHelper.GetScale(canvas.CurrentWorldExtent, canvas.Width, mapUnit);  
                         // Enforce the minimum and maximum scale properties  
                         if (currentScale > maximumScale) { currentScale = maximumScale; }  
                         if (currentScale < minimumScale) { currentScale = minimumScale; }  
 
                         // Calculate the size of the label based on the scale.  
                         //If the scale is between minimum scale and maximum scale, the size of the label will be calculated according  
                         //to the maximum scale/minimum scale ratio.  
                         //For example:  
                         //Minimum Scale:100,000  
                         //Maximum Scale:200,000  
                         //Minimum Size:9  
                         //Maximum Size:16  
                         //If the scale is 150,000 (mid value between 100,000 and 200,000), the size of the image will be  
                         //13 (mid value between 9 and 16).  
 
                         float labelSize = (float)(maximumLabelSize - ((currentScale - minimumScale) * (maximumLabelSize - minimumLabelSize)) /  
                                            (maximumScale - minimumScale));  
                         //for (int i = 0; i < labelingCandidate.LabelInformation.Count; i++)  
                         //{  
                         //    for (int j = 0; j < labelingCandidate.ScreenArea.OuterRing.Vertices.Count; j++)  
                         //    {  
                         //        labelingCandidate.ScreenArea.OuterRing.Vertices[j] = new Vertex(labelingCandidate.ScreenArea.OuterRing.Vertices[j].X, labelingCandidate.ScreenArea.OuterRing.Vertices[j].Y - labelSize);  
                         //    }  
                         //}  
 
                         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];  
                         // Call the canvas method to draw the label scaled.  
                         PointShape point = new PointShape(feature.GetWellKnownBinary());  
 
                         //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("Arial", labelSize), new GeoSolidBrush(GeoColor.StandardColors.Black),  
                                         null, new ScreenPointF[] { textScreenPoint }, DrawingLevel.LevelFour,  
                                         this.XOffsetInPixel, this.YOffsetInPixel - labelSize, (float)this.RotationAngle); //- labelSize  
 
                     }  
                 }  
             }  
         }  
 
         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;  
         }  
     }  
 }  
 

TestForm.cs

 using System;  
 using System.Drawing;  
 using System.Windows.Forms;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace ScalingTextStyle  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
 
        public TestForm()  
         {  
             InitializeComponent();  
         }  
 
         private void TestForm_Load(object sender, EventArgs e)  
         {  
             // Sets the full extent and the background color  
             mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-8.6922,61.2291,28.2812,33.2171), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             // Adds the static layers to the MapEngine  
             ShapeFileFeatureLayer 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);  
 
             //Uses ScalingImageStyle for displaying the point symbol of the layer.  
             ShapeFileFeatureLayer capitalLayer = new ShapeFileFeatureLayer(@"..\..\Data\worldcapitals.shp", ShapeFileReadWriteMode.ReadOnly);  
             capitalLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();  
             ScalingImageStyle scalingImageStyle = new ScalingImageStyle(GeographyUnit.DecimalDegree,new GeoImage( @"..\..\Data\Icon_red_ball.png"), 31000000, 3000000, 9, 4);  
             capitalLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(scalingImageStyle);  
             capitalLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("CapitalLayer", capitalLayer);  
 
             //Uses ScalingTextStyle for displaying the labels of the layer.  
             ShapeFileFeatureLayer capitalLabelLayer = new ShapeFileFeatureLayer(@"..\..\Data\worldcapitals.shp", ShapeFileReadWriteMode.ReadOnly);  
             capitalLabelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Clear();  
             ScalingTextStyle scalingTextStyle = new ScalingTextStyle(GeographyUnit.DecimalDegree, "City_name", 31000000, 3000000, 18, 8);  
             capitalLabelLayer.ZoomLevelSet.ZoomLevel01.CustomStyles.Add(scalingTextStyle);  
             capitalLabelLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("CapitalLabelLayer", capitalLabelLayer);  
 
             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_scalingtextstyle_cs_090918.zip.txt · Last modified: 2015/09/08 08:02 by admin