parseCode.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import defs from './defs';
  2. import wkt from 'wkt-parser';
  3. import projStr from './projString';
  4. import match from './match';
  5. function testObj(code){
  6. return typeof code === 'string';
  7. }
  8. function testDef(code){
  9. return code in defs;
  10. }
  11. var codeWords = ['PROJECTEDCRS', 'PROJCRS', 'GEOGCS','GEOCCS','PROJCS','LOCAL_CS', 'GEODCRS', 'GEODETICCRS', 'GEODETICDATUM', 'ENGCRS', 'ENGINEERINGCRS'];
  12. function testWKT(code){
  13. return codeWords.some(function (word) {
  14. return code.indexOf(word) > -1;
  15. });
  16. }
  17. var codes = ['3857', '900913', '3785', '102113'];
  18. function checkMercator(item) {
  19. var auth = match(item, 'authority');
  20. if (!auth) {
  21. return;
  22. }
  23. var code = match(auth, 'epsg');
  24. return code && codes.indexOf(code) > -1;
  25. }
  26. function checkProjStr(item) {
  27. var ext = match(item, 'extension');
  28. if (!ext) {
  29. return;
  30. }
  31. return match(ext, 'proj4');
  32. }
  33. function testProj(code){
  34. return code[0] === '+';
  35. }
  36. function parse(code){
  37. if (testObj(code)) {
  38. //check to see if this is a WKT string
  39. if (testDef(code)) {
  40. return defs[code];
  41. }
  42. if (testWKT(code)) {
  43. var out = wkt(code);
  44. // test of spetial case, due to this being a very common and often malformed
  45. if (checkMercator(out)) {
  46. return defs['EPSG:3857'];
  47. }
  48. var maybeProjStr = checkProjStr(out);
  49. if (maybeProjStr) {
  50. return projStr(maybeProjStr);
  51. }
  52. return out;
  53. }
  54. if (testProj(code)) {
  55. return projStr(code);
  56. }
  57. }else{
  58. return code;
  59. }
  60. }
  61. export default parse;