|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Connector to find routing. Connector based on service YOURS. |
| 4 | + * |
| 5 | + * @package App |
| 6 | + * |
| 7 | + * @copyright YetiForce Sp. z o.o |
| 8 | + * @license YetiForce Public License 3.0 (licenses/LicenseEN.txt or yetiforce.com) |
| 9 | + * @author Mariusz Krzaczkowski <m.krzaczkowski@yetiforce.com> |
| 10 | + * |
| 11 | + * @see https://wiki.openstreetmap.org/wiki/YOURS |
| 12 | + */ |
| 13 | + |
| 14 | +namespace App\Map\Routing; |
| 15 | + |
| 16 | +/** |
| 17 | + * Connector for service YOURS to get routing. |
| 18 | + */ |
| 19 | +class Yours extends Base |
| 20 | +{ |
| 21 | + /** |
| 22 | + * {@inheritdoc} |
| 23 | + */ |
| 24 | + public function calculate() |
| 25 | + { |
| 26 | + if (!\App\RequestUtil::isNetConnection()) { |
| 27 | + throw new \App\Exceptions\AppException('ERR_NO_INTERNET_CONNECTION'); |
| 28 | + } |
| 29 | + $coordinates = []; |
| 30 | + $travel = $distance = 0; |
| 31 | + $description = ''; |
| 32 | + foreach ($this->parsePoints() as $track) { |
| 33 | + $url = $this->url . '?format=geojson&flat=' . $track['startLat'] . '&flon=' . $track['startLon'] . '&tlat=' . $track['endLat'] . '&tlon=' . $track['endLon'] . '&lang=' . \App\Language::getLanguage() . '&instructions=1'; |
| 34 | + \App\Log::beginProfile("GET|Yours::calculate|{$url}", __NAMESPACE__); |
| 35 | + $response = (new \GuzzleHttp\Client(\App\RequestHttp::getOptions()))->request('GET', $url, [ |
| 36 | + 'timeout' => 60, |
| 37 | + 'http_errors' => false, |
| 38 | + ]); |
| 39 | + \App\Log::endProfile("GET|Yours::calculate|{$url}", __NAMESPACE__); |
| 40 | + if (200 === $response->getStatusCode()) { |
| 41 | + $json = \App\Json::decode($response->getBody()); |
| 42 | + } else { |
| 43 | + throw new \App\Exceptions\AppException('Error with connection |' . $response->getReasonPhrase() . '|' . $response->getBody()); |
| 44 | + } |
| 45 | + $coordinates = array_merge($coordinates, $json['coordinates']); |
| 46 | + $description .= $json['properties']['description']; |
| 47 | + $travel += $json['properties']['traveltime']; |
| 48 | + $distance += $json['properties']['distance']; |
| 49 | + } |
| 50 | + $this->geoJson = [ |
| 51 | + 'type' => 'LineString', |
| 52 | + 'coordinates' => $coordinates, |
| 53 | + ]; |
| 54 | + $this->travelTime = $travel; |
| 55 | + $this->distance = $distance; |
| 56 | + $this->description = $description; |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * {@inheritdoc} |
| 61 | + */ |
| 62 | + public function parsePoints(): array |
| 63 | + { |
| 64 | + $tracks = []; |
| 65 | + $startLat = $this->start['lat']; |
| 66 | + $startLon = $this->start['lon']; |
| 67 | + if (!empty($this->indirectPoints)) { |
| 68 | + foreach ($this->indirectPoints as $tempLon) { |
| 69 | + $endLon = $tempLon['lon']; |
| 70 | + $endLat = $tempLon['lat']; |
| 71 | + $tracks[] = [ |
| 72 | + 'startLat' => $startLat, |
| 73 | + 'startLon' => $startLon, |
| 74 | + 'endLat' => $endLat, |
| 75 | + 'endLon' => $endLon, |
| 76 | + ]; |
| 77 | + $startLat = $endLat; |
| 78 | + $startLon = $endLon; |
| 79 | + } |
| 80 | + } |
| 81 | + $tracks[] = [ |
| 82 | + 'startLat' => $startLat, |
| 83 | + 'startLon' => $startLon, |
| 84 | + 'endLat' => $this->end['lat'], |
| 85 | + 'endLon' => $this->end['lon'] |
| 86 | + ]; |
| 87 | + return $tracks; |
| 88 | + } |
| 89 | +} |
0 commit comments