Advertisement
Guest User

Untitled

a guest
Nov 27th, 2015
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. public static class GeoConverter
  2. {
  3. private static ICoordinateSystem _epsg3785;
  4.  
  5. public static ICoordinateSystem epsg3785
  6. {
  7. get
  8. {
  9. if (_epsg3785 == null)
  10. {
  11. var cf = new CoordinateSystemFactory();
  12.  
  13. _epsg3785 = cf.CreateFromWkt("PROJCS[\"WGS84 / Google Mercator\", " +
  14. " GEOGCS[\"WGS 84\", " +
  15. " DATUM[\"World Geodetic System 1984\", " +
  16. " SPHEROID[\"WGS 84\", 6378137.0, 298.257223563, AUTHORITY[\"EPSG\",\"7030\"]], " +
  17. " AUTHORITY[\"EPSG\",\"6326\"]], " +
  18. " PRIMEM[\"Greenwich\", 0.0, AUTHORITY[\"EPSG\",\"8901\"]], " +
  19. " UNIT[\"degree\", 0.017453292519943295], " +
  20. " AXIS[\"Longitude\", EAST], " +
  21. " AXIS[\"Latitude\", NORTH], " +
  22. " AUTHORITY[\"EPSG\",\"4326\"]], " +
  23. " PROJECTION[\"Mercator_1SP\"], " +
  24. " PARAMETER[\"semi_minor\", 6378137.0], " +
  25. " PARAMETER[\"latitude_of_origin\", 0.0], " +
  26. " PARAMETER[\"central_meridian\", 0.0], " +
  27. " PARAMETER[\"scale_factor\", 1.0], " +
  28. " PARAMETER[\"false_easting\", 0.0], " +
  29. " PARAMETER[\"false_northing\", 0.0], " +
  30. " UNIT[\"m\", 1.0], " +
  31. " AXIS[\"x\", EAST], " +
  32. " AXIS[\"y\", NORTH], " +
  33. " AUTHORITY[\"EPSG\",\"900913\"]]");
  34.  
  35. }
  36.  
  37. return _epsg3785;
  38. }
  39. }
  40.  
  41. public static double[] formWGS84toEPSG3785(double[] coordinates)
  42. {
  43. var converter = new CoordinateTransformationFactory().CreateFromCoordinateSystems(GeographicCoordinateSystem.WGS84, epsg3785);
  44. return converter.MathTransform.Transform(coordinates);
  45. }
  46.  
  47. public static double[] formEPSG3785toWGS84(double[] coordinates)
  48. {
  49. var converter = new CoordinateTransformationFactory().CreateFromCoordinateSystems(epsg3785, GeographicCoordinateSystem.WGS84);
  50. return converter.MathTransform.Transform(coordinates);
  51. }
  52.  
  53. public static string ConvertWKT(string wkt, string convertType)
  54. {
  55. var reExp = new Regex(@"([0-9]+\.[0-9]+) ([0-9]+\.[0-9]+)");
  56. var matches = reExp.Matches(wkt);
  57.  
  58. var points = new Dictionary<string, double[]>();
  59.  
  60. foreach (var match in matches)
  61. {
  62. var coord = new double[0];
  63. var coord1 = double.Parse((match as Match).Groups[1].ToString(), CultureInfo.InvariantCulture);
  64. var coord2 = double.Parse((match as Match).Groups[2].ToString(), CultureInfo.InvariantCulture);
  65. if (convertType == "formEPSG3785toWGS84")
  66. {
  67. coord = formEPSG3785toWGS84(new double[] { coord1, coord2 });
  68. }
  69.  
  70. if (convertType == "formWGS84toEPSG3785")
  71. {
  72. coord = formWGS84toEPSG3785(new double[] { coord1, coord2 });
  73. }
  74.  
  75. var oldCoords = (match as Match).Groups[0].ToString();
  76. if (!points.ContainsKey(oldCoords))
  77. {
  78. points.Add(oldCoords, coord);
  79. }
  80. }
  81.  
  82. foreach (var doublese in points)
  83. {
  84. wkt = wkt.Replace(doublese.Key, doublese.Value[0].ToString(CultureInfo.InvariantCulture) + " " + doublese.Value[0].ToString(CultureInfo.InvariantCulture));
  85. }
  86.  
  87. return wkt;
  88. }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement