LatLong_Conversion.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. using UnityEngine;
  2. using System.Collections;
  3. using System;
  4. namespace WorldComposer
  5. {
  6. [Serializable]
  7. public class LatLong_Conversion : MonoBehaviour
  8. {
  9. public latlong_class latlong_center = new latlong_class();
  10. public latlong_class[] latlong;
  11. public Vector2 offset = new Vector2(0, -27);
  12. const double minLatitude = -85.05112878;
  13. const double maxLatitude = 85.05112878;
  14. const double minLongitude = -180;
  15. const double maxLongitude = 180;
  16. void Start()
  17. {
  18. int counter = 0;
  19. latlong[2].latitude = 49.34544372558594;
  20. latlong[2].longitude = -119.579584441234;
  21. for (int i = 0; i < latlong.Length; i++)
  22. {
  23. latlong_class p = latlong[i];
  24. Vector2 pos = GetPosition(p.latitude, p.longitude);
  25. GameObject go = GameObject.CreatePrimitive(PrimitiveType.Cube);
  26. Transform newT = go.transform;
  27. newT.position = new Vector3(pos.x, 0, pos.y);
  28. double latitude, longitude;
  29. GetLatLong(newT.position, out latitude, out longitude);
  30. Debug.Log(latitude + " : " + longitude);
  31. go.name = "Test point " + counter;
  32. counter++;
  33. }
  34. }
  35. void GetLatLong(Vector3 pos, out double latitude, out double longitude)
  36. {
  37. map_pixel_class map_pixel = new map_pixel_class();
  38. map_pixel_class map_pixel_center = latlong_to_pixel2(latlong_center, 19);
  39. double map_resolution = calc_latlong_area_resolution(latlong_center, 19);
  40. map_pixel.x = ((pos.x - offset.x) / map_resolution) + map_pixel_center.x;
  41. map_pixel.y = (-(pos.z - offset.y) / map_resolution) + map_pixel_center.y;
  42. latlong_class returnVal = pixel_to_latlong2(map_pixel, 19);
  43. latitude = returnVal.latitude;
  44. longitude = returnVal.longitude;
  45. }
  46. Vector2 GetPosition(double lat, double lon)
  47. {
  48. var latlong = new latlong_class(lat, lon);
  49. Vector2 returnVal;
  50. map_pixel_class map_pixel = latlong_to_pixel2(latlong, 19);
  51. map_pixel_class map_pixel_center = latlong_to_pixel2(latlong_center, 19);
  52. double map_resolution = calc_latlong_area_resolution(latlong_center, 19);
  53. returnVal.x = (float)((map_pixel.x - map_pixel_center.x) * map_resolution);
  54. returnVal.y = (float)((-map_pixel.y + map_pixel_center.y) * map_resolution);
  55. returnVal += offset;
  56. return returnVal;
  57. }
  58. latlong_class clip_latlong(latlong_class latlong)
  59. {
  60. if (latlong.latitude > maxLatitude) { latlong.latitude -= (maxLatitude * 2); }
  61. else if (latlong.latitude < minLatitude) { latlong.latitude += (maxLatitude * 2); }
  62. if (latlong.longitude > 180) { latlong.longitude -= 360; }
  63. else if (latlong.longitude < -180) { latlong.longitude += 360; }
  64. return latlong;
  65. }
  66. map_pixel_class clip_pixel(map_pixel_class map_pixel, double zoom)
  67. {
  68. double mapSize = 256 * Mathf.Pow(2, (float)zoom);
  69. if (map_pixel.x > mapSize - 1) { map_pixel.x -= mapSize - 1; }
  70. else if (map_pixel.x < 0) { map_pixel.x = mapSize - 1 - map_pixel.x; }
  71. if (map_pixel.y > mapSize - 1) { map_pixel.y -= mapSize - 1; }
  72. else if (map_pixel.y < 0) { map_pixel.y = mapSize - 1 - map_pixel.y; }
  73. return map_pixel;
  74. }
  75. public map_pixel_class latlong_to_pixel2(latlong_class latlong, double zoom)
  76. {
  77. latlong = clip_latlong(latlong);
  78. double pi = 3.14159265358979323846264338327950288419716939937510;
  79. double x = (latlong.longitude + 180.0) / 360.0;
  80. double sinLatitude = Mathf.Sin((float)latlong.latitude * (float)pi / 180.0f);
  81. double y = 0.5 - Mathf.Log((float)((1.0f + sinLatitude) / (1.0f - sinLatitude))) / (4.0f * pi);
  82. x *= 256.0 * Mathf.Pow(2.0f, (float)zoom);
  83. y *= 256.0 * Mathf.Pow(2.0f, (float)zoom);
  84. map_pixel_class map_pixel = new map_pixel_class();
  85. map_pixel.x = x;
  86. map_pixel.y = y;
  87. return map_pixel;
  88. }
  89. public latlong_class pixel_to_latlong2(map_pixel_class map_pixel, double zoom)
  90. {
  91. map_pixel = clip_pixel(map_pixel, zoom);
  92. double pi = 3.14159265358979323846264338327950288419716939937510;
  93. double mapSize = 256.0f * Mathf.Pow(2.0f, (float)zoom);
  94. double x = (map_pixel.x / mapSize) - 0.5;
  95. double y = 0.5 - (map_pixel.y / mapSize);
  96. latlong_class latlong = new latlong_class();
  97. latlong.latitude = 90.0 - 360.0 * Mathf.Atan(Mathf.Exp((float)(-y * 2.0 * pi))) / pi;
  98. latlong.longitude = 360.0 * x;
  99. return latlong;
  100. }
  101. public double calc_latlong_area_resolution(latlong_class latlong, double zoom)
  102. {
  103. double pi = 3.14159265358979323846264338327950288419716939937510;
  104. double map_resolution = 156543.04 * Mathf.Cos((float)(latlong.latitude * (pi / 180))) / (Mathf.Pow(2, (float)zoom));
  105. return map_resolution;
  106. }
  107. }
  108. }