2017-12-21 01:28:21 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Services;
|
|
|
|
|
2017-12-27 05:26:12 +08:00
|
|
|
use App\Models\Acars;
|
2017-12-21 09:12:39 +08:00
|
|
|
use Log;
|
2017-12-25 07:38:38 +08:00
|
|
|
|
|
|
|
use \GeoJson\Geometry\Point;
|
2017-12-21 01:28:21 +08:00
|
|
|
use \GeoJson\Geometry\LineString;
|
|
|
|
use \GeoJson\Feature\Feature;
|
|
|
|
use \GeoJson\Feature\FeatureCollection;
|
|
|
|
|
|
|
|
use \League\Geotools\Geotools;
|
|
|
|
use \League\Geotools\Coordinate\Coordinate;
|
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
use App\Models\Flight;
|
2017-12-27 05:26:12 +08:00
|
|
|
use App\Models\Pirep;
|
2017-12-25 07:38:38 +08:00
|
|
|
use App\Repositories\NavdataRepository;
|
|
|
|
|
2017-12-21 09:12:39 +08:00
|
|
|
/**
|
2017-12-28 10:52:37 +08:00
|
|
|
* Return different points/features in GeoJSON format
|
2017-12-21 09:12:39 +08:00
|
|
|
* https://tools.ietf.org/html/rfc7946
|
|
|
|
*
|
|
|
|
* Once a PIREP is accepted, save this returned structure as a
|
|
|
|
* JSON-encoded string into the raw_data field of the PIREP row
|
|
|
|
*
|
|
|
|
*/
|
2017-12-21 01:28:21 +08:00
|
|
|
class GeoService extends BaseService
|
|
|
|
{
|
2017-12-21 09:12:39 +08:00
|
|
|
private $navRepo;
|
|
|
|
|
|
|
|
public function __construct(NavdataRepository $navRepo)
|
|
|
|
{
|
|
|
|
$this->navRepo = $navRepo;
|
|
|
|
}
|
|
|
|
|
2017-12-28 10:52:37 +08:00
|
|
|
/**
|
|
|
|
* Determine the closest set of coordinates from the starting position
|
|
|
|
* @param array $coordStart
|
|
|
|
* @param array $all_coords
|
|
|
|
* @return mixed
|
|
|
|
* @throws \League\Geotools\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function getClosestCoords($coordStart, $all_coords)
|
2017-12-21 09:12:39 +08:00
|
|
|
{
|
|
|
|
$distance = [];
|
|
|
|
$geotools = new Geotools();
|
|
|
|
$start = new Coordinate($coordStart);
|
|
|
|
|
|
|
|
foreach($all_coords as $coords) {
|
|
|
|
$coord = new Coordinate($coords);
|
|
|
|
$dist = $geotools->distance()->setFrom($start)->setTo($coord);
|
2017-12-28 10:52:37 +08:00
|
|
|
$distance[] = $dist->greatCircle();
|
2017-12-21 09:12:39 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
$distance = collect($distance);
|
|
|
|
$min = $distance->min();
|
|
|
|
return $all_coords[ $distance->search($min, true) ];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $dep_icao string ICAO to ignore
|
|
|
|
* @param $arr_icao string ICAO to ignore
|
2017-12-28 10:52:37 +08:00
|
|
|
* @param $start_coords array Starting point, [x, y]
|
2017-12-21 09:12:39 +08:00
|
|
|
* @param $route string Textual route
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getCoordsFromRoute($dep_icao, $arr_icao, $start_coords, $route)
|
|
|
|
{
|
|
|
|
$coords = [];
|
|
|
|
$split_route = explode(' ', $route);
|
|
|
|
|
|
|
|
$skip = [
|
|
|
|
$dep_icao,
|
|
|
|
$arr_icao,
|
|
|
|
'SID',
|
|
|
|
'STAR'
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($split_route as $route_point) {
|
|
|
|
|
|
|
|
$route_point = trim($route_point);
|
|
|
|
|
|
|
|
if (\in_array($route_point, $skip, true)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2017-12-28 10:52:37 +08:00
|
|
|
Log::debug('Looking for ' . $route_point);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
|
|
|
$points = $this->navRepo->findWhere(['id' => $route_point]);
|
|
|
|
$size = \count($points);
|
|
|
|
|
|
|
|
if($size === 0) {
|
|
|
|
continue;
|
|
|
|
} else if($size === 1) {
|
|
|
|
$point = $points[0];
|
|
|
|
Log::info('name: ' . $point->id . ' - ' . $point->lat . 'x' . $point->lon);
|
2017-12-25 07:38:38 +08:00
|
|
|
$coords[] = $point;
|
2017-12-21 09:12:39 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find the point with the shortest distance
|
|
|
|
Log::info('found ' . $size . ' for '. $route_point);
|
|
|
|
|
|
|
|
# Get the start point and then reverse the lat/lon reference
|
|
|
|
# If the first point happens to have multiple possibilities, use
|
|
|
|
# the starting point that was passed in
|
2017-12-25 07:38:38 +08:00
|
|
|
if (\count($coords) > 0) {
|
2017-12-21 09:12:39 +08:00
|
|
|
$start_point = $coords[\count($coords) - 1];
|
2017-12-25 07:38:38 +08:00
|
|
|
$start_point = [$start_point->lat, $start_point->lon];
|
2017-12-21 09:12:39 +08:00
|
|
|
} else {
|
|
|
|
$start_point = $start_coords;
|
|
|
|
}
|
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
# Put all of the lat/lon sets into an array to pick of what's clsest
|
|
|
|
# to the starting point
|
|
|
|
$potential_coords = [];
|
|
|
|
foreach($points as $point) {
|
|
|
|
$potential_coords[] = [$point->lat, $point->lon];
|
|
|
|
}
|
|
|
|
|
|
|
|
# returns an array with the closest lat/lon to start point
|
|
|
|
$closest_coords = $this->getClosestCoords($start_point, $potential_coords);
|
|
|
|
foreach($points as $point) {
|
|
|
|
if($point->lat === $closest_coords[0] && $point->lon === $closest_coords[1]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$coords[] = $point;
|
2017-12-21 09:12:39 +08:00
|
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Log::error($e);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $coords;
|
|
|
|
}
|
|
|
|
|
2017-12-28 06:47:22 +08:00
|
|
|
/**
|
|
|
|
* Determine the center point between two sets of coordinates
|
|
|
|
* @param $latA
|
|
|
|
* @param $lonA
|
|
|
|
* @param $latB
|
|
|
|
* @param $lonB
|
|
|
|
* @return array
|
|
|
|
* @throws \League\Geotools\Exception\InvalidArgumentException
|
|
|
|
*/
|
|
|
|
public function getCenter($latA, $lonA, $latB, $lonB)
|
|
|
|
{
|
|
|
|
$geotools = new Geotools();
|
|
|
|
$coordA = new Coordinate([$latA, $lonA]);
|
|
|
|
$coordB = new Coordinate([$latB, $lonB]);
|
|
|
|
|
|
|
|
$vertex = $geotools->vertex()->setFrom($coordA)->setTo($coordB);
|
|
|
|
$middlePoint = $vertex->middle();
|
|
|
|
|
|
|
|
$center = [
|
|
|
|
$middlePoint->getLatitude(),
|
|
|
|
$middlePoint->getLongitude()
|
|
|
|
];
|
|
|
|
|
|
|
|
return $center;
|
|
|
|
}
|
|
|
|
|
2017-12-27 05:26:12 +08:00
|
|
|
/**
|
|
|
|
* Read an array/relationship of ACARS model points
|
|
|
|
* @param Pirep $pirep
|
2017-12-28 10:52:37 +08:00
|
|
|
* @return FeatureCollection
|
2017-12-27 05:26:12 +08:00
|
|
|
*/
|
2017-12-28 10:52:37 +08:00
|
|
|
public function getFeatureFromAcars(Pirep $pirep)
|
2017-12-27 05:26:12 +08:00
|
|
|
{
|
|
|
|
$route_line = [];
|
|
|
|
$route_points = [];
|
|
|
|
|
|
|
|
$route_line[] = [$pirep->dpt_airport->lon, $pirep->dpt_airport->lat];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $point \App\Models\Acars
|
|
|
|
*/
|
|
|
|
foreach ($pirep->acars as $point)
|
|
|
|
{
|
|
|
|
$route_line[] = [$point->lon, $point->lat];
|
|
|
|
$route_points[] = new Feature(
|
|
|
|
new Point([$point->lon, $point->lat]), [
|
2017-12-28 10:52:37 +08:00
|
|
|
'pirep_id' => $pirep->id,
|
|
|
|
'name' => $point->altitude,
|
|
|
|
'popup' => 'GS: ' . $point->gs . '<br />Alt: ' . $point->altitude,
|
|
|
|
]);
|
2017-12-27 05:26:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
# Arrival
|
|
|
|
$route_line[] = [$pirep->arr_airport->lon, $pirep->arr_airport->lat];
|
2017-12-28 10:52:37 +08:00
|
|
|
$route_line = new Feature(new LineString($route_line));
|
2017-12-27 05:26:12 +08:00
|
|
|
|
2017-12-28 10:52:37 +08:00
|
|
|
return new FeatureCollection([$route_line, $route_points]);
|
2017-12-27 05:26:12 +08:00
|
|
|
}
|
|
|
|
|
2017-12-28 06:47:22 +08:00
|
|
|
/**
|
|
|
|
* Return a single feature point for the
|
|
|
|
*/
|
|
|
|
public function getFeatureForLiveFlights($pireps)
|
|
|
|
{
|
|
|
|
$flight_points = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var Pirep $pirep
|
|
|
|
*/
|
|
|
|
foreach($pireps as $pirep) {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var $point \App\Models\Acars
|
|
|
|
*/
|
|
|
|
$point = $pirep->position;
|
|
|
|
$flight_points[] = new Feature(
|
|
|
|
new Point([$point->lon, $point->lat]), [
|
2017-12-28 10:52:37 +08:00
|
|
|
'pirep_id' => $pirep->id,
|
|
|
|
'gs' => $point->gs,
|
|
|
|
'alt' => $point->altitude,
|
|
|
|
'heading' => $point->heading ?: 0,
|
|
|
|
'popup' => 'Flight: ' . $pirep->ident,
|
|
|
|
]);
|
2017-12-28 06:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return new FeatureCollection($flight_points);
|
|
|
|
}
|
|
|
|
|
2017-12-21 01:28:21 +08:00
|
|
|
/**
|
2017-12-21 09:12:39 +08:00
|
|
|
* Return a FeatureCollection GeoJSON object
|
|
|
|
* @param Flight $flight
|
2017-12-21 01:28:21 +08:00
|
|
|
* @return array
|
|
|
|
*/
|
2017-12-21 09:12:39 +08:00
|
|
|
public function flightGeoJson(Flight $flight): array
|
2017-12-21 01:28:21 +08:00
|
|
|
{
|
2017-12-25 07:38:38 +08:00
|
|
|
$route_coords = [];
|
|
|
|
$route_points = [];
|
|
|
|
|
|
|
|
## Departure Airport
|
|
|
|
$route_coords[] = [$flight->dpt_airport->lon, $flight->dpt_airport->lat];
|
|
|
|
|
|
|
|
$route_points[] = new Feature(
|
|
|
|
new Point([$flight->dpt_airport->lon, $flight->dpt_airport->lat]), [
|
|
|
|
'name' => $flight->dpt_airport->icao,
|
|
|
|
'popup' => $flight->dpt_airport->full_name,
|
|
|
|
'icon' => 'airport',
|
|
|
|
]
|
|
|
|
);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
|
|
|
if($flight->route) {
|
2017-12-25 07:38:38 +08:00
|
|
|
$all_route_points = $this->getCoordsFromRoute(
|
2017-12-21 09:12:39 +08:00
|
|
|
$flight->dpt_airport->icao,
|
|
|
|
$flight->arr_airport->icao,
|
|
|
|
[$flight->dpt_airport->lat, $flight->dpt_airport->lon],
|
|
|
|
$flight->route);
|
|
|
|
|
|
|
|
// lat, lon needs to be reversed for GeoJSON
|
2017-12-25 07:38:38 +08:00
|
|
|
foreach($all_route_points as $point) {
|
|
|
|
$route_coords[] = [$point->lon, $point->lat];
|
|
|
|
$route_points[] = new Feature(new Point([$point->lon, $point->lat]), [
|
|
|
|
'name' => $point->name,
|
|
|
|
'popup' => $point->name . ' (' . $point->name . ')',
|
|
|
|
'icon' => ''
|
|
|
|
]);
|
2017-12-21 09:12:39 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
## Arrival Airport
|
|
|
|
$route_coords[] = [$flight->arr_airport->lon, $flight->arr_airport->lat,];
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$route_points[] = new Feature(
|
|
|
|
new Point([$flight->arr_airport->lon, $flight->arr_airport->lat]), [
|
|
|
|
'name' => $flight->arr_airport->icao,
|
|
|
|
'popup' => $flight->arr_airport->full_name,
|
|
|
|
'icon' => 'airport',
|
|
|
|
]
|
|
|
|
);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$route_points = new FeatureCollection($route_points);
|
|
|
|
$planned_route_line = new FeatureCollection([new Feature(new LineString($route_coords), [])]);
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2017-12-21 09:12:39 +08:00
|
|
|
return [
|
2017-12-28 10:52:37 +08:00
|
|
|
'route_points' => $route_points,
|
|
|
|
'planned_route_line' => $planned_route_line,
|
2017-12-21 09:12:39 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a GeoJSON FeatureCollection for a PIREP
|
|
|
|
* @param Pirep $pirep
|
|
|
|
* @return array
|
|
|
|
*/
|
2017-12-27 05:26:12 +08:00
|
|
|
public function pirepGeoJson(Pirep $pirep)
|
2017-12-21 09:12:39 +08:00
|
|
|
{
|
2017-12-28 11:05:53 +08:00
|
|
|
$planned_rte_points = [];
|
2017-12-25 07:38:38 +08:00
|
|
|
$planned_rte_coords = [];
|
|
|
|
|
|
|
|
$planned_rte_coords[] = [$pirep->dpt_airport->lon, $pirep->dpt_airport->lat];
|
2017-12-28 11:05:53 +08:00
|
|
|
$feature = new Feature(
|
2017-12-25 07:38:38 +08:00
|
|
|
new Point([$pirep->dpt_airport->lon, $pirep->dpt_airport->lat]), [
|
2017-12-28 11:05:53 +08:00
|
|
|
'name' => $pirep->dpt_airport->icao,
|
2017-12-25 07:38:38 +08:00
|
|
|
'popup' => $pirep->dpt_airport->full_name,
|
2017-12-28 11:05:53 +08:00
|
|
|
'icon' => 'airport',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$planned_rte_points[] = $feature;
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2017-12-24 01:58:17 +08:00
|
|
|
if (!empty($pirep->route)) {
|
2017-12-25 07:38:38 +08:00
|
|
|
$all_route_points = $this->getCoordsFromRoute(
|
2017-12-24 01:58:17 +08:00
|
|
|
$pirep->dpt_airport->icao,
|
|
|
|
$pirep->arr_airport->icao,
|
|
|
|
[$pirep->dpt_airport->lat, $pirep->dpt_airport->lon],
|
|
|
|
$pirep->route);
|
|
|
|
|
|
|
|
// lat, lon needs to be reversed for GeoJSON
|
2017-12-25 07:38:38 +08:00
|
|
|
foreach ($all_route_points as $point) {
|
|
|
|
$planned_rte_coords[] = [$point->lon, $point->lat];
|
2017-12-28 11:05:53 +08:00
|
|
|
$planned_rte_points[] = new Feature(new Point([$point->lon, $point->lat]), [
|
2017-12-28 10:52:37 +08:00
|
|
|
'name' => $point->name,
|
2017-12-25 07:38:38 +08:00
|
|
|
'popup' => $point->name . ' (' . $point->name . ')',
|
2017-12-28 10:52:37 +08:00
|
|
|
'icon' => ''
|
2017-12-25 07:38:38 +08:00
|
|
|
]);
|
2017-12-24 01:58:17 +08:00
|
|
|
}
|
|
|
|
}
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$planned_rte_coords[] = [$pirep->arr_airport->lon, $pirep->arr_airport->lat];
|
2017-12-28 11:05:53 +08:00
|
|
|
|
|
|
|
$planned_rte_points[] = new Feature(
|
2017-12-25 07:38:38 +08:00
|
|
|
new Point([$pirep->arr_airport->lon, $pirep->arr_airport->lat]), [
|
2017-12-28 10:52:37 +08:00
|
|
|
'name' => $pirep->arr_airport->icao,
|
2017-12-25 07:38:38 +08:00
|
|
|
'popup' => $pirep->arr_airport->full_name,
|
2017-12-28 10:52:37 +08:00
|
|
|
'icon' => 'airport',
|
2017-12-25 07:38:38 +08:00
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2017-12-28 11:05:53 +08:00
|
|
|
$planned_rte_points = new FeatureCollection($planned_rte_points);
|
2017-12-21 09:12:39 +08:00
|
|
|
|
2017-12-25 07:38:38 +08:00
|
|
|
$planned_route = new FeatureCollection([
|
2017-12-28 11:05:53 +08:00
|
|
|
new Feature(new LineString($planned_rte_coords), [])
|
2017-12-21 09:12:39 +08:00
|
|
|
]);
|
2017-12-21 01:28:21 +08:00
|
|
|
|
2017-12-27 05:26:12 +08:00
|
|
|
$actual_route = $this->getFeatureFromAcars($pirep);
|
|
|
|
|
2017-12-21 01:28:21 +08:00
|
|
|
return [
|
2017-12-28 11:05:53 +08:00
|
|
|
'planned_rte_points' => $planned_rte_points,
|
|
|
|
'planned_rte_line' => $planned_route,
|
|
|
|
'actual_route_line' => $actual_route['line'],
|
|
|
|
'actual_route_points' => $actual_route['points'],
|
2017-12-21 01:28:21 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|