User Tools

Site Tools


source_code_serviceseditionsample_decimaldegreesformatting_cs_091007.zip

Source Code ServicesEditionSample DecimalDegreesFormatting CS 091007.zip

DecimalDegrees.cs

 using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Text;  
 
 namespace DecimalDegreesFormatting  
 {  
     static class DecimalDegrees  
     {  
         public enum Type { Longitude, Latitude };  
 
         //From Decimal-degrees to Degrees Decimal-minutes.  
         public static string d_To_Dm(double d, int precision,Type type)  
         {  
             string result;  
             string hemisphere = GetHemisphere(d, type);  
             d = Math.Abs(d);  
             double D = Math.Truncate(d);  
             double m = Math.Round(((d - D) * 60),precision);  
             string zeroHolder = "";  
             if (m < 10) { zeroHolder = "0"; }  
             result = D + "° " + zeroHolder + m + "' " + hemisphere;  
 
             return result;  
         }  
 
        //From Decimal-degrees to Degrees Minutes Decimal-seconds.  
        public static string d_To_DMs(double d, int precision, Type type)  
         {  
             string result ;  
             string hemisphere = GetHemisphere(d, type);  
             d = Math.Abs(d);  
             double D = Math.Truncate(d);  
             double M = Math.Truncate((d - D)* 60);  
             double s = Math.Round((d - D - (M / 60)) * 3600,precision);  
             string minuteZeroHolder = "";  
             string secondZeroHolder = "";  
             if (M < 10) { minuteZeroHolder = "0"; }  
             if (s < 10) { secondZeroHolder = "0"; }  
             result = D + "° " + minuteZeroHolder + M + "' " + secondZeroHolder + s + "// " + hemisphere;  
 
             return result;  
         }  
 
         //From Decimal-minutes to Decimal-degrees.  
         public static double Dm_To_d(double D, double m, int precision)  
         {  
             double d = Math.Abs(D) + (m / 60);  
             if (D < 0) { d = -d; }  
             d = Math.Round(d, precision);  
             return d;  
         }  
 
         //From Degrees Minutes Decimal-seconds to Decimal-degrees.  
         public static double DMs_To_d(double D, double M, double s, int precision)  
         {  
             double d = Math.Abs(D) + (Math.Abs(M) / 60) + (s / 3600);  
             if (D < 0) { d = -d; }  
             d = Math.Round(d, precision);  
             return d;  
         }  
 
         //Gets the Hemisphere according to the positive (North, East) or negative value (South, West).  
         private static string GetHemisphere(double d, Type type)  
         {  
             string result;  
             if (type == Type.Longitude)  
             {  
                 if (d > 0) { result = "E"; }  
                 else { result = "W"; }  
             }  
             else  
             {  
                 if (d > 0) { result = "N"; }  
                 else { result = "S"; }  
             }  
             return result;  
         }  
     }  
 
 
 }  
 

Program.cs

