Posted By

renjicode on 11/10/13


Tagged

algorithm


Versions (?)

Convert from WGS to GCJ-02 Geodetic System


 / Published in: Java
 

将wgs经纬度转换为GCJ-02坐标

  1. class EvilTransform
  2. {
  3. const double pi = 3.14159265358979324;
  4.  
  5. //
  6. // Krasovsky 1940
  7. //
  8. // a = 6378245.0, 1/f = 298.3
  9. // b = a * (1 - f)
  10. // ee = (a^2 - b^2) / a^2;
  11. const double a = 6378245.0;
  12. const double ee = 0.00669342162296594323;
  13.  
  14. //
  15. // World Geodetic System ==> Mars Geodetic System
  16. public static void transform(double wgLat, double wgLon, out double mgLat, out double mgLon)
  17. {
  18. if (outOfChina(wgLat, wgLon))
  19. {
  20. mgLat = wgLat;
  21. mgLon = wgLon;
  22. return;
  23. }
  24. double dLat = transformLat(wgLon - 105.0, wgLat - 35.0);
  25. double dLon = transformLon(wgLon - 105.0, wgLat - 35.0);
  26. double radLat = wgLat / 180.0 * pi;
  27. double magic = Math.Sin(radLat);
  28. magic = 1 - ee * magic * magic;
  29. double sqrtMagic = Math.Sqrt(magic);
  30. dLat = (dLat * 180.0) / ((a * (1 - ee)) / (magic * sqrtMagic) * pi);
  31. dLon = (dLon * 180.0) / (a / sqrtMagic * Math.Cos(radLat) * pi);
  32. mgLat = wgLat + dLat;
  33. mgLon = wgLon + dLon;
  34. }
  35.  
  36. static bool outOfChina(double lat, double lon)
  37. {
  38. if (lon < 72.004 || lon > 137.8347)
  39. return true;
  40. if (lat < 0.8293 || lat > 55.8271)
  41. return true;
  42. return false;
  43. }
  44.  
  45. static double transformLat(double x, double y)
  46. {
  47. double ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.Sqrt(Math.Abs(x));
  48. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  49. ret += (20.0 * Math.Sin(y * pi) + 40.0 * Math.Sin(y / 3.0 * pi)) * 2.0 / 3.0;
  50. ret += (160.0 * Math.Sin(y / 12.0 * pi) + 320 * Math.Sin(y * pi / 30.0)) * 2.0 / 3.0;
  51. return ret;
  52. }
  53.  
  54. static double transformLon(double x, double y)
  55. {
  56. double ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.Sqrt(Math.Abs(x));
  57. ret += (20.0 * Math.Sin(6.0 * x * pi) + 20.0 * Math.Sin(2.0 * x * pi)) * 2.0 / 3.0;
  58. ret += (20.0 * Math.Sin(x * pi) + 40.0 * Math.Sin(x / 3.0 * pi)) * 2.0 / 3.0;
  59. ret += (150.0 * Math.Sin(x / 12.0 * pi) + 300.0 * Math.Sin(x / 30.0 * pi)) * 2.0 / 3.0;
  60. return ret;
  61. }
  62. }

Report this snippet  

You need to login to post a comment.