This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
source_code_serviceseditionsample_worldcoordinates_cs_091017.zip [2015/08/20 03:08] 127.0.0.1 external edit |
source_code_serviceseditionsample_worldcoordinates_cs_091017.zip [2015/09/08 06:09] (current) admin |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Source_Code_ServicesEditionSample_WorldCoordinates_CS_091017.zip ====== | + | ====== Source Code ServicesEditionSample WorldCoordinates CS 091017.zip ====== |
- | <noinclude>{{article rating}}</noinclude> | ||
====DecimalDegrees.cs==== | ====DecimalDegrees.cs==== | ||
- | ^ Code ^ | + | |
- | | <source lang="csharp" line="1">using System; \\ using System.Collections.Generic; \\ using System.Linq; \\ using System.Text; \\ \\ namespace WorldCoordinates \\ { \\ static class DecimalDegrees \\ { \\ public enum Type { Longitude, Latitude }; \\ \\ <nowiki>//</nowiki>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; \\ } \\ \\ <nowiki>//</nowiki>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; \\ } \\ \\ <nowiki>//</nowiki>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; \\ } \\ \\ <nowiki>//</nowiki>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; \\ } \\ \\ <nowiki>//</nowiki>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; \\ } \\ } \\ \\ \\ } \\ </source> | | + | <code csharp> using System; |
+ | using System.Collections.Generic; | ||
+ | using System.Linq; | ||
+ | using System.Text; | ||
+ | |||
+ | namespace WorldCoordinates | ||
+ | { | ||
+ | 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; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | |||
+ | } | ||
+ | </code> | ||
====Program.cs==== | ====Program.cs==== | ||
- | ^ Code ^ | ||
- | | <source lang="csharp" line="1">using System; \\ using System.Collections.Generic; \\ using System.Linq; \\ using System.Windows.Forms; \\ \\ namespace WorldCoordinates \\ { \\ static class Program \\ { \\ <nowiki>//</nowiki>/ <summary> \\ <nowiki>//</nowiki>/ The main entry point for the application. \\ <nowiki>//</nowiki>/ </summary> \\ [[STAThread]] \\ static void Main() \\ { \\ Application.EnableVisualStyles(); \\ Application.SetCompatibleTextRenderingDefault(false); \\ Application.Run(new TestForm()); \\ } \\ } \\ } \\ </source> | | ||
+ | <code csharp> using System; | ||
+ | using System.Collections.Generic; | ||
+ | using System.Linq; | ||
+ | using System.Windows.Forms; | ||
+ | | ||
+ | namespace WorldCoordinates | ||
+ | { | ||
+ | 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()); | ||
+ | } | ||
+ | } | ||
+ | } | ||
+ | </code> | ||
- | ====TestForm.cs==== | ||
- | ^ Code ^ | ||
- | | <source lang="csharp" line="1">using System; \\ using System.Drawing; \\ using System.Windows.Forms; \\ using ThinkGeo.MapSuite.Core; \\ \\ namespace WorldCoordinates \\ { \\ public partial class TestForm : Form \\ { \\ private MapEngine mapEngine = new MapEngine(); \\ private Bitmap bitmap = null; \\ ShapeFileFeatureLayer worldLayer = null; \\ Proj4Projection proj4Projection = new Proj4Projection(); \\ \\ public TestForm() \\ { \\ InitializeComponent(); \\ } \\ \\ private void TestForm_Load(object sender, EventArgs e) \\ { \\ <nowiki>//</nowiki> Sets the full extent and the background color. \\ mapEngine.CurrentExtent = ExtentHelper.GetDrawingExtent(new RectangleShape(-16410000,11825813,19539000,-5980000), Map.Width, Map.Height); \\ mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); \\ \\ <nowiki>//</nowiki>Sets the internal and external projection for the projection. \\ <nowiki>//</nowiki>The projection will be used to go from Mercator to Geodetic at the mouse mouve event. \\ proj4Projection.InternalProjectionParametersString = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";<nowiki>//</nowiki>Mercator \\ proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326); <nowiki>//</nowiki>Geodetic (Longitude/Latitude) \\ \\ <nowiki>//</nowiki> Adds the static layer in the Mercator projection to the MapEngine. \\ worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\WorldMercator.shp", ShapeFileReadWriteMode.ReadOnly); \\ worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; \\ worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; \\ mapEngine.StaticLayers.Add("WorldLayer", worldLayer); \\ \\ DrawImage(); \\ } \\ \\ private void Map_MouseMove(object sender, MouseEventArgs e) \\ { \\ <nowiki>//</nowiki>Displays the X and Y in screen coordinates. \\ statusStrip1.Items[["toolStripStatusLabelScreen"]].Text = "X:" + e.X + " Y:" + e.Y; \\ \\ <nowiki>//</nowiki>Gets the PointShape in world coordinates (Mercator in meters) from screen coordinates. \\ PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height); \\ \\ <nowiki>//</nowiki>Displays world coordinates in meters from Mercator projection (native projection of the layer). \\ statusStrip1.Items[["toolStripStatusLabelMeters"]].Text = "(meters) X:" + Math.Round(pointShape.X,2) + " Y:" + Math.Round(pointShape.Y,2); \\ \\ <nowiki>//</nowiki>Converts the point to Geodetic from the Mercator projection. \\ proj4Projection.Open(); \\ Vertex geoVertex = proj4Projection.ConvertToExternalProjection(pointShape.X, pointShape.Y); \\ proj4Projection.Close(); \\ \\ <nowiki>//</nowiki>Displays world coordinates in Decimal Degrees. \\ statusStrip1.Items[["toolStripStatusLabelDecimalDegrees"]].Text = "Long:" + Math.Round(geoVertex.X,4) \\ + " Lat:" + Math.Round(geoVertex.Y,4); \\ \\ <nowiki>//</nowiki>Displays world coordiantes in Degrees Minute Decimal-seconds format. \\ statusStrip1.Items[["toolStripStatusLabelLongLat"]].Text = DecimalDegrees.d_To_DMs(geoVertex.X, 2, DecimalDegrees.Type.Longitude) + " " + \\ DecimalDegrees.d_To_DMs(geoVertex.Y, 2, DecimalDegrees.Type.Latitude); \\ \\ } \\ \\ private void DrawImage() \\ { \\ if (bitmap != null) { bitmap.Dispose(); } \\ bitmap = new Bitmap(Map.Width, Map.Height); \\ mapEngine.OpenAllLayers(); \\ mapEngine.DrawStaticLayers(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(); \\ } \\ \\ \\ } \\ } \\ </source> | | ||
+ | ====TestForm.cs==== | ||
+ | <code csharp> using System; | ||
+ | using System.Drawing; | ||
+ | using System.Windows.Forms; | ||
+ | using ThinkGeo.MapSuite.Core; | ||
+ | | ||
+ | namespace WorldCoordinates | ||
+ | { | ||
+ | public partial class TestForm : Form | ||
+ | { | ||
+ | private MapEngine mapEngine = new MapEngine(); | ||
+ | private Bitmap bitmap = null; | ||
+ | ShapeFileFeatureLayer worldLayer = null; | ||
+ | Proj4Projection proj4Projection = new Proj4Projection(); | ||
+ | | ||
+ | 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(-16410000,11825813,19539000,-5980000), Map.Width, Map.Height); | ||
+ | mapEngine.BackgroundFillBrush = new GeoSolidBrush(GeoColor.GeographicColors.ShallowOcean); | ||
+ | | ||
+ | //Sets the internal and external projection for the projection. | ||
+ | //The projection will be used to go from Mercator to Geodetic at the mouse mouve event. | ||
+ | proj4Projection.InternalProjectionParametersString = "+proj=merc +lon_0=0 +k=1 +x_0=0 +y_0=0 +ellps=WGS84 +datum=WGS84 +units=m +no_defs";//Mercator | ||
+ | proj4Projection.ExternalProjectionParametersString = Proj4Projection.GetEpsgParametersString(4326); //Geodetic (Longitude/Latitude) | ||
+ | | ||
+ | // Adds the static layer in the Mercator projection to the MapEngine. | ||
+ | worldLayer = new ShapeFileFeatureLayer(@"..\..\Data\WorldMercator.shp", ShapeFileReadWriteMode.ReadOnly); | ||
+ | worldLayer.ZoomLevelSet.ZoomLevel01.DefaultAreaStyle = AreaStyles.County1; | ||
+ | worldLayer.ZoomLevelSet.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.Level20; | ||
+ | mapEngine.StaticLayers.Add("WorldLayer", worldLayer); | ||
+ | | ||
+ | DrawImage(); | ||
+ | } | ||
+ | | ||
+ | private void Map_MouseMove(object sender, MouseEventArgs e) | ||
+ | { | ||
+ | //Displays the X and Y in screen coordinates. | ||
+ | statusStrip1.Items["toolStripStatusLabelScreen"].Text = "X:" + e.X + " Y:" + e.Y; | ||
+ | | ||
+ | //Gets the PointShape in world coordinates (Mercator in meters) from screen coordinates. | ||
+ | PointShape pointShape = ExtentHelper.ToWorldCoordinate(mapEngine.CurrentExtent, new ScreenPointF(e.X, e.Y), Map.Width, Map.Height); | ||
+ | | ||
+ | //Displays world coordinates in meters from Mercator projection (native projection of the layer). | ||
+ | statusStrip1.Items["toolStripStatusLabelMeters"].Text = "(meters) X:" + Math.Round(pointShape.X,2) + " Y:" + Math.Round(pointShape.Y,2); | ||
+ | | ||
+ | //Converts the point to Geodetic from the Mercator projection. | ||
+ | proj4Projection.Open(); | ||
+ | Vertex geoVertex = proj4Projection.ConvertToExternalProjection(pointShape.X, pointShape.Y); | ||
+ | proj4Projection.Close(); | ||
+ | | ||
+ | //Displays world coordinates in Decimal Degrees. | ||
+ | statusStrip1.Items["toolStripStatusLabelDecimalDegrees"].Text = "Long:" + Math.Round(geoVertex.X,4) | ||
+ | + " Lat:" + Math.Round(geoVertex.Y,4); | ||
+ | | ||
+ | //Displays world coordiantes in Degrees Minute Decimal-seconds format. | ||
+ | statusStrip1.Items["toolStripStatusLabelLongLat"].Text = DecimalDegrees.d_To_DMs(geoVertex.X, 2, DecimalDegrees.Type.Longitude) + " " + | ||
+ | DecimalDegrees.d_To_DMs(geoVertex.Y, 2, DecimalDegrees.Type.Latitude); | ||
+ | | ||
+ | } | ||
+ | | ||
+ | private void DrawImage() | ||
+ | { | ||
+ | if (bitmap != null) { bitmap.Dispose(); } | ||
+ | bitmap = new Bitmap(Map.Width, Map.Height); | ||
+ | mapEngine.OpenAllLayers(); | ||
+ | mapEngine.DrawStaticLayers(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(); | ||
+ | } | ||
+ | | ||
+ | | ||
+ | } | ||
+ | } | ||
+ | </code> |