UNPKG

geo-polyline-tools

Version:

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

3 lines (2 loc) 1.29 kB
var e={};function r(e){return Math.floor(Math.abs(e)+.5)*(e>=0?1:-1)}function o(e,o,n){var t=2*((e=r(e*n))-(o=r(o*n)));t<0&&(t=-t-1);for(var i="";t>=32;)i+=String.fromCharCode(63+(32|31&t)),t/=32;return i+=String.fromCharCode(63+(0|t))}function n(e){for(var r=[],o=0;o<e.length;o++){var n=e[o].slice();r.push([n[1],n[0]])}return r}e.encode=function(e,r){if(void 0===r&&(r=5),!e.length)return"";for(var n=Math.pow(10,Number.isInteger(r)?r:5),t=o(e[0][0],0,n)+o(e[0][1],0,n),i=1;i<e.length;i++){var u=e[i],a=e[i-1];t+=o(u[0],a[0],n),t+=o(u[1],a[1],n)}return t},e.decode=function(e,r){void 0===r&&(r=5);for(var o,n=0,t=0,i=0,u=[],a=0,d=0,c=null,f=Math.pow(10,Number.isInteger(r)?r:5);n<e.length;){c=null,a=1,d=0;do{d+=(31&(c=e.charCodeAt(n++)-63))*a,a*=32}while(c>=32);o=1&d?(-d-1)/2:d/2,a=1,d=0;do{d+=(31&(c=e.charCodeAt(n++)-63))*a,a*=32}while(c>=32);t+=o,i+=1&d?(-d-1)/2:d/2,u.push([t/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(n(r.coordinates),o):""},e.toGeoJSON=function(r,o){return void 0===o&&(o=5),{type:"LineString",coordinates:n(e.decode?e.decode(r,o):[])}};export{e as default}; //# sourceMappingURL=index.js.map