User Tools

Site Tools


source_code_serviceseditionsample_scalebar_cs_091015.zip

Source Code ServicesEditionSample ScaleBar CS 091015.zip

CustomUnitScaleBarAdornmentLayer.cs

 using System;  
 using System.Collections.Generic;  
 using System.Collections.ObjectModel;  
 using System.Linq;  
 using System.Text;  
 using System.Globalization;  
 using ThinkGeo.MapSuite.Core;  
 
 namespace ScaleBar  
 {  
     class CustomUnitScaleBarAdornmentLayer : AdornmentLayer  
     {  
         private int width = 200;  
         private string unitText;  
         private double meterToUnit;  
         private double unitRoundValue;  
 
         //Maximum width of the scale bar in screen coordinate  
         public int MaxWidth  
         {  
             get { return width; }  
             set { width = value; }  
         }  
 
         //Text to be displayed for the unit.  
         public string UnitText  
         {  
             get { return unitText; }  
             set { unitText = value; }  
         }  
 
         //Ratio of meter to the unit.  
         public double MeterToUnit  
         {  
             get { return meterToUnit; }  
             set { meterToUnit = value; }  
         }  
 
 
         protected override void DrawCore(GeoCanvas canvas, Collection<SimpleCandidate> labelsInAllLayers)  
         {  
             //Set the position of the scale bar on canvas.  
             float Xpos = 15;  
             float Ypos = canvas.Height - 15;  
 
             //Gets the left and right location of the scale bar in world coordinate according to the maximum width and X and Y position.  
             ScreenPointF screenLocation = GetDrawingLocation(canvas, width, Ypos);  
             PointShape scaleBarMapPointR = ExtentHelper.ToWorldCoordinate(canvas.CurrentWorldExtent, screenLocation.X, screenLocation.Y, canvas.Width, canvas.Height);  
             PointShape scaleBarMapRightPointR = ExtentHelper.ToWorldCoordinate(canvas.CurrentWorldExtent, screenLocation.X + this.width, screenLocation.Y, canvas.Width, canvas.Height);  
 
             //Gets the length of the scale bar according to the unit and the maximum width of the scale bar.  
             double fullBarLength = scaleBarMapPointR.GetDistanceTo(scaleBarMapRightPointR, canvas.MapUnit, DistanceUnit.Meter);  
             //Adjusts the length of the scale bar in order to have a round number.  
             unitRoundValue = GetRoundValue(fullBarLength / meterToUnit);  
             double barLength = ((unitRoundValue * meterToUnit) * this.width) / fullBarLength;  
 
             //Draw the line of the scale bar according to the adjusted length.  
             GeoPen pen = new GeoPen(GeoColor.StandardColors.Black, 1F);  
             canvas.DrawLine(new ScreenPointF[]  {new ScreenPointF(Xpos, Ypos - 10), new ScreenPointF(Xpos, Ypos), new ScreenPointF((float)barLength + Xpos,  
                         Ypos), new ScreenPointF((float)barLength + Xpos, Ypos - 10) }, pen, DrawingLevel.LevelOne, 0, 0);  
 
             //Displays the text for the value and unit text.  
             canvas.DrawText(Convert.ToString(unitRoundValue) + " " + unitText, new GeoFont("Arial", 8), new GeoSolidBrush(GeoColor.StandardColors.Black),  
                 new ScreenPointF[] { new ScreenPointF((float)(barLength / 2) + Xpos, Ypos - 10) }, DrawingLevel.LevelOne);  
 
         }  
 
         //Function to round down the length to fit within the maximum width of the scale bar.  
         private double GetRoundValue(double unitValue)  
         {  
             double result;  
             double interval = GetRoundingValue(unitValue);  
             result = FloorNumber(unitValue, interval);  
             return result;  
         }  
 
         //Gets the rounding value to be used according to the length of the unit value.  
         private double GetRoundingValue(double UnitValue)  
         {  
             double result = 0;  
             if (UnitValue > 100000) { result = 50000; }  
             else if (UnitValue > 10000) { result = 5000; }  
             else if (UnitValue > 1000) { result = 500; }  
             else if (UnitValue > 100) { result = 50; }  
             else if (UnitValue > 10) { result = 5; }  
             else if (UnitValue > 1) { result = 0.5; }  
             else if (UnitValue > 0.1) { result = 0.05; }  
             else if (UnitValue > 0.01) { result = 0.005; }  
 
             return result;  
         }  
 
         //Function used for rounding down a number according to the rounding value.  
         private double FloorNumber(double Number, double Rounding)  
         {  
             double result = 0;  
             double IEEERemainder = Math.IEEERemainder(Number, Rounding);  
             if (IEEERemainder > 0)  
                 result = Number - IEEERemainder;  
             else if (IEEERemainder < 0)  
                 result = (Number + Math.Abs(IEEERemainder)) - Rounding;  
             else  
                 result = Number;  
             return result;  
         }  
     }  
 }  
 

