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

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using SharpMap.Geometries;
  12. using SharpMap.CoordinateSystems;
  13. using SharpMap.CoordinateSystems.Transformations;
  14. public partial class TransformTests : System.Web.UI.Page
  15. {
  16. protected void Page_Load(object sender, EventArgs e)
  17. {
  18. TestMercator_1SP();
  19. TestMercator_2SP();
  20. TestTransverseMercator();
  21. TestLambertConicConformal_2SP();
  22. TestAlbers();
  23. TestGeocentric();
  24. }
  25. private void TestMercator_1SP()
  26. {
  27. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  28. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Bessel 1840", 6377397.155, 299.15281, LinearUnit.Metre);
  29. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Bessel 1840", DatumType.HD_Geocentric, ellipsoid, null);
  30. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Bessel 1840", AngularUnit.Degrees, datum,
  31. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  32. new AxisInfo("Lat", AxisOrientationEnum.North));
  33.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  34. parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
  35. parameters.Add(new ProjectionParameter("central_meridian", 110));
  36. parameters.Add(new ProjectionParameter("scale_factor", 0.997));
  37. parameters.Add(new ProjectionParameter("false_easting", 3900000));
  38. parameters.Add(new ProjectionParameter("false_northing", 900000));
  39. IProjection projection = cFac.CreateProjection("Mercator_1SP", "Mercator_1SP", parameters);
  40. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Makassar / NEIEZ", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  41. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  42. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(120, -3);
  43. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  44. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  45. result.Text += PrintResultTable(gcs, coordsys, pGeo, pUtm, new Point(5009726.58, 569150.82), pGeo2, "Mercator_1SP test");
  46. }
  47. private void TestMercator_2SP()
  48. {
  49. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  50. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Krassowski 1940", 6378245.0, 298.3, LinearUnit.Metre);
  51. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Krassowski 1940", DatumType.HD_Geocentric, ellipsoid, null);
  52. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Krassowski 1940", AngularUnit.Degrees, datum, 
  53. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  54. new AxisInfo("Lat", AxisOrientationEnum.North));
  55.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  56. parameters.Add(new ProjectionParameter("latitude_of_origin", 42));
  57. parameters.Add(new ProjectionParameter("central_meridian", 51));
  58. parameters.Add(new ProjectionParameter("false_easting", 0));
  59. parameters.Add(new ProjectionParameter("false_northing", 0));
  60. IProjection projection = cFac.CreateProjection("Mercator_2SP", "Mercator_2SP", parameters);
  61. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Pulkovo 1942 / Mercator Caspian Sea", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  62. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  63. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(53,53);
  64. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  65. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  66. result.Text += PrintResultTable(gcs, coordsys, pGeo, pUtm, new Point(165704.29, 5171848.07), pGeo2, "Mercator_2SP test");
  67. }
  68. private void TestTransverseMercator()
  69. {
  70. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  71. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Airy 1830", 6377563.396, 299.32496, LinearUnit.Metre);
  72. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Airy 1830", DatumType.HD_Geocentric, ellipsoid, null);
  73. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Airy 1830", AngularUnit.Degrees, datum,
  74. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  75. new AxisInfo("Lat", AxisOrientationEnum.North));
  76.         //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  77.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  78. parameters.Add(new ProjectionParameter("latitude_of_origin", 49));
  79. parameters.Add(new ProjectionParameter("central_meridian", -2));
  80. parameters.Add(new ProjectionParameter("scale_factor", 0.9996012717));
  81. parameters.Add(new ProjectionParameter("false_easting", 400000));
  82. parameters.Add(new ProjectionParameter("false_northing", -100000));
  83. IProjection projection = cFac.CreateProjection("Transverse Mercator", "Transverse_Mercator", parameters);
  84. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("OSGB 1936 / British National Grid", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  85. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  86. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(0.5, 50.5);
  87. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  88. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  89. result.Text += PrintResultTable(gcs, coordsys, pGeo, pUtm, new Point(577274.99, 69740.50), pGeo2, "Transverse Mercator test");
  90. }
  91. private void TestLambertConicConformal_2SP()
  92. {
  93. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  94. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Clarke 1866", 20925832.16, 294.97470, LinearUnit.USSurveyFoot);
  95. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Clarke 1866", DatumType.HD_Geocentric, ellipsoid, null);
  96. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Clarke 1866", AngularUnit.Degrees, datum,
  97. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  98. new AxisInfo("Lat", AxisOrientationEnum.North));
  99.         //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  100.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  101. parameters.Add(new ProjectionParameter("latitude_of_origin", 27.833333333));
  102. parameters.Add(new ProjectionParameter("central_meridian", -99));
  103. parameters.Add(new ProjectionParameter("standard_parallel_1", 28.3833333333));
  104. parameters.Add(new ProjectionParameter("standard_parallel_2", 30.2833333333));
  105. parameters.Add(new ProjectionParameter("false_easting", 2000000));
  106. parameters.Add(new ProjectionParameter("false_northing", 0));
  107. IProjection projection = cFac.CreateProjection("Lambert Conic Conformal (2SP)", "lambert_conformal_conic_2sp", parameters);
  108. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("NAD27 / Texas South Central", gcs, projection, LinearUnit.USSurveyFoot, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  109. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  110. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(-96, 28.5);
  111. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  112. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  113. result.Text += PrintResultTable(gcs, coordsys, pGeo, pUtm, new Point(2963503.91, 254759.80), pGeo2, "Lambert Conic Conformal 2SP test");
  114. }
  115. private void TestAlbers()
  116. {
  117. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  118. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Clarke 1866", 6378206.4, 294.9786982138982,LinearUnit.USSurveyFoot);
  119. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Clarke 1866", DatumType.HD_Geocentric, ellipsoid, null);
  120. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Clarke 1866", AngularUnit.Degrees, datum,
  121. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  122. new AxisInfo("Lat", AxisOrientationEnum.North));
  123.         System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  124. parameters.Add(new ProjectionParameter("central_meridian", -96));
  125. parameters.Add(new ProjectionParameter("latitude_of_origin", 23));
  126. parameters.Add(new ProjectionParameter("standard_parallel_1", 29.5));
  127. parameters.Add(new ProjectionParameter("standard_parallel_2", 45.5));
  128. parameters.Add(new ProjectionParameter("false_easting", 0));
  129. parameters.Add(new ProjectionParameter("false_northing", 0));
  130. IProjection projection = cFac.CreateProjection("Albers Conical Equal Area", "albers", parameters);
  131. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Albers Conical Equal Area", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  132. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  133. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(-75,35);
  134. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  135. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  136. result.Text += PrintResultTable(gcs, coordsys, pGeo, pUtm, new Point(1885472.7,1535925), pGeo2, "Albers Conical Equal Area test");
  137. }
  138. private void TestGeocentric()
  139. {
  140. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  141. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("WGS84", AngularUnit.Degrees, HorizontalDatum.WGS84,
  142. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East), new AxisInfo("Lat", AxisOrientationEnum.North));
  143. IGeocentricCoordinateSystem geoccs = cFac.CreateGeocentricCoordinateSystem("WGS84 geocentric", gcs.HorizontalDatum, LinearUnit.Metre, PrimeMeridian.Greenwich);
  144. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, geoccs);
  145. SharpMap.Geometries.Point3D pGeo = new SharpMap.Geometries.Point3D(2.12955, 53.80939444, 73);
  146. SharpMap.Geometries.Point3D pGc = trans.MathTransform.Transform(pGeo) as SharpMap.Geometries.Point3D;
  147. SharpMap.Geometries.Point3D pGeo2 = trans.MathTransform.Inverse().Transform(pGc) as SharpMap.Geometries.Point3D;
  148. result.Text += PrintResultTable(gcs, geoccs, pGeo, pGc, new Point3D(3771793.97, 140253.34, 5124304.35), pGeo2, "Geocentric test");
  149. return;
  150. }
  151. private string PrintResultTable(ICoordinateSystem fromCoordSys, ICoordinateSystem toCoordSys, Point fromPnt, Point toPnt, Point refPnt, Point backPnt, string header)
  152. {
  153. string table = "<table style="border: 1px solid #000; margin: 10px;">";
  154. table += "<tr><td colspan="2"><h3>" + header + "</h3></td></tr>";
  155. table += "<tr><td width="130px" valign="top">Input coordsys:</td><td>" + fromCoordSys.WKT + "</td></tr>";
  156. table += "<tr><td valign="top">Output coordsys:</td><td>" + toCoordSys.WKT + "</td></tr>";
  157. table += "<tr><td>Input coordinate:</td><td>" + fromPnt.ToString() + "</td></tr>";
  158. table += "<tr><td>Ouput coordinate:</td><td>" + toPnt.ToString() + "</td></tr>";
  159. table += "<tr><td>Expected coordinate:</td><td>" + refPnt.ToString() + "</td></tr>";
  160. table += "<tr><td>Difference:</td><td>" + (refPnt-toPnt).ToString() + "</td></tr>";
  161. table += "<tr><td>Reverse transform:</td><td>" + backPnt.ToString() + "</td></tr>";
  162. table += "<tr><td>Difference:</td><td>" + (backPnt - fromPnt).ToString() + "</td></tr>";
  163. table += "</table>";
  164. return table;
  165. }
  166. }