UNPKG

terra-route

Version:

A library for routing along GeoJSON LineString networks

3 lines (2 loc) 8.08 kB
function e(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 t(t,r){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,r){if(t){if("string"==typeof t)return e(t,r);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var o=0;return function(){return o>=t.length?{done:!0}:{done:!1,value:t[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 r=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};function n(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)}}var 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,r,n){n[e]=!0;for(var o,i=t(r[e]);!(o=i()).done;){var s=o.value;n[s]||a(s,r,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,n=0,o=0;o<t.length-1;o++)n+=r(t[o],t[o+1]);return n}var u=/*#__PURE__*/function(){function e(e){this.network=void 0,this.network=e}var r=e.prototype;return r.setNetwork=function(e){this.network=e},r.getNetwork=function(){return this.network},r.getConnectedComponents=function(){return function(e){var r=e.features,n=new Map,o=new Map;function a(e){return e[0]+","+e[1]}for(var i=0;i<r.length;i++)for(var s,u=t(r[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<r.length;d++){n.set(d,new Set);for(var f,c=t(r[d].geometry.coordinates);!(f=c()).done;){var v=a(f.value),g=o.get(v);if(g)for(var l,p=t(g);!(l=p()).done;){var y=l.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(r[i]);var s=n.get(i);if(s)for(var u,h=t(s);!(u=h()).done;){var d=u.value;w.has(d)||a.push(d)}}}}for(var k=0;k<r.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)},r.getConnectedComponentCount=function(){return function(e){for(var r,n=e.features,o=n.length,i=new Map,s=0;s<o;s++)for(var u,h=t(n[s].geometry.coordinates);!(u=h()).done;){var d=(r=u.value)[0]+","+r[1];i.has(d)||i.set(d,[]),i.get(d).push(s)}for(var f,c=Array.from({length:o},function(){return[]}),v=t(i.values());!(f=v()).done;)for(var g=f.value,l=0;l<g.length;l++)for(var p=l+1;p<g.length;p++){var y=g[l],w=g[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)},r.getNodeAndEdgeCount=function(){return function(e){for(var r,n=new Set,o=new Set,a=t(e.features);!(r=a()).done;){for(var i,s=r.value.geometry.coordinates,u=t(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)},r.getNodes=function(){return{type:"FeatureCollection",features:function(e){for(var r,n=new Set,o=[],a=t(e.features);!(r=a()).done;)for(var i,s=t(r.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)}},r.getNodeCount=function(){return this.getNodeAndEdgeCount().nodeCount},r.getEdges=function(){return function(e){for(var r,n=new Map,o=t(e.features);!(r=o()).done;)for(var a=r.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)},r.getLongestEdgeLength=function(){var e=this.getLongestEdge();return e?s(e):-1},r.getShortestEdgeLength=function(){var e=this.getShortestEdge();return e?s(e):-1},r.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]},r.getShortestEdge=function(){var e=this.getEdges().features;return 0===e.length?null:e.sort(function(e,t){return s(e)-s(t)})[0]},r.getEdgeCount=function(){return this.getNodeAndEdgeCount().edgeCount},e}(),h=/*#__PURE__*/function(){function e(e){var t,n;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:r,this.heapConstructor=null!=(n=null==e?void 0:e.heap)?n:o}var n=e.prototype;return n.buildRouteGraph=function(e){this.network=e,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[];for(var r,n=this.coordinateIndexMap,o=this.coordinates,a=this.adjacencyList,i=this.distanceMeasurement,s=t(e.features);!(r=s()).done;)for(var u=r.value.geometry.coordinates,h=0;h<u.length-1;h++){var d=u[h],f=d[0],c=d[1],v=u[h+1],g=v[0],l=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(g);w||(w=new Map,n.set(g,w));var M=w.get(l);void 0===M&&(M=o.length,o.push(u[h+1]),w.set(l,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})}},n.getRoute=function(e,r){if(!this.network)throw new Error("Network not built. Please call buildRouteGraph(network) first.");var n=this.getOrCreateIndex(e.geometry.coordinates),o=this.getOrCreateIndex(r.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=t(this.adjacencyList[d]||[]);!(f=c()).done;){var v=f.value,g=s[d]+v.distance;if(g<s[v.node]){s[v.node]=g,u[v.node]=d;var l=this.distanceMeasurement(this.coordinates[v.node],this.coordinates[o]);a.insert(g+l,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:{}}},n.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}();export{u as LineStringGraph,h as TerraRoute,n as createCheapRuler,r as haversineDistance}; //# sourceMappingURL=terra-route.module.js.map