using System;  
 using System.Collections.Generic;  
 using System.Linq;  
 using System.Windows.Forms;  
 
 namespace DecimalDegreesFormatting  
 {  
     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 DecimalDegreesFormatting  
 {  
     public partial class TestForm : Form  
     {  
         private MapEngine mapEngine = new MapEngine();  
         private Bitmap bitmap = null;  
         ShapeFileFeatureLayer worldLayer = 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(-180.0, 83.0, 180.0, -90.0), 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 dynamic layers to the MapEngine  
             InMemoryFeatureLayer inMemoryFeatureLayer = new InMemoryFeatureLayer();  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultTextStyle = TextStyles.CreateMaskTextStyle("Label", new GeoFont("Arial",10),  
                 new GeoSolidBrush(GeoColor.SimpleColors.Black), new AreaStyle(new GeoPen(GeoColor.StandardColors.Black), new GeoSolidBrush(GeoColor.StandardColors.LightGoldenrodYellow)),10,0);  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.DefaultPointStyle = PointStyles.CreateSimpleCircleStyle(GeoColor.SimpleColors.Red, 10);  
             inMemoryFeatureLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20;  
             inMemoryFeatureLayer.Open();  
             inMemoryFeatureLayer.Columns.Add(new FeatureSourceColumn("Label"));  
             inMemoryFeatureLayer.Close();  
 
             PointShape pointShape1 = new PointShape(-98.4587,30.2358);  
             Feature newFeature1 = new Feature(pointShape1);  
             //Displays the Longitude and Latitude in the format Degrees Minutes and Decimal-seconds with a three digit precision.  
             newFeature1.ColumnValues.Add("Label", DecimalDegrees.d_To_DMs(pointShape1.X,3,DecimalDegrees.Type.Longitude)  
                                                   + "  " + DecimalDegrees.d_To_DMs(pointShape1.Y,3,DecimalDegrees.Type.Latitude));  
             inMemoryFeatureLayer.InternalFeatures.Add("point1", newFeature1);  
 
             PointShape pointShape2 = new PointShape(-57.9063,-34.9256);  
             Feature newFeature2 = new Feature(pointShape2);  
             //Displays the Longitude and Latitude in the format Degrees Decimal-minutes with a four digit precision.  
             newFeature2.ColumnValues.Add("Label", DecimalDegrees.d_To_Dm(pointShape2.X, 4, DecimalDegrees.Type.Longitude)  
                                                   + "  " + DecimalDegrees.d_To_Dm(pointShape2.Y, 4, DecimalDegrees.Type.Latitude));  
             inMemoryFeatureLayer.InternalFeatures.Add("point2", newFeature2);  
 
             //Converts Degrees Minutes Decimal-seconds to Decimal-degrees. Decimal-degrees is the only format the X and Y can be input.  
             PointShape pointShape3 = new PointShape(DecimalDegrees.DMs_To_d(34,13,47.458,2),DecimalDegrees.DMs_To_d(40,1,19.458,2));  
             Feature newFeature3 = new Feature(pointShape3);  
             //Displays the Longitude and Latitude in the format Degrees Minutes Seconds. No decimals is expressed due to the zero precision.  
             newFeature3.ColumnValues.Add("Label", DecimalDegrees.d_To_DMs(pointShape3.X, 0, DecimalDegrees.Type.Longitude)  
                                                   + "  " + DecimalDegrees.d_To_DMs(pointShape3.Y, 0, DecimalDegrees.Type.Latitude));  
             inMemoryFeatureLayer.InternalFeatures.Add("point3", newFeature3);  
 
             //Converts Degrees Minutes Decimal-seconds to Decimal-degrees. Decimal-degrees is the only format the X and Y can be input.  
             PointShape pointShape4 = new PointShape(DecimalDegrees.Dm_To_d(47,31.4568,3), DecimalDegrees.Dm_To_d(-18,40.4568,3));  
             Feature newFeature4 = new Feature(pointShape4);  
             //Displays the Longitude and Latitude in the format Degrees Decimal-minutes with a four digit precision.  
             newFeature4.ColumnValues.Add("Label", DecimalDegrees.d_To_DMs(pointShape4.X, 4, DecimalDegrees.Type.Longitude)  
                                                   + "  " + DecimalDegrees.d_To_DMs(pointShape4.Y, 4, DecimalDegrees.Type.Latitude));  
             inMemoryFeatureLayer.InternalFeatures.Add("point4", newFeature4);  
 
             mapEngine.DynamicLayers.Add(inMemoryFeatureLayer);  
 
             DrawImage();  
         }  
 
 
         private void DrawImage()  
         {  
             if (bitmap != null) { bitmap.Dispose(); }  
             bitmap = new Bitmap(Map.Width, Map.Height);  
             mapEngine.OpenAllLayers();  
             mapEngine.DrawStaticLayers(bitmap, GeographyUnit.DecimalDegree);  
             mapEngine.DrawDynamicLayers(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_decimaldegreesformatting_cs_091007.zip.txt · Last modified: 2015/09/08 05:11 by admin