Loading src/Proj4.js +48 −2 Original line number Diff line number Diff line Loading @@ -39,8 +39,47 @@ $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $ /** * Global namespace object for proj4 library */ var proj4 = {}; function proj4(fromProj,toProj,coord){ var transformer = function(f,t,c){ var transformedArray; if(Array.isArray(c)){ transformedArray = proj4.transform(f,t,new proj4.Point(c)); if(c.length === 3){ return [transformedArray.x, transformedArray.y, transformedArray.z]; }else{ return [transformedArray.x, transformedArray.y]; } }else{ return proj4.transform(fromProj,toProj,c); } }; fromProj = fromProj instanceof proj4.Proj ? fromProj : new proj4.Proj(fromProj); if(typeof toProj === 'undefined'){ toProj = fromProj; fromProj = proj4.WGS84; }else if(typeof toProj === 'string'){ toProj = new proj4.Proj(toProj); }else if(toProj.x||Array.isArray(toProj)){ coord = toProj; toProj = fromProj; fromProj = proj4.WGS84; }else{ toProj = toProj instanceof proj4.Proj ? toProj : new proj4.Proj(toProj); } if(coord){ return transformer(fromProj,toProj,coord); } else { return { forward: function(coords){ return transformer(fromProj,toProj,coords); }, inverse: function(coords){ return transformer(toProj,fromProj,coords); } }; } } /** * Property: defaultDatum * The datum to use when no others a specified Loading Loading @@ -402,3 +441,10 @@ proj4.Class = function() { Class.prototype = extended; return Class; }; (function(){ /*global module*/ if(typeof module !== 'undefined'){ module.exports = proj4; } })(); test/test.js +105 −14 Original line number Diff line number Diff line Loading @@ -4,19 +4,110 @@ var xyEPSLN = 1.0e-2; describe('proj4', function () { describe('core',function(){ testPoints.forEach(function(testPoint){ it('should work with forwards ' + testPoint.code, function () { describe(testPoint.code,function(){ describe('traditional',function(){ it('should work with forwards', function () { var proj = new proj4.Proj(testPoint.code); var xy = proj4.transform(proj4.WGS84, proj, new proj4.Point(testPoint.ll)); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work with backwards ' + testPoint.code, function () { it('should work with backwards', function () { var proj = new proj4.Proj(testPoint.code); var ll = proj4.transform(proj,proj4.WGS84, new proj4.Point(testPoint.xy)); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'lng is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'lat is close'); }); }); describe('new method 2 param',function(){ it('shortcut method should work with an array', function(){ var xy = proj4(testPoint.code,testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.ll[0],y:testPoint.ll[1]}; var xy = proj4(testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.ll); var xy = proj4(testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); }); describe('new method 3 param',function(){ it('shortcut method should work with an array', function(){ var xy = proj4(proj4.WGS84,testPoint.code,testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.ll[0],y:testPoint.ll[1]}; var xy = proj4(proj4.WGS84,testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.ll); var xy = proj4(proj4.WGS84,testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); }); describe('new method 3 param other way',function(){ it('shortcut method should work with an array', function(){ var ll = proj4(testPoint.code,proj4.WGS84,testPoint.xy); assert.closeTo(ll[0], testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll[1], testPoint.ll[1],llEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.xy[0],y:testPoint.xy[1]}; var ll = proj4(testPoint.code,proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.xy); var ll = proj4(testPoint.code,proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); }); describe('1 param',function(){ it('forwards',function(){ var xy = proj4(testPoint.code).forward(testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('inverse',function(){ var ll = proj4(testPoint.code).inverse(testPoint.xy); assert.closeTo(ll[0], testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll[1], testPoint.ll[1],llEPSLN, 'y is close'); }); }); describe('proj object',function(){ it('should work with a 2 element array', function(){ var xy = proj4(new proj4.Proj(testPoint.code),testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work on element',function(){ var xy = proj4(new proj4.Proj(testPoint.code)).forward(testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work 3 element ponit object', function(){ var pt = new proj4.Point(testPoint.xy); var ll = proj4(new proj4.Proj(testPoint.code),proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); }); }); }); }); describe('errors',function(){ it('should throw an error for an unknown ref',function(){ Loading Loading
src/Proj4.js +48 −2 Original line number Diff line number Diff line Loading @@ -39,8 +39,47 @@ $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $ /** * Global namespace object for proj4 library */ var proj4 = {}; function proj4(fromProj,toProj,coord){ var transformer = function(f,t,c){ var transformedArray; if(Array.isArray(c)){ transformedArray = proj4.transform(f,t,new proj4.Point(c)); if(c.length === 3){ return [transformedArray.x, transformedArray.y, transformedArray.z]; }else{ return [transformedArray.x, transformedArray.y]; } }else{ return proj4.transform(fromProj,toProj,c); } }; fromProj = fromProj instanceof proj4.Proj ? fromProj : new proj4.Proj(fromProj); if(typeof toProj === 'undefined'){ toProj = fromProj; fromProj = proj4.WGS84; }else if(typeof toProj === 'string'){ toProj = new proj4.Proj(toProj); }else if(toProj.x||Array.isArray(toProj)){ coord = toProj; toProj = fromProj; fromProj = proj4.WGS84; }else{ toProj = toProj instanceof proj4.Proj ? toProj : new proj4.Proj(toProj); } if(coord){ return transformer(fromProj,toProj,coord); } else { return { forward: function(coords){ return transformer(fromProj,toProj,coords); }, inverse: function(coords){ return transformer(toProj,fromProj,coords); } }; } } /** * Property: defaultDatum * The datum to use when no others a specified Loading Loading @@ -402,3 +441,10 @@ proj4.Class = function() { Class.prototype = extended; return Class; }; (function(){ /*global module*/ if(typeof module !== 'undefined'){ module.exports = proj4; } })();
test/test.js +105 −14 Original line number Diff line number Diff line Loading @@ -4,19 +4,110 @@ var xyEPSLN = 1.0e-2; describe('proj4', function () { describe('core',function(){ testPoints.forEach(function(testPoint){ it('should work with forwards ' + testPoint.code, function () { describe(testPoint.code,function(){ describe('traditional',function(){ it('should work with forwards', function () { var proj = new proj4.Proj(testPoint.code); var xy = proj4.transform(proj4.WGS84, proj, new proj4.Point(testPoint.ll)); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work with backwards ' + testPoint.code, function () { it('should work with backwards', function () { var proj = new proj4.Proj(testPoint.code); var ll = proj4.transform(proj,proj4.WGS84, new proj4.Point(testPoint.xy)); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'lng is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'lat is close'); }); }); describe('new method 2 param',function(){ it('shortcut method should work with an array', function(){ var xy = proj4(testPoint.code,testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.ll[0],y:testPoint.ll[1]}; var xy = proj4(testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.ll); var xy = proj4(testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); }); describe('new method 3 param',function(){ it('shortcut method should work with an array', function(){ var xy = proj4(proj4.WGS84,testPoint.code,testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.ll[0],y:testPoint.ll[1]}; var xy = proj4(proj4.WGS84,testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.ll); var xy = proj4(proj4.WGS84,testPoint.code,pt); assert.closeTo(xy.x, testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy.y, testPoint.xy[1],xyEPSLN, 'y is close'); }); }); describe('new method 3 param other way',function(){ it('shortcut method should work with an array', function(){ var ll = proj4(testPoint.code,proj4.WGS84,testPoint.xy); assert.closeTo(ll[0], testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll[1], testPoint.ll[1],llEPSLN, 'y is close'); }); it('shortcut method should work with an object', function(){ var pt = {x:testPoint.xy[0],y:testPoint.xy[1]}; var ll = proj4(testPoint.code,proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); it('shortcut method should work with a point object', function(){ var pt = new proj4.Point(testPoint.xy); var ll = proj4(testPoint.code,proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); }); describe('1 param',function(){ it('forwards',function(){ var xy = proj4(testPoint.code).forward(testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('inverse',function(){ var ll = proj4(testPoint.code).inverse(testPoint.xy); assert.closeTo(ll[0], testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll[1], testPoint.ll[1],llEPSLN, 'y is close'); }); }); describe('proj object',function(){ it('should work with a 2 element array', function(){ var xy = proj4(new proj4.Proj(testPoint.code),testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work on element',function(){ var xy = proj4(new proj4.Proj(testPoint.code)).forward(testPoint.ll); assert.closeTo(xy[0], testPoint.xy[0],xyEPSLN, 'x is close'); assert.closeTo(xy[1], testPoint.xy[1],xyEPSLN, 'y is close'); }); it('should work 3 element ponit object', function(){ var pt = new proj4.Point(testPoint.xy); var ll = proj4(new proj4.Proj(testPoint.code),proj4.WGS84,pt); assert.closeTo(ll.x, testPoint.ll[0],llEPSLN, 'x is close'); assert.closeTo(ll.y, testPoint.ll[1],llEPSLN, 'y is close'); }); }); }); }); }); describe('errors',function(){ it('should throw an error for an unknown ref',function(){ Loading