terraformer-wkt-parser.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766
  1. (function (root, factory) {
  2. // Node.
  3. if(typeof module === 'object' && typeof module.exports === 'object') {
  4. exports = module.exports = factory(require("terraformer"));
  5. } else if(typeof navigator === "object") {
  6. // Browser Global.
  7. if (!root.Terraformer){
  8. throw new Error("Terraformer.WKT requires the core Terraformer library. http://github.com/esri/terraformer")
  9. }
  10. root.Terraformer.WKT = factory(root.Terraformer);
  11. }
  12. }(this, function(Terraformer) {
  13. var exports = { };
  14. /* Jison generated parser */
  15. var parser = (function(){
  16. var parser = {trace: function trace () { },
  17. yy: {},
  18. symbols_: {"error":2,"expressions":3,"point":4,"EOF":5,"linestring":6,"polygon":7,"multipoint":8,"multilinestring":9,"multipolygon":10,"coordinate":11,"DOUBLE_TOK":12,"ptarray":13,"COMMA":14,"ring_list":15,"ring":16,"(":17,")":18,"POINT":19,"Z":20,"ZM":21,"M":22,"EMPTY":23,"point_untagged":24,"polygon_list":25,"polygon_untagged":26,"point_list":27,"LINESTRING":28,"POLYGON":29,"MULTIPOINT":30,"MULTILINESTRING":31,"MULTIPOLYGON":32,"$accept":0,"$end":1},
  19. terminals_: {2:"error",5:"EOF",12:"DOUBLE_TOK",14:"COMMA",17:"(",18:")",19:"POINT",20:"Z",21:"ZM",22:"M",23:"EMPTY",28:"LINESTRING",29:"POLYGON",30:"MULTIPOINT",31:"MULTILINESTRING",32:"MULTIPOLYGON"},
  20. productions_: [0,[3,2],[3,2],[3,2],[3,2],[3,2],[3,2],[11,2],[11,3],[11,4],[13,3],[13,1],[15,3],[15,1],[16,3],[4,4],[4,5],[4,5],[4,5],[4,2],[24,1],[24,3],[25,3],[25,1],[26,3],[27,3],[27,1],[6,4],[6,5],[6,5],[6,5],[6,2],[7,4],[7,5],[7,5],[7,5],[7,2],[8,4],[8,5],[8,5],[8,5],[8,2],[9,4],[9,5],[9,5],[9,5],[9,2],[10,4],[10,5],[10,5],[10,5],[10,2]],
  21. performAction: function anonymous(yytext,yyleng,yylineno,yy,yystate,$,_$
  22. ) {
  23. var $0 = $.length - 1;
  24. switch (yystate) {
  25. case 1: return $[$0-1];
  26. break;
  27. case 2: return $[$0-1];
  28. break;
  29. case 3: return $[$0-1];
  30. break;
  31. case 4: return $[$0-1];
  32. break;
  33. case 5: return $[$0-1];
  34. break;
  35. case 6: return $[$0-1];
  36. break;
  37. case 7: this.$ = new PointArray([ Number($[$0-1]), Number($[$0]) ]);
  38. break;
  39. case 8: this.$ = new PointArray([ Number($[$0-2]), Number($[$0-1]), Number($[$0]) ]);
  40. break;
  41. case 9: this.$ = new PointArray([ Number($[$0-3]), Number($[$0-2]), Number($[$0-1]), Number($[$0]) ]);
  42. break;
  43. case 10: this.$ = $[$0-2].addPoint($[$0]);
  44. break;
  45. case 11: this.$ = $[$0];
  46. break;
  47. case 12: this.$ = $[$0-2].addRing($[$0]);
  48. break;
  49. case 13: this.$ = new RingList($[$0]);
  50. break;
  51. case 14: this.$ = new Ring($[$0-1]);
  52. break;
  53. case 15: this.$ = { "type": "Point", "coordinates": $[$0-1].data[0] };
  54. break;
  55. case 16: this.$ = { "type": "Point", "coordinates": $[$0-1].data[0], "properties": { z: true } };
  56. break;
  57. case 17: this.$ = { "type": "Point", "coordinates": $[$0-1].data[0], "properties": { z: true, m: true } };
  58. break;
  59. case 18: this.$ = { "type": "Point", "coordinates": $[$0-1].data[0], "properties": { m: true } };
  60. break;
  61. case 19: this.$ = { "type": "Point", "coordinates": [ ] };
  62. break;
  63. case 20: this.$ = $[$0];
  64. break;
  65. case 21: this.$ = $[$0-1];
  66. break;
  67. case 22: this.$ = $[$0-2].addPolygon($[$0]);
  68. break;
  69. case 23: this.$ = new PolygonList($[$0]);
  70. break;
  71. case 24: this.$ = $[$0-1];
  72. break;
  73. case 25: this.$ = $[$0-2].addPoint($[$0]);
  74. break;
  75. case 26: this.$ = $[$0];
  76. break;
  77. case 27: this.$ = { "type": "LineString", "coordinates": $[$0-1].data };
  78. break;
  79. case 28: this.$ = { "type": "LineString", "coordinates": $[$0-1].data, "properties": { z: true } };
  80. break;
  81. case 29: this.$ = { "type": "LineString", "coordinates": $[$0-1].data, "properties": { m: true } };
  82. break;
  83. case 30: this.$ = { "type": "LineString", "coordinates": $[$0-1].data, "properties": { z: true, m: true } };
  84. break;
  85. case 31: this.$ = { "type": "LineString", "coordinates": [ ] };
  86. break;
  87. case 32: this.$ = { "type": "Polygon", "coordinates": $[$0-1].toJSON() };
  88. break;
  89. case 33: this.$ = { "type": "Polygon", "coordinates": $[$0-1].toJSON(), "properties": { z: true } };
  90. break;
  91. case 34: this.$ = { "type": "Polygon", "coordinates": $[$0-1].toJSON(), "properties": { m: true } };
  92. break;
  93. case 35: this.$ = { "type": "Polygon", "coordinates": $[$0-1].toJSON(), "properties": { z: true, m: true } };
  94. break;
  95. case 36: this.$ = { "type": "Polygon", "coordinates": [ ] };
  96. break;
  97. case 37: this.$ = { "type": "MultiPoint", "coordinates": $[$0-1].data };
  98. break;
  99. case 38: this.$ = { "type": "MultiPoint", "coordinates": $[$0-1].data, "properties": { z: true } };
  100. break;
  101. case 39: this.$ = { "type": "MultiPoint", "coordinates": $[$0-1].data, "properties": { m: true } };
  102. break;
  103. case 40: this.$ = { "type": "MultiPoint", "coordinates": $[$0-1].data, "properties": { z: true, m: true } };
  104. break;
  105. case 41: this.$ = { "type": "MultiPoint", "coordinates": [ ] }
  106. break;
  107. case 42: this.$ = { "type": "MultiLineString", "coordinates": $[$0-1].toJSON() };
  108. break;
  109. case 43: this.$ = { "type": "MultiLineString", "coordinates": $[$0-1].toJSON(), "properties": { z: true } };
  110. break;
  111. case 44: this.$ = { "type": "MultiLineString", "coordinates": $[$0-1].toJSON(), "properties": { m: true } };
  112. break;
  113. case 45: this.$ = { "type": "MultiLineString", "coordinates": $[$0-1].toJSON(), "properties": { z: true, m: true } };
  114. break;
  115. case 46: this.$ = { "type": "MultiLineString", "coordinates": [ ] };
  116. break;
  117. case 47: this.$ = { "type": "MultiPolygon", "coordinates": $[$0-1].toJSON() };
  118. break;
  119. case 48: this.$ = { "type": "MultiPolygon", "coordinates": $[$0-1].toJSON(), "properties": { z: true } };
  120. break;
  121. case 49: this.$ = { "type": "MultiPolygon", "coordinates": $[$0-1].toJSON(), "properties": { m: true } };
  122. break;
  123. case 50: this.$ = { "type": "MultiPolygon", "coordinates": $[$0-1].toJSON(), "properties": { z: true, m: true } };
  124. break;
  125. case 51: this.$ = { "type": "MultiPolygon", "coordinates": [ ] };
  126. break;
  127. }
  128. },
  129. table: [{3:1,4:2,6:3,7:4,8:5,9:6,10:7,19:[1,8],28:[1,9],29:[1,10],30:[1,11],31:[1,12],32:[1,13]},{1:[3]},{5:[1,14]},{5:[1,15]},{5:[1,16]},{5:[1,17]},{5:[1,18]},{5:[1,19]},{17:[1,20],20:[1,21],21:[1,22],22:[1,23],23:[1,24]},{17:[1,25],20:[1,26],21:[1,28],22:[1,27],23:[1,29]},{17:[1,30],20:[1,31],21:[1,33],22:[1,32],23:[1,34]},{17:[1,35],20:[1,36],21:[1,38],22:[1,37],23:[1,39]},{17:[1,40],20:[1,41],21:[1,43],22:[1,42],23:[1,44]},{17:[1,45],20:[1,46],21:[1,48],22:[1,47],23:[1,49]},{1:[2,1]},{1:[2,2]},{1:[2,3]},{1:[2,4]},{1:[2,5]},{1:[2,6]},{11:51,12:[1,52],13:50},{17:[1,53]},{17:[1,54]},{17:[1,55]},{5:[2,19]},{11:58,12:[1,52],17:[1,59],24:57,27:56},{17:[1,60]},{17:[1,61]},{17:[1,62]},{5:[2,31]},{15:63,16:64,17:[1,65]},{17:[1,66]},{17:[1,67]},{17:[1,68]},{5:[2,36]},{11:58,12:[1,52],17:[1,59],24:57,27:69},{17:[1,70]},{17:[1,71]},{17:[1,72]},{5:[2,41]},{15:73,16:64,17:[1,65]},{17:[1,74]},{17:[1,75]},{17:[1,76]},{5:[2,46]},{17:[1,79],25:77,26:78},{17:[1,80]},{17:[1,81]},{17:[1,82]},{5:[2,51]},{14:[1,84],18:[1,83]},{14:[2,11],18:[2,11]},{12:[1,85]},{11:51,12:[1,52],13:86},{11:51,12:[1,52],13:87},{11:51,12:[1,52],13:88},{14:[1,90],18:[1,89]},{14:[2,26],18:[2,26]},{14:[2,20],18:[2,20]},{11:91,12:[1,52]},{11:58,12:[1,52],17:[1,59],24:57,27:92},{11:58,12:[1,52],17:[1,59],24:57,27:93},{11:58,12:[1,52],17:[1,59],24:57,27:94},{14:[1,96],18:[1,95]},{14:[2,13],18:[2,13]},{11:51,12:[1,52],13:97},{15:98,16:64,17:[1,65]},{15:99,16:64,17:[1,65]},{15:100,16:64,17:[1,65]},{14:[1,90],18:[1,101]},{11:58,12:[1,52],17:[1,59],24:57,27:102},{11:58,12:[1,52],17:[1,59],24:57,27:103},{11:58,12:[1,52],17:[1,59],24:57,27:104},{14:[1,96],18:[1,105]},{15:106,16:64,17:[1,65]},{15:107,16:64,17:[1,65]},{15:108,16:64,17:[1,65]},{14:[1,110],18:[1,109]},{14:[2,23],18:[2,23]},{15:111,16:64,17:[1,65]},{17:[1,79],25:112,26:78},{17:[1,79],25:113,26:78},{17:[1,79],25:114,26:78},{5:[2,15]},{11:115,12:[1,52]},{12:[1,116],14:[2,7],18:[2,7]},{14:[1,84],18:[1,117]},{14:[1,84],18:[1,118]},{14:[1,84],18:[1,119]},{5:[2,27]},{11:58,12:[1,52],17:[1,59],24:120},{18:[1,121]},{14:[1,90],18:[1,122]},{14:[1,90],18:[1,123]},{14:[1,90],18:[1,124]},{5:[2,32]},{16:125,17:[1,65]},{14:[1,84],18:[1,126]},{14:[1,96],18:[1,127]},{14:[1,96],18:[1,128]},{14:[1,96],18:[1,129]},{5:[2,37]},{14:[1,90],18:[1,130]},{14:[1,90],18:[1,131]},{14:[1,90],18:[1,132]},{5:[2,42]},{14:[1,96],18:[1,133]},{14:[1,96],18:[1,134]},{14:[1,96],18:[1,135]},{5:[2,47]},{17:[1,79],26:136},{14:[1,96],18:[1,137]},{14:[1,110],18:[1,138]},{14:[1,110],18:[1,139]},{14:[1,110],18:[1,140]},{14:[2,10],18:[2,10]},{12:[1,141],14:[2,8],18:[2,8]},{5:[2,16]},{5:[2,17]},{5:[2,18]},{14:[2,25],18:[2,25]},{14:[2,21],18:[2,21]},{5:[2,28]},{5:[2,29]},{5:[2,30]},{14:[2,12],18:[2,12]},{14:[2,14],18:[2,14]},{5:[2,33]},{5:[2,34]},{5:[2,35]},{5:[2,38]},{5:[2,39]},{5:[2,40]},{5:[2,43]},{5:[2,44]},{5:[2,45]},{14:[2,22],18:[2,22]},{14:[2,24],18:[2,24]},{5:[2,48]},{5:[2,49]},{5:[2,50]},{14:[2,9],18:[2,9]}],
  130. defaultActions: {14:[2,1],15:[2,2],16:[2,3],17:[2,4],18:[2,5],19:[2,6],24:[2,19],29:[2,31],34:[2,36],39:[2,41],44:[2,46],49:[2,51],83:[2,15],89:[2,27],95:[2,32],101:[2,37],105:[2,42],109:[2,47],117:[2,16],118:[2,17],119:[2,18],122:[2,28],123:[2,29],124:[2,30],127:[2,33],128:[2,34],129:[2,35],130:[2,38],131:[2,39],132:[2,40],133:[2,43],134:[2,44],135:[2,45],138:[2,48],139:[2,49],140:[2,50]},
  131. parseError: function parseError (str, hash) {
  132. throw new Error(str);
  133. },
  134. parse: function parse(input) {
  135. var self = this, stack = [0], vstack = [null], lstack = [], table = this.table, yytext = "", yylineno = 0, yyleng = 0, recovering = 0, TERROR = 2, EOF = 1;
  136. this.lexer.setInput(input);
  137. this.lexer.yy = this.yy;
  138. this.yy.lexer = this.lexer;
  139. this.yy.parser = this;
  140. if (typeof this.lexer.yylloc == "undefined")
  141. this.lexer.yylloc = {};
  142. var yyloc = this.lexer.yylloc;
  143. lstack.push(yyloc);
  144. var ranges = this.lexer.options && this.lexer.options.ranges;
  145. if (typeof this.yy.parseError === "function")
  146. this.parseError = this.yy.parseError;
  147. function popStack(n) {
  148. stack.length = stack.length - 2 * n;
  149. vstack.length = vstack.length - n;
  150. lstack.length = lstack.length - n;
  151. }
  152. function lex() {
  153. var token;
  154. token = self.lexer.lex() || 1;
  155. if (typeof token !== "number") {
  156. token = self.symbols_[token] || token;
  157. }
  158. return token;
  159. }
  160. var symbol, preErrorSymbol, state, action, a, r, yyval = {}, p, len, newState, expected;
  161. while (true) {
  162. state = stack[stack.length - 1];
  163. if (this.defaultActions[state]) {
  164. action = this.defaultActions[state];
  165. } else {
  166. if (symbol === null || typeof symbol == "undefined") {
  167. symbol = lex();
  168. }
  169. action = table[state] && table[state][symbol];
  170. }
  171. if (typeof action === "undefined" || !action.length || !action[0]) {
  172. var errStr = "";
  173. if (!recovering) {
  174. expected = [];
  175. for (p in table[state])
  176. if (this.terminals_[p] && p > 2) {
  177. expected.push("'" + this.terminals_[p] + "'");
  178. }
  179. if (this.lexer.showPosition) {
  180. errStr = "Parse error on line " + (yylineno + 1) + ":\n" + this.lexer.showPosition() + "\nExpecting " + expected.join(", ") + ", got '" + (this.terminals_[symbol] || symbol) + "'";
  181. } else {
  182. errStr = "Parse error on line " + (yylineno + 1) + ": Unexpected " + (symbol == 1?"end of input":"'" + (this.terminals_[symbol] || symbol) + "'");
  183. }
  184. this.parseError(errStr, {text: this.lexer.match, token: this.terminals_[symbol] || symbol, line: this.lexer.yylineno, loc: yyloc, expected: expected});
  185. }
  186. }
  187. if (action[0] instanceof Array && action.length > 1) {
  188. throw new Error("Parse Error: multiple actions possible at state: " + state + ", token: " + symbol);
  189. }
  190. switch (action[0]) {
  191. case 1:
  192. stack.push(symbol);
  193. vstack.push(this.lexer.yytext);
  194. lstack.push(this.lexer.yylloc);
  195. stack.push(action[1]);
  196. symbol = null;
  197. if (!preErrorSymbol) {
  198. yyleng = this.lexer.yyleng;
  199. yytext = this.lexer.yytext;
  200. yylineno = this.lexer.yylineno;
  201. yyloc = this.lexer.yylloc;
  202. if (recovering > 0)
  203. recovering--;
  204. } else {
  205. symbol = preErrorSymbol;
  206. preErrorSymbol = null;
  207. }
  208. break;
  209. case 2:
  210. len = this.productions_[action[1]][1];
  211. yyval.$ = vstack[vstack.length - len];
  212. yyval._$ = {first_line: lstack[lstack.length - (len || 1)].first_line, last_line: lstack[lstack.length - 1].last_line, first_column: lstack[lstack.length - (len || 1)].first_column, last_column: lstack[lstack.length - 1].last_column};
  213. if (ranges) {
  214. yyval._$.range = [lstack[lstack.length - (len || 1)].range[0], lstack[lstack.length - 1].range[1]];
  215. }
  216. r = this.performAction.call(yyval, yytext, yyleng, yylineno, this.yy, action[1], vstack, lstack);
  217. if (typeof r !== "undefined") {
  218. return r;
  219. }
  220. if (len) {
  221. stack = stack.slice(0, -1 * len * 2);
  222. vstack = vstack.slice(0, -1 * len);
  223. lstack = lstack.slice(0, -1 * len);
  224. }
  225. stack.push(this.productions_[action[1]][0]);
  226. vstack.push(yyval.$);
  227. lstack.push(yyval._$);
  228. newState = table[stack[stack.length - 2]][stack[stack.length - 1]];
  229. stack.push(newState);
  230. break;
  231. case 3:
  232. return true;
  233. }
  234. }
  235. return true;
  236. }
  237. };
  238. undefined/* Jison generated lexer */
  239. var lexer = (function(){
  240. var lexer = ({EOF:1,
  241. parseError:function parseError(str, hash) {
  242. if (this.yy.parser) {
  243. this.yy.parser.parseError(str, hash);
  244. } else {
  245. throw new Error(str);
  246. }
  247. },
  248. setInput:function (input) {
  249. this._input = input;
  250. this._more = this._less = this.done = false;
  251. this.yylineno = this.yyleng = 0;
  252. this.yytext = this.matched = this.match = '';
  253. this.conditionStack = ['INITIAL'];
  254. this.yylloc = {first_line:1,first_column:0,last_line:1,last_column:0};
  255. if (this.options.ranges) this.yylloc.range = [0,0];
  256. this.offset = 0;
  257. return this;
  258. },
  259. input:function () {
  260. var ch = this._input[0];
  261. this.yytext += ch;
  262. this.yyleng++;
  263. this.offset++;
  264. this.match += ch;
  265. this.matched += ch;
  266. var lines = ch.match(/(?:\r\n?|\n).*/g);
  267. if (lines) {
  268. this.yylineno++;
  269. this.yylloc.last_line++;
  270. } else {
  271. this.yylloc.last_column++;
  272. }
  273. if (this.options.ranges) this.yylloc.range[1]++;
  274. this._input = this._input.slice(1);
  275. return ch;
  276. },
  277. unput:function (ch) {
  278. var len = ch.length;
  279. var lines = ch.split(/(?:\r\n?|\n)/g);
  280. this._input = ch + this._input;
  281. this.yytext = this.yytext.substr(0, this.yytext.length-len-1);
  282. //this.yyleng -= len;
  283. this.offset -= len;
  284. var oldLines = this.match.split(/(?:\r\n?|\n)/g);
  285. this.match = this.match.substr(0, this.match.length-1);
  286. this.matched = this.matched.substr(0, this.matched.length-1);
  287. if (lines.length-1) this.yylineno -= lines.length-1;
  288. var r = this.yylloc.range;
  289. this.yylloc = {first_line: this.yylloc.first_line,
  290. last_line: this.yylineno+1,
  291. first_column: this.yylloc.first_column,
  292. last_column: lines ?
  293. (lines.length === oldLines.length ? this.yylloc.first_column : 0) + oldLines[oldLines.length - lines.length].length - lines[0].length:
  294. this.yylloc.first_column - len
  295. };
  296. if (this.options.ranges) {
  297. this.yylloc.range = [r[0], r[0] + this.yyleng - len];
  298. }
  299. return this;
  300. },
  301. more:function () {
  302. this._more = true;
  303. return this;
  304. },
  305. less:function (n) {
  306. this.unput(this.match.slice(n));
  307. },
  308. pastInput:function () {
  309. var past = this.matched.substr(0, this.matched.length - this.match.length);
  310. return (past.length > 20 ? '...':'') + past.substr(-20).replace(/\n/g, "");
  311. },
  312. upcomingInput:function () {
  313. var next = this.match;
  314. if (next.length < 20) {
  315. next += this._input.substr(0, 20-next.length);
  316. }
  317. return (next.substr(0,20)+(next.length > 20 ? '...':'')).replace(/\n/g, "");
  318. },
  319. showPosition:function () {
  320. var pre = this.pastInput();
  321. var c = new Array(pre.length + 1).join("-");
  322. return pre + this.upcomingInput() + "\n" + c+"^";
  323. },
  324. next:function () {
  325. if (this.done) {
  326. return this.EOF;
  327. }
  328. if (!this._input) this.done = true;
  329. var token,
  330. match,
  331. tempMatch,
  332. index,
  333. col,
  334. lines;
  335. if (!this._more) {
  336. this.yytext = '';
  337. this.match = '';
  338. }
  339. var rules = this._currentRules();
  340. for (var i=0;i < rules.length; i++) {
  341. tempMatch = this._input.match(this.rules[rules[i]]);
  342. if (tempMatch && (!match || tempMatch[0].length > match[0].length)) {
  343. match = tempMatch;
  344. index = i;
  345. if (!this.options.flex) break;
  346. }
  347. }
  348. if (match) {
  349. lines = match[0].match(/(?:\r\n?|\n).*/g);
  350. if (lines) this.yylineno += lines.length;
  351. this.yylloc = {first_line: this.yylloc.last_line,
  352. last_line: this.yylineno+1,
  353. first_column: this.yylloc.last_column,
  354. last_column: lines ? lines[lines.length-1].length-lines[lines.length-1].match(/\r?\n?/)[0].length : this.yylloc.last_column + match[0].length};
  355. this.yytext += match[0];
  356. this.match += match[0];
  357. this.matches = match;
  358. this.yyleng = this.yytext.length;
  359. if (this.options.ranges) {
  360. this.yylloc.range = [this.offset, this.offset += this.yyleng];
  361. }
  362. this._more = false;
  363. this._input = this._input.slice(match[0].length);
  364. this.matched += match[0];
  365. token = this.performAction.call(this, this.yy, this, rules[index],this.conditionStack[this.conditionStack.length-1]);
  366. if (this.done && this._input) this.done = false;
  367. if (token) return token;
  368. else return;
  369. }
  370. if (this._input === "") {
  371. return this.EOF;
  372. } else {
  373. return this.parseError('Lexical error on line '+(this.yylineno+1)+'. Unrecognized text.\n'+this.showPosition(),
  374. {text: "", token: null, line: this.yylineno});
  375. }
  376. },
  377. lex:function lex () {
  378. var r = this.next();
  379. if (typeof r !== 'undefined') {
  380. return r;
  381. } else {
  382. return this.lex();
  383. }
  384. },
  385. begin:function begin (condition) {
  386. this.conditionStack.push(condition);
  387. },
  388. popState:function popState () {
  389. return this.conditionStack.pop();
  390. },
  391. _currentRules:function _currentRules () {
  392. return this.conditions[this.conditionStack[this.conditionStack.length-1]].rules;
  393. },
  394. topState:function () {
  395. return this.conditionStack[this.conditionStack.length-2];
  396. },
  397. pushState:function begin (condition) {
  398. this.begin(condition);
  399. }});
  400. lexer.options = {};
  401. lexer.performAction = function anonymous(yy,yy_,$avoiding_name_collisions,YY_START
  402. ) {
  403. var YYSTATE=YY_START
  404. switch($avoiding_name_collisions) {
  405. case 0:// ignore
  406. break;
  407. case 1:return 17
  408. break;
  409. case 2:return 18
  410. break;
  411. case 3:return 12
  412. break;
  413. case 4:return 19
  414. break;
  415. case 5:return 28
  416. break;
  417. case 6:return 29
  418. break;
  419. case 7:return 30
  420. break;
  421. case 8:return 31
  422. break;
  423. case 9:return 32
  424. break;
  425. case 10:return 14
  426. break;
  427. case 11:return 23
  428. break;
  429. case 12:return 22
  430. break;
  431. case 13:return 20
  432. break;
  433. case 14:return 21
  434. break;
  435. case 15:return 5
  436. break;
  437. case 16:return "INVALID"
  438. break;
  439. }
  440. };
  441. lexer.rules = [/^(?:\s+)/,/^(?:\()/,/^(?:\))/,/^(?:-?[0-9]+(\.[0-9]+)?([eE][\-\+]?[0-9]+)?)/,/^(?:POINT\b)/,/^(?:LINESTRING\b)/,/^(?:POLYGON\b)/,/^(?:MULTIPOINT\b)/,/^(?:MULTILINESTRING\b)/,/^(?:MULTIPOLYGON\b)/,/^(?:,)/,/^(?:EMPTY\b)/,/^(?:M\b)/,/^(?:Z\b)/,/^(?:ZM\b)/,/^(?:$)/,/^(?:.)/];
  442. lexer.conditions = {"INITIAL":{"rules":[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],"inclusive":true}};
  443. return lexer;})()
  444. parser.lexer = lexer;
  445. function Parser () { this.yy = {}; }Parser.prototype = parser;parser.Parser = Parser;
  446. return new Parser;
  447. })();
  448. function PointArray (point) {
  449. this.data = [ point ];
  450. this.type = 'PointArray';
  451. }
  452. PointArray.prototype.addPoint = function (point) {
  453. if (point.type === 'PointArray') {
  454. this.data = this.data.concat(point.data);
  455. } else {
  456. this.data.push(point);
  457. }
  458. return this;
  459. };
  460. PointArray.prototype.toJSON = function () {
  461. return this.data;
  462. };
  463. function Ring (point) {
  464. this.data = point;
  465. this.type = 'Ring';
  466. }
  467. Ring.prototype.toJSON = function () {
  468. var data = [ ];
  469. for (var i = 0; i < this.data.data.length; i++) {
  470. data.push(this.data.data[i]);
  471. }
  472. return data;
  473. };
  474. function RingList (ring) {
  475. this.data = [ ring ];
  476. this.type = 'RingList';
  477. }
  478. RingList.prototype.addRing = function (ring) {
  479. this.data.push(ring);
  480. return this;
  481. };
  482. RingList.prototype.toJSON = function () {
  483. var data = [ ];
  484. for (var i = 0; i < this.data.length; i++) {
  485. data.push(this.data[i].toJSON());
  486. }
  487. if (data.length === 1) {
  488. return data;
  489. } else {
  490. return data;
  491. }
  492. };
  493. function PolygonList (polygon) {
  494. this.data = [ polygon ];
  495. this.type = 'PolygonList';
  496. }
  497. PolygonList.prototype.addPolygon = function (polygon) {
  498. this.data.push(polygon);
  499. return this;
  500. };
  501. PolygonList.prototype.toJSON = function () {
  502. var data = [ ];
  503. for (var i = 0; i < this.data.length; i++) {
  504. data = data.concat( [ this.data[i].toJSON() ] );
  505. }
  506. return data;
  507. };
  508. function _parse () {
  509. return parser.parse.apply(parser, arguments);
  510. }
  511. function parse (element) {
  512. var res, primitive;
  513. try {
  514. res = parser.parse(element);
  515. } catch (err) {
  516. throw Error("Unable to parse: " + err);
  517. }
  518. return Terraformer.Primitive(res);
  519. }
  520. function arrayToRing (arr) {
  521. var parts = [ ], ret = '';
  522. for (var i = 0; i < arr.length; i++) {
  523. parts.push(arr[i].join(' '));
  524. }
  525. ret += '(' + parts.join(', ') + ')';
  526. return ret;
  527. }
  528. function pointToWKTPoint (primitive) {
  529. var ret = 'POINT ';
  530. if (primitive.coordinates === undefined || primitive.coordinates.length === 0) {
  531. ret += 'EMPTY';
  532. return ret;
  533. } else if (primitive.coordinates.length === 3) {
  534. // 3d or time? default to 3d
  535. if (primitive.properties && primitive.properties.m === true) {
  536. ret += 'M ';
  537. } else {
  538. ret += 'Z ';
  539. }
  540. } else if (primitive.coordinates.length === 4) {
  541. // 3d and time
  542. ret += 'ZM ';
  543. }
  544. // include coordinates
  545. ret += '(' + primitive.coordinates.join(' ') + ')';
  546. return ret;
  547. }
  548. function lineStringToWKTLineString (primitive) {
  549. var ret = 'LINESTRING ';
  550. if (primitive.coordinates === undefined || primitive.coordinates.length === 0 || primitive.coordinates[0].length === 0) {
  551. ret += 'EMPTY';
  552. return ret;
  553. } else if (primitive.coordinates[0].length === 3) {
  554. if (primitive.properties && primitive.properties.m === true) {
  555. ret += 'M ';
  556. } else {
  557. ret += 'Z ';
  558. }
  559. } else if (primitive.coordinates[0].length === 4) {
  560. ret += 'ZM ';
  561. }
  562. ret += arrayToRing(primitive.coordinates);
  563. return ret;
  564. }
  565. function polygonToWKTPolygon (primitive) {
  566. var ret = 'POLYGON ';
  567. if (primitive.coordinates === undefined || primitive.coordinates.length === 0 || primitive.coordinates[0].length === 0) {
  568. ret += 'EMPTY';
  569. return ret;
  570. } else if (primitive.coordinates[0][0].length === 3) {
  571. if (primitive.properties && primitive.properties.m === true) {
  572. ret += 'M ';
  573. } else {
  574. ret += 'Z ';
  575. }
  576. } else if (primitive.coordinates[0][0].length === 4) {
  577. ret += 'ZM ';
  578. }
  579. ret += '(';
  580. var parts = [ ];
  581. for (var i = 0; i < primitive.coordinates.length; i++) {
  582. parts.push(arrayToRing(primitive.coordinates[i]));
  583. }
  584. ret += parts.join(', ');
  585. ret += ')';
  586. return ret;
  587. }
  588. function multiPointToWKTMultiPoint (primitive) {
  589. var ret = 'MULTIPOINT ';
  590. if (primitive.coordinates === undefined || primitive.coordinates.length === 0 || primitive.coordinates[0].length === 0) {
  591. ret += 'EMPTY';
  592. return ret;
  593. } else if (primitive.coordinates[0].length === 3) {
  594. if (primitive.properties && primitive.properties.m === true) {
  595. ret += 'M ';
  596. } else {
  597. ret += 'Z ';
  598. }
  599. } else if (primitive.coordinates[0].length === 4) {
  600. ret += 'ZM ';
  601. }
  602. ret += arrayToRing(primitive.coordinates);
  603. return ret;
  604. }
  605. function multiLineStringToWKTMultiLineString (primitive) {
  606. var ret = 'MULTILINESTRING ';
  607. if (primitive.coordinates === undefined || primitive.coordinates.length === 0 || primitive.coordinates[0].length === 0) {
  608. ret += 'EMPTY';
  609. return ret;
  610. } else if (primitive.coordinates[0][0].length === 3) {
  611. if (primitive.properties && primitive.properties.m === true) {
  612. ret += 'M ';
  613. } else {
  614. ret += 'Z ';
  615. }
  616. } else if (primitive.coordinates[0][0].length === 4) {
  617. ret += 'ZM ';
  618. }
  619. ret += '(';
  620. var parts = [ ];
  621. for (var i = 0; i < primitive.coordinates.length; i++) {
  622. parts.push(arrayToRing(primitive.coordinates[i]));
  623. }
  624. ret += parts.join(', ');
  625. ret += ')';
  626. return ret;
  627. }
  628. function multiPolygonToWKTMultiPolygon (primitive) {
  629. var ret = 'MULTIPOLYGON ';
  630. if (primitive.coordinates === undefined || primitive.coordinates.length === 0 || primitive.coordinates[0].length === 0 || primitive.coordinates[0][0].length === 0) {
  631. ret += 'EMPTY';
  632. return ret;
  633. } else if (primitive.coordinates[0][0][0].length === 3) {
  634. if (primitive.properties && primitive.properties.m === true) {
  635. ret += 'M ';
  636. } else {
  637. ret += 'Z ';
  638. }
  639. } else if (primitive.coordinates[0][0][0].length === 4) {
  640. ret += 'ZM ';
  641. }
  642. ret += '(';
  643. var inner = [ ];
  644. for (var c = 0; c < primitive.coordinates.length; c++) {
  645. var it = '(';
  646. var parts = [ ];
  647. for (var i = 0; i < primitive.coordinates[c].length; i++) {
  648. parts.push(arrayToRing(primitive.coordinates[c][i]));
  649. }
  650. it += parts.join(', ');
  651. it += ')';
  652. inner.push(it);
  653. }
  654. ret += inner.join(', ');
  655. ret += ')';
  656. return ret;
  657. }
  658. function convert (primitive) {
  659. switch (primitive.type) {
  660. case 'Point':
  661. return pointToWKTPoint(primitive);
  662. case 'LineString':
  663. return lineStringToWKTLineString(primitive);
  664. case 'Polygon':
  665. return polygonToWKTPolygon(primitive);
  666. case 'MultiPoint':
  667. return multiPointToWKTMultiPoint(primitive);
  668. case 'MultiLineString':
  669. return multiLineStringToWKTMultiLineString(primitive);
  670. case 'MultiPolygon':
  671. return multiPolygonToWKTMultiPolygon(primitive);
  672. case 'GeometryCollection':
  673. var ret = 'GEOMETRYCOLLECTION';
  674. var parts = [ ];
  675. for (i = 0; i < primitive.geometries.length; i++){
  676. parts.push(convert(primitive.geometries[i]));
  677. }
  678. return ret + '(' + parts.join(', ') + ')';
  679. default:
  680. throw Error ("Unknown Type: " + primitive.type);
  681. }
  682. }
  683. exports.parser = parser;
  684. exports.Parser = parser.Parser;
  685. exports.parse = parse;
  686. exports.convert = convert;
  687. return exports;
  688. }));