core.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. var proj = require('./Proj');
  2. var transform = require('./transform');
  3. var wgs84 = proj('WGS84');
  4. function transformer(from, to, coords) {
  5. var transformedArray;
  6. if (Array.isArray(coords)) {
  7. transformedArray = transform(from, to, coords);
  8. if (coords.length === 3) {
  9. return [transformedArray.x, transformedArray.y, transformedArray.z];
  10. }
  11. else {
  12. return [transformedArray.x, transformedArray.y];
  13. }
  14. }
  15. else {
  16. return transform(from, to, coords);
  17. }
  18. }
  19. function checkProj(item) {
  20. if (item instanceof proj) {
  21. return item;
  22. }
  23. if (item.oProj) {
  24. return item.oProj;
  25. }
  26. return proj(item);
  27. }
  28. function proj4(fromProj, toProj, coord) {
  29. fromProj = checkProj(fromProj);
  30. var single = false;
  31. var obj;
  32. if (typeof toProj === 'undefined') {
  33. toProj = fromProj;
  34. fromProj = wgs84;
  35. single = true;
  36. }
  37. else if (typeof toProj.x !== 'undefined' || Array.isArray(toProj)) {
  38. coord = toProj;
  39. toProj = fromProj;
  40. fromProj = wgs84;
  41. single = true;
  42. }
  43. toProj = checkProj(toProj);
  44. if (coord) {
  45. return transformer(fromProj, toProj, coord);
  46. }
  47. else {
  48. obj = {
  49. forward: function(coords) {
  50. return transformer(fromProj, toProj, coords);
  51. },
  52. inverse: function(coords) {
  53. return transformer(toProj, fromProj, coords);
  54. }
  55. };
  56. if (single) {
  57. obj.oProj = toProj;
  58. }
  59. return obj;
  60. }
  61. }
  62. module.exports = proj4;