| 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 theConfig from '../../data/Config.js'; |
| 26 | import theSphericalTrigonometry from '../../core/lib/SphericalTrigonometry.js'; |
| 27 | import SvgBuilder from './SvgBuilder.js'; |
| 28 | import OverpassAPIDataLoader from '../../core/lib/OverpassAPIDataLoader.js'; |
| 29 | import StreetFinder from './StreetFinder.js'; |
| 30 | import ArrowAndTooltipFinder from './ArrowAndTooltipFinder.js'; |
| 31 | import TranslationRotationFinder from './TranslationRotationFinder.js'; |
| 32 | import NoteDataForMapIcon from './NoteDataForMapIcon.js'; |
| 33 | import ComputeDataForMapIcon from './ComputeDataForMapIcon.js'; |
| 34 | |
| 35 | import { ICON_POSITION, ICON_DIMENSIONS, LAT_LNG, INVALID_OBJ_ID, ZERO, ONE } from '../../main/Constants.js'; |
| 36 | |
| 37 | |
| 38 | |
| 39 | |
| 40 | |
| 41 | |
| 42 | |
| 43 | class MapIconFromOsmFactory { |
| 44 | |
| 45 | |
| 46 | |
| 47 | |
| 48 | |
| 49 | |
| 50 | #noteData; |
| 51 | |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | #computeData; |
| 58 | |
| 59 | |
| 60 | |
| 61 | |
| 62 | |
| 63 | |
| 64 | #overpassAPIDataLoader; |
| 65 | |
| 66 | |
| 67 | |
| 68 | |
| 69 | |
| 70 | |
| 71 | #queryDistance; |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | #requestStarted; |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | static get #SEARCH_AROUND_FACTOR ( ) { return 1.5; } |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | #searchNearestItineraryPoint ( ) { |
| 93 | |
| 94 | let nearestItineraryPoint = null; |
| 95 | |
| 96 | |
| 97 | let minDistance = Number.MAX_VALUE; |
| 98 | |
| 99 | |
| 100 | this.#computeData.route.itinerary.itineraryPoints.forEach ( |
| 101 | itineraryPoint => { |
| 102 | const itineraryPointDistance = |
| 103 | theSphericalTrigonometry.pointsDistance ( this.#noteData.latLng, itineraryPoint.latLng ); |
| 104 | if ( minDistance > itineraryPointDistance ) { |
| 105 | minDistance = itineraryPointDistance; |
| 106 | nearestItineraryPoint = itineraryPoint; |
| 107 | } |
| 108 | } |
| 109 | ); |
| 110 | |
| 111 | |
| 112 | this.#noteData.latLng = nearestItineraryPoint.latLng; |
| 113 | this.#computeData.nearestItineraryPointObjId = nearestItineraryPoint.objId; |
| 114 | } |
| 115 | |
| 116 | |
| 117 | |
| 118 | |
| 119 | |
| 120 | #buildIconAndAdress ( ) { |
| 121 | |
| 122 | |
| 123 | new TranslationRotationFinder ( ).findData ( this.#computeData, this.#noteData ); |
| 124 | new ArrowAndTooltipFinder ( ).findData ( this.#computeData, this.#noteData ); |
| 125 | new StreetFinder ( ).findData ( this.#computeData, this.#noteData, this.#overpassAPIDataLoader ); |
| 126 | new SvgBuilder ( ).buildSvg ( this.#computeData, this.#noteData, this.#overpassAPIDataLoader ); |
| 127 | |
| 128 | this.#requestStarted = false; |
| 129 | |
| 130 | |
| 131 | return this.#noteData; |
| 132 | } |
| 133 | |
| 134 | |
| 135 | |
| 136 | |
| 137 | |
| 138 | async #exeGetIconAndAdress ( ) { |
| 139 | if ( this.#requestStarted ) { |
| 140 | |
| 141 | |
| 142 | return null; |
| 143 | } |
| 144 | |
| 145 | this.#requestStarted = true; |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | this.#computeData.nearestItineraryPointObjId = INVALID_OBJ_ID; |
| 151 | this.#computeData.positionOnRoute = ICON_POSITION.onRoute; |
| 152 | this.#computeData.direction = null; |
| 153 | this.#computeData.directionArrow = ' '; |
| 154 | this.#computeData.translation = [ ZERO, ZERO ]; |
| 155 | this.#computeData.rotation = ZERO; |
| 156 | this.#computeData.rcnRef = ''; |
| 157 | |
| 158 | this.#noteData.iconContent = ''; |
| 159 | this.#noteData.tooltipContent = ''; |
| 160 | this.#noteData.address = ''; |
| 161 | |
| 162 | |
| 163 | this.#searchNearestItineraryPoint ( ); |
| 164 | |
| 165 | |
| 166 | |
| 167 | |
| 168 | |
| 169 | |
| 170 | |
| 171 | |
| 172 | |
| 173 | const queryLatLng = |
| 174 | this.#noteData.latLng [ ZERO ].toFixed ( LAT_LNG.fixed ) + |
| 175 | ',' + |
| 176 | this.#noteData.latLng [ ONE ].toFixed ( LAT_LNG.fixed ); |
| 177 | |
| 178 | const queries = [ |
| 179 | 'way[highway](around:' + |
| 180 | ( ICON_DIMENSIONS.svgViewboxDim * MapIconFromOsmFactory.#SEARCH_AROUND_FACTOR ).toFixed ( ZERO ) + |
| 181 | ',' + queryLatLng + ')->.a;(.a >;.a;)->.a;.a out;' + |
| 182 | 'is_in(' + queryLatLng + ')->.e;area.e[admin_level][boundary="administrative"];out;' + |
| 183 | 'node(around:' + this.#queryDistance + ',' + queryLatLng + ')[place];out;' |
| 184 | ]; |
| 185 | |
| 186 | await this.#overpassAPIDataLoader.loadData ( queries, this.#noteData.latLng ); |
| 187 | if ( this.#overpassAPIDataLoader.statusOk ) { |
| 188 | return this.#buildIconAndAdress ( ); |
| 189 | } |
| 190 | return null; |
| 191 | } |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | |
| 198 | |
| 199 | async #exeGetIconAndAdressWithPromise ( onOk, onError ) { |
| 200 | const result = await this.#exeGetIconAndAdress ( ); |
| 201 | |
| 202 | if ( result ) { |
| 203 | onOk ( result ); |
| 204 | } |
| 205 | else { |
| 206 | onError ( 'An error occurs...' ); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | |
| 211 | |
| 212 | |
| 213 | |
| 214 | constructor ( ) { |
| 215 | Object.freeze ( this ); |
| 216 | this.#noteData = new NoteDataForMapIcon ( ); |
| 217 | this.#computeData = new ComputeDataForMapIcon ( ); |
| 218 | this.#overpassAPIDataLoader = new OverpassAPIDataLoader ( { searchRelations : false, setGeometry : false } ); |
| 219 | this.#queryDistance = Math.max ( |
| 220 | theConfig.geoCoder.distances.hamlet, |
| 221 | theConfig.geoCoder.distances.village, |
| 222 | theConfig.geoCoder.distances.city, |
| 223 | theConfig.geoCoder.distances.town |
| 224 | ); |
| 225 | this.#requestStarted = false; |
| 226 | } |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | |
| 234 | |
| 235 | async getIconAndAdressAsync ( iconLatLng, route ) { |
| 236 | this.#noteData.latLng = iconLatLng; |
| 237 | this.#computeData.route = route; |
| 238 | |
| 239 | return this.#exeGetIconAndAdress ( ); |
| 240 | } |
| 241 | |
| 242 | |
| 243 | |
| 244 | |
| 245 | |
| 246 | |
| 247 | |
| 248 | |
| 249 | getIconAndAdressWithPromise ( mapIconData ) { |
| 250 | this.#noteData.latLng = mapIconData.latLng; |
| 251 | this.#computeData.route = mapIconData.route; |
| 252 | |
| 253 | return new Promise ( ( onOk, onError ) => this.#exeGetIconAndAdressWithPromise ( onOk, onError ) ); |
| 254 | } |
| 255 | |
| 256 | } |
| 257 | |
| 258 | export default MapIconFromOsmFactory; |
| 259 | |
| 260 | |
| 261 | |