transform.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. var D2R = 0.01745329251994329577;
  2. var R2D = 57.29577951308232088;
  3. var PJD_3PARAM = 1;
  4. var PJD_7PARAM = 2;
  5. var datum_transform = require('./datum_transform');
  6. var adjust_axis = require('./adjust_axis');
  7. var proj = require('./Proj');
  8. var toPoint = require('./common/toPoint');
  9. module.exports = function transform(source, dest, point) {
  10. var wgs84;
  11. if (Array.isArray(point)) {
  12. point = toPoint(point);
  13. }
  14. function checkNotWGS(source, dest) {
  15. return ((source.datum.datum_type === PJD_3PARAM || source.datum.datum_type === PJD_7PARAM) && dest.datumCode !== "WGS84");
  16. }
  17. // Workaround for datum shifts towgs84, if either source or destination projection is not wgs84
  18. if (source.datum && dest.datum && (checkNotWGS(source, dest) || checkNotWGS(dest, source))) {
  19. wgs84 = new proj('WGS84');
  20. transform(source, wgs84, point);
  21. source = wgs84;
  22. }
  23. // DGR, 2010/11/12
  24. if (source.axis !== "enu") {
  25. adjust_axis(source, false, point);
  26. }
  27. // Transform source points to long/lat, if they aren't already.
  28. if (source.projName === "longlat") {
  29. point.x *= D2R; // convert degrees to radians
  30. point.y *= D2R;
  31. }
  32. else {
  33. if (source.to_meter) {
  34. point.x *= source.to_meter;
  35. point.y *= source.to_meter;
  36. }
  37. source.inverse(point); // Convert Cartesian to longlat
  38. }
  39. // Adjust for the prime meridian if necessary
  40. if (source.from_greenwich) {
  41. point.x += source.from_greenwich;
  42. }
  43. // Convert datums if needed, and if possible.
  44. point = datum_transform(source.datum, dest.datum, point);
  45. // Adjust for the prime meridian if necessary
  46. if (dest.from_greenwich) {
  47. point.x -= dest.from_greenwich;
  48. }
  49. if (dest.projName === "longlat") {
  50. // convert radians to decimal degrees
  51. point.x *= R2D;
  52. point.y *= R2D;
  53. }
  54. else { // else project
  55. dest.forward(point);
  56. if (dest.to_meter) {
  57. point.x /= dest.to_meter;
  58. point.y /= dest.to_meter;
  59. }
  60. }
  61. // DGR, 2010/11/12
  62. if (dest.axis !== "enu") {
  63. adjust_axis(dest, true, point);
  64. }
  65. return point;
  66. };