UNPKG

geo-polyline-tools

Version:

A javaScript library that provides utility functions to encode and decode polyline coordinates

3 lines (2 loc) 1.3 kB
"use strict";var e={};function r(e){return Math.floor(Math.abs(e)+.5)*(e>=0?1:-1)}function o(e,o,t){var n=2*((e=r(e*t))-(o=r(o*t)));n<0&&(n=-n-1);for(var i="";n>=32;)i+=String.fromCharCode(63+(32|31&n)),n/=32;return i+=String.fromCharCode(63+(0|n))}function t(e){for(var r=[],o=0;o<e.length;o++){var t=e[o].slice();r.push([t[1],t[0]])}return r}e.encode=function(e,r){if(void 0===r&&(r=5),!e.length)return"";for(var t=Math.pow(10,Number.isInteger(r)?r:5),n=o(e[0][0],0,t)+o(e[0][1],0,t),i=1;i<e.length;i++){var u=e[i],d=e[i-1];n+=o(u[0],d[0],t),n+=o(u[1],d[1],t)}return n},e.decode=function(e,r){void 0===r&&(r=5);for(var o,t=0,n=0,i=0,u=[],d=0,a=0,c=null,f=Math.pow(10,Number.isInteger(r)?r:5);t<e.length;){c=null,d=1,a=0;do{a+=(31&(c=e.charCodeAt(t++)-63))*d,d*=32}while(c>=32);o=1&a?(-a-1)/2:a/2,d=1,a=0;do{a+=(31&(c=e.charCodeAt(t++)-63))*d,d*=32}while(c>=32);n+=o,i+=1&a?(-a-1)/2:a/2,u.push([n/f,i/f])}return u},e.fromGeoJSON=function(r,o){if(void 0===o&&(o=5),r&&"Feature"===r.type&&(r=r.geometry),!r||"LineString"!==r.type)throw new Error("Input must be a GeoJSON LineString");return e.encode?e.encode(t(r.coordinates),o):""},e.toGeoJSON=function(r,o){return void 0===o&&(o=5),{type:"LineString",coordinates:t(e.decode?e.decode(r,o):[])}},module.exports=e; //# sourceMappingURL=index.js.map