Transformation.aspx.cs
上传用户:sex100000
上传日期:2013-11-09
资源大小:1377k
文件大小:11k
源码类别:

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Drawing;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using SharpMap.CoordinateSystems;
  13. using SharpMap.CoordinateSystems.Transformations; 
  14. public partial class Transformation : System.Web.UI.Page
  15. {
  16. private SharpMap.Map myMap;
  17. ICoordinateSystem datacoordsys;
  18. protected void Page_Load(object sender, EventArgs e)
  19. {
  20. //Set up the map. We use the method in the App_Code folder for initializing the map
  21. myMap = InitializeMap(new System.Drawing.Size((int)imgMap.Width.Value, (int)imgMap.Height.Value));
  22. if (Page.IsPostBack)
  23. {
  24. //Page is post back. Restore center and zoom-values from viewstate
  25. myMap.Center = (SharpMap.Geometries.Point)ViewState["mapCenter"];
  26. myMap.Zoom = (double)ViewState["mapZoom"];
  27. }
  28. else
  29. {
  30. //This is the initial view of the map. Zoom to the extents of the map:
  31. myMap.Zoom = 80;
  32. myMap.Center = new SharpMap.Geometries.Point(-95, 37);
  33. //Create the map
  34. GenerateMap();
  35. }
  36. }
  37. protected void imgMap_Click(object sender, ImageClickEventArgs e)
  38. {
  39. //Set center of the map to where the client clicked
  40. myMap.Center = myMap.ImageToWorld(new System.Drawing.Point(e.X, e.Y));
  41. //Set zoom value if any of the zoom tools were selected
  42. if (rblMapTools.SelectedValue == "0") //Zoom in
  43. myMap.Zoom = myMap.Zoom * 0.5;
  44. else if (rblMapTools.SelectedValue == "1") //Zoom out
  45. myMap.Zoom = myMap.Zoom * 2;
  46. //Create the map
  47. GenerateMap();
  48. }
  49. /// <summary>
  50. /// Creates the map, inserts it into the cache and sets the ImageButton Url
  51. /// </summary>
  52. private void GenerateMap()
  53. {
  54. //Save the current mapcenter and zoom in the viewstate
  55. ViewState.Add("mapCenter", myMap.Center);
  56. ViewState.Add("mapZoom", myMap.Zoom);
  57. ViewState.Add("currentProj", ddlProjection.SelectedValue);
  58. //Render the map
  59. System.Drawing.Image img = myMap.GetMap();
  60. string imgID = SharpMap.Web.Caching.InsertIntoCache(1, img);
  61. imgMap.ImageUrl = "getmap.aspx?ID=" + HttpUtility.UrlEncode(imgID);
  62. litEnvelope.Text = myMap.Envelope.Left.ToString("#.##") + "," + myMap.Envelope.Bottom.ToString("#.##") + " -> " +
  63. myMap.Envelope.Right.ToString("#.##") + "," + myMap.Envelope.Top.ToString("#.##") + " (Projected coordinate system)";
  64. }
  65. protected void ddlProjection_SelectedIndexChanged(object sender, EventArgs e)
  66. {
  67. //Transform current view to new coordinate system and zoom to the transformed box
  68. string PreviousProj = ViewState["currentProj"].ToString();
  69. string SelectedProj = ddlProjection.SelectedValue;
  70. //Points defining the current view 
  71. SharpMap.Geometries.Point left = new SharpMap.Geometries.Point(myMap.Envelope.Left, myMap.Center.Y);
  72. SharpMap.Geometries.Point right = new SharpMap.Geometries.Point(myMap.Envelope.Right, myMap.Center.Y);
  73. SharpMap.Geometries.Point center = myMap.Center;
  74. if (PreviousProj != "Pseudo")
  75. {
  76. //Transform current view back to geographic coordinates
  77. ICoordinateTransformation trans = GetTransform(PreviousProj);
  78. left = GeometryTransform.TransformPoint(new SharpMap.Geometries.Point(myMap.Envelope.Left, myMap.Center.Y), trans.MathTransform.Inverse());
  79. right = GeometryTransform.TransformPoint(new SharpMap.Geometries.Point(myMap.Envelope.Right, myMap.Center.Y), trans.MathTransform.Inverse());
  80. center = GeometryTransform.TransformPoint(myMap.Center, trans.MathTransform.Inverse());
  81. }
  82. //If both PreviousSRID and SelectedSRID are projected coordsys, first transform to geographic
  83. if (SelectedProj == "Pseudo")
  84. {
  85. myMap.Center = center;
  86. myMap.Zoom = Math.Abs(right.X - left.X);
  87. }
  88. else //Project coordinates to new projection
  89. {
  90. //Transform back to geographic and over to new projection
  91. ICoordinateTransformation trans = GetTransform(SelectedProj);
  92. left = GeometryTransform.TransformPoint(left, trans.MathTransform);
  93. right = GeometryTransform.TransformPoint(right, trans.MathTransform);
  94. center = GeometryTransform.TransformPoint(center, trans.MathTransform);
  95. myMap.Center = center;
  96. myMap.Zoom = Math.Abs(right.X - left.X);
  97. SharpMap.Geometries.BoundingBox envelopeGcs =GeometryTransform.TransformBox(myMap.Envelope, trans.MathTransform.Inverse());
  98. litEnvelopeLatLong.Text = envelopeGcs.ToString();
  99. }
  100. GenerateMap();
  101. }
  102. public SharpMap.Map InitializeMap(System.Drawing.Size size)
  103. {
  104. HttpContext.Current.Trace.Write("Initializing map...");
  105. //Initialize a new map of size 'imagesize'
  106. SharpMap.Map map = new SharpMap.Map(size);
  107. //Set up the countries layer
  108. SharpMap.Layers.VectorLayer layCountries = new SharpMap.Layers.VectorLayer("Countries");
  109. //Set the datasource to a shapefile in the App_data folder
  110. SharpMap.Data.Providers.ShapeFile datasource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~App_dataUSAstates.shp"), true);
  111. layCountries.DataSource = datasource;
  112. datacoordsys = datasource.CoordinateSystem;
  113. //Set fill-style to green
  114. layCountries.Style.Fill = new SolidBrush(Color.Green);
  115. //Set the polygons to have a black outline
  116. layCountries.Style.Outline = System.Drawing.Pens.Black;
  117. layCountries.Style.EnableOutline = true;
  118. layCountries.CoordinateTransformation = GetTransform(ddlProjection.SelectedValue);
  119. if (layCountries.CoordinateTransformation != null)
  120. {
  121. litInputCoordsys.Text = layCountries.CoordinateTransformation.TargetCS.WKT;
  122. litCoordsys.Text = layCountries.CoordinateTransformation.SourceCS.WKT;
  123. litTransform.Text = layCountries.CoordinateTransformation.MathTransform.WKT;
  124. }
  125. else
  126. {
  127. litInputCoordsys.Text = datasource.CoordinateSystem.WKT;
  128. litCoordsys.Text = "None";
  129. litTransform.Text = "None";
  130. }
  131. SharpMap.Layers.VectorLayer layGrid = new SharpMap.Layers.VectorLayer("Grid");
  132. //Set the datasource to a shapefile in the App_data folder
  133. layGrid.DataSource = new SharpMap.Data.Providers.ShapeFile(HttpContext.Current.Server.MapPath(@"~App_dataUSAlatlong.shp"), true);
  134. layGrid.CoordinateTransformation = layCountries.CoordinateTransformation;
  135. layGrid.Style.Line = new Pen(Color.FromArgb(127, 255, 0, 0), 1);
  136. //Add the layers to the map object.
  137. map.Layers.Add(layCountries);
  138. map.Layers.Add(layGrid);
  139. map.BackColor = Color.LightBlue;
  140. HttpContext.Current.Trace.Write("Map initialized");
  141. return map;
  142. }
  143. public ICoordinateTransformation GetTransform(string name)
  144. {
  145. switch (name)
  146. {
  147. case "Mercator": return Transform2Mercator(datacoordsys);
  148. case "Albers": return Transform2Albers(datacoordsys);
  149. case "Lambert": return Transform2Lambert(datacoordsys);
  150. default:
  151. return null;
  152. }
  153. }
  154. public static ICoordinateTransformation Transform2Albers(ICoordinateSystem source)
  155. {
  156. if (source == null)
  157. throw new ArgumentException("Source coordinate system is null");
  158. if (!(source is IGeographicCoordinateSystem))
  159. throw new ArgumentException("Source coordinate system must be geographic");
  160. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  161.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  162. parameters.Add(new ProjectionParameter("central_meridian", -95));
  163. parameters.Add(new ProjectionParameter("latitude_of_origin", 50));
  164. parameters.Add(new ProjectionParameter("standard_parallel_1", 29.5));
  165. parameters.Add(new ProjectionParameter("standard_parallel_2", 45.5));
  166. parameters.Add(new ProjectionParameter("false_easting", 0));
  167. parameters.Add(new ProjectionParameter("false_northing", 0));
  168. IProjection projection = cFac.CreateProjection("Albers_Conic_Equal_Area", "albers", parameters);
  169. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Albers_Conic_Equal_Area", source as IGeographicCoordinateSystem, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  170. return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
  171. }
  172. public static ICoordinateTransformation Transform2Mercator(ICoordinateSystem source)
  173. {
  174. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  175.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  176. parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
  177. parameters.Add(new ProjectionParameter("central_meridian", 0));
  178. parameters.Add(new ProjectionParameter("false_easting", 0));
  179. parameters.Add(new ProjectionParameter("false_northing", 0));
  180. IProjection projection = cFac.CreateProjection("Mercator", "Mercator_2SP", parameters);
  181. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Mercator", source as IGeographicCoordinateSystem, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  182. return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
  183. }
  184. public static ICoordinateTransformation Transform2Lambert(ICoordinateSystem source)
  185. {
  186. if (source == null)
  187. throw new ArgumentException("Source coordinate system is null");
  188. if (!(source is IGeographicCoordinateSystem))
  189. throw new ArgumentException("Source coordinate system must be geographic");
  190. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  191.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  192. parameters.Add(new ProjectionParameter("latitude_of_origin", 50));
  193. parameters.Add(new ProjectionParameter("central_meridian", -95));
  194. parameters.Add(new ProjectionParameter("standard_parallel_1", 33));
  195. parameters.Add(new ProjectionParameter("standard_parallel_2", 45));
  196. parameters.Add(new ProjectionParameter("false_easting", 0));
  197. parameters.Add(new ProjectionParameter("false_northing", 0));
  198. IProjection projection = cFac.CreateProjection("Lambert Conformal Conic 2SP", "lambert_conformal_conic_2sp", parameters);
  199. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Lambert Conformal Conic 2SP", source as IGeographicCoordinateSystem, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  200. return new CoordinateTransformationFactory().CreateFromCoordinateSystems(source, coordsys);
  201. }
  202. }