test.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  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"],
  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('parse', function() {
  12. it('should parse units', function() {
  13. assert.equal(proj4.defs('testmerc2').units, 'm');
  14. });
  15. });
  16. describe('proj2proj', function() {
  17. it('should work transforming from one projection to another', function() {
  18. var sweref99tm = '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs';
  19. 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';
  20. var rslt = proj4(sweref99tm, rt90).forward([319180, 6399862]);
  21. assert.closeTo(rslt[0], 1271137.927561178, 0.000001);
  22. assert.closeTo(rslt[1], 6404230.291456626, 0.000001);
  23. });
  24. it('should work with a proj object', function() {
  25. var sweref99tm = proj4('+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs');
  26. 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');
  27. var rslt = proj4(sweref99tm, rt90).forward([319180, 6399862]);
  28. assert.closeTo(rslt[0], 1271137.927561178, 0.000001);
  29. assert.closeTo(rslt[1], 6404230.291456626, 0.000001);
  30. });
  31. });
  32. describe('proj4', function() {
  33. describe('core', function() {
  34. testPoints.forEach(function(testPoint) {
  35. describe(testPoint.code, function() {
  36. var xyAcc = 2,
  37. llAcc = 6;
  38. if ('acc' in testPoint) {
  39. if ('xy' in testPoint.acc) {
  40. xyAcc = testPoint.acc.xy;
  41. }
  42. if ('ll' in testPoint.acc) {
  43. llAcc = testPoint.acc.ll;
  44. }
  45. }
  46. var xyEPSLN = Math.pow(10, - 1 * xyAcc);
  47. var llEPSLN = Math.pow(10, - 1 * llAcc);
  48. describe('traditional', function() {
  49. it('should work with forwards', function() {
  50. var proj = new proj4.Proj(testPoint.code);
  51. var xy = proj4.transform(proj4.WGS84, proj, proj4.toPoint(testPoint.ll));
  52. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  53. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  54. });
  55. it('should work with backwards', function() {
  56. var proj = new proj4.Proj(testPoint.code);
  57. var ll = proj4.transform(proj, proj4.WGS84, proj4.toPoint(testPoint.xy));
  58. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'lng is close');
  59. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'lat is close');
  60. });
  61. });
  62. describe('new method 2 param', function() {
  63. it('shortcut method should work with an array', function() {
  64. var xy = proj4(testPoint.code, testPoint.ll);
  65. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  66. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  67. });
  68. it('shortcut method should work with an object', function() {
  69. var pt = {
  70. x: testPoint.ll[0],
  71. y: testPoint.ll[1]
  72. };
  73. var xy = proj4(testPoint.code, pt);
  74. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  75. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  76. });
  77. it('shortcut method should work with a point object', function() {
  78. var pt = proj4.toPoint(testPoint.ll);
  79. var xy = proj4(testPoint.code, pt);
  80. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  81. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  82. });
  83. });
  84. describe('new method 3 param', function() {
  85. it('shortcut method should work with an array', function() {
  86. var xy = proj4(proj4.WGS84, testPoint.code, testPoint.ll);
  87. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  88. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  89. });
  90. it('shortcut method should work with an object', function() {
  91. var pt = {
  92. x: testPoint.ll[0],
  93. y: testPoint.ll[1]
  94. };
  95. var xy = proj4(proj4.WGS84, testPoint.code, pt);
  96. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  97. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  98. });
  99. it('shortcut method should work with a point object', function() {
  100. var pt = proj4.toPoint(testPoint.ll);
  101. var xy = proj4(proj4.WGS84, testPoint.code, pt);
  102. assert.closeTo(xy.x, testPoint.xy[0], xyEPSLN, 'x is close');
  103. assert.closeTo(xy.y, testPoint.xy[1], xyEPSLN, 'y is close');
  104. });
  105. });
  106. describe('new method 3 param other way', function() {
  107. it('shortcut method should work with an array', function() {
  108. var ll = proj4(testPoint.code, proj4.WGS84, testPoint.xy);
  109. assert.closeTo(ll[0], testPoint.ll[0], llEPSLN, 'x is close');
  110. assert.closeTo(ll[1], testPoint.ll[1], llEPSLN, 'y is close');
  111. });
  112. it('shortcut method should work with an object', function() {
  113. var pt = {
  114. x: testPoint.xy[0],
  115. y: testPoint.xy[1]
  116. };
  117. // in case of geocentric proj we need Z value.
  118. if (typeof testPoint.xy[2] === 'number') {
  119. pt.z = testPoint.xy[2]
  120. }
  121. var ll = proj4(testPoint.code, proj4.WGS84, pt);
  122. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  123. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  124. });
  125. it('shortcut method should work with a point object', function() {
  126. var pt = proj4.toPoint(testPoint.xy);
  127. var ll = proj4(testPoint.code, proj4.WGS84, pt);
  128. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  129. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  130. });
  131. });
  132. describe('1 param', function() {
  133. it('forwards', function() {
  134. var xy = proj4(testPoint.code).forward(testPoint.ll);
  135. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  136. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  137. });
  138. it('inverse', function() {
  139. var ll = proj4(testPoint.code).inverse(testPoint.xy);
  140. assert.closeTo(ll[0], testPoint.ll[0], llEPSLN, 'x is close');
  141. assert.closeTo(ll[1], testPoint.ll[1], llEPSLN, 'y is close');
  142. });
  143. });
  144. describe('proj object', function() {
  145. it('should work with a 2 element array', function() {
  146. var xy = proj4(new proj4.Proj(testPoint.code), testPoint.ll);
  147. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  148. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  149. });
  150. it('should work on element', function() {
  151. var xy = proj4(new proj4.Proj(testPoint.code)).forward(testPoint.ll);
  152. assert.closeTo(xy[0], testPoint.xy[0], xyEPSLN, 'x is close');
  153. assert.closeTo(xy[1], testPoint.xy[1], xyEPSLN, 'y is close');
  154. });
  155. it('should work 3 element point object', function() {
  156. var pt = proj4.toPoint(testPoint.xy);
  157. var ll = proj4(new proj4.Proj(testPoint.code), proj4.WGS84, pt);
  158. assert.closeTo(ll.x, testPoint.ll[0], llEPSLN, 'x is close');
  159. assert.closeTo(ll.y, testPoint.ll[1], llEPSLN, 'y is close');
  160. });
  161. });
  162. describe('proj coord object', function() {
  163. it('should not be modified', function() {
  164. var expected = {x: 100000, y: 100000};
  165. var inpxy = {x: expected.x, y: expected.y};
  166. proj4('EPSG:3857', proj4.WGS84, inpxy);
  167. assert.deepEqual(inpxy, expected, "input is unmodified");
  168. });
  169. });
  170. });
  171. });
  172. });
  173. describe('points', function () {
  174. it('should not create a z if none was provided', function() {
  175. const result = proj4(
  176. 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]',
  177. 'PROJCS["OSGB 1936 / British National Grid",GEOGCS["OSGB 1936",DATUM["OSGB_1936",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],AUTHORITY["EPSG","6277"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4277"]],UNIT["metre",1,AUTHORITY["EPSG","9001"]],PROJECTION["Transverse_Mercator"],PARAMETER["latitude_of_origin",49],PARAMETER["central_meridian",-2],PARAMETER["scale_factor",0.9996012717],PARAMETER["false_easting",400000],PARAMETER["false_northing",-100000],AUTHORITY["EPSG","27700"],AXIS["Easting",EAST],AXIS["Northing",NORTH]]',
  178. {x: -0.12793738, y: 51.507747});
  179. assert.closeTo(result.x, 530018.229301635, 1e-6);
  180. assert.closeTo(result.y, 180418.4380560551, 1e-6);
  181. assert.equal(result.z, undefined);
  182. });
  183. it('should ignore stuff it does not know', function () {
  184. var sweref99tm = '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs';
  185. 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';
  186. var rslt = proj4(sweref99tm, rt90).forward({
  187. x: 319180,
  188. y: 6399862,
  189. z: 0,
  190. m: 1000,
  191. method: function () {
  192. return 'correct answer';
  193. }
  194. });
  195. assert.closeTo(rslt.x, 1271137.927561178, 0.000001);
  196. assert.closeTo(rslt.y, 6404230.291456626, 0.000001);
  197. assert.equal(rslt.z, 0);
  198. assert.equal(rslt.m, 1000);
  199. assert.equal(rslt.method(), 'correct answer');
  200. });
  201. it('should be able to compute X Y Z M in geocenteric coordinates', function () {
  202. var epsg4978 = '+proj=geocent +datum=WGS84 +units=m +no_defs';
  203. var rslt = proj4(epsg4978).forward({
  204. x: -7.76166,
  205. y: 39.19685,
  206. z: 0,
  207. m: 1000,
  208. method: function () {
  209. return 'correct answer';
  210. }
  211. });
  212. assert.closeTo(rslt.x, 4904199.584207411, 0.000001);
  213. assert.closeTo(rslt.y, -668448.8153664203, 0.000001);
  214. assert.closeTo(rslt.z, 4009276.930771821, 0.000001);
  215. assert.equal(rslt.m, 1000);
  216. assert.equal(rslt.method(), 'correct answer');
  217. });
  218. });
  219. describe('points array', function () {
  220. it('should ignore stuff it does not know', function () {
  221. var sweref99tm = '+proj=utm +zone=33 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs';
  222. 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';
  223. var rslt = proj4(sweref99tm, rt90).forward([
  224. 319180,
  225. 6399862,
  226. 0,
  227. 1000,
  228. ]);
  229. assert.closeTo(rslt[0], 1271137.927561178, 0.000001);
  230. assert.closeTo(rslt[1], 6404230.291456626, 0.000001);
  231. assert.equal(rslt[2], 0);
  232. assert.equal(rslt[3], 1000);
  233. });
  234. it('should be able to compute X Y Z M in geocenteric coordinates', function () {
  235. var epsg4978 = '+proj=geocent +datum=WGS84 +units=m +no_defs';
  236. var rslt = proj4(epsg4978).forward([
  237. -7.76166,
  238. 39.19685,
  239. 0,
  240. 1000
  241. ]);
  242. assert.closeTo(rslt[0], 4904199.584207411, 0.000001);
  243. assert.closeTo(rslt[1], -668448.8153664203, 0.000001);
  244. assert.closeTo(rslt[2], 4009276.930771821, 0.000001);
  245. assert.equal(rslt[3], 1000);
  246. });
  247. });
  248. it('should use [x,y] axis order', function() {
  249. var enu = 'PROJCS["NAD83 / Massachusetts Mainland", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Lambert_Conformal_Conic_2SP", AUTHORITY["EPSG","9802"]], PARAMETER["central_meridian", -71.5], PARAMETER["latitude_of_origin", 41.0], PARAMETER["standard_parallel_1", 42.68333333333334], PARAMETER["false_easting", 200000.0], PARAMETER["false_northing", 750000.0], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 41.71666666666667], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26986"]]';
  250. var neu = 'PROJCS["NAD83 / Massachusetts Mainland NE", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic latitude", NORTH], AXIS["Geodetic longitude", EAST], AUTHORITY["EPSG","4269"]], PROJECTION["Lambert_Conformal_Conic_2SP", AUTHORITY["EPSG","9802"]], PARAMETER["central_meridian", -71.5], PARAMETER["latitude_of_origin", 41.0], PARAMETER["standard_parallel_1", 42.68333333333334], PARAMETER["false_easting", 200000.0], PARAMETER["false_northing", 750000.0], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 41.71666666666667], UNIT["m", 1.0], AXIS["Northing", NORTH], AXIS["Easting", EAST], AUTHORITY["EPSG","26986"]]';
  251. var rslt = proj4(enu, neu).forward({
  252. x: 10.2,
  253. y: 43.4
  254. });
  255. assert.closeTo(rslt.x, 10.2, 0.000001);
  256. assert.closeTo(rslt.y, 43.4, 0.000001);
  257. });
  258. it('should use correct axis order with proj4.transform()', function() {
  259. var enu = 'PROJCS["NAD83 / Massachusetts Mainland", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic longitude", EAST], AXIS["Geodetic latitude", NORTH], AUTHORITY["EPSG","4269"]], PROJECTION["Lambert_Conformal_Conic_2SP", AUTHORITY["EPSG","9802"]], PARAMETER["central_meridian", -71.5], PARAMETER["latitude_of_origin", 41.0], PARAMETER["standard_parallel_1", 42.68333333333334], PARAMETER["false_easting", 200000.0], PARAMETER["false_northing", 750000.0], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 41.71666666666667], UNIT["m", 1.0], AXIS["Easting", EAST], AXIS["Northing", NORTH], AUTHORITY["EPSG","26986"]]';
  260. var neu = 'PROJCS["NAD83 / Massachusetts Mainland NE", GEOGCS["NAD83", DATUM["North American Datum 1983", SPHEROID["GRS 1980", 6378137.0, 298.257222101, AUTHORITY["EPSG","7019"]], TOWGS84[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], AUTHORITY["EPSG","6269"]], PRIMEM["Greenwich", 0.0, AUTHORITY["EPSG","8901"]], UNIT["degree", 0.017453292519943295], AXIS["Geodetic latitude", NORTH], AXIS["Geodetic longitude", EAST], AUTHORITY["EPSG","4269"]], PROJECTION["Lambert_Conformal_Conic_2SP", AUTHORITY["EPSG","9802"]], PARAMETER["central_meridian", -71.5], PARAMETER["latitude_of_origin", 41.0], PARAMETER["standard_parallel_1", 42.68333333333334], PARAMETER["false_easting", 200000.0], PARAMETER["false_northing", 750000.0], PARAMETER["scale_factor", 1.0], PARAMETER["standard_parallel_2", 41.71666666666667], UNIT["m", 1.0], AXIS["Northing", NORTH], AXIS["Easting", EAST], AUTHORITY["EPSG","26986"]]';
  261. var rslt = proj4(enu, neu).forward({
  262. x: 10.2,
  263. y: 43.4
  264. }, true);
  265. assert.closeTo(rslt.x, 43.4, 0.000001);
  266. assert.closeTo(rslt.y, 10.2, 0.000001);
  267. });
  268. it('axes should be invertable with proj4.transform()', function () {
  269. var enu = '+proj=longlat +axis=enu';
  270. var esu = '+proj=longlat +axis=esu';
  271. var wnu = '+proj=longlat +axis=wnu';
  272. var result = proj4(enu, esu).forward({x: 40, y: 50}, true);
  273. assert.closeTo(result.x, 40, 0.000001);
  274. assert.closeTo(result.y, -50, 0.000001);
  275. var result = proj4(enu, wnu).forward({x: 40, y: 50}, true);
  276. assert.closeTo(result.x, -40, 0.000001);
  277. assert.closeTo(result.y, 50, 0.000001);
  278. });
  279. describe('defs', function () {
  280. assert.equal(proj4.defs('testmerc'), proj4.defs['testmerc']);
  281. proj4.defs('foo', '+proj=merc +lon_0=5.937 +lat_ts=45.027 +ellps=sphere');
  282. assert.typeOf(proj4.defs['foo'], 'object');
  283. proj4.defs('urn:x-ogc:def:crs:EPSG:4326', proj4.defs('EPSG:4326'));
  284. assert.strictEqual(proj4.defs['urn:x-ogc:def:crs:EPSG:4326'], proj4.defs['EPSG:4326']);
  285. describe('wkt', function () {
  286. it('should provide the correct conversion factor for WKT GEOGCS projections', function () {
  287. proj4.defs('EPSG:4269', 'GEOGCS["NAD83",DATUM["North_American_Datum_1983",SPHEROID["GRS 1980",6378137,298.257222101,AUTHORITY["EPSG","7019"]],AUTHORITY["EPSG","6269"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4269"]]');
  288. assert.equal(proj4.defs['EPSG:4269'].to_meter, 6378137 * 0.01745329251994328);
  289. proj4.defs('EPSG:4279', 'GEOGCS["OS(SN)80",DATUM["OS_SN_1980",SPHEROID["Airy 1830",6377563.396,299.3249646,AUTHORITY["EPSG","7001"]],AUTHORITY["EPSG","6279"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.01745329251994328,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4279"]]');
  290. assert.equal(proj4.defs['EPSG:4279'].to_meter, 6377563.396 * 0.01745329251994328);
  291. });
  292. it('should parse wkt and proj4 of the same crs and result in the same params', function () {
  293. var s1 = 'GEOGCS["PSD93",DATUM["PDO_Survey_Datum_1993",SPHEROID["Clarke 1880 (RGS)",6378249.145,293.465,AUTHORITY["EPSG","7012"]],TOWGS84[-180.624,-225.516,173.919,-0.81,-1.898,8.336,16.7101],AUTHORITY["EPSG","6134"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4134"]]';
  294. var s2 = '+proj=longlat +ellps=clrk80 +towgs84=-180.624,-225.516,173.919,-0.81,-1.898,8.336,16.7101 +no_defs';
  295. var crs1 = proj4(s1);
  296. var crs2 = proj4(s2);
  297. assert.equal(crs1.oProj.a, crs2.oProj.a);
  298. // proj4 has different ellipsoid parameters that EPSG: http://epsg.io/4134
  299. // assert.equal(crs1.oProj.b, crs2.oProj.b);
  300. });
  301. it('should handled defined points correctly', function () {
  302. var prj = '+proj=utm +zone=31';
  303. var proj = proj4(prj);
  304. var res = proj.forward([3, 0]);
  305. assert.deepEqual(res, [500000, 0]);
  306. });
  307. });
  308. });
  309. describe('errors', function() {
  310. it('should throw an error for an unknown ref', function() {
  311. assert.throws(function() {
  312. new proj4.Proj('fake one');
  313. }, 'fake one', 'should work');
  314. });
  315. it('should throw when passed null', function() {
  316. assert.throws(function() {
  317. proj4('+proj=utm +zone=31', [null, 0]);
  318. }, 'coordinates must be finite numbers', 'should work');
  319. });
  320. it('should throw when passed NaN', function() {
  321. assert.throws(function() {
  322. proj4('+proj=utm +zone=31', [0, NaN]);
  323. }, 'coordinates must be finite numbers', 'should work');
  324. });
  325. it('should throw when passed Infinity', function() {
  326. assert.throws(function() {
  327. proj4('+proj=utm +zone=31', [Infinity, 0]);
  328. }, 'coordinates must be finite numbers', 'should work');
  329. });
  330. it('should throw when passed -Infinity', function() {
  331. assert.throws(function() {
  332. proj4('+proj=utm +zone=31', [-Infinity, 0]);
  333. }, 'coordinates must be finite numbers', 'should work');
  334. });
  335. });
  336. describe('utility', function() {
  337. it('should have MGRS available in the proj4.util namespace', function() {
  338. assert.typeOf(proj4.mgrs, "object", "MGRS available in the proj4.util namespace");
  339. });
  340. it('should have fromMGRS method added to proj4.Point prototype', function() {
  341. assert.typeOf(proj4.Point.fromMGRS, "function", "fromMGRS method added to proj4.Point prototype");
  342. });
  343. it('should have toMGRS method added to proj4.Point prototype', function() {
  344. assert.typeOf(proj4.Point.prototype.toMGRS, "function", "toMGRS method added to proj4.Point prototype");
  345. });
  346. describe('First MGRS set', function() {
  347. var mgrs = "33UXP04";
  348. var point = proj4.Point.fromMGRS(mgrs);
  349. it('Longitude of point from MGRS correct.', function() {
  350. assert.equal(point.x.toPrecision(7), "16.41450", "Longitude of point from MGRS correct.");
  351. });
  352. it('Latitude of point from MGRS correct.', function() {
  353. assert.equal(point.y.toPrecision(7), "48.24949", "Latitude of point from MGRS correct.");
  354. });
  355. it('MGRS reference with highest accuracy correct.', function() {
  356. assert.equal(point.toMGRS(), "33UXP0500444998", "MGRS reference with highest accuracy correct.");
  357. });
  358. it('MGRS reference with 1-digit accuracy correct.', function() {
  359. assert.equal(point.toMGRS(1), mgrs, "MGRS reference with 1-digit accuracy correct.");
  360. });
  361. });
  362. describe('Second MGRS set', function() {
  363. var mgrs = "24XWT783908"; // near UTM zone border, so there are two ways to reference this
  364. var point = proj4.Point.fromMGRS(mgrs);
  365. it("Longitude of point from MGRS correct.", function() {
  366. assert.equal(point.x.toPrecision(7), "-32.66433", "Longitude of point from MGRS correct.");
  367. });
  368. it("Latitude of point from MGRS correct.", function() {
  369. assert.equal(point.y.toPrecision(7), "83.62778", "Latitude of point from MGRS correct.");
  370. });
  371. it("MGRS reference with 3-digit accuracy correct.", function() {
  372. assert.equal(point.toMGRS(3), "25XEN041865", "MGRS reference with 3-digit accuracy correct.");
  373. });
  374. });
  375. describe('Defs and Datum definition', function() {
  376. 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");
  377. var point = proj4.transform(proj4.Proj("WGS84"), proj4.Proj("EPSG:5514"),
  378. proj4.toPoint([12.806988, 49.452262]));
  379. it("Longitude of point from WGS84 correct.", function() {
  380. assert.equal(point.x.toPrecision(8), "-868208.61", "Longitude of point from WGS84 correct.");
  381. });
  382. it("Latitude of point from WGS84 correct.", function() {
  383. assert.equal(point.y.toPrecision(9), "-1095793.64", "Latitude of point from WGS84 correct.");
  384. });
  385. var point2 = proj4.transform(proj4.Proj("WGS84"), proj4.Proj("EPSG:5514"),
  386. proj4.toPoint([12.806988, 49.452262]));
  387. it("Longitude of point from WGS84 with second call for EPSG:5514 correct.", function() {
  388. assert.equal(point2.x.toPrecision(8), "-868208.61", "Longitude of point from WGS84 correct.");
  389. });
  390. it("Latitude of point from WGS84 with second call for EPSG:5514 correct.", function() {
  391. assert.equal(point2.y.toPrecision(9), "-1095793.64", "Latitude of point from WGS84 correct.");
  392. });
  393. });
  394. });
  395. describe('Nadgrids BETA2007', function() {
  396. var tests = [
  397. ['EPSG:31466', 'EPSG:4326', 2559552, 5670982, 6.850861772, 51.170707759, 0.0000001, 0.01],
  398. ['EPSG:31466', 'EPSG:3857', 2559552, 5670982, 762634.443931574, 6651545.680265270, 0.01, 0.01],
  399. ['EPSG:31466', 'EPSG:25832', 2559552, 5670982, 349757.381712518, 5671004.065049540, 0.01, 0.01],
  400. ];
  401. function initializeNadgrid(buffer) {
  402. proj4.nadgrid('BETA2007.gsb', buffer);
  403. proj4.defs('EPSG:31466', '+proj=tmerc +lat_0=0 +lon_0=6 +k=1 +x_0=2500000 +y_0=0 +ellps=bessel +nadgrids=BETA2007.gsb +units=m +no_defs +type=crs');
  404. proj4.defs('EPSG:25832', '+proj=utm +zone=32 +ellps=GRS80 +towgs84=0,0,0,0,0,0,0 +units=m +no_defs +type=crs');
  405. }
  406. before(function(done) {
  407. if (typeof XMLHttpRequest !== 'undefined') {
  408. const xhr = new XMLHttpRequest();
  409. xhr.open('GET', 'BETA2007.gsb', true);
  410. xhr.responseType = 'arraybuffer';
  411. xhr.addEventListener('load', function() {
  412. initializeNadgrid(xhr.response);
  413. done();
  414. });
  415. xhr.addEventListener('error', done);
  416. xhr.send();
  417. } else if (typeof require === 'function') {
  418. const fs = require('fs');
  419. const path = require('path');
  420. fs.readFile(path.join(__dirname, 'BETA2007.gsb'), function(err, data) {
  421. if (err) {
  422. done(err);
  423. } else {
  424. initializeNadgrid(data.buffer);
  425. done();
  426. }
  427. })
  428. }
  429. });
  430. tests.forEach(function(test) {
  431. var fromProj = test[0];
  432. var toProj = test[1];
  433. var fromX = test[2];
  434. var fromY = test[3];
  435. var toX = test[4];
  436. var toY = test[5];
  437. var fromPrecision = test[6];
  438. var toPrecision = test[7];
  439. it('should transform ' + fromProj + ' to ' + toProj, function () {
  440. var transformed = proj4(fromProj, toProj, [fromX, fromY]);
  441. assert.approximately(transformed[0], toX, fromPrecision);
  442. assert.approximately(transformed[1], toY, fromPrecision);
  443. });
  444. it('should transform ' + toProj + ' to ' + fromProj, function () {
  445. var transformed = proj4(toProj, fromProj, [toX, toY]);
  446. assert.approximately(transformed[0], fromX, toPrecision);
  447. assert.approximately(transformed[1], fromY, toPrecision);
  448. });
  449. });
  450. });
  451. describe('Nadgrids ntv2', function() {
  452. var tests = [
  453. [-44.382211538462, 40.3768, -44.380749, 40.377457], // just inside the lower limit
  454. [-87.617788, 59.623262, -87.617659, 59.623441], // just inside the upper limit
  455. [-44.5, 40.5, -44.498553, 40.500632], // inside the first square
  456. [-60, 50, -59.999192, 50.000058], // a general point towards the middle of the grid
  457. [0, 0, 0, 0] // fall back to null
  458. ];
  459. var converter;
  460. function initializeNadgrid(buffer) {
  461. proj4.nadgrid('ntv2', buffer);
  462. proj4.defs('ntv2_from', '+proj=longlat +ellps=clrk66 +nadgrids=@ignorable,ntv2,null');
  463. proj4.defs('ntv2_to', '+proj=longlat +datum=WGS84 +no_defs');
  464. converter = proj4('ntv2_from', 'ntv2_to');
  465. }
  466. before(function(done) {
  467. if (typeof XMLHttpRequest !== 'undefined') {
  468. const xhr = new XMLHttpRequest();
  469. xhr.open('GET', 'ntv2_0_downsampled.gsb', true);
  470. xhr.responseType = 'arraybuffer';
  471. xhr.addEventListener('load', function() {
  472. initializeNadgrid(xhr.response);
  473. done();
  474. });
  475. xhr.addEventListener('error', done);
  476. xhr.send();
  477. } else if (typeof require === 'function') {
  478. const fs = require('fs');
  479. const path = require('path');
  480. fs.readFile(path.join(__dirname, 'ntv2_0_downsampled.gsb'), function(err, data) {
  481. if (err) {
  482. done(err);
  483. } else {
  484. initializeNadgrid(data.buffer);
  485. done();
  486. }
  487. })
  488. }
  489. });
  490. tests.forEach(function(test) {
  491. var fromLng = test[0];
  492. var fromLat = test[1];
  493. var toLng = test[2];
  494. var toLat = test[3];
  495. it('should interpolate ' + [fromLng, fromLat] + ' to ' + [toLng, toLat], function () {
  496. var actual = converter.forward([fromLng, fromLat]);
  497. assert.approximately(actual[0], toLng, 0.000001);
  498. assert.approximately(actual[1], toLat, 0.000001);
  499. });
  500. });
  501. var inverseTests = [
  502. [-44.5, 40.5, -44.498553, 40.500632],
  503. [-60, 50, -59.999192, 50.000058]
  504. ];
  505. inverseTests.forEach(function(test) {
  506. var fromLng = test[0];
  507. var fromLat = test[1];
  508. var toLng = test[2];
  509. var toLat = test[3];
  510. it('should inverse interpolate ' + [toLng, toLat] + ' to ' + [fromLng, fromLat], function () {
  511. var actual = converter.inverse([toLng, toLat]);
  512. assert.approximately(actual[0], fromLng, 0.000001);
  513. assert.approximately(actual[1], fromLat, 0.000001);
  514. });
  515. });
  516. });
  517. });
  518. }
  519. if(typeof process !== 'undefined'&&process.toString() === '[object process]'){
  520. (function(){
  521. startTests(require('chai'), require('../dist/proj4-src'), require('./testData'));
  522. })();
  523. }