Proj.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import parseCode from './parseCode';
  2. import extend from './extend';
  3. import projections from './projections';
  4. import {sphere as dc_sphere, eccentricity as dc_eccentricity} from './deriveConstants';
  5. import Datum from './constants/Datum';
  6. import datum from './datum';
  7. import match from './match';
  8. import {getNadgrids} from "./nadgrid";
  9. function Projection(srsCode,callback) {
  10. if (!(this instanceof Projection)) {
  11. return new Projection(srsCode);
  12. }
  13. callback = callback || function(error){
  14. if(error){
  15. throw error;
  16. }
  17. };
  18. var json = parseCode(srsCode);
  19. if(typeof json !== 'object'){
  20. callback(srsCode);
  21. return;
  22. }
  23. var ourProj = Projection.projections.get(json.projName);
  24. if(!ourProj){
  25. callback(srsCode);
  26. return;
  27. }
  28. if (json.datumCode && json.datumCode !== 'none') {
  29. var datumDef = match(Datum, json.datumCode);
  30. if (datumDef) {
  31. json.datum_params = json.datum_params || (datumDef.towgs84 ? datumDef.towgs84.split(',') : null);
  32. json.ellps = datumDef.ellipse;
  33. json.datumName = datumDef.datumName ? datumDef.datumName : json.datumCode;
  34. }
  35. }
  36. json.k0 = json.k0 || 1.0;
  37. json.axis = json.axis || 'enu';
  38. json.ellps = json.ellps || 'wgs84';
  39. json.lat1 = json.lat1 || json.lat0; // Lambert_Conformal_Conic_1SP, for example, needs this
  40. var sphere_ = dc_sphere(json.a, json.b, json.rf, json.ellps, json.sphere);
  41. var ecc = dc_eccentricity(sphere_.a, sphere_.b, sphere_.rf, json.R_A);
  42. var nadgrids = getNadgrids(json.nadgrids);
  43. var datumObj = json.datum || datum(json.datumCode, json.datum_params, sphere_.a, sphere_.b, ecc.es, ecc.ep2,
  44. nadgrids);
  45. extend(this, json); // transfer everything over from the projection because we don't know what we'll need
  46. extend(this, ourProj); // transfer all the methods from the projection
  47. // copy the 4 things over we calculated in deriveConstants.sphere
  48. this.a = sphere_.a;
  49. this.b = sphere_.b;
  50. this.rf = sphere_.rf;
  51. this.sphere = sphere_.sphere;
  52. // copy the 3 things we calculated in deriveConstants.eccentricity
  53. this.es = ecc.es;
  54. this.e = ecc.e;
  55. this.ep2 = ecc.ep2;
  56. // add in the datum object
  57. this.datum = datumObj;
  58. // init the projection
  59. this.init();
  60. // legecy callback from back in the day when it went to spatialreference.org
  61. callback(null, this);
  62. }
  63. Projection.projections = projections;
  64. Projection.projections.start();
  65. export default Projection;