test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // You can do this in the grunt config for each mocha task, see the `options` config
  2. // Start the main app logic.
  3. function startTests(chai, proj4, testPoints) {
  4. var assert = chai.assert;
  5. proj4.defs([
  6. ["EPSG:102018", "+proj=gnom +lat_0=90 +lon_0=0 +x_0=6300000 +y_0=6300000 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"],
  7. ["testmerc", "+proj=merc +lon_0=5.937 +lat_ts=45.027 +ellps=sphere +datum=none"],
  8. ["testmerc2", "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +units=m +k=1.0 +nadgrids=@null +no_defs"]
  9. ]);
  10. proj4.defs('esriOnline', 'PROJCS["WGS_1984_Web_Mercator_Auxiliary_Sphere",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Mercator_Auxiliary_Sphere"],PARAMETER["False_Easting",0.0],PARAMETER["False_Northing",0.0],PARAMETER["Central_Meridian",0.0],PARAMETER["Standard_Parallel_1",0.0],PARAMETER["Auxiliary_Sphere_Type",0.0],UNIT["Meter",1.0]]');
  11. describe('proj2proj', function() {
  12. it('should work transforming from one projection to another', function() {
  13. var sweref99tm = '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs';
  14. var rt90 = '+lon_0=15.808277777799999 +lat_0=0.0 +k=1.0 +x_0=1500000.0 +y_0=0.0 +proj=tmerc +ellps=bessel +units=m +towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +no_defs';
  15. var rslt = proj4(sweref99tm, rt90).forward([319180, 6399862]);
  16. assert.closeTo(rslt[0], 1271137.927154, 0.000001);
  17. assert.closeTo(rslt[1], 6404230.291456, 0.000001);
  18. });
  19. it('should work with a proj object', function() {
  20. var sweref99tm = proj4('+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
  21. var rt90 = proj4('+lon_0=15.808277777799999 +lat_0=0.0 +k=1.0 +x_0=1500000.0 +y_0=0.0 +proj=tmerc +ellps=bessel +units=m +towgs84=414.1,41.3,603.1,-0.855,2.141,-7.023,0 +no_defs');
  22. var rslt = proj4(sweref99tm, rt90).forward([319180, 6399862]);
  23. assert.closeTo(rslt[0], 1271137.927154, 0.000001);
  24. assert.closeTo(rslt[1], 6404230.291456, 0.000001);
  25. });
  26. });
  27. describe('proj4', function() {
  28. describe('core', function() {
  29. testPoints.forEach(function(testPoint) {
  30. describe(testPoint.code, function() {
  31. var xyAcc = 2,
  32. llAcc = 6;
  33. if ('acc' in testPoint) {
  34. if ('xy' in testPoint.acc) {
  35. xyAcc = testPoint.acc.xy;
  36. }
  37. if ('ll' in testPoint.acc) {
  38. llAcc = testPoint.acc.ll;
  39. }
  40. }
  41. var xyEPSLN = Math.pow(10, - 1 * xyAcc);
  42. var llEPSLN = Math.pow(10, - 1 * llAcc);
  43. describe('traditional', function() {
  44. it('should work with forwards', function() {
  45. var proj = new proj4.Proj(testPoint.code);
  46. var xy = proj4.transform(proj4.WGS84, proj, proj4.toPoint(testPoint.ll));
  47. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  48. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  49. });
  50. it('should work with backwards', function() {
  51. var proj = new proj4.Proj(testPoint.code);
  52. var ll = proj4.transform(proj, proj4.WGS84, proj4.toPoint(testPoint.xy));
  53. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'lng is close');
  54. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'lat is close');
  55. });
  56. });
  57. describe('new method 2 param', function() {
  58. it('shortcut method should work with an array', function() {
  59. var xy = proj4(testPoint.code, testPoint.ll);
  60. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  61. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  62. });
  63. it('shortcut method should work with an object', function() {
  64. var pt = {
  65. x: testPoint.ll[0],
  66. y: testPoint.ll[1]
  67. };
  68. var xy = proj4(testPoint.code, pt);
  69. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  70. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  71. });
  72. it('shortcut method should work with a point object', function() {
  73. var pt = proj4.toPoint(testPoint.ll);
  74. var xy = proj4(testPoint.code, pt);
  75. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  76. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  77. });
  78. });
  79. describe('new method 3 param', function() {
  80. it('shortcut method should work with an array', function() {
  81. var xy = proj4(proj4.WGS84, testPoint.code, testPoint.ll);
  82. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  83. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  84. });
  85. it('shortcut method should work with an object', function() {
  86. var pt = {
  87. x: testPoint.ll[0],
  88. y: testPoint.ll[1]
  89. };
  90. var xy = proj4(proj4.WGS84, testPoint.code, pt);
  91. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  92. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  93. });
  94. it('shortcut method should work with a point object', function() {
  95. var pt = proj4.toPoint(testPoint.ll);
  96. var xy = proj4(proj4.WGS84, testPoint.code, pt);
  97. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  98. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  99. });
  100. });
  101. describe('new method 3 param other way', function() {
  102. it('shortcut method should work with an array', function() {
  103. var ll = proj4(testPoint.code, proj4.WGS84, testPoint.xy);
  104. assert.closeTo(ll[0], testPoint.ll[0], llEPSLN, 'x is close');
  105. assert.closeTo(ll[1], testPoint.ll[1], llEPSLN, 'y is close');
  106. });
  107. it('shortcut method should work with an object', function() {
  108. var pt = {
  109. x: testPoint.xy[0],
  110. y: testPoint.xy[1]
  111. };
  112. var ll = proj4(testPoint.code, proj4.WGS84, pt);
  113. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  114. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  115. });
  116. it('shortcut method should work with a point object', function() {
  117. var pt = proj4.toPoint(testPoint.xy);
  118. var ll = proj4(testPoint.code, proj4.WGS84, pt);
  119. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  120. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  121. });
  122. });
  123. describe('1 param', function() {
  124. it('forwards', function() {
  125. var xy = proj4(testPoint.code).forward(testPoint.ll);
  126. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  127. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  128. });
  129. it('inverse', function() {
  130. var ll = proj4(testPoint.code).inverse(testPoint.xy);
  131. assert.closeTo(ll[0], testPoint.ll[0], llEPSLN, 'x is close');
  132. assert.closeTo(ll[1], testPoint.ll[1], llEPSLN, 'y is close');
  133. });
  134. });
  135. describe('proj object', function() {
  136. it('should work with a 2 element array', function() {
  137. var xy = proj4(new proj4.Proj(testPoint.code), testPoint.ll);
  138. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  139. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  140. });
  141. it('should work on element', function() {
  142. var xy = proj4(new proj4.Proj(testPoint.code)).forward(testPoint.ll);
  143. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  144. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  145. });
  146. it('should work 3 element ponit object', function() {
  147. var pt = proj4.toPoint(testPoint.xy);
  148. var ll = proj4(new proj4.Proj(testPoint.code), proj4.WGS84, pt);
  149. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  150. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  151. });
  152. });
  153. });
  154. });
  155. });
  156. describe('defs', function() {
  157. assert.equal(proj4.defs('testmerc'), proj4.defs['testmerc']);
  158. proj4.defs('foo', '+proj=merc +lon_0=5.937 +lat_ts=45.027 +ellps=sphere +datum=none');
  159. assert.typeOf(proj4.defs['foo'], 'object');
  160. proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
  161. assert.strictEqual(proj4.defs['urn:x-ogc:def:crs:EPSG:4326'], proj4.defs['EPSG:4326']);
  162. });
  163. describe('errors', function() {
  164. it('should throw an error for an unknown ref', function() {
  165. assert.throws(function() {
  166. new proj4.Proj('fake one');
  167. }, 'fake one', 'should work');
  168. });
  169. });
  170. describe('utility', function() {
  171. it('should have MGRS available in the proj4.util namespace', function() {
  172. assert.typeOf(proj4.mgrs, "object", "MGRS available in the proj4.util namespace");
  173. });
  174. it('should have fromMGRS method added to proj4.Point prototype', function() {
  175. assert.typeOf(proj4.Point.fromMGRS, "function", "fromMGRS method added to proj4.Point prototype");
  176. });
  177. it('should have toMGRS method added to proj4.Point prototype', function() {
  178. assert.typeOf(proj4.Point.prototype.toMGRS, "function", "toMGRS method added to proj4.Point prototype");
  179. });
  180. describe('First MGRS set', function() {
  181. var mgrs = "33UXP04";
  182. var point = proj4.Point.fromMGRS(mgrs);
  183. it('Longitude of point from MGRS correct.', function() {
  184. assert.equal(point.x.toPrecision(7), "16.41450", "Longitude of point from MGRS correct.");
  185. });
  186. it('Latitude of point from MGRS correct.', function() {
  187. assert.equal(point.y.toPrecision(7), "48.24949", "Latitude of point from MGRS correct.");
  188. });
  189. it('MGRS reference with highest accuracy correct.', function() {
  190. assert.equal(point.toMGRS(), "33UXP0500444998", "MGRS reference with highest accuracy correct.");
  191. });
  192. it('MGRS reference with 1-digit accuracy correct.', function() {
  193. assert.equal(point.toMGRS(1), mgrs, "MGRS reference with 1-digit accuracy correct.");
  194. });
  195. });
  196. describe('Second MGRS set', function() {
  197. var mgrs = "24XWT783908"; // near UTM zone border, so there are two ways to reference this
  198. var point = proj4.Point.fromMGRS(mgrs);
  199. it("Longitude of point from MGRS correct.", function() {
  200. assert.equal(point.x.toPrecision(7), "-32.66433", "Longitude of point from MGRS correct.");
  201. });
  202. it("Latitude of point from MGRS correct.", function() {
  203. assert.equal(point.y.toPrecision(7), "83.62778", "Latitude of point from MGRS correct.");
  204. });
  205. it("MGRS reference with 3-digit accuracy correct.", function() {
  206. assert.equal(point.toMGRS(3), "25XEN041865", "MGRS reference with 3-digit accuracy correct.");
  207. });
  208. });
  209. describe('Defs and Datum definition', function() {
  210. proj4.defs("EPSG:5514", "+proj=krovak +lat_0=49.5 +lon_0=24.83333333333333 +alpha=30.28813972222222 +k=0.9999 +x_0=0 +y_0=0 +ellps=bessel +pm=greenwich +units=m +no_defs +towgs84=570.8,85.7,462.8,4.998,1.587,5.261,3.56");
  211. var point = proj4.transform(proj4.Proj("WGS84"), proj4.Proj("EPSG:5514"),
  212. proj4.toPoint([12.806988, 49.452262]));
  213. it("Longitude of point from WGS84 correct.", function() {
  214. assert.equal(point.x.toPrecision(8), "-868208.61", "Longitude of point from WGS84 correct.");
  215. });
  216. it("Latitude of point from WGS84 correct.", function() {
  217. assert.equal(point.y.toPrecision(9), "-1095793.64", "Latitude of point from WGS84 correct.");
  218. });
  219. var point2 = proj4.transform(proj4.Proj("WGS84"), proj4.Proj("EPSG:5514"),
  220. proj4.toPoint([12.806988, 49.452262]));
  221. it("Longitude of point from WGS84 with second call for EPSG:5514 correct.", function() {
  222. assert.equal(point2.x.toPrecision(8), "-868208.61", "Longitude of point from WGS84 correct.");
  223. });
  224. it("Latitude of point from WGS84 with second call for EPSG:5514 correct.", function() {
  225. assert.equal(point2.y.toPrecision(9), "-1095793.64", "Latitude of point from WGS84 correct.");
  226. });
  227. });
  228. });
  229. });
  230. }
  231. if(typeof process !== 'undefined'&&process.toString() === '[object process]'){
  232. (function(){
  233. startTests(require('chai'), require('../lib'), require('./testData'));
  234. })();
  235. }