terra-route 0.0.10 → 0.0.11
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +8 -9
- package/dist/heap/four-ary-heap.d.ts +17 -0
- package/dist/terra-route.cjs +1 -1
- package/dist/terra-route.cjs.map +1 -1
- package/dist/terra-route.d.ts +22 -14
- package/dist/terra-route.modern.js +1 -1
- package/dist/terra-route.modern.js.map +1 -1
- package/dist/terra-route.module.js +1 -1
- package/dist/terra-route.module.js.map +1 -1
- package/dist/terra-route.umd.js +1 -1
- package/dist/terra-route.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/heap/four-ary-heap.spec.ts +149 -0
- package/src/heap/four-ary-heap.ts +143 -0
- package/src/terra-route.ts +279 -104
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
<p></p>
|
|
8
8
|
|
|
9
|
-
Terra Route aims to be a fast library for routing on GeoJSON LineStrings networks, where LineStrings share identical coordinates.
|
|
9
|
+
Terra Route aims to be a fast library for routing on GeoJSON LineStrings networks, where LineStrings share identical coordinates.
|
|
10
10
|
|
|
11
11
|
## Install
|
|
12
12
|
|
|
@@ -47,7 +47,7 @@ console.log("Shortest route:", JSON.stringify(route, null, 2));
|
|
|
47
47
|
|
|
48
48
|
## Additional Functionality
|
|
49
49
|
|
|
50
|
-
Terra Route
|
|
50
|
+
Terra Route provides a utility called LineStringGraph for analyzing GeoJSON route networks. This class is especially useful for debugging, as it includes methods to identify unique nodes, edges, and connected components, along with their counts. Beyond debugging, these methods help you programmatically explore and understand the structure of your graph — for example, by measuring its size and examining how its parts are connected.
|
|
51
51
|
|
|
52
52
|
```typescript
|
|
53
53
|
const graph = new LineStringGraph(network);
|
|
@@ -57,9 +57,6 @@ const graphPoints = graph.getNodes();
|
|
|
57
57
|
|
|
58
58
|
// Return all the unique edges as FeatureCollection<LineString>, where each unique edge is a Feature<LineString>
|
|
59
59
|
const graphEdges = graph.getEdges();
|
|
60
|
-
|
|
61
|
-
// The longest possible shortest path in the graph between two nodes (i.e. graph diameter)
|
|
62
|
-
const longestShortestPath = graph.getMaxLengthShortestPath()
|
|
63
60
|
```
|
|
64
61
|
|
|
65
62
|
## Benchmarks
|
|
@@ -73,15 +70,17 @@ npm run benchmark
|
|
|
73
70
|
Here is an example output of a benchmark run for routing:
|
|
74
71
|
|
|
75
72
|
<pre>
|
|
76
|
-
Terra Route
|
|
77
|
-
GeoJSON Path Finder
|
|
78
|
-
ngraph.graph
|
|
73
|
+
Terra Route | ██████ 186ms
|
|
74
|
+
GeoJSON Path Finder | ██████████████████ 566ms
|
|
75
|
+
ngraph.graph | ██████████████████████████████████████████████████ 1577ms
|
|
79
76
|
</pre>
|
|
80
77
|
|
|
81
|
-
Using default Haversine distance, Terra Route is approximately
|
|
78
|
+
Using default Haversine distance, Terra Route is approximately 3x faster than GeoJSON Path Finder with Haversine distance for A -> B path finding. If you pass in the CheapRuler distance metric (you can use the exposed `createCheapRuler` function), it is approximately x8 faster than GeoJSON Path Finder with Haversine distance.
|
|
82
79
|
|
|
83
80
|
For initialisation of the network, Terra Route is approximately 10x faster with Haversine than GeoJSON Path Finder. Terra Draw splits out instantiating the Class of the library from the actual graph building, which is done via `buildRouteGraph`. This allows you to defer graph creation to an appropriate time.
|
|
84
81
|
|
|
82
|
+
Terra Route uses an [A* algorthm for pathfinding](https://en.wikipedia.org/wiki/A*_search_algorithm) and by default uses a [four-ary heap](https://en.wikipedia.org/wiki/D-ary_heap) for the underlying priority queue, although this is configurable.
|
|
83
|
+
|
|
85
84
|
## Limitations
|
|
86
85
|
|
|
87
86
|
- Terra Route does not currently support weighting functions.
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Heap } from "./heap";
|
|
2
|
+
/**
|
|
3
|
+
* A 4-ary min-heap with stable tie-breaking on insertion order.
|
|
4
|
+
* Parent(i) = floor((i - 1) / 4)
|
|
5
|
+
* Children(i) = 4*i + 1 .. 4*i + 4
|
|
6
|
+
*/
|
|
7
|
+
export declare class FourAryHeap implements Heap {
|
|
8
|
+
private keys;
|
|
9
|
+
private values;
|
|
10
|
+
private idxs;
|
|
11
|
+
private length;
|
|
12
|
+
private insertCounter;
|
|
13
|
+
insert(key: number, value: number): void;
|
|
14
|
+
extractMin(): number | null;
|
|
15
|
+
size(): number;
|
|
16
|
+
private bubbleDown;
|
|
17
|
+
}
|
package/dist/terra-route.cjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
function t(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function e(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 a=0;return function(){return a>=e.length?{done:!0}:{done:!1,value:e[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(){return r=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},r.apply(null,arguments)}var n=function(t,e){var r=function(t){return t*Math.PI/180},n=r(t[1]),a=r(t[0]),i=r(e[1]),o=i-n,s=r(e[0])-a,u=Math.sin(o/2)*Math.sin(o/2)+Math.cos(n)*Math.cos(i)*Math.sin(s/2)*Math.sin(s/2);return 2*Math.atan2(Math.sqrt(u),Math.sqrt(1-u))*6371e3/1e3},a=/*#__PURE__*/function(){function t(){this.heap=[],this.insertCounter=0}var e=t.prototype;return e.insert=function(t,e){var r={key:t,value:e,index:this.insertCounter++},n=this.heap.length;for(this.heap.push(r);n>0;){var a=n-1>>>1,i=this.heap[a];if(t>i.key||t===i.key&&r.index>i.index)break;this.heap[n]=i,n=a}this.heap[n]=r},e.extractMin=function(){var t=this.heap,e=t.length;if(0===e)return null;var r=t[0],n=t.pop();return e>1&&(t[0]=n,this.bubbleDown(0)),r.value},e.size=function(){return this.heap.length},e.bubbleDown=function(t){for(var e=this.heap,r=e.length,n=e[t],a=n.key,i=n.index;;){var o=1+(t<<1);if(o>=r)break;var s=o,u=e[o],h=o+1;if(h<r){var f=e[h];(f.key<u.key||f.key===u.key&&f.index<u.index)&&(s=h,u=f)}if(!(u.key<a||u.key===a&&u.index<i))break;e[t]=u,t=s}e[t]=n},t}();function i(t,r,n){n[t]=!0;for(var a,o=e(r[t]);!(a=o()).done;){var s=a.value;n[s]||i(s,r,n)}}function o(t,e){var r=function(t,e){var r=t[0],n=e[0];return r<n||r===n&&t[1]<=e[1]?[t,e]:[e,t]}(t,e);return JSON.stringify([r[0],r[1]])}function s(t){for(var r,n=new Map,a=e(t.features);!(r=a()).done;)for(var i=r.value.geometry.coordinates,s=0;s<i.length-1;s++){var u=i[s],h=i[s+1],f=o(u,h);n.has(f)||n.set(f,{type:"Feature",geometry:{type:"LineString",coordinates:[u,h]},properties:{}})}return{type:"FeatureCollection",features:Array.from(n.values())}}function u(t){for(var e=t.geometry.coordinates,r=0,a=0;a<e.length-1;a++)r+=n(e[a],e[a+1]);return r}function h(t,e){var r=t.length,n=e.length;if(r>n)return!1;for(var a=0;a<=n-r;a++){for(var i=!0,o=0;o<r;o++)if(e[a+o][0]!==t[o][0]||e[a+o][1]!==t[o][1]){i=!1;break}if(i)return!0}for(var s=[].concat(t).reverse(),u=0;u<=n-r;u++){for(var h=!0,f=0;f<r;f++)if(e[u+f][0]!==s[f][0]||e[u+f][1]!==s[f][1]){h=!1;break}if(h)return!0}return!1}function f(t){var e=s(t),r=new Map;function n(t){return t.join(",")}for(var a=0;a<e.features.length;a++){var i=e.features[a].geometry.coordinates;if(!(i.length<2)){var o=n(i[0]),u=n(i[i.length-1]);r.set(o,(r.get(o)||0)+1),r.set(u,(r.get(u)||0)+1)}}for(var h=new Map,f=new Map,d=0;d<e.features.length;d++){var v=e.features[d],c=v.geometry.coordinates;if(!(c.length<2)){var l=n(c[0]),g=n(c[c.length-1]),p=r.get(l)||0,y=r.get(g)||0,m=c.map(n).join(";");1===p||1===y?h.has(m)||h.set(m,v):f.has(m)||f.set(m,v)}}return{leafEdges:{type:"FeatureCollection",features:Array.from(h.values())},nonLeafEdges:{type:"FeatureCollection",features:Array.from(f.values())}}}function d(t,r,n,a,i){for(var o,s=e(t.geometry.coordinates);!(o=s()).done;)if(!v(o.value,r,n,a,i))return!1;return!0}function v(t,e,r,n,a){var i=t[0],o=t[1];return i>=e&&i<=n&&o>=r&&o<=a}var c=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],l=/*#__PURE__*/function(){function t(t,e,r,n){if(void 0===e&&(e=64),void 0===r&&(r=Float64Array),this.data=void 0,this.ids=void 0,this.coords=void 0,this._pos=void 0,this._finished=void 0,this.numItems=void 0,this.nodeSize=void 0,this.ArrayType=void 0,this.IndexArrayType=void 0,isNaN(t)||t<0)throw new Error("Unexpected numItems value: "+t+".");this.numItems=t,this.nodeSize=Math.min(Math.max(e,2),65535),this.ArrayType=r,this.IndexArrayType=t<65536?Uint16Array:Uint32Array;var a=c.indexOf(this.ArrayType),i=2*t*this.ArrayType.BYTES_PER_ELEMENT,o=t*this.IndexArrayType.BYTES_PER_ELEMENT,s=(8-o%8)%8;if(a<0)throw new Error("Unexpected typed array class: "+r+".");n?(this.data=n,this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+o+s,2*t),this._pos=2*t,this._finished=!0):(this.data=new ArrayBuffer(8+i+o+s),this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+o+s,2*t),this._pos=0,this._finished=!1,new Uint8Array(this.data,0,2).set([219,16+a]),new Uint16Array(this.data,2,1)[0]=this.nodeSize,new Uint32Array(this.data,4,1)[0]=this.numItems)}var e=t.prototype;return e.add=function(t,e){var r=this._pos>>1;return this.ids[r]=r,this.coords[this._pos++]=t,this.coords[this._pos++]=e,r},e.finish=function(){var t=this._pos>>1;if(t!==this.numItems)throw new Error("Added "+t+" items when expected "+this.numItems+".");return g(this.ids,this.coords,this.nodeSize,0,this.numItems-1,0),this._finished=!0,this},t}();function g(t,e,r,n,a,i){if(!(a-n<=r)){var o=n+a>>1;p(t,e,o,n,a,i),g(t,e,r,n,o-1,1-i),g(t,e,r,o+1,a,1-i)}}function p(t,e,r,n,a,i){for(;a>n;){if(a-n>600){var o=a-n+1,s=r-n+1,u=Math.log(o),h=.5*Math.exp(2*u/3),f=.5*Math.sqrt(u*h*(o-h)/o)*(s-o/2<0?-1:1);p(t,e,r,Math.max(n,Math.floor(r-s*h/o+f)),Math.min(a,Math.floor(r+(o-s)*h/o+f)),i)}var d=e[2*r+i],v=n,c=a;for(y(t,e,n,r),e[2*a+i]>d&&y(t,e,n,a);v<c;){for(y(t,e,v,c),v++,c--;e[2*v+i]<d;)v++;for(;e[2*c+i]>d;)c--}e[2*n+i]===d?y(t,e,n,c):y(t,e,++c,a),c<=r&&(n=c+1),r<=c&&(a=c-1)}}function y(t,e,r,n){m(t,r,n),m(e,2*r,2*n),m(e,2*r+1,2*n+1)}function m(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}var w=/*#__PURE__*/function(){function t(t,e){if(void 0===t&&(t=[]),void 0===e&&(e=function(t,e){return t<e?-1:t>e?1:0}),this.data=void 0,this.length=void 0,this.compare=void 0,this.data=t,this.length=this.data.length,this.compare=e,this.length>0)for(var r=(this.length>>1)-1;r>=0;r--)this._down(r)}var e=t.prototype;return e.push=function(t){this.data.push(t),this._up(this.length++)},e.pop=function(){if(0!==this.length){var t=this.data[0],e=this.data.pop();return this.length--,this.length>0&&(this.data[0]=e,this._down(0)),t}},e.peek=function(){return this.data[0]},e._up=function(t){for(var e=this.data,r=this.compare,n=e[t];t>0;){var a=t-1>>1,i=e[a];if(r(n,i)>=0)break;e[t]=i,t=a}e[t]=n},e._down=function(t){for(var e=this.data,r=this.compare,n=this.length>>1,a=e[t];t<n;){var i=1+(t<<1),o=i+1;if(o<this.length&&r(e[o],e[i])<0&&(i=o),r(e[i],a)>=0)break;e[t]=e[i],t=i}e[t]=a},t}(),M=Math.PI/180;function x(t,e,r,n){var a=n.minLng,i=n.maxLng,o=n.minLat,s=n.maxLat;if(t>=a&&t<=i)return e<o?A((e-o)*M):e>s?A((e-s)*M):0;var u=Math.min(A((t-a)*M),A((t-i)*M)),h=function(t,e){var r=1-2*e;return r<=0?t>0?90:-90:Math.atan(Math.tan(t*M)/r)/M}(e,u);return h>o&&h<s?L(u,r,e,h):Math.min(L(u,r,e,o),L(u,r,e,s))}function k(t,e){return t.dist-e.dist}function A(t){var e=Math.sin(t/2);return e*e}function L(t,e,r,n){return e*Math.cos(n*M)*t+A((r-n)*M)}function I(t,e,r,n,a){return L(A((t-r)*M),a,e,n)}var E=/*#__PURE__*/function(){function t(t){this.network=void 0,this.network=t}var a=t.prototype;return a.setNetwork=function(t){this.network=t},a.getNetwork=function(){return this.network},a.getNetworkInBoundingBox=function(t){return function(t,r){var n=r[0],a=r[1],i=r[2],o=r[3];if(n>=i||a>=o)throw new Error("Invalid bounding box: min values must be less than max values");for(var s,u=[],h=e(t.features);!(s=h()).done;){var f=s.value;d(f,n,a,i,o)&&u.push(f)}return{type:"FeatureCollection",features:u}}(this.network,t)},a.getNetworkWithoutDuplicatesOrSubsections=function(){return function(t){for(var e=t.features,r=new Set,n=0;n<e.length;n++)for(var a=e[n].geometry.coordinates,i=0;i<e.length;i++)if(n!==i){var o=e[i].geometry.coordinates;if(h(a,o)&&(a.length<o.length||a.length===o.length&&n>i)){r.add(n);break}}return{type:"FeatureCollection",features:e.filter(function(t,e){return!r.has(e)})}}(this.network)},a.getConnectedComponents=function(){return function(t){var r=t.features,n=new Map,a=new Map;function i(t){return t[0]+","+t[1]}for(var o=0;o<r.length;o++)for(var s,u=e(r[o].geometry.coordinates);!(s=u()).done;){var h=i(s.value);a.has(h)||a.set(h,new Set),a.get(h).add(o)}for(var f=0;f<r.length;f++){n.set(f,new Set);for(var d,v=e(r[f].geometry.coordinates);!(d=v()).done;){var c=i(d.value),l=a.get(c);if(l)for(var g,p=e(l);!(g=p()).done;){var y=g.value;y!==f&&n.get(f).add(y)}}}var m=new Set,w=[];function M(t,a){for(var i=[t];i.length>0;){var o=i.pop();if(!m.has(o)){m.add(o),a.push(r[o]);var s=n.get(o);if(s)for(var u,h=e(s);!(u=h()).done;){var f=u.value;m.has(f)||i.push(f)}}}}for(var x=0;x<r.length;x++)if(!m.has(x)){var k=[];M(x,k),w.push({type:"FeatureCollection",features:k})}return w.sort(function(t,e){return t.features.length-e.features.length}),w}(this.network)},a.getConnectedComponentCount=function(){return function(t){for(var r,n=t.features,a=n.length,o=new Map,s=0;s<a;s++)for(var u,h=e(n[s].geometry.coordinates);!(u=h()).done;){var f=(r=u.value)[0]+","+r[1];o.has(f)||o.set(f,[]),o.get(f).push(s)}for(var d,v=Array.from({length:a},function(){return[]}),c=e(o.values());!(d=c()).done;)for(var l=d.value,g=0;g<l.length;g++)for(var p=g+1;p<l.length;p++){var y=l[g],m=l[p];v[y].push(m),v[m].push(y)}for(var w=new Array(a).fill(!1),M=0,x=0;x<a;x++)w[x]||(i(x,v,w),M++);return M}(this.network)},a.getNodeAndEdgeCount=function(){return function(t){for(var r,n=new Set,a=new Set,i=e(t.features);!(r=i()).done;){for(var o,s=r.value.geometry.coordinates,u=e(s);!(o=u()).done;)n.add(JSON.stringify(o.value));for(var h=0;h<s.length-1;h++){var f=(d=s[h+1],(v=JSON.stringify(s[h]))<(c=JSON.stringify(d))?v+"|"+c:c+"|"+v);a.add(f)}}var d,v,c;return{nodeCount:n.size,edgeCount:a.size}}(this.network)},a.getNodes=function(){return{type:"FeatureCollection",features:function(t){for(var r,n=new Set,a=[],i=e(t.features);!(r=i()).done;)for(var o,s=e(r.value.geometry.coordinates);!(o=s()).done;){var u=o.value,h=u.join(",");n.has(h)||(n.add(h),a.push({type:"Feature",geometry:{type:"Point",coordinates:u},properties:{}}))}return a}(this.network)}},a.getNodeCount=function(){return this.getNodeAndEdgeCount().nodeCount},a.getEdges=function(){return s(this.network)},a.getLongestEdgeLength=function(){var t=this.getLongestEdge();return t?u(t):-1},a.getShortestEdgeLength=function(){var t=this.getShortestEdge();return t?u(t):-1},a.getLongestEdge=function(){var t=this.getEdges().features;if(0===t.length)return null;var e=t.sort(function(t,e){return u(t)-u(e)});return e[e.length-1]},a.getShortestEdge=function(){var t=this.getEdges().features;return 0===t.length?null:t.sort(function(t,e){return u(t)-u(e)})[0]},a.getEdgeCount=function(){return this.getNodeAndEdgeCount().edgeCount},a.getLeafEdges=function(){return f(this.network).leafEdges},a.getPrunedEdges=function(t){if(t&&t>0){for(var e=this.network,r=0;r<t;r++)e=f(e).nonLeafEdges;return e}return f(this.network).nonLeafEdges},a.getUnifiedNetwork=function(t){return function(t,a){if(t.features.length<2)return t;for(var i,o=new Set,s=new Map,u=[],h=new Map,f=e(t.features);!(i=f()).done;)for(var d,v=e(i.value.geometry.coordinates);!(d=v()).done;){var c=d.value,g=c[0]+","+c[1];h.has(g)||(h.set(g,u.length),u.push(c))}for(var p=new l(u.length),y=0,m=u;y<m.length;y++){var L=m[y];p.add(L[0],L[1])}function E(t,r,i){for(var s,h=null,f=Infinity,d=e(function(t,e,r,n,a){void 0===n&&(n=Infinity),void 0===a&&(a=Infinity);var i=1,o=[];void 0===n&&(n=Infinity),void 0!==a&&(i=A(a/6371));for(var s=new w([],k),u={left:0,right:t.ids.length-1,axis:0,dist:0,minLng:-180,minLat:-90,maxLng:180,maxLat:90},h=Math.cos(r*M);u;){var f=u.right,d=u.left;if(f-d<=t.nodeSize)for(var v=d;v<=f;v++){var c=t.ids[v],l=I(e,r,t.coords[2*v],t.coords[2*v+1],h);s.push({id:c,dist:l})}else{var g=d+f>>1,p=t.coords[2*g],y=t.coords[2*g+1],m=t.ids[g],L=I(e,r,p,y,h);s.push({id:m,dist:L});var E=(u.axis+1)%2,b={left:d,right:g-1,axis:E,minLng:u.minLng,minLat:u.minLat,maxLng:0===u.axis?p:u.maxLng,maxLat:1===u.axis?y:u.maxLat,dist:0},C={left:g+1,right:f,axis:E,minLng:0===u.axis?p:u.minLng,minLat:1===u.axis?y:u.minLat,maxLng:u.maxLng,maxLat:u.maxLat,dist:0};b.dist=x(e,r,h,b),C.dist=x(e,r,h,C),s.push(b),s.push(C)}for(;s.length&&null!=s.peek().id;){var S=s.pop();if(S.dist>i)return o;if(o.push(S.id),o.length===n)return o}u=s.pop()}return o}(p,t[0],t[1],Infinity,a/1e3));!(s=d()).done;){var v=u[s.value],c=v[0]+","+v[1];if(o.has(c)&&!r.includes(v)&&!i.has(c)){var l=1e3*n(v,t);l<=a&&l<f&&(h=v,f=l)}}return null!==h?h:(o.add(t[0]+","+t[1]),t)}p.finish();var b=t.features.map(function(t){for(var n,a=[],i=[],o=new Set,u=e(t.geometry.coordinates);!(n=u()).done;){var h=n.value,f=h[0]+","+h[1];if(!s.has(f)){var d=E(h,a,o);o.has(d[0]+","+d[1])?s.set(f,h):s.set(f,d)}var v=s.get(f),c=v[0]+","+v[1];o.has(c)||(i.push(v),o.add(c)),a.push(h)}return r({},t,{geometry:r({},t.geometry,{coordinates:i})})});return r({},t,{features:b})}(this.network,t)},t}(),b=/*#__PURE__*/function(){function t(t){var e,r;this.network=null,this.distanceMeasurement=void 0,this.heapConstructor=void 0,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[],this.distanceMeasurement=null!=(e=null==t?void 0:t.distanceMeasurement)?e:n,this.heapConstructor=null!=(r=null==t?void 0:t.heap)?r:a}var r=t.prototype;return r.buildRouteGraph=function(t){this.network=t,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[];for(var r,n=this.coordinateIndexMap,a=this.coordinates,i=this.adjacencyList,o=this.distanceMeasurement,s=e(t.features);!(r=s()).done;)for(var u=r.value.geometry.coordinates,h=0;h<u.length-1;h++){var f=u[h],d=f[0],v=f[1],c=u[h+1],l=c[0],g=c[1],p=n.get(d);p||(p=new Map,n.set(d,p));var y=p.get(v);void 0===y&&(y=a.length,a.push(u[h]),p.set(v,y),i[y]=[]);var m=n.get(l);m||(m=new Map,n.set(l,m));var w=m.get(g);void 0===w&&(w=a.length,a.push(u[h+1]),m.set(g,w),i[w]=[]);var M=o(u[h],u[h+1]);i[y].push({node:w,distance:M}),i[w].push({node:y,distance:M})}},r.getRoute=function(t,r){if(!this.network)throw new Error("Network not built. Please call buildRouteGraph(network) first.");var n=this.getOrCreateIndex(t.geometry.coordinates),a=this.getOrCreateIndex(r.geometry.coordinates);if(n===a)return null;var i=new this.heapConstructor;i.insert(0,n);var o=this.coordinates.length,s=new Array(o).fill(Infinity),u=new Array(o).fill(-1),h=new Array(o).fill(!1);for(s[n]=0;i.size()>0;){var f=i.extractMin();if(!h[f]){if(f===a)break;h[f]=!0;for(var d,v=e(this.adjacencyList[f]||[]);!(d=v()).done;){var c=d.value,l=s[f]+c.distance;if(l<s[c.node]){s[c.node]=l,u[c.node]=f;var g=this.distanceMeasurement(this.coordinates[c.node],this.coordinates[a]);i.insert(l+g,c.node)}}}}if(u[a]<0)return null;for(var p=[],y=a;y!==n;)p.unshift(this.coordinates[y]),y=u[y];return p.unshift(this.coordinates[n]),{type:"Feature",geometry:{type:"LineString",coordinates:p},properties:{}}},r.getOrCreateIndex=function(t){var e=t[0],r=t[1],n=this.coordinateIndexMap.get(e);n||(n=new Map,this.coordinateIndexMap.set(e,n));var a=n.get(r);return void 0===a&&(a=this.coordinates.length,this.coordinates.push(t),n.set(r,a),this.adjacencyList[a]=[]),a},t}();exports.LineStringGraph=E,exports.TerraRoute=b,exports.createCheapRuler=function(t){var e=1/298.257223563,r=e*(2-e),n=Math.PI/180,a=Math.cos(t*n),i=1/(1-r*(1-a*a)),o=Math.sqrt(i),s=6378.137*n,u=s*o*a,h=s*o*i*(1-r);return function(t,e){for(var r=t[0]-e[0];r<-180;)r+=360;for(;r>180;)r-=360;var n=r*u,a=(t[1]-e[1])*h;return Math.sqrt(n*n+a*a)}},exports.haversineDistance=n;
|
|
1
|
+
var t=function(t,e){var r=function(t){return t*Math.PI/180},n=r(t[1]),i=r(t[0]),a=r(e[1]),s=a-n,o=r(e[0])-i,h=Math.sin(s/2)*Math.sin(s/2)+Math.cos(n)*Math.cos(a)*Math.sin(o/2)*Math.sin(o/2);return 2*Math.atan2(Math.sqrt(h),Math.sqrt(1-h))*6371e3/1e3};function e(t,e){(null==e||e>t.length)&&(e=t.length);for(var r=0,n=Array(e);r<e;r++)n[r]=t[r];return n}function r(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 i=0;return function(){return i>=t.length?{done:!0}:{done:!1,value:t[i++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function n(){return n=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var r=arguments[e];for(var n in r)({}).hasOwnProperty.call(r,n)&&(t[n]=r[n])}return t},n.apply(null,arguments)}function i(t,e,n){n[t]=!0;for(var a,s=r(e[t]);!(a=s()).done;){var o=a.value;n[o]||i(o,e,n)}}function a(t,e){var r=function(t,e){var r=t[0],n=e[0];return r<n||r===n&&t[1]<=e[1]?[t,e]:[e,t]}(t,e);return JSON.stringify([r[0],r[1]])}function s(t){for(var e,n=new Map,i=r(t.features);!(e=i()).done;)for(var s=e.value.geometry.coordinates,o=0;o<s.length-1;o++){var h=s[o],u=s[o+1],f=a(h,u);n.has(f)||n.set(f,{type:"Feature",geometry:{type:"LineString",coordinates:[h,u]},properties:{}})}return{type:"FeatureCollection",features:Array.from(n.values())}}function o(e){for(var r=e.geometry.coordinates,n=0,i=0;i<r.length-1;i++)n+=t(r[i],r[i+1]);return n}function h(t,e){var r=t.length,n=e.length;if(r>n)return!1;for(var i=0;i<=n-r;i++){for(var a=!0,s=0;s<r;s++)if(e[i+s][0]!==t[s][0]||e[i+s][1]!==t[s][1]){a=!1;break}if(a)return!0}for(var o=[].concat(t).reverse(),h=0;h<=n-r;h++){for(var u=!0,f=0;f<r;f++)if(e[h+f][0]!==o[f][0]||e[h+f][1]!==o[f][1]){u=!1;break}if(u)return!0}return!1}function u(t){var e=s(t),r=new Map;function n(t){return t.join(",")}for(var i=0;i<e.features.length;i++){var a=e.features[i].geometry.coordinates;if(!(a.length<2)){var o=n(a[0]),h=n(a[a.length-1]);r.set(o,(r.get(o)||0)+1),r.set(h,(r.get(h)||0)+1)}}for(var u=new Map,f=new Map,c=0;c<e.features.length;c++){var d=e.features[c],v=d.geometry.coordinates;if(!(v.length<2)){var l=n(v[0]),g=n(v[v.length-1]),p=r.get(l)||0,y=r.get(g)||0,m=v.map(n).join(";");1===p||1===y?u.has(m)||u.set(m,d):f.has(m)||f.set(m,d)}}return{leafEdges:{type:"FeatureCollection",features:Array.from(u.values())},nonLeafEdges:{type:"FeatureCollection",features:Array.from(f.values())}}}function f(t,e,n,i,a){for(var s,o=r(t.geometry.coordinates);!(s=o()).done;)if(!c(s.value,e,n,i,a))return!1;return!0}function c(t,e,r,n,i){var a=t[0],s=t[1];return a>=e&&a<=n&&s>=r&&s<=i}var d=[Int8Array,Uint8Array,Uint8ClampedArray,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array],v=/*#__PURE__*/function(){function t(t,e,r,n){if(void 0===e&&(e=64),void 0===r&&(r=Float64Array),this.data=void 0,this.ids=void 0,this.coords=void 0,this._pos=void 0,this._finished=void 0,this.numItems=void 0,this.nodeSize=void 0,this.ArrayType=void 0,this.IndexArrayType=void 0,isNaN(t)||t<0)throw new Error("Unexpected numItems value: "+t+".");this.numItems=t,this.nodeSize=Math.min(Math.max(e,2),65535),this.ArrayType=r,this.IndexArrayType=t<65536?Uint16Array:Uint32Array;var i=d.indexOf(this.ArrayType),a=2*t*this.ArrayType.BYTES_PER_ELEMENT,s=t*this.IndexArrayType.BYTES_PER_ELEMENT,o=(8-s%8)%8;if(i<0)throw new Error("Unexpected typed array class: "+r+".");n?(this.data=n,this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+s+o,2*t),this._pos=2*t,this._finished=!0):(this.data=new ArrayBuffer(8+a+s+o),this.ids=new this.IndexArrayType(this.data,8,t),this.coords=new this.ArrayType(this.data,8+s+o,2*t),this._pos=0,this._finished=!1,new Uint8Array(this.data,0,2).set([219,16+i]),new Uint16Array(this.data,2,1)[0]=this.nodeSize,new Uint32Array(this.data,4,1)[0]=this.numItems)}var e=t.prototype;return e.add=function(t,e){var r=this._pos>>1;return this.ids[r]=r,this.coords[this._pos++]=t,this.coords[this._pos++]=e,r},e.finish=function(){var t=this._pos>>1;if(t!==this.numItems)throw new Error("Added "+t+" items when expected "+this.numItems+".");return l(this.ids,this.coords,this.nodeSize,0,this.numItems-1,0),this._finished=!0,this},t}();function l(t,e,r,n,i,a){if(!(i-n<=r)){var s=n+i>>1;g(t,e,s,n,i,a),l(t,e,r,n,s-1,1-a),l(t,e,r,s+1,i,1-a)}}function g(t,e,r,n,i,a){for(;i>n;){if(i-n>600){var s=i-n+1,o=r-n+1,h=Math.log(s),u=.5*Math.exp(2*h/3),f=.5*Math.sqrt(h*u*(s-u)/s)*(o-s/2<0?-1:1);g(t,e,r,Math.max(n,Math.floor(r-o*u/s+f)),Math.min(i,Math.floor(r+(s-o)*u/s+f)),a)}var c=e[2*r+a],d=n,v=i;for(p(t,e,n,r),e[2*i+a]>c&&p(t,e,n,i);d<v;){for(p(t,e,d,v),d++,v--;e[2*d+a]<c;)d++;for(;e[2*v+a]>c;)v--}e[2*n+a]===c?p(t,e,n,v):p(t,e,++v,i),v<=r&&(n=v+1),r<=v&&(i=v-1)}}function p(t,e,r,n){y(t,r,n),y(e,2*r,2*n),y(e,2*r+1,2*n+1)}function y(t,e,r){var n=t[e];t[e]=t[r],t[r]=n}var m=/*#__PURE__*/function(){function t(t,e){if(void 0===t&&(t=[]),void 0===e&&(e=function(t,e){return t<e?-1:t>e?1:0}),this.data=void 0,this.length=void 0,this.compare=void 0,this.data=t,this.length=this.data.length,this.compare=e,this.length>0)for(var r=(this.length>>1)-1;r>=0;r--)this._down(r)}var e=t.prototype;return e.push=function(t){this.data.push(t),this._up(this.length++)},e.pop=function(){if(0!==this.length){var t=this.data[0],e=this.data.pop();return this.length--,this.length>0&&(this.data[0]=e,this._down(0)),t}},e.peek=function(){return this.data[0]},e._up=function(t){for(var e=this.data,r=this.compare,n=e[t];t>0;){var i=t-1>>1,a=e[i];if(r(n,a)>=0)break;e[t]=a,t=i}e[t]=n},e._down=function(t){for(var e=this.data,r=this.compare,n=this.length>>1,i=e[t];t<n;){var a=1+(t<<1),s=a+1;if(s<this.length&&r(e[s],e[a])<0&&(a=s),r(e[a],i)>=0)break;e[t]=e[a],t=a}e[t]=i},t}(),w=Math.PI/180;function M(t,e,r,n){var i=n.minLng,a=n.maxLng,s=n.minLat,o=n.maxLat;if(t>=i&&t<=a)return e<s?S((e-s)*w):e>o?S((e-o)*w):0;var h=Math.min(S((t-i)*w),S((t-a)*w)),u=function(t,e){var r=1-2*e;return r<=0?t>0?90:-90:Math.atan(Math.tan(t*w)/r)/w}(e,h);return u>s&&u<o?A(h,r,e,u):Math.min(A(h,r,e,s),A(h,r,e,o))}function x(t,e){return t.dist-e.dist}function S(t){var e=Math.sin(t/2);return e*e}function A(t,e,r,n){return e*Math.cos(n*w)*t+S((r-n)*w)}function I(t,e,r,n,i){return A(S((t-r)*w),i,e,n)}var k=/*#__PURE__*/function(){function e(t){this.network=void 0,this.network=t}var a=e.prototype;return a.setNetwork=function(t){this.network=t},a.getNetwork=function(){return this.network},a.getNetworkInBoundingBox=function(t){return function(t,e){var n=e[0],i=e[1],a=e[2],s=e[3];if(n>=a||i>=s)throw new Error("Invalid bounding box: min values must be less than max values");for(var o,h=[],u=r(t.features);!(o=u()).done;){var c=o.value;f(c,n,i,a,s)&&h.push(c)}return{type:"FeatureCollection",features:h}}(this.network,t)},a.getNetworkWithoutDuplicatesOrSubsections=function(){return function(t){for(var e=t.features,r=new Set,n=0;n<e.length;n++)for(var i=e[n].geometry.coordinates,a=0;a<e.length;a++)if(n!==a){var s=e[a].geometry.coordinates;if(h(i,s)&&(i.length<s.length||i.length===s.length&&n>a)){r.add(n);break}}return{type:"FeatureCollection",features:e.filter(function(t,e){return!r.has(e)})}}(this.network)},a.getConnectedComponents=function(){return function(t){var e=t.features,n=new Map,i=new Map;function a(t){return t[0]+","+t[1]}for(var s=0;s<e.length;s++)for(var o,h=r(e[s].geometry.coordinates);!(o=h()).done;){var u=a(o.value);i.has(u)||i.set(u,new Set),i.get(u).add(s)}for(var f=0;f<e.length;f++){n.set(f,new Set);for(var c,d=r(e[f].geometry.coordinates);!(c=d()).done;){var v=a(c.value),l=i.get(v);if(l)for(var g,p=r(l);!(g=p()).done;){var y=g.value;y!==f&&n.get(f).add(y)}}}var m=new Set,w=[];function M(t,i){for(var a=[t];a.length>0;){var s=a.pop();if(!m.has(s)){m.add(s),i.push(e[s]);var o=n.get(s);if(o)for(var h,u=r(o);!(h=u()).done;){var f=h.value;m.has(f)||a.push(f)}}}}for(var x=0;x<e.length;x++)if(!m.has(x)){var S=[];M(x,S),w.push({type:"FeatureCollection",features:S})}return w.sort(function(t,e){return t.features.length-e.features.length}),w}(this.network)},a.getConnectedComponentCount=function(){return function(t){for(var e,n=t.features,a=n.length,s=new Map,o=0;o<a;o++)for(var h,u=r(n[o].geometry.coordinates);!(h=u()).done;){var f=(e=h.value)[0]+","+e[1];s.has(f)||s.set(f,[]),s.get(f).push(o)}for(var c,d=Array.from({length:a},function(){return[]}),v=r(s.values());!(c=v()).done;)for(var l=c.value,g=0;g<l.length;g++)for(var p=g+1;p<l.length;p++){var y=l[g],m=l[p];d[y].push(m),d[m].push(y)}for(var w=new Array(a).fill(!1),M=0,x=0;x<a;x++)w[x]||(i(x,d,w),M++);return M}(this.network)},a.getNodeAndEdgeCount=function(){return function(t){for(var e,n=new Set,i=new Set,a=r(t.features);!(e=a()).done;){for(var s,o=e.value.geometry.coordinates,h=r(o);!(s=h()).done;)n.add(JSON.stringify(s.value));for(var u=0;u<o.length-1;u++){var f=(c=o[u+1],(d=JSON.stringify(o[u]))<(v=JSON.stringify(c))?d+"|"+v:v+"|"+d);i.add(f)}}var c,d,v;return{nodeCount:n.size,edgeCount:i.size}}(this.network)},a.getNodes=function(){return{type:"FeatureCollection",features:function(t){for(var e,n=new Set,i=[],a=r(t.features);!(e=a()).done;)for(var s,o=r(e.value.geometry.coordinates);!(s=o()).done;){var h=s.value,u=h.join(",");n.has(u)||(n.add(u),i.push({type:"Feature",geometry:{type:"Point",coordinates:h},properties:{}}))}return i}(this.network)}},a.getNodeCount=function(){return this.getNodeAndEdgeCount().nodeCount},a.getEdges=function(){return s(this.network)},a.getLongestEdgeLength=function(){var t=this.getLongestEdge();return t?o(t):-1},a.getShortestEdgeLength=function(){var t=this.getShortestEdge();return t?o(t):-1},a.getLongestEdge=function(){var t=this.getEdges().features;if(0===t.length)return null;var e=t.sort(function(t,e){return o(t)-o(e)});return e[e.length-1]},a.getShortestEdge=function(){var t=this.getEdges().features;return 0===t.length?null:t.sort(function(t,e){return o(t)-o(e)})[0]},a.getEdgeCount=function(){return this.getNodeAndEdgeCount().edgeCount},a.getLeafEdges=function(){return u(this.network).leafEdges},a.getPrunedEdges=function(t){if(t&&t>0){for(var e=this.network,r=0;r<t;r++)e=u(e).nonLeafEdges;return e}return u(this.network).nonLeafEdges},a.getUnifiedNetwork=function(e){return function(e,i){if(e.features.length<2)return e;for(var a,s=new Set,o=new Map,h=[],u=new Map,f=r(e.features);!(a=f()).done;)for(var c,d=r(a.value.geometry.coordinates);!(c=d()).done;){var l=c.value,g=l[0]+","+l[1];u.has(g)||(u.set(g,h.length),h.push(l))}for(var p=new v(h.length),y=0,A=h;y<A.length;y++){var k=A[y];p.add(k[0],k[1])}function L(e,n,a){for(var o,u=null,f=Infinity,c=r(function(t,e,r,n,i){void 0===n&&(n=Infinity),void 0===i&&(i=Infinity);var a=1,s=[];void 0===n&&(n=Infinity),void 0!==i&&(a=S(i/6371));for(var o=new m([],x),h={left:0,right:t.ids.length-1,axis:0,dist:0,minLng:-180,minLat:-90,maxLng:180,maxLat:90},u=Math.cos(r*w);h;){var f=h.right,c=h.left;if(f-c<=t.nodeSize)for(var d=c;d<=f;d++){var v=t.ids[d],l=I(e,r,t.coords[2*d],t.coords[2*d+1],u);o.push({id:v,dist:l})}else{var g=c+f>>1,p=t.coords[2*g],y=t.coords[2*g+1],A=t.ids[g],k=I(e,r,p,y,u);o.push({id:A,dist:k});var L=(h.axis+1)%2,C={left:c,right:g-1,axis:L,minLng:h.minLng,minLat:h.minLat,maxLng:0===h.axis?p:h.maxLng,maxLat:1===h.axis?y:h.maxLat,dist:0},E={left:g+1,right:f,axis:L,minLng:0===h.axis?p:h.minLng,minLat:1===h.axis?y:h.minLat,maxLng:h.maxLng,maxLat:h.maxLat,dist:0};C.dist=M(e,r,u,C),E.dist=M(e,r,u,E),o.push(C),o.push(E)}for(;o.length&&null!=o.peek().id;){var b=o.pop();if(b.dist>a)return s;if(s.push(b.id),s.length===n)return s}h=o.pop()}return s}(p,e[0],e[1],Infinity,i/1e3));!(o=c()).done;){var d=h[o.value],v=d[0]+","+d[1];if(s.has(v)&&!n.includes(d)&&!a.has(v)){var l=1e3*t(d,e);l<=i&&l<f&&(u=d,f=l)}}return null!==u?u:(s.add(e[0]+","+e[1]),e)}p.finish();var C=e.features.map(function(t){for(var e,i=[],a=[],s=new Set,h=r(t.geometry.coordinates);!(e=h()).done;){var u=e.value,f=u[0]+","+u[1];if(!o.has(f)){var c=L(u,i,s);s.has(c[0]+","+c[1])?o.set(f,u):o.set(f,c)}var d=o.get(f),v=d[0]+","+d[1];s.has(v)||(a.push(d),s.add(v)),i.push(u)}return n({},t,{geometry:n({},t.geometry,{coordinates:a})})});return n({},e,{features:C})}(this.network,e)},e}(),L=/*#__PURE__*/function(){function t(){this.keys=[],this.values=[],this.idxs=[],this.length=0,this.insertCounter=0}var e=t.prototype;return e.insert=function(t,e){var r=this.length;this.length=r+1;for(var n=t,i=e,a=this.insertCounter++;r>0;){var s=r-1>>>2,o=this.keys[s],h=this.idxs[s];if(n>o||n===o&&a>h)break;this.keys[r]=o,this.values[r]=this.values[s],this.idxs[r]=h,r=s}this.keys[r]=n,this.values[r]=i,this.idxs[r]=a},e.extractMin=function(){var t=this.length;if(0===t)return null;var e=this.values[0],r=t-1;return this.length=r,r>0&&(this.keys[0]=this.keys[r],this.values[0]=this.values[r],this.idxs[0]=this.idxs[r],this.bubbleDown(0)),e},e.size=function(){return this.length},e.bubbleDown=function(t){for(var e=this.length,r=this.keys,n=this.values,i=this.idxs,a=r[t],s=n[t],o=i[t];;){var h=1+(t<<2);if(h>=e)break;var u=h,f=r[h],c=i[h],d=n[h],v=h+1;if(v<e){var l=r[v],g=i[v];(l<f||l===f&&g<c)&&(u=v,f=l,c=g,d=n[v])}var p=h+2;if(p<e){var y=r[p],m=i[p];(y<f||y===f&&m<c)&&(u=p,f=y,c=m,d=n[p])}var w=h+3;if(w<e){var M=r[w],x=i[w];(M<f||M===f&&x<c)&&(u=w,f=M,c=x,d=n[w])}if(!(f<a||f===a&&c<o))break;r[t]=f,n[t]=d,i[t]=c,t=u}r[t]=a,n[t]=s,i[t]=o},t}(),C=/*#__PURE__*/function(){function e(e){var r,n;this.network=null,this.distanceMeasurement=void 0,this.heapConstructor=void 0,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[],this.csrOffsets=null,this.csrIndices=null,this.csrDistances=null,this.csrNodeCount=0,this.gScoreScratch=null,this.cameFromScratch=null,this.visitedScratch=null,this.hScratch=null,this.scratchCapacity=0,this.distanceMeasurement=null!=(r=null==e?void 0:e.distanceMeasurement)?r:t,this.heapConstructor=null!=(n=null==e?void 0:e.heap)?n:L}var r=e.prototype;return r.buildRouteGraph=function(t){this.network=t,this.coordinateIndexMap=new Map,this.coordinates=[],this.adjacencyList=[],this.csrOffsets=null,this.csrIndices=null,this.csrDistances=null,this.csrNodeCount=0;for(var e=this.coordinateIndexMap,r=this.coordinates,n=this.distanceMeasurement,i=t.features,a=[],s=0,o=i.length;s<o;s++)for(var h=i[s].geometry.coordinates,u=0,f=h.length-1;u<f;u++){var c,d,v=h[u],l=h[u+1],g=v[0],p=v[1],y=l[0],m=l[1],w=e.get(g);void 0===w&&(w=new Map,e.set(g,w));var M=w.get(p);void 0===M&&(M=r.length,r.push(v),w.set(p,M));var x=e.get(y);void 0===x&&(x=new Map,e.set(y,x));var S=x.get(m);void 0===S&&(S=r.length,r.push(l),x.set(m,S)),a[M]=(null!=(c=a[M])?c:0)+1,a[S]=(null!=(d=a[S])?d:0)+1}var A=this.coordinates.length;this.csrNodeCount=A;for(var I=new Int32Array(A+1),k=0;k<A;k++){var L,C=null!=(L=a[k])?L:0;I[k+1]=I[k]+C}for(var E=I[A],b=new Int32Array(E),N=new Float64Array(E),O=I.slice(),F=0,_=i.length;F<_;F++)for(var T=i[F].geometry.coordinates,U=0,j=T.length-1;U<j;U++){var P=T[U],z=T[U+1],D=P[1],R=z[0],q=z[1],B=this.coordinateIndexMap.get(P[0]).get(D),J=this.coordinateIndexMap.get(R).get(q),G=n(P,z),Y=O[B]++;b[Y]=J,N[Y]=G,b[Y=O[J]++]=B,N[Y]=G}this.csrOffsets=I,this.csrIndices=b,this.csrDistances=N,this.adjacencyList=new Array(A)},r.getRoute=function(t,e){if(null===this.network)throw new Error("Network not built. Please call buildRouteGraph(network) first.");var r=this.getOrCreateIndex(t.geometry.coordinates),n=this.getOrCreateIndex(e.geometry.coordinates);if(r===n)return null;var i=this.coordinates,a=this.adjacencyList,s=this.distanceMeasurement,o=i.length;this.ensureScratch(o);var h=this.gScoreScratch,u=this.cameFromScratch,f=this.visitedScratch,c=this.hScratch;h.fill(Number.POSITIVE_INFINITY,0,o),u.fill(-1,0,o),f.fill(0,0,o),c.fill(-1,0,o);var d=new this.heapConstructor,v=i[n],l=c[r];for(l<0&&(l=s(i[r],v),c[r]=l),d.insert(l,r),h[r]=0;d.size()>0;){var g=d.extractMin();if(0===f[g]){if(g===n)break;if(f[g]=1,this.csrOffsets&&g<this.csrNodeCount)for(var p=this.csrOffsets,y=this.csrIndices,m=this.csrDistances,w=p[g+1],M=p[g];M<w;M++){var x=y[M],S=h[g]+m[M];if(S<h[x]){h[x]=S,u[x]=g;var A=c[x];A<0&&(A=s(i[x],v),c[x]=A),d.insert(S+A,x)}}else{var I=a[g];if(!I||0===I.length)continue;for(var k=0,L=I.length;k<L;k++){var C=I[k],E=C.node,b=h[g]+C.distance;if(b<h[E]){h[E]=b,u[E]=g;var N=c[E];N<0&&(N=s(i[E],v),c[E]=N),d.insert(b+N,E)}}}}}if(u[n]<0)return null;for(var O=[],F=n;F!==r;)O.push(i[F]),F=u[F];return O.push(i[r]),O.reverse(),{type:"Feature",geometry:{type:"LineString",coordinates:O},properties:{}}},r.getOrCreateIndex=function(t){var e=t[0],r=t[1],n=this.coordinateIndexMap.get(e);void 0===n&&(n=new Map,this.coordinateIndexMap.set(e,n));var i=n.get(r);if(void 0===i&&(i=this.coordinates.length,this.coordinates.push(t),n.set(r,i),this.adjacencyList[i]=[],this.csrOffsets)){var a=this.csrNodeCount;if(i===a){var s=new Int32Array(a+2);s.set(this.csrOffsets,0),s[a+1]=s[a],this.csrOffsets=s,this.csrNodeCount=a+1}}return i},r.ensureScratch=function(t){if(!(this.scratchCapacity>=t&&this.gScoreScratch&&this.cameFromScratch&&this.visitedScratch&&this.hScratch)){var e=0|t;this.gScoreScratch=new Float64Array(e),this.cameFromScratch=new Int32Array(e),this.visitedScratch=new Uint8Array(e),this.hScratch=new Float64Array(e),this.scratchCapacity=e}},e}();exports.LineStringGraph=k,exports.TerraRoute=C,exports.createCheapRuler=function(t){var e=1/298.257223563,r=e*(2-e),n=Math.PI/180,i=Math.cos(t*n),a=1/(1-r*(1-i*i)),s=Math.sqrt(a),o=6378.137*n,h=o*s*i,u=o*s*a*(1-r);return function(t,e){for(var r=t[0]-e[0];r<-180;)r+=360;for(;r>180;)r-=360;var n=r*h,i=(t[1]-e[1])*u;return Math.sqrt(n*n+i*i)}},exports.haversineDistance=t;
|
|
2
2
|
//# sourceMappingURL=terra-route.cjs.map
|