1 | /* |
2 | Copyright - 2017 2023 - wwwouaiebe - Contact: https://www.ouaie.be/ |
3 | |
4 | This program is free software; |
5 | you can redistribute it and/or modify it under the terms of the |
6 | GNU General Public License as published by the Free Software Foundation; |
7 | either version 3 of the License, or any later version. |
8 | |
9 | This program is distributed in the hope that it will be useful, |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | GNU General Public License for more details. |
13 | |
14 | You should have received a copy of the GNU General Public License |
15 | along with this program; if not, write to the Free Software |
16 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
17 | */ |
18 | /* |
19 | Changes: |
20 | - v4.0.0: |
21 | - created from v3.6.0 |
22 | Doc reviewed 202208 |
23 | */ |
24 | |
25 | import theTravelNotesData from '../data/TravelNotesData.js'; |
26 | import theGeometry from '../core/lib/Geometry.js'; |
27 | import theSphericalTrigonometry from '../core/lib/SphericalTrigonometry.js'; |
28 | import NoteAndRoute from '../data/NoteAndRoute.js'; |
29 | import DataSearchEngineNearestRouteData from './DataSearchEngineNearestRouteData.js'; |
30 | |
31 | import { INVALID_OBJ_ID } from '../main/Constants.js'; |
32 | |
33 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
34 | /** |
35 | Class with helper methods to search data |
36 | See theDataSearchEngine for the one and only one instance of this class |
37 | */ |
38 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
39 | |
40 | class DataSearchEngine { |
41 | |
42 | /** |
43 | Helper method for the getNearestRouteData method |
44 | @param {Route} route The route that must be treated |
45 | @param {Array.<Number>} latLng The latitude and longitude of the nearest point on route |
46 | @param {DataSearchEngineNearestRouteData} nearestRouteData the DataSearchEngineNearestRouteData object used |
47 | */ |
48 | |
49 | #setNearestRouteData ( route, latLng, nearestRouteData ) { |
50 | if ( route.objId !== theTravelNotesData.editedRouteObjId ) { |
51 | const pointAndDistance = theGeometry.getClosestLatLngDistance ( route, latLng ); |
52 | if ( pointAndDistance ) { |
53 | const distanceToRoute = theSphericalTrigonometry.pointsDistance ( |
54 | latLng, |
55 | pointAndDistance.latLng |
56 | ); |
57 | if ( distanceToRoute < nearestRouteData.distance ) { |
58 | nearestRouteData.route = route; |
59 | nearestRouteData.distance = distanceToRoute; |
60 | nearestRouteData.latLngOnRoute = pointAndDistance.latLng; |
61 | nearestRouteData.distanceOnRoute = pointAndDistance.distance; |
62 | } |
63 | } |
64 | } |
65 | } |
66 | |
67 | /** |
68 | The constructor |
69 | */ |
70 | |
71 | constructor ( ) { |
72 | Object.freeze ( this ); |
73 | } |
74 | |
75 | /** |
76 | This method search route data for the nearest route of a given point |
77 | @param {Array.<Number>} latLng The latitude and longitude of the point |
78 | @return {DataSearchEngineNearestRouteData} A DataSearchEngineNearestRouteData object |
79 | */ |
80 | |
81 | getNearestRouteData ( latLng ) { |
82 | const nearestRouteData = new DataSearchEngineNearestRouteData ( ); |
83 | |
84 | theTravelNotesData.travel.routes.forEach ( route => this.#setNearestRouteData ( route, latLng, nearestRouteData ) ); |
85 | if ( INVALID_OBJ_ID !== theTravelNotesData.editedRouteObjId ) { |
86 | this.#setNearestRouteData ( theTravelNotesData.travel.editedRoute, latLng, nearestRouteData ); |
87 | } |
88 | |
89 | return Object.freeze ( nearestRouteData ); |
90 | } |
91 | |
92 | /** |
93 | Search a route with the route objId |
94 | @param {Number} routeObjId the objId of the route to search |
95 | @return {?Route} the searched route or null if not found |
96 | */ |
97 | |
98 | getRoute ( routeObjId ) { |
99 | let route = null; |
100 | route = theTravelNotesData.travel.routes.getAt ( routeObjId ); |
101 | if ( ! route ) { |
102 | if ( routeObjId === theTravelNotesData.travel.editedRoute.objId ) { |
103 | route = theTravelNotesData.travel.editedRoute; |
104 | } |
105 | } |
106 | return route; |
107 | } |
108 | |
109 | /** |
110 | Search a Note and a the Route to witch the Note is attached with the Note objId |
111 | @param {Number} noteObjId the objId of the note to search |
112 | @return {NoteAndRoute} a NoteAndRoute object with the route and note |
113 | */ |
114 | |
115 | getNoteAndRoute ( noteObjId ) { |
116 | let note = null; |
117 | let route = null; |
118 | note = theTravelNotesData.travel.notes.getAt ( noteObjId ); |
119 | if ( ! note ) { |
120 | const routeIterator = theTravelNotesData.travel.routes.iterator; |
121 | while ( ! ( routeIterator.done || note ) ) { |
122 | note = routeIterator.value.notes.getAt ( noteObjId ); |
123 | if ( note ) { |
124 | route = routeIterator.value; |
125 | } |
126 | } |
127 | if ( ! note ) { |
128 | note = theTravelNotesData.travel.editedRoute.notes.getAt ( noteObjId ); |
129 | if ( note ) { |
130 | route = theTravelNotesData.travel.editedRoute; |
131 | } |
132 | } |
133 | } |
134 | return new NoteAndRoute ( note, route ); |
135 | } |
136 | |
137 | /** |
138 | Search a WayPoint with the WayPoint objId |
139 | @param {Number} wayPointObjId the objId of the WayPoint to search |
140 | @return {WayPoint} a WayPoint |
141 | */ |
142 | |
143 | getWayPoint ( wayPointObjId ) { |
144 | let wayPoint = theTravelNotesData.travel.editedRoute.wayPoints.getAt ( wayPointObjId ); |
145 | if ( ! wayPoint ) { |
146 | const routeIterator = theTravelNotesData.travel.routes.iterator; |
147 | while ( ! ( routeIterator.done || wayPoint ) ) { |
148 | wayPoint = routeIterator.value.wayPoints.getAt ( wayPointObjId ); |
149 | } |
150 | } |
151 | return wayPoint; |
152 | } |
153 | } |
154 | |
155 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
156 | /** |
157 | The one and only one instance of DataSearchEngine class |
158 | @type {DataSearchEngine} |
159 | */ |
160 | /* ------------------------------------------------------------------------------------------------------------------------- */ |
161 | |
162 | const theDataSearchEngine = new DataSearchEngine ( ); |
163 | |
164 | export default theDataSearchEngine; |
165 | |
166 | /* --- End of file --------------------------------------------------------------------------------------------------------- */ |
167 |