datum_transform.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. var PJD_3PARAM = 1;
  2. var PJD_7PARAM = 2;
  3. var PJD_GRIDSHIFT = 3;
  4. var PJD_NODATUM = 5; // WGS84 or equivalent
  5. var SRS_WGS84_SEMIMAJOR = 6378137; // only used in grid shift transforms
  6. var SRS_WGS84_ESQUARED = 0.006694379990141316; //DGR: 2012-07-29
  7. module.exports = function(source, dest, point) {
  8. var wp, i, l;
  9. function checkParams(fallback) {
  10. return (fallback === PJD_3PARAM || fallback === PJD_7PARAM);
  11. }
  12. // Short cut if the datums are identical.
  13. if (source.compare_datums(dest)) {
  14. return point; // in this case, zero is sucess,
  15. // whereas cs_compare_datums returns 1 to indicate TRUE
  16. // confusing, should fix this
  17. }
  18. // Explicitly skip datum transform by setting 'datum=none' as parameter for either source or dest
  19. if (source.datum_type === PJD_NODATUM || dest.datum_type === PJD_NODATUM) {
  20. return point;
  21. }
  22. //DGR: 2012-07-29 : add nadgrids support (begin)
  23. var src_a = source.a;
  24. var src_es = source.es;
  25. var dst_a = dest.a;
  26. var dst_es = dest.es;
  27. var fallback = source.datum_type;
  28. // If this datum requires grid shifts, then apply it to geodetic coordinates.
  29. if (fallback === PJD_GRIDSHIFT) {
  30. if (this.apply_gridshift(source, 0, point) === 0) {
  31. source.a = SRS_WGS84_SEMIMAJOR;
  32. source.es = SRS_WGS84_ESQUARED;
  33. }
  34. else {
  35. // try 3 or 7 params transformation or nothing ?
  36. if (!source.datum_params) {
  37. source.a = src_a;
  38. source.es = source.es;
  39. return point;
  40. }
  41. wp = 1;
  42. for (i = 0, l = source.datum_params.length; i < l; i++) {
  43. wp *= source.datum_params[i];
  44. }
  45. if (wp === 0) {
  46. source.a = src_a;
  47. source.es = source.es;
  48. return point;
  49. }
  50. if (source.datum_params.length > 3) {
  51. fallback = PJD_7PARAM;
  52. }
  53. else {
  54. fallback = PJD_3PARAM;
  55. }
  56. }
  57. }
  58. if (dest.datum_type === PJD_GRIDSHIFT) {
  59. dest.a = SRS_WGS84_SEMIMAJOR;
  60. dest.es = SRS_WGS84_ESQUARED;
  61. }
  62. // Do we need to go through geocentric coordinates?
  63. if (source.es !== dest.es || source.a !== dest.a || checkParams(fallback) || checkParams(dest.datum_type)) {
  64. //DGR: 2012-07-29 : add nadgrids support (end)
  65. // Convert to geocentric coordinates.
  66. source.geodetic_to_geocentric(point);
  67. // CHECK_RETURN;
  68. // Convert between datums
  69. if (checkParams(source.datum_type)) {
  70. source.geocentric_to_wgs84(point);
  71. // CHECK_RETURN;
  72. }
  73. if (checkParams(dest.datum_type)) {
  74. dest.geocentric_from_wgs84(point);
  75. // CHECK_RETURN;
  76. }
  77. // Convert back to geodetic coordinates
  78. dest.geocentric_to_geodetic(point);
  79. // CHECK_RETURN;
  80. }
  81. // Apply grid shift to destination if required
  82. if (dest.datum_type === PJD_GRIDSHIFT) {
  83. this.apply_gridshift(dest, 1, point);
  84. // CHECK_RETURN;
  85. }
  86. source.a = src_a;
  87. source.es = src_es;
  88. dest.a = dst_a;
  89. dest.es = dst_es;
  90. return point;
  91. };