krovak.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import adjust_lon from '../common/adjust_lon';
  2. export function init() {
  3. this.a = 6377397.155;
  4. this.es = 0.006674372230614;
  5. this.e = Math.sqrt(this.es);
  6. if (!this.lat0) {
  7. this.lat0 = 0.863937979737193;
  8. }
  9. if (!this.long0) {
  10. this.long0 = 0.7417649320975901 - 0.308341501185665;
  11. }
  12. /* if scale not set default to 0.9999 */
  13. if (!this.k0) {
  14. this.k0 = 0.9999;
  15. }
  16. this.s45 = 0.785398163397448; /* 45 */
  17. this.s90 = 2 * this.s45;
  18. this.fi0 = this.lat0;
  19. this.e2 = this.es;
  20. this.e = Math.sqrt(this.e2);
  21. this.alfa = Math.sqrt(1 + (this.e2 * Math.pow(Math.cos(this.fi0), 4)) / (1 - this.e2));
  22. this.uq = 1.04216856380474;
  23. this.u0 = Math.asin(Math.sin(this.fi0) / this.alfa);
  24. this.g = Math.pow((1 + this.e * Math.sin(this.fi0)) / (1 - this.e * Math.sin(this.fi0)), this.alfa * this.e / 2);
  25. this.k = Math.tan(this.u0 / 2 + this.s45) / Math.pow(Math.tan(this.fi0 / 2 + this.s45), this.alfa) * this.g;
  26. this.k1 = this.k0;
  27. this.n0 = this.a * Math.sqrt(1 - this.e2) / (1 - this.e2 * Math.pow(Math.sin(this.fi0), 2));
  28. this.s0 = 1.37008346281555;
  29. this.n = Math.sin(this.s0);
  30. this.ro0 = this.k1 * this.n0 / Math.tan(this.s0);
  31. this.ad = this.s90 - this.uq;
  32. }
  33. /* ellipsoid */
  34. /* calculate xy from lat/lon */
  35. /* Constants, identical to inverse transform function */
  36. export function forward(p) {
  37. var gfi, u, deltav, s, d, eps, ro;
  38. var lon = p.x;
  39. var lat = p.y;
  40. var delta_lon = adjust_lon(lon - this.long0);
  41. /* Transformation */
  42. gfi = Math.pow(((1 + this.e * Math.sin(lat)) / (1 - this.e * Math.sin(lat))), (this.alfa * this.e / 2));
  43. u = 2 * (Math.atan(this.k * Math.pow(Math.tan(lat / 2 + this.s45), this.alfa) / gfi) - this.s45);
  44. deltav = -delta_lon * this.alfa;
  45. s = Math.asin(Math.cos(this.ad) * Math.sin(u) + Math.sin(this.ad) * Math.cos(u) * Math.cos(deltav));
  46. d = Math.asin(Math.cos(u) * Math.sin(deltav) / Math.cos(s));
  47. eps = this.n * d;
  48. ro = this.ro0 * Math.pow(Math.tan(this.s0 / 2 + this.s45), this.n) / Math.pow(Math.tan(s / 2 + this.s45), this.n);
  49. p.y = ro * Math.cos(eps) / 1;
  50. p.x = ro * Math.sin(eps) / 1;
  51. if (!this.czech) {
  52. p.y *= -1;
  53. p.x *= -1;
  54. }
  55. return (p);
  56. }
  57. /* calculate lat/lon from xy */
  58. export function inverse(p) {
  59. var u, deltav, s, d, eps, ro, fi1;
  60. var ok;
  61. /* Transformation */
  62. /* revert y, x*/
  63. var tmp = p.x;
  64. p.x = p.y;
  65. p.y = tmp;
  66. if (!this.czech) {
  67. p.y *= -1;
  68. p.x *= -1;
  69. }
  70. ro = Math.sqrt(p.x * p.x + p.y * p.y);
  71. eps = Math.atan2(p.y, p.x);
  72. d = eps / Math.sin(this.s0);
  73. s = 2 * (Math.atan(Math.pow(this.ro0 / ro, 1 / this.n) * Math.tan(this.s0 / 2 + this.s45)) - this.s45);
  74. u = Math.asin(Math.cos(this.ad) * Math.sin(s) - Math.sin(this.ad) * Math.cos(s) * Math.cos(d));
  75. deltav = Math.asin(Math.cos(s) * Math.sin(d) / Math.cos(u));
  76. p.x = this.long0 - deltav / this.alfa;
  77. fi1 = u;
  78. ok = 0;
  79. var iter = 0;
  80. do {
  81. p.y = 2 * (Math.atan(Math.pow(this.k, - 1 / this.alfa) * Math.pow(Math.tan(u / 2 + this.s45), 1 / this.alfa) * Math.pow((1 + this.e * Math.sin(fi1)) / (1 - this.e * Math.sin(fi1)), this.e / 2)) - this.s45);
  82. if (Math.abs(fi1 - p.y) < 0.0000000001) {
  83. ok = 1;
  84. }
  85. fi1 = p.y;
  86. iter += 1;
  87. } while (ok === 0 && iter < 15);
  88. if (iter >= 15) {
  89. return null;
  90. }
  91. return (p);
  92. }
  93. export var names = ["Krovak", "krovak"];
  94. export default {
  95. init: init,
  96. forward: forward,
  97. inverse: inverse,
  98. names: names
  99. };