contraction-hierarchy-js
Version:
Contraction Hierarchy
2 lines (1 loc) • 33.3 kB
JavaScript
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e=e||self).contractionHierarchy={})}(this,function(e){"use strict";function t(e,r,n,s,o,a){if(!(o-s<=n)){var d=Math.floor((s+o)/2);!function e(t,r,n,s,o,a){for(;o>s;){if(o-s>600){var d=o-s+1,h=n-s+1,c=Math.log(d),u=.5*Math.exp(2*c/3),l=.5*Math.sqrt(c*u*(d-u)/d)*(h-d/2<0?-1:1),p=Math.max(s,Math.floor(n-h*u/d+l)),f=Math.min(o,Math.floor(n+(d-h)*u/d+l));e(t,r,n,p,f,a)}var _=r[2*n+a],g=s,y=o;for(i(t,r,s,n),r[2*o+a]>_&&i(t,r,s,o);g<y;){for(i(t,r,g,y),g++,y--;r[2*g+a]<_;)g++;for(;r[2*y+a]>_;)y--}r[2*s+a]===_?i(t,r,s,y):i(t,r,++y,o),y<=n&&(s=y+1),n<=y&&(o=y-1)}}(e,r,d,s,o,a%2),t(e,r,n,s,d-1,a+1),t(e,r,n,d+1,o,a+1)}}function i(e,t,i,n){r(e,i,n),r(t,2*i,2*n),r(t,2*i+1,2*n+1)}function r(e,t,i){var r=e[t];e[t]=e[i],e[i]=r}function n(e,t,i,r){var n=e-i,s=t-r;return n*n+s*s}function s(e,t,i,r,n){return new o(e,t,i,r,n)}function o(e,i,r,n,s){i=i||a,r=r||d,s=s||Array,this.nodeSize=n||64,this.points=e,this.ids=new s(e.length),this.coords=new s(2*e.length);for(var o=0;o<e.length;o++)this.ids[o]=o,this.coords[2*o]=i(e[o]),this.coords[2*o+1]=r(e[o]);t(this.ids,this.coords,this.nodeSize,0,this.ids.length-1,0)}function a(e){return e[0]}function d(e){return e[1]}o.prototype={range:function(e,t,i,r){return function(e,t,i,r,n,s,o){for(var a,d,h=[0,e.length-1,0],c=[];h.length;){var u=h.pop(),l=h.pop(),p=h.pop();if(l-p<=o)for(var f=p;f<=l;f++)a=t[2*f],d=t[2*f+1],a>=i&&a<=n&&d>=r&&d<=s&&c.push(e[f]);else{var _=Math.floor((p+l)/2);a=t[2*_],d=t[2*_+1],a>=i&&a<=n&&d>=r&&d<=s&&c.push(e[_]);var g=(u+1)%2;(0===u?i<=a:r<=d)&&(h.push(p),h.push(_-1),h.push(g)),(0===u?n>=a:s>=d)&&(h.push(_+1),h.push(l),h.push(g))}}return c}(this.ids,this.coords,e,t,i,r,this.nodeSize)},within:function(e,t,i){return function(e,t,i,r,s,o){for(var a=[0,e.length-1,0],d=[],h=s*s;a.length;){var c=a.pop(),u=a.pop(),l=a.pop();if(u-l<=o)for(var p=l;p<=u;p++)n(t[2*p],t[2*p+1],i,r)<=h&&d.push(e[p]);else{var f=Math.floor((l+u)/2),_=t[2*f],g=t[2*f+1];n(_,g,i,r)<=h&&d.push(e[f]);var y=(c+1)%2;(0===c?i-s<=_:r-s<=g)&&(a.push(l),a.push(f-1),a.push(y)),(0===c?i+s>=_:r+s>=g)&&(a.push(f+1),a.push(u),a.push(y))}}return d}(this.ids,this.coords,e,t,i,this.nodeSize)}};var h=u,c=u;function u(e,t){if(!(this instanceof u))return new u(e,t);if(this.data=e||[],this.length=this.data.length,this.compare=t||l,this.length>0)for(var i=(this.length>>1)-1;i>=0;i--)this._down(i)}function l(e,t){return e<t?-1:e>t?1:0}u.prototype={push:function(e){this.data.push(e),this.length++,this._up(this.length-1)},pop:function(){if(0!==this.length){var e=this.data[0];return this.length--,this.length>0&&(this.data[0]=this.data[this.length],this._down(0)),this.data.pop(),e}},peek:function(){return this.data[0]},_up:function(e){for(var t=this.data,i=this.compare,r=t[e];e>0;){var n=e-1>>1,s=t[n];if(i(r,s)>=0)break;t[e]=s,e=n}t[e]=r},_down:function(e){for(var t=this.data,i=this.compare,r=this.length>>1,n=t[e];e<r;){var s=1+(e<<1),o=s+1,a=t[s];if(o<this.length&&i(t[o],a)<0&&(s=o,a=t[o]),i(a,n)>=0)break;t[e]=a,e=s}t[e]=n}},h.default=c;var p=function(e,t,i,r,n,s){var o=[];void 0===r&&(r=1/0);void 0===n&&(n=1/0);var a=Math.cos(i*y),d=Math.sin(i*y),c=h(null,v),u={left:0,right:e.ids.length-1,axis:0,dist:0,minLng:-180,minLat:-90,maxLng:180,maxLat:90};for(;u;){var l=u.right,p=u.left;if(l-p<=e.nodeSize)for(var f=p;f<=l;f++){var _=e.points[e.ids[f]];s&&!s(_)||c.push({item:_,dist:x(t,i,e.coords[2*f],e.coords[2*f+1],a,d)})}else{var g=p+l>>1,m=e.coords[2*g],F=e.coords[2*g+1];_=e.points[e.ids[g]],s&&!s(_)||c.push({item:_,dist:x(t,i,m,F,a,d)});var k=(u.axis+1)%2,b={left:p,right:g-1,axis:k,minLng:u.minLng,minLat:u.minLat,maxLng:0===u.axis?m:u.maxLng,maxLat:1===u.axis?F:u.maxLat,dist:0},j={left:g+1,right:l,axis:k,minLng:0===u.axis?m:u.minLng,minLat:1===u.axis?F:u.minLat,maxLng:u.maxLng,maxLat:u.maxLat,dist:0};b.dist=w(t,i,b,a,d),j.dist=w(t,i,j,a,d),c.push(b),c.push(j)}for(;c.length&&c.peek().item;){var S=c.pop();if(S.dist>n)return o;if(o.push(S.item),o.length===r)return o}u=c.pop()}return o},f=function(e,t,i,r){return x(e,t,i,r,Math.cos(t*y),Math.sin(t*y))},_=6371,g=40007,y=Math.PI/180;function w(e,t,i,r,n){var s=i.minLng,o=i.maxLng,a=i.minLat,d=i.maxLat;if(e>=s&&e<=o)return t<=a?g*(a-t)/360:t>=d?g*(t-d)/360:0;var h=(s-e+360)%360<=(e-o+360)%360?s:o,c=Math.cos((h-e)*y),u=Math.atan(n/(r*c))/y,l=Math.max(m(a,r,n,c),m(d,r,n,c));return u>a&&u<d&&(l=Math.max(l,m(u,r,n,c))),_*Math.acos(l)}function v(e,t){return e.dist-t.dist}function x(e,t,i,r,n,s){var o=Math.cos((i-e)*y);return _*Math.acos(m(r,n,s,o))}function m(e,t,i,r){var n=i*Math.sin(e*y)+t*Math.cos(e*y)*r;return Math.min(n,1)}var F={around:p,distance:f};function k(e){if(!e._geoJsonFlag)throw new Error("Cannot use Coordinate Lookup on a non-GeoJson network.");const t=new Set;Object.keys(e._nodeToIndexLookup).forEach(e=>{t.add(e)});const i=[];t.forEach(e=>{i.push(e.split(",").map(e=>Number(e)))}),this.index=s(i,e=>e[0],e=>e[1])}k.prototype.getClosestNetworkPt=function(e,t){return F.around(this.index,e,t,1)[0]};const b=F,j=s;function S(e){if(!(this instanceof S))return new S(e);if(!(e=e||{}).compare)throw new Error("Please supply a comparison function to NodeHeap");if(this.data=[],this.length=this.data.length,this.compare=e.compare,this.setNodeId=function(e,t){e.heapIndex=t},this.length>0)for(var t=this.length>>1;t>=0;t--)this._down(t);if(e.setNodeId)for(t=0;t<this.length;++t)this.setNodeId(this.data[t],t)}S.prototype={push:function(e){this.data.push(e),this.setNodeId(e,this.length),this.length++,this._up(this.length-1)},pop:function(){if(0!==this.length){var e=this.data[0];return this.length--,this.length>0&&(this.data[0]=this.data[this.length],this.setNodeId(this.data[0],0),this._down(0)),this.data.pop(),e}},peek:function(){return this.data[0]},updateItem:function(e){this._down(e),this._up(e)},_up:function(e){for(var t=this.data,i=this.compare,r=this.setNodeId,n=t[e];e>0;){var s=e-1>>1,o=t[s];if(i(n,o)>=0)break;t[e]=o,r(o,e),e=s}t[e]=n,r(n,e)},_down:function(e){for(var t=this.data,i=this.compare,r=this.length>>1,n=t[e],s=this.setNodeId;e<r;){var o=1+(e<<1),a=o+1,d=t[o];if(a<this.length&&i(t[a],d)<0&&(o=a,d=t[a]),i(d,n)>=0)break;t[e]=d,s(d,e),e=o}t[e]=n,s(n,e)}};var L,E;try{L=Map}catch(e){}try{E=Set}catch(e){}function I(e){return function e(t,i,r){if(!t||"object"!=typeof t||"function"==typeof t)return t;if(t.nodeType&&"cloneNode"in t)return t.cloneNode(!0);if(t instanceof Date)return new Date(t.getTime());if(t instanceof RegExp)return new RegExp(t);if(Array.isArray(t))return t.map(I);if(L&&t instanceof L)return new Map(Array.from(t.entries()));if(E&&t instanceof E)return new Set(Array.from(t.values()));if(t instanceof Object){i.push(t);var n=Object.create(t);for(var s in r.push(n),t){var o=i.findIndex(function(e){return e===t[s]});n[s]=o>-1?r[o]:e(t[s],i,r)}return n}return t}(e,[],[])}var M=function(e,t){return e(t={exports:{}},t.exports),t.exports}(function(e,t){var i=t.ContractionHierarchy={};i.read=function(e,t){return e.readFields(i._readField,{_locked:!1,_geoJsonFlag:!1,adjacency_list:[],reverse_adjacency_list:[],_nodeToIndexLookup:{},_edgeProperties:[],_edgeGeometry:[]},t)},i._readField=function(e,t,r){if(1===e)t._locked=r.readBoolean();else if(2===e)t._geoJsonFlag=r.readBoolean();else if(3===e)t.adjacency_list.push(i.AdjList.read(r,r.readVarint()+r.pos));else if(4===e)t.reverse_adjacency_list.push(i.AdjList.read(r,r.readVarint()+r.pos));else if(5===e){var n=i._FieldEntry5.read(r,r.readVarint()+r.pos);t._nodeToIndexLookup[n.key]=n.value}else 6===e?t._edgeProperties.push(r.readString()):7===e&&t._edgeGeometry.push(i.GeometryArray.read(r,r.readVarint()+r.pos))},i.write=function(e,t){if(e._locked&&t.writeBooleanField(1,e._locked),e._geoJsonFlag&&t.writeBooleanField(2,e._geoJsonFlag),e.adjacency_list)for(var r=0;r<e.adjacency_list.length;r++)t.writeMessage(3,i.AdjList.write,e.adjacency_list[r]);if(e.reverse_adjacency_list)for(r=0;r<e.reverse_adjacency_list.length;r++)t.writeMessage(4,i.AdjList.write,e.reverse_adjacency_list[r]);if(e._nodeToIndexLookup)for(r in e._nodeToIndexLookup)Object.prototype.hasOwnProperty.call(e._nodeToIndexLookup,r)&&t.writeMessage(5,i._FieldEntry5.write,{key:r,value:e._nodeToIndexLookup[r]});if(e._edgeProperties)for(r=0;r<e._edgeProperties.length;r++)t.writeStringField(6,e._edgeProperties[r]);if(e._edgeGeometry)for(r=0;r<e._edgeGeometry.length;r++)t.writeMessage(7,i.GeometryArray.write,e._edgeGeometry[r])},i.EdgeAttrs={},i.EdgeAttrs.read=function(e,t){return e.readFields(i.EdgeAttrs._readField,{end:0,cost:0,attrs:0},t)},i.EdgeAttrs._readField=function(e,t,i){1===e?t.end=i.readVarint():2===e?t.cost=i.readDouble():3===e&&(t.attrs=i.readVarint())},i.EdgeAttrs.write=function(e,t){e.end&&t.writeVarintField(1,e.end),e.cost&&t.writeDoubleField(2,e.cost),e.attrs&&t.writeVarintField(3,e.attrs)},i.AdjList={},i.AdjList.read=function(e,t){return e.readFields(i.AdjList._readField,{edges:[]},t)},i.AdjList._readField=function(e,t,r){1===e&&t.edges.push(i.EdgeAttrs.read(r,r.readVarint()+r.pos))},i.AdjList.write=function(e,t){if(e.edges)for(var r=0;r<e.edges.length;r++)t.writeMessage(1,i.EdgeAttrs.write,e.edges[r])},i.LineStringAray={},i.LineStringAray.read=function(e,t){return e.readFields(i.LineStringAray._readField,{coords:[]},t)},i.LineStringAray._readField=function(e,t,i){1===e&&i.readPackedDouble(t.coords)},i.LineStringAray.write=function(e,t){e.coords&&t.writePackedDouble(1,e.coords)},i.GeometryArray={},i.GeometryArray.read=function(e,t){return e.readFields(i.GeometryArray._readField,{linestrings:[]},t)},i.GeometryArray._readField=function(e,t,r){1===e&&t.linestrings.push(i.LineStringAray.read(r,r.readVarint()+r.pos))},i.GeometryArray.write=function(e,t){if(e.linestrings)for(var r=0;r<e.linestrings.length;r++)t.writeMessage(1,i.LineStringAray.write,e.linestrings[r])},i._FieldEntry5={},i._FieldEntry5.read=function(e,t){return e.readFields(i._FieldEntry5._readField,{key:"",value:0},t)},i._FieldEntry5._readField=function(e,t,i){1===e?t.key=i.readString():2===e&&(t.value=i.readVarint())},i._FieldEntry5.write=function(e,t){e.key&&t.writeStringField(1,e.key),e.value&&t.writeVarintField(2,e.value)}}).ContractionHierarchy,P=function(e,t,i,r,n){var s,o,a=8*n-r-1,d=(1<<a)-1,h=d>>1,c=-7,u=i?n-1:0,l=i?-1:1,p=e[t+u];for(u+=l,s=p&(1<<-c)-1,p>>=-c,c+=a;c>0;s=256*s+e[t+u],u+=l,c-=8);for(o=s&(1<<-c)-1,s>>=-c,c+=r;c>0;o=256*o+e[t+u],u+=l,c-=8);if(0===s)s=1-h;else{if(s===d)return o?NaN:1/0*(p?-1:1);o+=Math.pow(2,r),s-=h}return(p?-1:1)*o*Math.pow(2,s-r)},N=function(e,t,i,r,n,s){var o,a,d,h=8*s-n-1,c=(1<<h)-1,u=c>>1,l=23===n?Math.pow(2,-24)-Math.pow(2,-77):0,p=r?0:s-1,f=r?1:-1,_=t<0||0===t&&1/t<0?1:0;for(t=Math.abs(t),isNaN(t)||t===1/0?(a=isNaN(t)?1:0,o=c):(o=Math.floor(Math.log(t)/Math.LN2),t*(d=Math.pow(2,-o))<1&&(o--,d*=2),(t+=o+u>=1?l/d:l*Math.pow(2,1-u))*d>=2&&(o++,d/=2),o+u>=c?(a=0,o=c):o+u>=1?(a=(t*d-1)*Math.pow(2,n),o+=u):(a=t*Math.pow(2,u-1)*Math.pow(2,n),o=0));n>=8;e[i+p]=255&a,p+=f,a/=256,n-=8);for(o=o<<n|a,h+=n;h>0;e[i+p]=255&o,p+=f,o/=256,h-=8);e[i+p-f]|=128*_},V=A;function A(e){this.buf=ArrayBuffer.isView&&ArrayBuffer.isView(e)?e:new Uint8Array(e||0),this.pos=0,this.type=0,this.length=this.buf.length}A.Varint=0,A.Fixed64=1,A.Bytes=2,A.Fixed32=5;function T(e){return e.type===A.Bytes?e.readVarint()+e.pos:e.pos+1}function G(e,t,i){return i?4294967296*t+(e>>>0):4294967296*(t>>>0)+(e>>>0)}function B(e,t,i){var r=t<=16383?1:t<=2097151?2:t<=268435455?3:Math.floor(Math.log(t)/(7*Math.LN2));i.realloc(r);for(var n=i.pos-1;n>=e;n--)i.buf[n+r]=i.buf[n]}function J(e,t){for(var i=0;i<e.length;i++)t.writeVarint(e[i])}function C(e,t){for(var i=0;i<e.length;i++)t.writeSVarint(e[i])}function D(e,t){for(var i=0;i<e.length;i++)t.writeFloat(e[i])}function O(e,t){for(var i=0;i<e.length;i++)t.writeDouble(e[i])}function H(e,t){for(var i=0;i<e.length;i++)t.writeBoolean(e[i])}function U(e,t){for(var i=0;i<e.length;i++)t.writeFixed32(e[i])}function z(e,t){for(var i=0;i<e.length;i++)t.writeSFixed32(e[i])}function $(e,t){for(var i=0;i<e.length;i++)t.writeFixed64(e[i])}function q(e,t){for(var i=0;i<e.length;i++)t.writeSFixed64(e[i])}function R(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16)+16777216*e[t+3]}function K(e,t,i){e[i]=t,e[i+1]=t>>>8,e[i+2]=t>>>16,e[i+3]=t>>>24}function Q(e,t){return(e[t]|e[t+1]<<8|e[t+2]<<16)+(e[t+3]<<24)}A.prototype={destroy:function(){this.buf=null},readFields:function(e,t,i){for(i=i||this.length;this.pos<i;){var r=this.readVarint(),n=r>>3,s=this.pos;this.type=7&r,e(n,t,this),this.pos===s&&this.skip(r)}return t},readMessage:function(e,t){return this.readFields(e,t,this.readVarint()+this.pos)},readFixed32:function(){var e=R(this.buf,this.pos);return this.pos+=4,e},readSFixed32:function(){var e=Q(this.buf,this.pos);return this.pos+=4,e},readFixed64:function(){var e=R(this.buf,this.pos)+4294967296*R(this.buf,this.pos+4);return this.pos+=8,e},readSFixed64:function(){var e=R(this.buf,this.pos)+4294967296*Q(this.buf,this.pos+4);return this.pos+=8,e},readFloat:function(){var e=P(this.buf,this.pos,!0,23,4);return this.pos+=4,e},readDouble:function(){var e=P(this.buf,this.pos,!0,52,8);return this.pos+=8,e},readVarint:function(e){var t,i,r=this.buf;return t=127&(i=r[this.pos++]),i<128?t:(t|=(127&(i=r[this.pos++]))<<7,i<128?t:(t|=(127&(i=r[this.pos++]))<<14,i<128?t:(t|=(127&(i=r[this.pos++]))<<21,i<128?t:function(e,t,i){var r,n,s=i.buf;if(n=s[i.pos++],r=(112&n)>>4,n<128)return G(e,r,t);if(n=s[i.pos++],r|=(127&n)<<3,n<128)return G(e,r,t);if(n=s[i.pos++],r|=(127&n)<<10,n<128)return G(e,r,t);if(n=s[i.pos++],r|=(127&n)<<17,n<128)return G(e,r,t);if(n=s[i.pos++],r|=(127&n)<<24,n<128)return G(e,r,t);if(n=s[i.pos++],r|=(1&n)<<31,n<128)return G(e,r,t);throw new Error("Expected varint not more than 10 bytes")}(t|=(15&(i=r[this.pos]))<<28,e,this))))},readVarint64:function(){return this.readVarint(!0)},readSVarint:function(){var e=this.readVarint();return e%2==1?(e+1)/-2:e/2},readBoolean:function(){return Boolean(this.readVarint())},readString:function(){var e=this.readVarint()+this.pos,t=function(e,t,i){var r="",n=t;for(;n<i;){var s,o,a,d=e[n],h=null,c=d>239?4:d>223?3:d>191?2:1;if(n+c>i)break;1===c?d<128&&(h=d):2===c?128==(192&(s=e[n+1]))&&(h=(31&d)<<6|63&s)<=127&&(h=null):3===c?(s=e[n+1],o=e[n+2],128==(192&s)&&128==(192&o)&&((h=(15&d)<<12|(63&s)<<6|63&o)<=2047||h>=55296&&h<=57343)&&(h=null)):4===c&&(s=e[n+1],o=e[n+2],a=e[n+3],128==(192&s)&&128==(192&o)&&128==(192&a)&&((h=(15&d)<<18|(63&s)<<12|(63&o)<<6|63&a)<=65535||h>=1114112)&&(h=null)),null===h?(h=65533,c=1):h>65535&&(h-=65536,r+=String.fromCharCode(h>>>10&1023|55296),h=56320|1023&h),r+=String.fromCharCode(h),n+=c}return r}(this.buf,this.pos,e);return this.pos=e,t},readBytes:function(){var e=this.readVarint()+this.pos,t=this.buf.subarray(this.pos,e);return this.pos=e,t},readPackedVarint:function(e,t){if(this.type!==A.Bytes)return e.push(this.readVarint(t));var i=T(this);for(e=e||[];this.pos<i;)e.push(this.readVarint(t));return e},readPackedSVarint:function(e){if(this.type!==A.Bytes)return e.push(this.readSVarint());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readSVarint());return e},readPackedBoolean:function(e){if(this.type!==A.Bytes)return e.push(this.readBoolean());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readBoolean());return e},readPackedFloat:function(e){if(this.type!==A.Bytes)return e.push(this.readFloat());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readFloat());return e},readPackedDouble:function(e){if(this.type!==A.Bytes)return e.push(this.readDouble());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readDouble());return e},readPackedFixed32:function(e){if(this.type!==A.Bytes)return e.push(this.readFixed32());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readFixed32());return e},readPackedSFixed32:function(e){if(this.type!==A.Bytes)return e.push(this.readSFixed32());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readSFixed32());return e},readPackedFixed64:function(e){if(this.type!==A.Bytes)return e.push(this.readFixed64());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readFixed64());return e},readPackedSFixed64:function(e){if(this.type!==A.Bytes)return e.push(this.readSFixed64());var t=T(this);for(e=e||[];this.pos<t;)e.push(this.readSFixed64());return e},skip:function(e){var t=7&e;if(t===A.Varint)for(;this.buf[this.pos++]>127;);else if(t===A.Bytes)this.pos=this.readVarint()+this.pos;else if(t===A.Fixed32)this.pos+=4;else{if(t!==A.Fixed64)throw new Error("Unimplemented type: "+t);this.pos+=8}},writeTag:function(e,t){this.writeVarint(e<<3|t)},realloc:function(e){for(var t=this.length||16;t<this.pos+e;)t*=2;if(t!==this.length){var i=new Uint8Array(t);i.set(this.buf),this.buf=i,this.length=t}},finish:function(){return this.length=this.pos,this.pos=0,this.buf.subarray(0,this.length)},writeFixed32:function(e){this.realloc(4),K(this.buf,e,this.pos),this.pos+=4},writeSFixed32:function(e){this.realloc(4),K(this.buf,e,this.pos),this.pos+=4},writeFixed64:function(e){this.realloc(8),K(this.buf,-1&e,this.pos),K(this.buf,Math.floor(e*(1/4294967296)),this.pos+4),this.pos+=8},writeSFixed64:function(e){this.realloc(8),K(this.buf,-1&e,this.pos),K(this.buf,Math.floor(e*(1/4294967296)),this.pos+4),this.pos+=8},writeVarint:function(e){(e=+e||0)>268435455||e<0?function(e,t){var i,r;e>=0?(i=e%4294967296|0,r=e/4294967296|0):(r=~(-e/4294967296),4294967295^(i=~(-e%4294967296))?i=i+1|0:(i=0,r=r+1|0));if(e>=0x10000000000000000||e<-0x10000000000000000)throw new Error("Given varint doesn't fit into 10 bytes");t.realloc(10),function(e,t,i){i.buf[i.pos++]=127&e|128,e>>>=7,i.buf[i.pos++]=127&e|128,e>>>=7,i.buf[i.pos++]=127&e|128,e>>>=7,i.buf[i.pos++]=127&e|128,e>>>=7,i.buf[i.pos]=127&e}(i,0,t),function(e,t){var i=(7&e)<<4;if(t.buf[t.pos++]|=i|((e>>>=3)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;if(t.buf[t.pos++]=127&e|((e>>>=7)?128:0),!e)return;t.buf[t.pos++]=127&e}(r,t)}(e,this):(this.realloc(4),this.buf[this.pos++]=127&e|(e>127?128:0),e<=127||(this.buf[this.pos++]=127&(e>>>=7)|(e>127?128:0),e<=127||(this.buf[this.pos++]=127&(e>>>=7)|(e>127?128:0),e<=127||(this.buf[this.pos++]=e>>>7&127))))},writeSVarint:function(e){this.writeVarint(e<0?2*-e-1:2*e)},writeBoolean:function(e){this.writeVarint(Boolean(e))},writeString:function(e){e=String(e),this.realloc(4*e.length),this.pos++;var t=this.pos;this.pos=function(e,t,i){for(var r,n,s=0;s<t.length;s++){if((r=t.charCodeAt(s))>55295&&r<57344){if(!n){r>56319||s+1===t.length?(e[i++]=239,e[i++]=191,e[i++]=189):n=r;continue}if(r<56320){e[i++]=239,e[i++]=191,e[i++]=189,n=r;continue}r=n-55296<<10|r-56320|65536,n=null}else n&&(e[i++]=239,e[i++]=191,e[i++]=189,n=null);r<128?e[i++]=r:(r<2048?e[i++]=r>>6|192:(r<65536?e[i++]=r>>12|224:(e[i++]=r>>18|240,e[i++]=r>>12&63|128),e[i++]=r>>6&63|128),e[i++]=63&r|128)}return i}(this.buf,e,this.pos);var i=this.pos-t;i>=128&&B(t,i,this),this.pos=t-1,this.writeVarint(i),this.pos+=i},writeFloat:function(e){this.realloc(4),N(this.buf,e,this.pos,!0,23,4),this.pos+=4},writeDouble:function(e){this.realloc(8),N(this.buf,e,this.pos,!0,52,8),this.pos+=8},writeBytes:function(e){var t=e.length;this.writeVarint(t),this.realloc(t);for(var i=0;i<t;i++)this.buf[this.pos++]=e[i]},writeRawMessage:function(e,t){this.pos++;var i=this.pos;e(t,this);var r=this.pos-i;r>=128&&B(i,r,this),this.pos=i-1,this.writeVarint(r),this.pos+=r},writeMessage:function(e,t,i){this.writeTag(e,A.Bytes),this.writeRawMessage(t,i)},writePackedVarint:function(e,t){t.length&&this.writeMessage(e,J,t)},writePackedSVarint:function(e,t){t.length&&this.writeMessage(e,C,t)},writePackedBoolean:function(e,t){t.length&&this.writeMessage(e,H,t)},writePackedFloat:function(e,t){t.length&&this.writeMessage(e,D,t)},writePackedDouble:function(e,t){t.length&&this.writeMessage(e,O,t)},writePackedFixed32:function(e,t){t.length&&this.writeMessage(e,U,t)},writePackedSFixed32:function(e,t){t.length&&this.writeMessage(e,z,t)},writePackedFixed64:function(e,t){t.length&&this.writeMessage(e,$,t)},writePackedSFixed64:function(e,t){t.length&&this.writeMessage(e,q,t)},writeBytesField:function(e,t){this.writeTag(e,A.Bytes),this.writeBytes(t)},writeFixed32Field:function(e,t){this.writeTag(e,A.Fixed32),this.writeFixed32(t)},writeSFixed32Field:function(e,t){this.writeTag(e,A.Fixed32),this.writeSFixed32(t)},writeFixed64Field:function(e,t){this.writeTag(e,A.Fixed64),this.writeFixed64(t)},writeSFixed64Field:function(e,t){this.writeTag(e,A.Fixed64),this.writeSFixed64(t)},writeVarintField:function(e,t){this.writeTag(e,A.Varint),this.writeVarint(t)},writeSVarintField:function(e,t){this.writeTag(e,A.Varint),this.writeSVarint(t)},writeStringField:function(e,t){this.writeTag(e,A.Bytes),this.writeString(t)},writeFloatField:function(e,t){this.writeTag(e,A.Fixed32),this.writeFloat(t)},writeDoubleField:function(e,t){this.writeTag(e,A.Fixed64),this.writeDouble(t)},writeBooleanField:function(e,t){this.writeVarintField(e,Boolean(t))}};function W(e){this.id=e.id,this.dist=void 0!==e.dist?e.dist:1/0,this.prev=void 0,this.visited=void 0,this.opened=!1,this.heapIndex=-1}function X(){var e=0,t=[];return{createNewState:function(i){var r=t[e];r?(r.id=i.id,r.dist=void 0!==i.dist?i.dist:1/0,r.prev=void 0,r.visited=void 0,r.opened=!1,r.heapIndex=-1):(r=new W(i),t[e]=r);return e++,r},reset:function(){e=0}}}function Y(e,t){this.score=e,this.id=t}const Z=k,ee=b,te=j;function ie(e,t){const i=t||{};this.debugMode=i.debugMode||!1,this.adjacency_list=[],this.reverse_adjacency_list=[],this._createNodePool=X,this._currentNodeIndex=-1,this._nodeToIndexLookup={},this._indexToNodeLookup={},this._currentEdgeIndex=-1,this._edgeProperties=[],this._edgeGeometry=[],this._maxUncontractedEdgeIndex=0,this._locked=!1,this._geoJsonFlag=!1,this._manualAdd=!1,e&&(this._loadFromGeoJson(e),this.debugMode&&(console.log("Nodes: ",this._currentNodeIndex),console.log("Edges: ",this._currentEdgeIndex)))}ie.prototype.createPathfinder=function(e){const t=this.adjacency_list,i=this.reverse_adjacency_list,r=this._edgeProperties,n=this._edgeGeometry,s=this._createNodePool(),o=this._nodeToIndexLookup,a=this._indexToNodeLookup;return e||(e={}),{queryContractionHierarchy:function(d,h){s.reset();const c=o[String(d)],u=o[String(h)],l=[],p=[],f={},_={};let g=s.createNewState({id:c,dist:0});l[c]=g,g.opened=1,f[g.id]=0;let y=s.createNewState({id:u,dist:0});p[u]=y,y.opened=1,_[y.id]=0;const w=I(t,g,l,f,p,_),v=I(i,y,p,_,l,f);let x,m,F=!1,k=!1,b=1/0,j=null;if(c!==u)do{F||(x=w.next()).done&&(F=!0),k||(m=v.next()).done&&(k=!0)}while(f[x.value.id]<b||_[m.value.id]<b);else b=0;let L,E={total_cost:b!==1/0?b:0};if(e.ids||e.path||e.nodes||e.properties)if(null!=j)L=function(e,t,i,r,n,s,o,a){const d=[],h=[s];let c=r[s],u=n[s];if(c)for(;null!=c.attrs;)d.push({id:c.attrs,direction:"f"}),h.push(c.prev),c=r[c.prev];if(d.reverse(),h.reverse(),u)for(;null!=u.attrs;)d.push({id:u.attrs,direction:"b"}),h.push(u.prev),u=n[u.prev];let l=a;const p=d.map(e=>{const i="f"===e.direction?t[e.id]._start_index:t[e.id]._end_index,r="f"===e.direction?t[e.id]._end_index:t[e.id]._start_index,n=[...t[e.id]._ordered];return l!==i?(n.reverse(),l=i):l=r,n}),f=[].concat(...p),_=f.map(e=>t[e]._id);let g,y,w,v;return e.nodes&&(v=h.map(e=>o[e])),(e.properties||e.path)&&(y=f.map(e=>{const{_start_index:i,_end_index:r,_ordered:n,...s}=t[e];return s})),e.path&&(w={type:"FeatureCollection",features:f.map((e,t)=>({type:"Feature",properties:y[t],geometry:{type:"LineString",coordinates:i[e]}}))}),e.properties&&(g=y),{ids:_,path:w,properties:g,nodes:v}}(e,r,n,l,p,j,a,c);else{let t,i,r,n;e.ids&&(t=[]),e.path&&(i={}),e.properties&&(r=[]),e.nodes&&(n=[]),L={ids:t,path:i,properties:r,nodes:n}}return Object.assign(E,{...L});function*I(e,t,i,r,n,o){for(var a=new S({compare:(e,t)=>e.dist-t.dist});;){if((e[t.id]||[]).forEach(e=>{let n=i[e.end];if(void 0===n&&((n=s.createNewState({id:e.end})).attrs=e.attrs,i[e.end]=n),!0===n.visited)return;n.opened||(a.push(n),n.opened=!0);const d=t.dist+e.cost;if(d>=n.dist)return;n.dist=d,r[n.id]=d,n.attrs=e.attrs,n.prev=t.id,a.updateItem(n.heapIndex);const h=o[e.end];if(h>=0){const t=d+h;b>t&&(b=t,j=e.end)}}),t.visited=!0,!(t=a.pop()))return"";yield t}}}}},ie.prototype._loadFromGeoJson=function(e){if(this._locked)throw new Error("Cannot add GeoJSON to a contracted network");if(this._geoJsonFlag)throw new Error("Cannot load more than one GeoJSON file.");if(this._manualAdd)throw new Error("Cannot load GeoJSON file after adding Edges manually via the API.");const t=I(e);this._cleanseGeoJsonNetwork(t).forEach((e,t)=>{const i=e.geometry.coordinates,r=e.properties;if(!r||!i||!r._cost)return void(this.debugMode&&console.log("invalid feature detected. skipping..."));const n=i[0],s=i[i.length-1];this._addEdge(n,s,r,I(i)),this._addEdge(s,n,r,I(i).reverse())}),this._geoJsonFlag=!0},ie.prototype._cleanseGeoJsonNetwork=function(e){const t={},i=e.features;return i.forEach(e=>{const i=e.geometry.coordinates[0].join(","),r=e.geometry.coordinates[e.geometry.coordinates.length-1].join(","),n=`${i}|${r}`,s=`${r}|${i}`;if(t[n]){this.debugMode&&console.log("Duplicate feature found, choosing shortest.");const i=t[n].properties._cost;e.properties._cost<i?(t[n].properties.__markDelete=!0,t[n]=e):e.properties.__markDelete=!0}else t[n]=e;if(t[s]){const i=t[s].properties._cost;e.properties._cost<i?(t[s].properties.__markDelete=!0,t[s]=e):e.properties.__markDelete=!0}else t[s]=e}),i.filter(e=>!e.properties.__markDelete)},ie.prototype._addContractedEdge=function(e,t,i){this._currentEdgeIndex++,this._edgeProperties[this._currentEdgeIndex]=i,this._edgeProperties[this._currentEdgeIndex]._start_index=e,this._edgeProperties[this._currentEdgeIndex]._end_index=t;const r={end:t,cost:i._cost,attrs:this._currentEdgeIndex};this.adjacency_list[e]?this.adjacency_list[e].push(r):this.adjacency_list[e]=[r];const n={end:e,cost:i._cost,attrs:this._currentEdgeIndex};this.reverse_adjacency_list[t]?this.reverse_adjacency_list[t].push(n):this.reverse_adjacency_list[t]=[n]},ie.prototype.addEdge=function(e,t,i,r,n){if(this._locked)throw new Error("Graph has been contracted. No additional edges can be added.");if(this._geoJsonFlag)throw new Error("Can not add additional edges manually to a GeoJSON network.");this._manualAdd=!0,this._addEdge(e,t,i,r,n)},ie.prototype._addEdge=function(e,t,i,r,n){const s=String(e),o=String(t);if(s===o)return void(this.debugMode&&console.log("Start and End Nodes are the same. Ignoring."));null==this._nodeToIndexLookup[s]&&(this._currentNodeIndex++,this._nodeToIndexLookup[s]=this._currentNodeIndex,this._indexToNodeLookup[this._currentNodeIndex]=s),null==this._nodeToIndexLookup[o]&&(this._currentNodeIndex++,this._nodeToIndexLookup[o]=this._currentNodeIndex,this._indexToNodeLookup[this._currentNodeIndex]=o);let a=this._nodeToIndexLookup[s],d=this._nodeToIndexLookup[o];this._currentEdgeIndex++,this._edgeProperties[this._currentEdgeIndex]=JSON.parse(JSON.stringify(i)),this._edgeProperties[this._currentEdgeIndex]._start_index=a,this._edgeProperties[this._currentEdgeIndex]._end_index=d,r&&(this._edgeGeometry[this._currentEdgeIndex]=JSON.parse(JSON.stringify(r)));const h={end:d,cost:i._cost,attrs:this._currentEdgeIndex};this.adjacency_list[a]?this.adjacency_list[a].push(h):this.adjacency_list[a]=[h];const c={end:a,cost:i._cost,attrs:this._currentEdgeIndex};this.reverse_adjacency_list[d]?this.reverse_adjacency_list[d].push(c):this.reverse_adjacency_list[d]=[c],n&&(this.adjacency_list[d]?this.adjacency_list[d].push(c):this.adjacency_list[d]=[c],this.reverse_adjacency_list[a]?this.reverse_adjacency_list[a].push(h):this.reverse_adjacency_list[a]=[h])},ie.prototype.loadCH=function(e){const t="object"==typeof e?e:JSON.parse(e);this._locked=t._locked,this._geoJsonFlag=t._geoJsonFlag,this.adjacency_list=t.adjacency_list,this.reverse_adjacency_list=t.reverse_adjacency_list,this._nodeToIndexLookup=t._nodeToIndexLookup,this._edgeProperties=t._edgeProperties,this._edgeGeometry=t._edgeGeometry},ie.prototype.saveCH=function(){if(!this._locked)throw new Error("No sense in saving network before it is contracted.");return JSON.stringify({_locked:this._locked,_geoJsonFlag:this._geoJsonFlag,adjacency_list:this.adjacency_list,reverse_adjacency_list:this.reverse_adjacency_list,_nodeToIndexLookup:this._nodeToIndexLookup,_edgeProperties:this._edgeProperties,_edgeGeometry:this._edgeGeometry})},ie.prototype.loadPbfCH=function(e){var t=new V(e),i=M.read(t);i.adjacency_list=i.adjacency_list.map(e=>e.edges),i.reverse_adjacency_list=i.reverse_adjacency_list.map(e=>e.edges),i._edgeGeometry=i._edgeGeometry.map(e=>e.linestrings.map(e=>e.coords)),i._edgeProperties=i._edgeProperties.map(e=>JSON.parse(e)),this._locked=i._locked,this._geoJsonFlag=i._geoJsonFlag,this.adjacency_list=i.adjacency_list,this.reverse_adjacency_list=i.reverse_adjacency_list,this._nodeToIndexLookup=i._nodeToIndexLookup,this._edgeProperties=i._edgeProperties,this._edgeGeometry=i._edgeGeometry,console.log("done loading pbf")},ie.prototype.savePbfCH=function(e){if(!require)return void console.log("saving as PBF only works in NodeJS");const t=require("fs");if(!this._locked)throw new Error("No sense in saving network before it is contracted.");const i={_locked:this._locked,_geoJsonFlag:this._geoJsonFlag,adjacency_list:this.adjacency_list,reverse_adjacency_list:this.reverse_adjacency_list,_nodeToIndexLookup:this._nodeToIndexLookup,_edgeProperties:this._edgeProperties,_edgeGeometry:this._edgeGeometry};i.adjacency_list=i.adjacency_list.map(e=>({edges:e.map(e=>e)})),i.reverse_adjacency_list=i.reverse_adjacency_list.map(e=>({edges:e.map(e=>e)})),i._edgeGeometry=i._edgeGeometry.map(e=>({linestrings:e.map(e=>({coords:e}))})),i._edgeProperties=i._edgeProperties.map(e=>JSON.stringify(e));var r=new V;M.write(i,r);var n=r.finish();t.writeFileSync(e,n,null),console.log(`done saving ${e}`)},ie.prototype.contractGraph=function(){if(this._locked)throw new Error("Network has already been contracted");this._locked=!0,this._maxUncontractedEdgeIndex=this._currentEdgeIndex;const e=this._createChShortcutter(),t=t=>this._contract(t,!0,e)-(this.adjacency_list[t]||[]).length+i(t),i=e=>(this.adjacency_list[e]||[]).reduce((e,t)=>e+(null!=this.contracted_nodes[t.end]?1:0),0),r=new S({compare:(e,t)=>e.score-t.score});this.contracted_nodes=[],Object.keys(this._nodeToIndexLookup).forEach(e=>{const i=this._nodeToIndexLookup[e],n=new Y(t(i),i);r.push(n)});let n=1;const s=r.length;for(;r.length>0;){const i=r.length;i%50==0&&(this.debugMode&&console.log(i/s),this._cleanAdjList(this.adjacency_list),this._cleanAdjList(this.reverse_adjacency_list));let o=!1,a=r.peek();const d=a.score;do{const e=a.id,i=t(e);i>d&&(a.score=i,r.updateItem(a.heapIndex)),(a=r.peek()).id===e&&(o=!0)}while(!1===o);const h=r.pop();this._contract(h.id,!1,e),this.contracted_nodes[h.id]=n,n++}this._cleanAdjList(this.adjacency_list),this._cleanAdjList(this.reverse_adjacency_list),this._arrangeContractedPaths(this.adjacency_list),this._arrangeContractedPaths(this.reverse_adjacency_list),this.debugMode&&console.log("Contraction complete")},ie.prototype._arrangeContractedPaths=function(e){e.forEach((e,t)=>{e.forEach(e=>{const i=t;let r=[],n=[];for(n=[e.attrs];n.length;){const e=n.pop();e<=this._maxUncontractedEdgeIndex?r.push(e):n.push(...this._edgeProperties[e]._id)}const s={};r.forEach(e=>{const t=this._edgeProperties[e],i=t._start_index,r=t._end_index;s[i]?s[i].push(e):s[i]=[e],s[r]?s[r].push(e):s[r]=[e]});const o=[];let a=String(i),d=s[a][0];for(;null!=d;){o.push(d);const e=this._edgeProperties[d],t=String(e._start_index),i=String(e._end_index),r=t===a?i:t;a=r;const n=s[r];if(1===n.length)break;n.length>2&&(console.error("too many edges in array. unexpected. unrecoverable."),process.exit()),d=n[0]===d?n[1]:n[0]}this._edgeProperties[e.attrs]._ordered=o})})},ie.prototype._cleanAdjList=function(e){e.forEach((t,i)=>{const r=this.contracted_nodes[i];null!=r&&(e[i]=e[i].filter(e=>{const t=this.contracted_nodes[e.end];return null==t||r<t}))})},ie.prototype._contract=function(e,t,i){const r=(this.reverse_adjacency_list[e]||[]).filter(e=>!this.contracted_nodes[e.end]),n=(this.adjacency_list[e]||[]).filter(e=>!this.contracted_nodes[e.end]);let s=0;return r.forEach(r=>{let o=0;const a=r.cost;if(n.forEach(e=>{if(r.end===e.end)return;const t=e.cost,i=a+t;i>o&&(o=i)}),!n.length)return;const d=i.runDijkstra(r.end,null,e,o);n.forEach(e=>{if(r.end===e.end)return;const i=e.cost,n=a+i;if(n<(d.distances[e.end]||1/0)&&(s++,!t)){const t={_cost:n,_id:[r.attrs,e.attrs],_start_index:r.end,_end_index:e.end};this._addContractedEdge(r.end,e.end,t)}})}),s},ie.prototype._createChShortcutter=function(){const e=this._createNodePool(),t=this.adjacency_list;return{runDijkstra:function(i,r,n,s){e.reset();const o=[],a={};var d=new S({compare:(e,t)=>e.dist-t.dist});let h=e.createNewState({id:i,dist:0});for(o[i]=h,h.opened=1,a[h.id]=0,i===r&&(h="");h;){(t[h.id]||[]).filter(e=>e.end!==n).forEach(t=>{let i=o[t.end];if(void 0===i&&(i=e.createNewState({id:t.end}),o[t.end]=i),!0===i.visited)return;i.opened||(d.push(i),i.opened=!0);const r=h.dist+t.cost;r>=i.dist||(i.dist=r,a[i.id]=r,i.prev=h.id,d.updateItem(i.heapIndex))}),h.visited=!0;const i=h.dist;(h=d.pop())&&h.id===r&&(h=""),i>s&&(h="")}return{distances:a,nodeState:o}}}},e.CoordinateLookup=Z,e.Graph=ie,e.__geoindex=ee,e.__kdindex=te,Object.defineProperty(e,"__esModule",{value:!0})});