Commit b29ecca9 authored by Calvin Metcalf's avatar Calvin Metcalf
Browse files

break up proj proccess

parent 374b9868
Loading
Loading
Loading
Loading
+22 −130
Original line number Diff line number Diff line
var parseCode = require("./parseCode");
var extend = require('./extend');
var defs = require('./defs');
var constants = {};
constants.grids = require('./constants/grids');
constants.Datum = require('./constants/Datum');
constants.Ellipsoid = require('./constants/Ellipsoid');
var datum = require('./datum');
var projections = require('./projections');
var wkt = require('./wkt');
var projStr = require('./projString');
var EPSLN = 1.0e-10;
// ellipoid pj_set_ell.c
var SIXTH = 0.1666666666666666667;
/* 1/6 */
var RA4 = 0.04722222222222222222;
/* 17/360 */
var RA6 = 0.02215608465608465608;
function Projection(srsCode) {
var deriveConstants = require('./deriveConstants');

function Projection(srsCode,callback) {
  if (!(this instanceof Projection)) {
    return new Projection(srsCode);
  }
  this.srsCodeInput = srsCode;
  this.x0 = 0;
  this.y0 = 0;
  var obj;
  if (typeof srsCode === 'string') {
    //check to see if this is a WKT string
    if (srsCode in defs) {
      this.deriveConstants(defs[srsCode]);
      extend(this, defs[srsCode]);
    }
    else if ((srsCode.indexOf('GEOGCS') >= 0) || (srsCode.indexOf('GEOCCS') >= 0) || (srsCode.indexOf('PROJCS') >= 0) || (srsCode.indexOf('LOCAL_CS') >= 0)) {
      obj = wkt(srsCode);
      this.deriveConstants(obj);
      extend(this, obj);
      //this.loadProjCode(this.projName);
    }
    else if (srsCode[0] === '+') {
      obj = projStr(srsCode);
      this.deriveConstants(obj);
      extend(this, obj);
    }
  callback = callback || function(error){
    if(error){
      throw error;
    }
  else {
    this.deriveConstants(srsCode);
    extend(this, srsCode);
  }

  this.initTransforms(this.projName);
  };
  var json = parseCode(srsCode);
  if(typeof json !== 'object'){
    callback(srsCode);
    return;
  }
Projection.projections = projections;
Projection.projections.start();
Projection.prototype = {
  /**
   * Function: initTransforms
   *    Finalize the initialization of the Proj object
   *
   */
  initTransforms: function(projName) {
    var ourProj = Projection.projections.get(projName);
  var modifiedJSON = deriveConstants(json);
  var ourProj = Projection.projections.get(modifiedJSON.projName);
  if(ourProj){
    extend(this, modifiedJSON);
    extend(this, ourProj);
    this.init();
    }
    else {
      throw ("unknown projection " + projName);
    }
  },

  deriveConstants: function(self) {
    // DGR 2011-03-20 : nagrids -> nadgrids
    if (self.nadgrids && self.nadgrids.length === 0) {
      self.nadgrids = null;
    }
    if (self.nadgrids) {
      self.grids = self.nadgrids.split(",");
      var g = null,
        l = self.grids.length;
      if (l > 0) {
        for (var i = 0; i < l; i++) {
          g = self.grids[i];
          var fg = g.split("@");
          if (fg[fg.length - 1] === "") {
            //..reportError("nadgrids syntax error '" + self.nadgrids + "' : empty grid found");
            continue;
          }
          self.grids[i] = {
            mandatory: fg.length === 1, //@=> optional grid (no error if not found)
            name: fg[fg.length - 1],
            grid: constants.grids[fg[fg.length - 1]] //FIXME: grids loading ...
          };
          if (self.grids[i].mandatory && !self.grids[i].grid) {
            //..reportError("Missing '" + self.grids[i].name + "'");
    callback(null, this);
  }else{
    callback(srsCode);
  }
}
      }
      // DGR, 2011-03-20: grids is an array of objects that hold
      // the loaded grids, its name and the mandatory informations of it.
    }
    if (self.datumCode && self.datumCode !== 'none') {
      var datumDef = constants.Datum[self.datumCode];
      if (datumDef) {
        self.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
        self.ellps = datumDef.ellipse;
        self.datumName = datumDef.datumName ? datumDef.datumName : self.datumCode;
      }
    }
    if (!self.a) { // do we have an ellipsoid?
      var ellipse = constants.Ellipsoid[self.ellps] ? constants.Ellipsoid[self.ellps] : constants.Ellipsoid.WGS84;
      extend(self, ellipse);
    }
    if (self.rf && !self.b) {
      self.b = (1.0 - 1.0 / self.rf) * self.a;
    }
    if (self.rf === 0 || Math.abs(self.a - self.b) < EPSLN) {
      self.sphere = true;
      self.b = self.a;
    }
    self.a2 = self.a * self.a; // used in geocentric
    self.b2 = self.b * self.b; // used in geocentric
    self.es = (self.a2 - self.b2) / self.a2; // e ^ 2
    self.e = Math.sqrt(self.es); // eccentricity
    if (self.R_A) {
      self.a *= 1 - self.es * (SIXTH + self.es * (RA4 + self.es * RA6));
      self.a2 = self.a * self.a;
      self.b2 = self.b * self.b;
      self.es = 0;
    }
    self.ep2 = (self.a2 - self.b2) / self.b2; // used in geocentric
    if (!self.k0) {
      self.k0 = 1.0; //default value
    }
    //DGR 2010-11-12: axis
    if (!self.axis) {
      self.axis = "enu";
    }

    self.datum = datum(self);
  }
};
Projection.projections = projections;
Projection.projections.start();
module.exports = Projection;

lib/deriveConstants.js

0 → 100644
+54 −0
Original line number Diff line number Diff line
var Datum = require('./constants/Datum');
var Ellipsoid = require('./constants/Ellipsoid');
var extend = require('./extend');
var datum = require('./datum');
var EPSLN = 1.0e-10;
// ellipoid pj_set_ell.c
var SIXTH = 0.1666666666666666667;
/* 1/6 */
var RA4 = 0.04722222222222222222;
/* 17/360 */
var RA6 = 0.02215608465608465608;
module.exports = function(json) {
  // DGR 2011-03-20 : nagrids -> nadgrids
  if (json.datumCode && json.datumCode !== 'none') {
    var datumDef = Datum[json.datumCode];
    if (datumDef) {
      json.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
      json.ellps = datumDef.ellipse;
      json.datumName = datumDef.datumName ? datumDef.datumName : json.datumCode;
    }
  }
  if (!json.a) { // do we have an ellipsoid?
    var ellipse = Ellipsoid[json.ellps] ? Ellipsoid[json.ellps] : Ellipsoid.WGS84;
    extend(json, ellipse);
  }
  if (json.rf && !json.b) {
    json.b = (1.0 - 1.0 / json.rf) * json.a;
  }
  if (json.rf === 0 || Math.abs(json.a - json.b) < EPSLN) {
    json.sphere = true;
    json.b = json.a;
  }
  json.a2 = json.a * json.a; // used in geocentric
  json.b2 = json.b * json.b; // used in geocentric
  json.es = (json.a2 - json.b2) / json.a2; // e ^ 2
  json.e = Math.sqrt(json.es); // eccentricity
  if (json.R_A) {
    json.a *= 1 - json.es * (SIXTH + json.es * (RA4 + json.es * RA6));
    json.a2 = json.a * json.a;
    json.b2 = json.b * json.b;
    json.es = 0;
  }
  json.ep2 = (json.a2 - json.b2) / json.b2; // used in geocentric
  if (!json.k0) {
    json.k0 = 1.0; //default value
  }
  //DGR 2010-11-12: axis
  if (!json.axis) {
    json.axis = "enu";
  }

  json.datum = datum(json);
  return json;
};
 No newline at end of file

lib/parseCode.js

0 → 100644
+36 −0
Original line number Diff line number Diff line
var defs = require('./defs');
var wkt = require('./wkt');
var projStr = require('./projString');
function testObj(code){
  return typeof code === 'string';
}
function testDef(code){
  return code in defs;
}
function testWKT(code){
  var codeWords = ['GEOGCS','GEOCCS','PROJCS','LOCAL_CS'];
  return codeWords.reduce(function(a,b){
    return a+1+code.indexOf(b);
  },0);
}
function testProj(code){
  return code[0] === '+';
}
function parse(code){
  if (testObj(code)) {
    //check to see if this is a WKT string
    if (testDef(code)) {
      return defs[code];
    }
    else if (testWKT(code)) {
      return wkt(code);
    }
    else if (testProj(code)) {
      return projStr(code);
    }
  }else{
    return code;
  }
}

module.exports = parse;
 No newline at end of file
+6 −0
Original line number Diff line number Diff line
@@ -9,6 +9,12 @@ var phi2z = require('../common/phi2z');
exports.init = function() {
  var con = this.b / this.a;
  this.es = 1 - con * con;
  if(!('x0' in this)){
    this.x0 = 0;
  }
  if(!('y0' in this)){
    this.y0 = 0;
  }
  this.e = Math.sqrt(this.es);
  if (this.lat_ts) {
    if (this.sphere) {
+1 −1
Original line number Diff line number Diff line
@@ -163,7 +163,7 @@ function startTests(chai, proj4, testPoints) {
      it('should throw an error for an unknown ref', function() {
        assert.throws(function() {
          new proj4.Proj('fake one');
        }, 'unknown projection', 'should work');
        }, 'fake one', 'should work');
      });
    })
    describe('utility', function() {