GreatCircle.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. define("dojox/geo/openlayers/GreatCircle", ["dojo/_base/lang",
  2. "dojox/geo/openlayers/GeometryFeature",
  3. "dojox/geo/openlayers/Point",
  4. "dojox/geo/openlayers/LineString"], function(lang, GeometryFeature, Point, lineString){
  5. lang.getObject("geo.openlayers", true, dojox);
  6. dojox.geo.openlayers.GreatCircle = {
  7. toPointArray : function(p1, p2, increment){
  8. // summary:
  9. // Create a geodetic line as an array of OpenLayers.Point.
  10. // descritpion:
  11. // Create a geodetic line as an array of OpenLayers.Point between the point p1
  12. // and the point p2. Result is a polyline approximation for which a new point is
  13. // calculated every <em>increment</em> degrees.
  14. // p1: Point
  15. // The first point of the geodetic line. x and y fields are longitude and
  16. // latitude in decimal degrees.
  17. // p2: Point
  18. // The second point of the geodetic line. x and y fields are longitude and
  19. // latitude in decimal degrees.
  20. // increment: Float
  21. // The value at which a new point is computed.
  22. var startLon = p1.x;
  23. var endLon = p2.x;
  24. var sl = Math.min(startLon, endLon);
  25. var el = Math.max(startLon, endLon);
  26. var d2r = this.DEG2RAD;
  27. var lat1 = p1.y * d2r;
  28. var lon1 = p1.x * d2r;
  29. var lat2 = p2.y * d2r;
  30. var lon2 = p2.x * d2r;
  31. if (Math.abs(lon1 - lon2) <= this.TOLERANCE) {
  32. var l = Math.min(lon1, lon2);
  33. lon2 = l + Math.PI;
  34. }
  35. if (Math.abs(lon2 - lon1) == Math.PI) {
  36. if (lat1 + lat2 == 0.0) {
  37. lat2 += Math.PI / 180000000;
  38. }
  39. }
  40. var lon = sl * d2r;
  41. var elon = el * d2r;
  42. var incr = increment * d2r;
  43. var wp = [];
  44. var k = 0;
  45. var r2d = this.RAD2DEG;
  46. while (lon <= elon) {
  47. lat = Math.atan((Math.sin(lat1) * Math.cos(lat2) * Math.sin(lon - lon2) - Math.sin(lat2) * Math.cos(lat1)
  48. * Math.sin(lon - lon1))
  49. / (Math.cos(lat1) * Math.cos(lat2) * Math.sin(lon1 - lon2)));
  50. var p = {
  51. x : lon * r2d,
  52. y : lat * r2d
  53. };
  54. wp[k++] = p;
  55. if (lon < elon && (lon + incr) >= elon)
  56. lon = elon;
  57. else
  58. lon = lon + incr;
  59. }
  60. return wp;
  61. },
  62. toLineString : function(p1, p2, increment){
  63. // summary:
  64. // Create a geodetic line as an array of OpenLayers.Geometry.LineString.
  65. // descritpion:
  66. // Create a geodetic line as a OpenLayers.Geometry.LineString between the point p1
  67. // and the point p2. Result is a polyline approximation for which a new point is
  68. // calculated every <em>increment</em> degrees.
  69. // p1: Point
  70. // The first point of the geodetic line. x and y fields are longitude and
  71. // latitude in decimal degrees.
  72. // p2: Point
  73. // The second point of the geodetic line. x and y fields are longitude and
  74. // latitude in decimal degrees.
  75. // increment: Float
  76. // The value at which a new point is computed.
  77. var wp = this.toPointArray(p1, p2, increment);
  78. var ls = new OpenLayers.Geometry.LineString(wp);
  79. return ls;
  80. },
  81. toGeometryFeature : function(p1, p2, increment){
  82. // summary:
  83. // Create a geodetic line as an array of dojox.geo.openlayers.GeometryFeature.
  84. // description:
  85. // Create a geodetic line as a dojox.geo.openlayers.GeometryFeature between the point p1
  86. // ant the point p2. Result is a polyline approximation for which a new point is
  87. // calculated every <em>increment</em> degrees.
  88. // p1: Point
  89. // The first point of the geodetic line. x and y fields are longitude and
  90. // latitude in decimal degrees.
  91. // p2: Point
  92. // The second point of the geodetic line. x and y fields are longitude and
  93. // latitude in decimal degrees.
  94. // increment: Float
  95. // The value at which a new point is computed.
  96. // returns: GeometryFeature
  97. // The geodetic line as a GeometryFeature
  98. var ls = this.toLineString(p1, p2, increment);
  99. return new GeometryFeature(ls);
  100. },
  101. DEG2RAD : Math.PI / 180,
  102. RAD2DEG : 180 / Math.PI,
  103. TOLERANCE : 0.00001
  104. };
  105. return dojox.geo.openlayers.GreatCircle;
  106. });