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

GIS编程

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using SharpMap.Geometries;
  5. using SharpMap.CoordinateSystems;
  6. using SharpMap.CoordinateSystems.Transformations;
  7. using NUnit.Framework;
  8. namespace UnitTests
  9. {
  10. [TestFixture]
  11. public class CoordinateTransformTests
  12. {
  13. [Test]
  14. public void TestAlbersProjection()
  15. {
  16. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  17. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Clarke 1866", 6378206.4, 294.9786982138982, LinearUnit.USSurveyFoot);
  18. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Clarke 1866", DatumType.HD_Geocentric, ellipsoid, null);
  19. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Clarke 1866", AngularUnit.Degrees, datum,
  20. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  21. new AxisInfo("Lat", AxisOrientationEnum.North));
  22.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  23.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  24. parameters.Add(new ProjectionParameter("central_meridian", -96));
  25. parameters.Add(new ProjectionParameter("latitude_of_origin", 23));
  26. parameters.Add(new ProjectionParameter("standard_parallel_1", 29.5));
  27. parameters.Add(new ProjectionParameter("standard_parallel_2", 45.5));
  28. parameters.Add(new ProjectionParameter("false_easting", 0));
  29. parameters.Add(new ProjectionParameter("false_northing", 0));
  30. IProjection projection = cFac.CreateProjection("Albers Conical Equal Area", "albers", parameters);
  31. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Albers Conical Equal Area", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  32. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  33. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(-75, 35);
  34. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  35. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  36. SharpMap.Geometries.Point expected = new Point(1885472.7, 1535925);
  37. Assert.IsTrue(ToleranceLessThan(pUtm, expected, 0.05), String.Format("Albers forward transformation outside tolerance, Expected {0}, got {1}", expected.ToString(), pUtm.ToString()));
  38. Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), String.Format("Albers reverse transformation outside tolerance, Expected {0}, got {1}", pGeo.ToString(), pGeo2.ToString()));
  39. }
  40. [Test]
  41. public void TestMercator_1SP_Projection()
  42. {
  43. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  44. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Bessel 1840", 6377397.155, 299.15281, LinearUnit.Metre);
  45. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Bessel 1840", DatumType.HD_Geocentric, ellipsoid, null);
  46. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Bessel 1840", AngularUnit.Degrees, datum,
  47. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  48. new AxisInfo("Lat", AxisOrientationEnum.North));
  49.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  50.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  51. parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
  52. parameters.Add(new ProjectionParameter("central_meridian", 110));
  53. parameters.Add(new ProjectionParameter("scale_factor", 0.997));
  54. parameters.Add(new ProjectionParameter("false_easting", 3900000));
  55. parameters.Add(new ProjectionParameter("false_northing", 900000));
  56. IProjection projection = cFac.CreateProjection("Mercator_1SP", "Mercator_1SP", parameters);
  57. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Makassar / NEIEZ", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  58. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  59. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(120, -3);
  60. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  61. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  62. SharpMap.Geometries.Point expected = new Point(5009726.58, 569150.82);
  63. Assert.IsTrue(ToleranceLessThan(pUtm, expected, 0.02), String.Format("Mercator_1SP forward transformation outside tolerance, Expected {0}, got {1}", expected.ToString(), pUtm.ToString()));
  64. Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), String.Format("Mercator_1SP reverse transformation outside tolerance, Expected {0}, got {1}", pGeo.ToString(), pGeo2.ToString()));
  65. }
  66. [Test]
  67. public void TestMercator_2SP_Projection()
  68. {
  69. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  70. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Krassowski 1940", 6378245.0, 298.3, LinearUnit.Metre);
  71. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Krassowski 1940", DatumType.HD_Geocentric, ellipsoid, null);
  72. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Krassowski 1940", AngularUnit.Degrees, datum,
  73. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  74. new AxisInfo("Lat", AxisOrientationEnum.North));
  75.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  76.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  77. parameters.Add(new ProjectionParameter("latitude_of_origin", 42));
  78. parameters.Add(new ProjectionParameter("central_meridian", 51));
  79. parameters.Add(new ProjectionParameter("false_easting", 0));
  80. parameters.Add(new ProjectionParameter("false_northing", 0));
  81. IProjection projection = cFac.CreateProjection("Mercator_2SP", "Mercator_2SP", parameters);
  82. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("Pulkovo 1942 / Mercator Caspian Sea", gcs, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  83. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  84. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(53, 53);
  85. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  86. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  87. SharpMap.Geometries.Point expected = new Point(165704.29, 5171848.07);
  88. Assert.IsTrue(ToleranceLessThan(pUtm, expected, 0.02), String.Format("Mercator_2SP forward transformation outside tolerance, Expected {0}, got {1}", expected.ToString(), pUtm.ToString()));
  89. Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), String.Format("Mercator_2SP reverse transformation outside tolerance, Expected {0}, got {1}", pGeo.ToString(), pGeo2.ToString()));
  90. }
  91. [Test]
  92. public void TestTransverseMercator_Projection()
  93. {
  94. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  95. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Airy 1830", 6377563.396, 299.32496, LinearUnit.Metre);
  96. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Airy 1830", DatumType.HD_Geocentric, ellipsoid, null);
  97. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Airy 1830", AngularUnit.Degrees, datum,
  98. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  99. new AxisInfo("Lat", AxisOrientationEnum.North));
  100.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  101.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  102. parameters.Add(new ProjectionParameter("latitude_of_origin", 49));
  103. parameters.Add(new ProjectionParameter("central_meridian", -2));
  104. parameters.Add(new ProjectionParameter("scale_factor", 0.9996012717));
  105. parameters.Add(new ProjectionParameter("false_easting", 400000));
  106. parameters.Add(new ProjectionParameter("false_northing", -100000));
  107. IProjection projection = cFac.CreateProjection("Transverse Mercator", "Transverse_Mercator", parameters);
  108. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("OSGB 1936 / British National Grid", gcs, projection, LinearUnit.Metre, 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(0.5, 50.5);
  111. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  112. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  113. SharpMap.Geometries.Point expected = new Point(577274.99, 69740.50);
  114. Assert.IsTrue(ToleranceLessThan(pUtm, expected, 0.02), String.Format("TransverseMercator forward transformation outside tolerance, Expected {0}, got {1}", expected.ToString(), pUtm.ToString()));
  115. Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), String.Format("TransverseMercator reverse transformation outside tolerance, Expected {0}, got {1}", pGeo.ToString(), pGeo2.ToString()));
  116. }
  117. [Test]
  118. public void TestLambertConicConformal2SP_Projection()
  119. {
  120. CoordinateSystemFactory cFac = new SharpMap.CoordinateSystems.CoordinateSystemFactory();
  121. IEllipsoid ellipsoid = cFac.CreateFlattenedSphere("Clarke 1866", 20925832.16, 294.97470, LinearUnit.USSurveyFoot);
  122. IHorizontalDatum datum = cFac.CreateHorizontalDatum("Clarke 1866", DatumType.HD_Geocentric, ellipsoid, null);
  123. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("Clarke 1866", AngularUnit.Degrees, datum,
  124. PrimeMeridian.Greenwich, new AxisInfo("Lon", AxisOrientationEnum.East),
  125. new AxisInfo("Lat", AxisOrientationEnum.North));
  126.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  127.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  128. parameters.Add(new ProjectionParameter("latitude_of_origin", 27.833333333));
  129. parameters.Add(new ProjectionParameter("central_meridian", -99));
  130. parameters.Add(new ProjectionParameter("standard_parallel_1", 28.3833333333));
  131. parameters.Add(new ProjectionParameter("standard_parallel_2", 30.2833333333));
  132. parameters.Add(new ProjectionParameter("false_easting", 2000000));
  133. parameters.Add(new ProjectionParameter("false_northing", 0));
  134. IProjection projection = cFac.CreateProjection("Lambert Conic Conformal (2SP)", "lambert_conformal_conic_2sp", parameters);
  135. IProjectedCoordinateSystem coordsys = cFac.CreateProjectedCoordinateSystem("NAD27 / Texas South Central", gcs, projection, LinearUnit.USSurveyFoot, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  136. ICoordinateTransformation trans = new CoordinateTransformationFactory().CreateFromCoordinateSystems(gcs, coordsys);
  137. SharpMap.Geometries.Point pGeo = new SharpMap.Geometries.Point(-96, 28.5);
  138. SharpMap.Geometries.Point pUtm = trans.MathTransform.Transform(pGeo);
  139. SharpMap.Geometries.Point pGeo2 = trans.MathTransform.Inverse().Transform(pUtm);
  140. SharpMap.Geometries.Point expected = new Point(2963503.91, 254759.80);
  141. Assert.IsTrue(ToleranceLessThan(pUtm, expected, 0.02), String.Format("LambertConicConformal2SP forward transformation outside tolerance, Expected {0}, got {1}", expected.ToString(), pUtm.ToString()));
  142. Assert.IsTrue(ToleranceLessThan(pGeo, pGeo2, 0.0000001), String.Format("LambertConicConformal2SP reverse transformation outside tolerance, Expected {0}, got {1}", pGeo.ToString(), pGeo2.ToString()));
  143. }
  144. [Test]
  145. public void TestGeocentric()
  146. {
  147. CoordinateSystemFactory cFac = new CoordinateSystemFactory();
  148. IGeographicCoordinateSystem gcs = cFac.CreateGeographicCoordinateSystem("ETRF89 Geographic", AngularUnit.Degrees, HorizontalDatum.ETRF89, PrimeMeridian.Greenwich,
  149. new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  150. IGeocentricCoordinateSystem gcenCs = cFac.CreateGeocentricCoordinateSystem("ETRF89 Geocentric", HorizontalDatum.ETRF89, LinearUnit.Metre, PrimeMeridian.Greenwich);
  151. CoordinateTransformationFactory gtFac = new CoordinateTransformationFactory();
  152. ICoordinateTransformation ct = gtFac.CreateFromCoordinateSystems(gcs,gcenCs);
  153. Point pExpected = Point.FromDMS(2, 7, 46.38, 53, 48, 33.82);
  154. Point3D pExpected3D = new Point3D(pExpected.X, pExpected.Y, 73.0);
  155. Point3D p0 = new Point3D(3771793.97, 140253.34, 5124304.35);
  156. Point3D p1 = ct.MathTransform.Transform(pExpected3D) as Point3D;
  157. Point3D p2 = ct.MathTransform.Inverse().Transform(p1) as Point3D;
  158. Assert.IsTrue(ToleranceLessThan(p1, p0, 0.01));
  159. Assert.IsTrue(ToleranceLessThan(p2, pExpected, 0.00001));
  160. }
  161. [Test]
  162. public void TestDatumTransform()
  163. {
  164. CoordinateSystemFactory cFac = new CoordinateSystemFactory();
  165. //Define datums
  166. HorizontalDatum wgs72 = HorizontalDatum.WGS72;
  167. HorizontalDatum ed50 = HorizontalDatum.ED50;
  168. //Define geographic coordinate systems
  169. IGeographicCoordinateSystem gcsWGS72 = cFac.CreateGeographicCoordinateSystem("WGS72 Geographic", AngularUnit.Degrees, wgs72, PrimeMeridian.Greenwich,
  170. new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  171. IGeographicCoordinateSystem gcsWGS84 = cFac.CreateGeographicCoordinateSystem("WGS84 Geographic", AngularUnit.Degrees, HorizontalDatum.WGS84, PrimeMeridian.Greenwich,
  172. new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  173. IGeographicCoordinateSystem gcsED50 = cFac.CreateGeographicCoordinateSystem("ED50 Geographic", AngularUnit.Degrees, ed50, PrimeMeridian.Greenwich,
  174. new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  175. //Define geocentric coordinate systems
  176. IGeocentricCoordinateSystem gcenCsWGS72 = cFac.CreateGeocentricCoordinateSystem("WGS72 Geocentric", wgs72, LinearUnit.Metre, PrimeMeridian.Greenwich);
  177. IGeocentricCoordinateSystem gcenCsWGS84 = cFac.CreateGeocentricCoordinateSystem("WGS84 Geocentric", HorizontalDatum.WGS84, LinearUnit.Metre, PrimeMeridian.Greenwich);
  178. IGeocentricCoordinateSystem gcenCsED50 = cFac.CreateGeocentricCoordinateSystem("ED50 Geocentric", ed50, LinearUnit.Metre, PrimeMeridian.Greenwich);
  179. //Define projections
  180.             //System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>(5);
  181.             System.Collections.ObjectModel.Collection<ProjectionParameter> parameters = new System.Collections.ObjectModel.Collection<ProjectionParameter>();
  182. parameters.Add(new ProjectionParameter("latitude_of_origin", 0));
  183. parameters.Add(new ProjectionParameter("central_meridian", 9));
  184. parameters.Add(new ProjectionParameter("scale_factor", 0.9996));
  185. parameters.Add(new ProjectionParameter("false_easting", 500000));
  186. parameters.Add(new ProjectionParameter("false_northing", 0));
  187. IProjection projection = cFac.CreateProjection("Transverse Mercator", "Transverse_Mercator", parameters);
  188. IProjectedCoordinateSystem utmED50 = cFac.CreateProjectedCoordinateSystem("ED50 UTM Zone 32N", gcsED50, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  189. IProjectedCoordinateSystem utmWGS84 = cFac.CreateProjectedCoordinateSystem("WGS84 UTM Zone 32N", gcsWGS84, projection, LinearUnit.Metre, new AxisInfo("East", AxisOrientationEnum.East), new AxisInfo("North", AxisOrientationEnum.North));
  190. //Set TOWGS84 parameters
  191. wgs72.Wgs84Parameters = new Wgs84ConversionInfo(0, 0, 4.5, 0, 0, 0.554, 0.219);
  192. ed50.Wgs84Parameters = new Wgs84ConversionInfo(-81.0703, -89.3603, -115.7526,
  193.    -0.48488, -0.02436, -0.41321,
  194.    -0.540645); //Parameters for Denmark
  195. //Set up coordinate transformations
  196. CoordinateTransformationFactory ctFac = new CoordinateTransformationFactory();
  197. ICoordinateTransformation ctForw = ctFac.CreateFromCoordinateSystems(gcsWGS72, gcenCsWGS72); //Geographic->Geocentric (WGS72)
  198. ICoordinateTransformation ctWGS84_Gcen2Geo = ctFac.CreateFromCoordinateSystems(gcenCsWGS84, gcsWGS84);  //Geocentric->Geographic (WGS84)
  199. ICoordinateTransformation ctWGS84_Geo2UTM = ctFac.CreateFromCoordinateSystems(gcsWGS84, utmWGS84);  //UTM ->Geographic (WGS84)
  200. ICoordinateTransformation ctED50_UTM2Geo = ctFac.CreateFromCoordinateSystems(utmED50, gcsED50);  //UTM ->Geographic (ED50)
  201. ICoordinateTransformation ctED50_Geo2Gcen = ctFac.CreateFromCoordinateSystems(gcsED50, gcenCsED50); //Geographic->Geocentric (ED50)
  202. //Test datum-shift from WGS72 to WGS84
  203. //Point3D pGeoCenWGS72 = ctForw.MathTransform.Transform(pLongLatWGS72) as Point3D;
  204. Point3D pGeoCenWGS72 = new Point3D(3657660.66, 255768.55, 5201382.11);
  205. ICoordinateTransformation geocen_ed50_2_Wgs84 = ctFac.CreateFromCoordinateSystems(gcenCsWGS72, gcenCsWGS84);
  206. Point3D pGeoCenWGS84 = geocen_ed50_2_Wgs84.MathTransform.Transform(pGeoCenWGS72) as Point3D;
  207. //Point3D pGeoCenWGS84 = wgs72.Wgs84Parameters.Apply(pGeoCenWGS72);
  208. Assert.IsTrue(ToleranceLessThan(new Point3D(3657660.78, 255778.43, 5201387.75), pGeoCenWGS84, 0.01));
  209. ICoordinateTransformation utm_ed50_2_Wgs84 = ctFac.CreateFromCoordinateSystems(utmED50, utmWGS84);
  210. Point pUTMED50 = new Point(600000, 6100000);
  211. Point pUTMWGS84 = utm_ed50_2_Wgs84.MathTransform.Transform(pUTMED50);
  212. Assert.IsTrue(ToleranceLessThan(new Point(599928.6, 6099790.2), pUTMWGS84, 0.1));
  213. //Perform reverse
  214. ICoordinateTransformation utm_Wgs84_2_Ed50 = ctFac.CreateFromCoordinateSystems(utmWGS84, utmED50);
  215. pUTMED50 = utm_Wgs84_2_Ed50.MathTransform.Transform(pUTMWGS84);
  216. Assert.IsTrue(ToleranceLessThan(new Point(600000, 6100000), pUTMED50, 0.1));
  217. //Assert.IsTrue(Math.Abs((pUTMWGS84 as Point3D).Z - 36.35) < 0.5);
  218. //Point pExpected = Point.FromDMS(2, 7, 46.38, 53, 48, 33.82);
  219. //ED50_to_WGS84_Denmark: datum.Wgs84Parameters = new Wgs84ConversionInfo(-89.5, -93.8, 127.6, 0, 0, 4.5, 1.2);
  220. }
  221. private bool ToleranceLessThan(Point p1, Point p2, double tolerance)
  222. {
  223. return Math.Abs(p1.X - p2.X) < tolerance && Math.Abs(p1.Y - p2.Y) < tolerance;
  224. }
  225. private bool ToleranceLessThan(Point3D p1, Point3D p2, double tolerance)
  226. {
  227. return Math.Abs(p1.X - p2.X) < tolerance && Math.Abs(p1.Y - p2.Y) < tolerance && Math.Abs(p1.Z - p2.Z) < tolerance;
  228. }
  229. }
  230. }