UNPKG

terra-route

Version:

A library for routing along GeoJSON LineString networks

3 lines (2 loc) 8.24 kB
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports):"function"==typeof define&&define.amd?define(["exports"],t):t((e||self).terraRoute={})}(this,function(e){function t(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=Array(t);r<t;r++)n[r]=e[r];return n}function r(e,r){var n="undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(n)return(n=n.call(e)).next.bind(n);if(Array.isArray(e)||(n=function(e,r){if(e){if("string"==typeof e)return t(e,r);var n={}.toString.call(e).slice(8,-1);return"Object"===n&&e.constructor&&(n=e.constructor.name),"Map"===n||"Set"===n?Array.from(e):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?t(e,r):void 0}}(e))||r&&e&&"number"==typeof e.length){n&&(e=n);var o=0;return function(){return o>=e.length?{done:!0}:{done:!1,value:e[o++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}var n=function(e,t){var r=function(e){return e*Math.PI/180},n=r(e[1]),o=r(e[0]),a=r(t[1]),i=a-n,s=r(t[0])-o,u=Math.sin(i/2)*Math.sin(i/2)+Math.cos(n)*Math.cos(a)*Math.sin(s/2)*Math.sin(s/2);return 2*Math.atan2(Math.sqrt(u),Math.sqrt(1-u))*6371e3/1e3},o=/*#__PURE__*/function(){function e(){this.heap=[],this.insertCounter=0}var t=e.prototype;return t.insert=function(e,t){var r={key:e,value:t,index:this.insertCounter++},n=this.heap.length;for(this.heap.push(r);n>0;){var o=n-1>>>1,a=this.heap[o];if(e>a.key||e===a.key&&r.index>a.index)break;this.heap[n]=a,n=o}this.heap[n]=r},t.extractMin=function(){var e=this.heap,t=e.length;if(0===t)return null;var r=e[0],n=e.pop();return t>1&&(e[0]=n,this.bubbleDown(0)),r.value},t.size=function(){return this.heap.length},t.bubbleDown=function(e){for(var t=this.heap,r=t.length,n=t[e],o=n.key,a=n.index;;){var i=1+(e<<1);if(i>=r)break;var s=i,u=t[i],h=i+1;if(h<r){var d=t[h];(d.key<u.key||d.key===u.key&&d.index<u.index)&&(s=h,u=d)}if(!(u.key<o||u.key===o&&u.index<a))break;t[e]=u,e=s}t[e]=n},e}();function a(e,t,n){n[e]=!0;for(var o,i=r(t[e]);!(o=i()).done;){var s=o.value;n[s]||a(s,t,n)}}function i(e,t){var r=function(e,t){var r=e[0],n=t[0];return r<n||r===n&&e[1]<=t[1]?[e,t]:[t,e]}(e,t);return JSON.stringify([r[0],r[1]])}function s(e){for(var t=e.geometry.coordinates,r=0,o=0;o<t.length-1;o++)r+=n(t[o],t[o+1]);return r}var u=/*#__PURE__*/function(){function e(e){this.network=void 0,this.network=e}var t=e.prototype;return t.setNetwork=function(e){this.network=e},t.getNetwork=function(){return this.network},t.getConnectedComponents=function(){return function(e){var t=e.features,n=new Map,o=new Map;function a(e){return e[0]+","+e[1]}for(var i=0;i<t.length;i++)for(var s,u=r(t[i].geometry.coordinates);!(s=u()).done;){var h=a(s.value);o.has(h)||o.set(h,new Set),o.get(h).add(i)}for(var d=0;d<t.length;d++){n.set(d,new Set);for(var f,c=r(t[d].geometry.coordinates);!(f=c()).done;){var v=a(f.value),l=o.get(v);if(l)for(var g,p=r(l);!(g=p()).done;){var y=g.value;y!==d&&n.get(d).add(y)}}}var w=new Set,M=[];function m(e,o){for(var a=[e];a.length>0;){var i=a.pop();if(!w.has(i)){w.add(i),o.push(t[i]);var s=n.get(i);if(s)for(var u,h=r(s);!(u=h()).done;){var d=u.value;w.has(d)||a.push(d)}}}}for(var k=0;k<t.length;k++)if(!w.has(k)){var C=[];m(k,C),M.push({type:"FeatureCollection",features:C})}return M.sort(function(e,t){return e.features.length-t.features.length}),M}(this.network)},t.getConnectedComponentCount=function(){return function(e){for(var t,n=e.features,o=n.length,i=new Map,s=0;s<o;s++)for(var u,h=r(n[s].geometry.coordinates);!(u=h()).done;){var d=(t=u.value)[0]+","+t[1];i.has(d)||i.set(d,[]),i.get(d).push(s)}for(var f,c=Array.from({length:o},function(){return[]}),v=r(i.values());!(f=v()).done;)for(var l=f.value,g=0;g<l.length;g++)for(var p=g+1;p<l.length;p++){var y=l[g],w=l[p];c[y].push(w),c[w].push(y)}for(var M=new Array(o).fill(!1),m=0,k=0;k<o;k++)M[k]||(a(k,c,M),m++);return m}(this.network)},t.getNodeAndEdgeCount=function(){return function(e){for(var t,n=new Set,o=new Set,a=r(e.features);!(t=a()).done;){for(var i,s=t.value.geometry.coordinates,u=r(s);!(i=u()).done;)n.add(JSON.stringify(i.value));for(var h=0;h<s.length-1;h++){var d=(f=s[h+1],void 0,void 0,(c=JSON.stringify(s[h]))<(v=JSON.stringify(f))?c+"|"+v:v+"|"+c);o.add(d)}}var f,c,v;return{nodeCount:n.size,edgeCount:o.size}}(this.network)},t.getNodes=function(){return{type:"FeatureCollection",features:function(e){for(var t,n=new Set,o=[],a=r(e.features);!(t=a()).done;)for(var i,s=r(t.value.geometry.coordinates);!(i=s()).done;){var u=i.value,h=u.join(",");n.has(h)||(n.add(h),o.push({type:"Feature",geometry:{type:"Point",coordinates:u},properties:{}}))}return o}(this.network)}},t.getNodeCount=function(){return this.getNodeAndEdgeCount().nodeCount},t.getEdges=function(){return function(e){for(var t,n=new Map,o=r(e.features);!(t=o()).done;)for(var a=t.value.geometry.coordinates,s=0;s<a.length-1;s++){var u=a[s],h=a[s+1],d=i(u,h);n.has(d)||n.set(d,{type:"Feature",geometry:{type:"LineString",coordinates:[u,h]},properties:{}})}return{type:"FeatureCollection",features:Array.from(n.values())}}(this.network)},t.getLongestEdgeLength=function(){var e=this.getLongestEdge();return e?s(e):-1},t.getShortestEdgeLength=function(){var e=this.getShortestEdge();return e?s(e):-1},t.getLongestEdge=function(){var e=this.getEdges().features;if(0===e.length)return null;var t=e.sort(function(e,t){return s(e)-s(t)});return t[t.length-1]},t.getShortestEdge=function(){var e=this.getEdges().features;return 0===e.length?null:e.sort(function(e,t){return s(e)-s(t)})[0]},t.getEdgeCount=function(){return this.getNodeAndEdgeCount().edgeCount},e}(),h=/*#__PURE__*/function(){function e(e){var t,r;this.network=null,this.distanceMeasurement=void 0,this.heapConstructor=void 0,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[],this.distanceMeasurement=null!=(t=null==e?void 0:e.distanceMeasurement)?t:n,this.heapConstructor=null!=(r=null==e?void 0:e.heap)?r:o}var t=e.prototype;return t.buildRouteGraph=function(e){this.network=e,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[];for(var t,n=this.coordinateIndexMap,o=this.coordinates,a=this.adjacencyList,i=this.distanceMeasurement,s=r(e.features);!(t=s()).done;)for(var u=t.value.geometry.coordinates,h=0;h<u.length-1;h++){var d=u[h],f=d[0],c=d[1],v=u[h+1],l=v[0],g=v[1],p=n.get(f);p||(p=new Map,n.set(f,p));var y=p.get(c);void 0===y&&(y=o.length,o.push(u[h]),p.set(c,y),a[y]=[]);var w=n.get(l);w||(w=new Map,n.set(l,w));var M=w.get(g);void 0===M&&(M=o.length,o.push(u[h+1]),w.set(g,M),a[M]=[]);var m=i(u[h],u[h+1]);a[y].push({node:M,distance:m}),a[M].push({node:y,distance:m})}},t.getRoute=function(e,t){if(!this.network)throw new Error("Network not built. Please call buildRouteGraph(network) first.");var n=this.getOrCreateIndex(e.geometry.coordinates),o=this.getOrCreateIndex(t.geometry.coordinates);if(n===o)return null;var a=new this.heapConstructor;a.insert(0,n);var i=this.coordinates.length,s=new Array(i).fill(Infinity),u=new Array(i).fill(-1),h=new Array(i).fill(!1);for(s[n]=0;a.size()>0;){var d=a.extractMin();if(!h[d]){if(d===o)break;h[d]=!0;for(var f,c=r(this.adjacencyList[d]||[]);!(f=c()).done;){var v=f.value,l=s[d]+v.distance;if(l<s[v.node]){s[v.node]=l,u[v.node]=d;var g=this.distanceMeasurement(this.coordinates[v.node],this.coordinates[o]);a.insert(l+g,v.node)}}}}if(u[o]<0)return null;for(var p=[],y=o;y!==n;)p.unshift(this.coordinates[y]),y=u[y];return p.unshift(this.coordinates[n]),{type:"Feature",geometry:{type:"LineString",coordinates:p},properties:{}}},t.getOrCreateIndex=function(e){var t=e[0],r=e[1],n=this.coordinateIndexMap.get(t);n||(n=new Map,this.coordinateIndexMap.set(t,n));var o=n.get(r);return void 0===o&&(o=this.coordinates.length,this.coordinates.push(e),n.set(r,o),this.adjacencyList[o]=[]),o},e}();e.LineStringGraph=u,e.TerraRoute=h,e.createCheapRuler=function(e){var t=1/298.257223563,r=t*(2-t),n=Math.PI/180,o=Math.cos(e*n),a=1/(1-r*(1-o*o)),i=Math.sqrt(a),s=6378.137*n,u=s*i*o,h=s*i*a*(1-r);return function(e,t){for(var r=e[0]-t[0];r<-180;)r+=360;for(;r>180;)r-=360;var n=r*u,o=(e[1]-t[1])*h;return Math.sqrt(n*n+o*o)}},e.haversineDistance=n}); //# sourceMappingURL=terra-route.umd.js.map