1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | import theSphericalTrigonometry from '../core/lib/SphericalTrigonometry.js'; |
26 | |
27 | import { ZERO, ONE, TWO, THREE } from '../main/Constants.js'; |
28 | |
29 | |
30 | |
31 | |
32 | |
33 | |
34 | |
35 | |
36 | class PublicTransportHolesRemover { |
37 | |
38 | #publicTransportData; |
39 | |
40 | #computeDistances ( node1, node2, distancesBetweenWays ) { |
41 | if ( node1.isNode3Ways || node2.isNode3Ways ) { |
42 | return; |
43 | } |
44 | distancesBetweenWays.push ( |
45 | { |
46 | distance : theSphericalTrigonometry.pointsDistance ( [ node1.lat, node1.lon ], [ node2.lat, node2.lon ] ), |
47 | nodesId : [ node1.id, node2.id ] |
48 | } |
49 | ); |
50 | } |
51 | |
52 | |
53 | |
54 | |
55 | #removeHoles ( ) { |
56 | |
57 | |
58 | |
59 | |
60 | const distancesBetweenWays = []; |
61 | const waysArray = Array.from ( this.#publicTransportData.waysMap.values ( ) ); |
62 | let loopCounter = ONE; |
63 | |
64 | waysArray.forEach ( |
65 | way => { |
66 | for ( let wayCounter = loopCounter; wayCounter < waysArray.length; wayCounter ++ ) { |
67 | const nodesIds = []; |
68 | nodesIds.push ( this.#publicTransportData.nodesMap.get ( |
69 | this.#publicTransportData.firstOf ( way.nodesIds ) ) |
70 | ); |
71 | nodesIds.push ( this.#publicTransportData.nodesMap.get ( |
72 | this.#publicTransportData.lastOf ( way.nodesIds ) ) |
73 | ); |
74 | nodesIds.push ( this.#publicTransportData.nodesMap.get ( |
75 | this.#publicTransportData.firstOf ( waysArray [ wayCounter ].nodesIds ) ) |
76 | ); |
77 | nodesIds.push ( this.#publicTransportData.nodesMap.get ( |
78 | this.#publicTransportData.lastOf ( waysArray [ wayCounter ].nodesIds ) ) |
79 | ); |
80 | |
81 | this.#computeDistances ( nodesIds [ ZERO ], nodesIds [ TWO ], distancesBetweenWays ); |
82 | this.#computeDistances ( nodesIds [ ZERO ], nodesIds [ THREE ], distancesBetweenWays ); |
83 | this.#computeDistances ( nodesIds [ ONE ], nodesIds [ TWO ], distancesBetweenWays ); |
84 | this.#computeDistances ( nodesIds [ ONE ], nodesIds [ THREE ], distancesBetweenWays ); |
85 | } |
86 | loopCounter ++; |
87 | } |
88 | ); |
89 | |
90 | |
91 | let minDistance = distancesBetweenWays [ ZERO ]; |
92 | distancesBetweenWays.forEach ( |
93 | distanceBetwwenWays => { |
94 | if ( distanceBetwwenWays.distance < minDistance.distance ) { |
95 | minDistance = distanceBetwwenWays; |
96 | } |
97 | } |
98 | ); |
99 | |
100 | |
101 | const newWay = { |
102 | id : this.#publicTransportData.newId, |
103 | type : 'way', |
104 | nodesIds : minDistance.nodesId, |
105 | distance : minDistance.distance |
106 | }; |
107 | this.#publicTransportData.waysMap.set ( newWay.id, newWay ); |
108 | |
109 | |
110 | const startNode = this.#publicTransportData.nodesMap.get ( minDistance.nodesId [ ZERO ] ); |
111 | const wayIdAtStart = startNode.startingWaysIds.concat ( startNode.endingWaysIds ) [ ZERO ]; |
112 | startNode.startingWaysIds.push ( newWay.id ); |
113 | const endNode = this.#publicTransportData.nodesMap.get ( minDistance.nodesId [ ONE ] ); |
114 | const wayIdAtEnd = endNode.startingWaysIds.concat ( endNode.endingWaysIds ) [ ZERO ]; |
115 | endNode.endingWaysIds.push ( newWay.id ); |
116 | |
117 | |
118 | this.#publicTransportData.mergeWays ( this.#publicTransportData.mergeWays ( newWay.id, wayIdAtStart ), wayIdAtEnd ); |
119 | |
120 | |
121 | if ( this.#publicTransportData.waysMap.size > ( ( this.#publicTransportData.nodes3WaysCounter * TWO ) + ONE ) ) { |
122 | this.#removeHoles ( ); |
123 | } |
124 | } |
125 | |
126 | |
127 | |
128 | |
129 | |
130 | constructor ( publicTransportData ) { |
131 | Object.freeze ( this ); |
132 | this.#publicTransportData = publicTransportData; |
133 | } |
134 | |
135 | removeHoles ( ) { |
136 | this.#removeHoles ( ); |
137 | } |
138 | } |
139 | |
140 | export default PublicTransportHolesRemover; |
141 | |
142 | |
143 | |