Program.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace ScaleBar  
 {  
     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 ScaleBar  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
 
         CustomUnitScaleBarAdornmentLayer customUnitScaleBarAdornmentLayer = 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(-91.31,30.98,-88.43,28.85), Map.Width, Map.Height);  
             mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean);  
 
             // Adds the static layers to the MapEngine.  
             ShapeFileFeatureLayer stateLayer = new ShapeFileFeatureLayer(@"..\..\Data\USStates.shp", ShapeFileReadWriteMode.ReadOnly);  
             stateLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1;  
             stateLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             mapEngine.StaticLayers.Add("StatesLayer", stateLayer);  
 
             // Adds the CustomUnitScaleBarAdornmentLayer to the Adornment layers of the MapEngine.  
             customUnitScaleBarAdornmentLayer = new CustomUnitScaleBarAdornmentLayer();  
             // Text to be displayed on the scale bar for Nautical Miles.  
             customUnitScaleBarAdornmentLayer.UnitText = "Nautical Miles";  
             //Ratio of meters to nautical mile.  
             customUnitScaleBarAdornmentLayer.MeterToUnit = 1852;  
             mapEngine.AdornmentLayers.Add(customUnitScaleBarAdornmentLayer);  
 
             DrawImage();  
         }  
 
         private void radioBtnMeter_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnMeter.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for Meters.  
                 //Sets Ratio of meters to meters.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Meters";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 1;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnKilometer_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnKilometer.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for kilometers.  
                 //Sets Ratio of meters to kilometers.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Kilometers";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 1000;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnFoot_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnFoot.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for feet.  
                 //Sets Ratio of meters to foot.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Feet";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 0.3048;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnMile_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnMile.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for miles.  
                 //Sets Ratio of meters to miles.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Miles";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 1609.344;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnYard_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnYard.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for yards.  
                 //Sets Ratio of meters to yards.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Yards";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 0.9144;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnNauticalMile_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnNauticalMile.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for nautical miles.  
                 //Sets Ratio of meters to nautical miles.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Nautical Miles";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 1852;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnChain_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnChain.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for chains.  
                 //Sets Ratio of meters to chain.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Chains";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 20.1;  
                 DrawImage();  
             }  
         }  
 
         private void radioBtnRod_CheckedChanged(object sender, EventArgs e)  
         {  
             if (radioBtnRod.Checked == true)  
             {  
                 // Text to be displayed on the scale bar for rods.  
                 //Sets Ratio of meters to rod.  
                 customUnitScaleBarAdornmentLayer.UnitText = "Rods";  
                 customUnitScaleBarAdornmentLayer.MeterToUnit = 5;  
                 DrawImage();  
             }  
         }  
 
 
         private void DrawImage()  
         {  
             if (bitmap != null) { bitmap.Dispose(); }  
             bitmap = new Bitmap(Map.Width, Map.Height);  
             mapEngine.OpenAllLayers();  
             mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.DrawAdornmentLayers(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_scalebar_cs_091015.zip.txt · Last modified: 2015/09/09 03:19 by admin