Loading src/Proj4.js +12 −9 Original line number Diff line number Diff line Loading @@ -41,26 +41,29 @@ $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $ */ function proj4(fromProj,toProj,coord){ var trnc = function(f,t,c){ var tc,nc; nc = c instanceof proj4.Point ? c : new proj4.Point(c); var tc; if(Array.isArray(c)){ tc = proj4.tranformation(f,t,nc); tc = proj4.transform(f,t,new proj4.Point(c)); if(c.length === 3){ return [tc.x,tc.y,tc.z]; }else{ return [tc.x,tc.y]; } }else{ return proj4.tranformation(fromProj,toProj,nc); return proj4.transform(fromProj,toProj,c); } }; fromProj = fromProj instanceof proj4.Proj ? fromProj : new proj4.Proj(fromProj); if(typeof toProj === 'undefined'){ toProj = proj4.WGS84; } else if(('x' in toProj)||Array.isArray(toProj)){ 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 = proj4.WGS84; toProj = fromProj; fromProj = proj4.WGS84; }else{ toProj = toProj instanceof proj4.Proj ? toProj : new proj4.Proj(toProj); } Loading @@ -69,10 +72,10 @@ function proj4(fromProj,toProj,coord){ } else { return { forward: function(coords){ return trnc(fromProj,toProj,coord); return trnc(fromProj,toProj,coords); }, inverse: function(coords){ return trnc(toProj,fromProj,coord); return trnc(toProj,fromProj,coords); } }; } Loading test/test.js +86 −13 Original line number Diff line number Diff line Loading @@ -4,19 +4,92 @@ 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('errors',function(){ it('should throw an error for an unknown ref',function(){ Loading Loading
src/Proj4.js +12 −9 Original line number Diff line number Diff line Loading @@ -41,26 +41,29 @@ $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $ */ function proj4(fromProj,toProj,coord){ var trnc = function(f,t,c){ var tc,nc; nc = c instanceof proj4.Point ? c : new proj4.Point(c); var tc; if(Array.isArray(c)){ tc = proj4.tranformation(f,t,nc); tc = proj4.transform(f,t,new proj4.Point(c)); if(c.length === 3){ return [tc.x,tc.y,tc.z]; }else{ return [tc.x,tc.y]; } }else{ return proj4.tranformation(fromProj,toProj,nc); return proj4.transform(fromProj,toProj,c); } }; fromProj = fromProj instanceof proj4.Proj ? fromProj : new proj4.Proj(fromProj); if(typeof toProj === 'undefined'){ toProj = proj4.WGS84; } else if(('x' in toProj)||Array.isArray(toProj)){ 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 = proj4.WGS84; toProj = fromProj; fromProj = proj4.WGS84; }else{ toProj = toProj instanceof proj4.Proj ? toProj : new proj4.Proj(toProj); } Loading @@ -69,10 +72,10 @@ function proj4(fromProj,toProj,coord){ } else { return { forward: function(coords){ return trnc(fromProj,toProj,coord); return trnc(fromProj,toProj,coords); }, inverse: function(coords){ return trnc(toProj,fromProj,coord); return trnc(toProj,fromProj,coords); } }; } Loading
test/test.js +86 −13 Original line number Diff line number Diff line Loading @@ -4,19 +4,92 @@ 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('errors',function(){ it('should throw an error for an unknown ref',function(){ Loading