Add flight route map; algorithm for picking from multiple nav points; some tests

This commit is contained in:
Nabeel Shahzad
2017-12-20 19:12:39 -06:00
parent 8d1281d6e3
commit 6c546b5094
18 changed files with 418 additions and 221 deletions

View File

@@ -25,6 +25,7 @@ class CreateNavdataTables extends Migration
$table->float('lon', 7, 4)->default(0.0);
$table->string('freq', 7)->nullable();
$table->primary(['id', 'name']);
$table->index('id');
$table->index('name');
});

View File

@@ -13,13 +13,9 @@ class CreateAcarsTables extends Migration
*/
public function up()
{
/**
* See for defs, modify/update based on this
* https://github.com/skiselkov/openfmc/blob/master/airac.h
*/
Schema::create('acars', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('acars_id', 12);
$table->string('pirep_id', 12);
$table->string('name', 10)->nullable();
$table->float('lat', 7, 4)->default(0.0);
$table->float('lon', 7, 4)->default(0.0);
@@ -29,8 +25,8 @@ class CreateAcarsTables extends Migration
# polymorphic relation columns.
# parent_type can be flight, pirep or acars
# once
$table->unsignedBigInteger('parent_id');
$table->string('parent_type');
#$table->unsignedBigInteger('parent_id');
#$table->string('parent_type');
$table->timestamps();

View File

@@ -193,7 +193,7 @@ flights:
flight_number: 100
dpt_airport_id: KAUS
arr_airport_id: KJFK
route: KAUS KJFK
route: KAUS SID TNV J87 IAH J2 LCH J22 MEI J239 ATL J52 AJFEB J14 BYJAC Q60 JAXSN J14 COLIN J61 HUBBS J55 SIE STAR KJFK
dpt_time: 6PM CST
arr_time: 11PM EST
- id: flightid_2

View File

@@ -4,6 +4,7 @@ namespace App\Http\Controllers\Frontend;
use App\Repositories\AirlineRepository;
use App\Repositories\AirportRepository;
use App\Services\GeoService;
use Log;
use Illuminate\Http\Request;
@@ -19,16 +20,21 @@ use Prettus\Repository\Exceptions\RepositoryException;
class FlightController extends AppBaseController
{
private $airlineRepo, $airportRepo, $flightRepo;
private $airlineRepo,
$airportRepo,
$flightRepo,
$geoSvc;
public function __construct(
AirlineRepository $airlineRepo,
AirportRepository $airportRepo,
FlightRepository $flightRepo
FlightRepository $flightRepo,
GeoService $geoSvc
) {
$this->airlineRepo = $airlineRepo;
$this->airportRepo = $airportRepo;
$this->flightRepo = $flightRepo;
$this->geoSvc = $geoSvc;
}
public function index(Request $request)
@@ -110,13 +116,22 @@ class FlightController extends AppBaseController
}
}
public function update()
{
}
/**
* Show the flight information page
*/
public function show($id)
{
$flight = $this->flightRepo->find($id);
if (empty($flight)) {
Flash::error('Flight not found!');
return redirect(route('frontend.dashboard.index'));
}
$coords = $this->geoSvc->flightGeoJson($flight);
return $this->view('flights.show', [
'flight' => $flight,
'coords' => $coords,
]);
}
}

View File

@@ -120,7 +120,7 @@ class PirepController extends Controller
return redirect(route('frontend.pirep.index'));
}
$coords = $this->geoSvc->getRouteCoordsGeoJSON($pirep);
$coords = $this->geoSvc->pirepGeoJson($pirep);
return $this->view('pireps.show', [
'pirep' => $pirep,

View File

@@ -28,7 +28,8 @@ class Airport extends Model
];
protected $casts = [
'lat' => 'float',
'lon' => 'float',
];
/**

View File

@@ -61,11 +61,15 @@ class Flight extends Model
/**
* Get the flight ident, e.,g JBU1900
* @param $value
*/
public function getIdentAttribute()
{
$flight_id = $this->airline->code;
$flight_id .= $this->flight_number;
# TODO: Add in code/leg if set
return $flight_id;
}
/**

View File

@@ -20,7 +20,9 @@ class Navdata extends Model
];
public $casts = [
'id' => 'string',
'type' => 'integer',
'id' => 'string',
'type' => 'integer',
'lat' => 'float',
'lon' => 'float',
];
}

View File

@@ -0,0 +1,20 @@
<?php
namespace App\Repositories;
use App\Models\Navdata;
use App\Repositories\Traits\CacheableRepository;
use Prettus\Repository\Contracts\CacheableInterface;
class NavdataRepository extends BaseRepository implements CacheableInterface
{
use CacheableRepository;
// Super short lived cache for when the navdata stuff is re-imported
protected $cacheMinutes = 5;
public function model()
{
return Navdata::class;
}
}

View File

@@ -2,45 +2,200 @@
namespace App\Services;
use Log;
//use \League\Geotools\Geotools;
use App\Models\Flight;
use App\Repositories\NavdataRepository;
use \GeoJson\Geometry\LineString;
use \GeoJson\Feature\Feature;
use \GeoJson\Feature\FeatureCollection;
use League\Flysystem\Exception;
use \League\Geotools\Geotools;
use \League\Geotools\Coordinate\Coordinate;
/**
* Return all of the coordinates, start to finish
* Returned in the GeoJSON format
* https://tools.ietf.org/html/rfc7946
*
* TODO: Save this data:
* Once a PIREP is accepted, save this returned structure as a
* JSON-encoded string into the raw_data field of the PIREP row
*
*/
class GeoService extends BaseService
{
private $navRepo;
public function __construct(NavdataRepository $navRepo)
{
$this->navRepo = $navRepo;
}
public function getClosestCoords($coordStart, $all_coords, $measure='flat')
{
$distance = [];
$geotools = new Geotools();
$start = new Coordinate($coordStart);
foreach($all_coords as $coords) {
$coord = new Coordinate($coords);
$dist = $geotools->distance()->setFrom($start)->setTo($coord);
if($measure === 'flat') {
$distance[] = $dist->flat();
} elseif ($measure === 'greatcircle') {
$distance[] = $dist->greatCircle();
}
}
$distance = collect($distance);
$min = $distance->min();
return $all_coords[ $distance->search($min, true) ];
}
/**
* Return all of the coordinates, start to finish
* Returned in the GeoJSON format
* https://tools.ietf.org/html/rfc7946
*
* TODO: Save this data:
* Once a PIREP is accepted, save this returned structure as a
* JSON-encoded string into the raw_data field of the PIREP row
*
* @param \App\Models\Pirep|\App\Models\Flight $model
* @param $dep_icao string ICAO to ignore
* @param $arr_icao string ICAO to ignore
* @param $start_coords array [x, y]
* @param $route string Textual route
* @return array
*/
public function getRouteCoordsGeoJSON($model): array
public function getCoordsFromRoute($dep_icao, $arr_icao, $start_coords, $route)
{
# NOTE!! GeoJSON takes coords in [lon, lat] format!!
$line = new LineString([
[$model->dpt_airport->lon, $model->dpt_airport->lat],
[$model->arr_airport->lon, $model->arr_airport->lat],
$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 {
Log::info('Looking for ' . $route_point);
$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);
$coords[] = [
$point->lat,
$point->lon,
];
continue;
}
# Find the point with the shortest distance
Log::info('found ' . $size . ' for '. $route_point);
$potential_coords = [];
foreach($points as $point) {
#Log::debug('name: ' . $point->id . ' - '.$point->lat .'x'.$point->lon);
$potential_coords[] = [$point->lat, $point->lon];
}
# 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
if(\count($coords) > 0) {
$start_point = $coords[\count($coords) - 1];
$start_point = [$start_point[0], $start_point[1]];
} else {
$start_point = $start_coords;
}
$coords[] = $this->getClosestCoords($start_point, $potential_coords);
} catch (\Exception $e) {
Log::error($e);
continue;
}
}
return $coords;
}
/**
* Return a FeatureCollection GeoJSON object
* @param Flight $flight
* @return array
*/
public function flightGeoJson(Flight $flight): array
{
$coords = [];
$coords[] = [$flight->dpt_airport->lon, $flight->dpt_airport->lat];
// TODO: Add markers for the start/end airports
// TODO: Read from the ACARS data table
if($flight->route) {
$route_coords =$this->getCoordsFromRoute(
$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
foreach($route_coords as $rc) {
$coords[] = [$rc[1], $rc[0]];
}
}
$coords[] = [$flight->arr_airport->lon, $flight->arr_airport->lat];
$line = new LineString($coords);
$features = new FeatureCollection([
new Feature($line, [], 1)
]);
return [
'features' => $features,
];
}
/**
* Return a GeoJSON FeatureCollection for a PIREP
* @param Pirep $pirep
* @return array
*/
public function pirepGeoJson(Pirep $pirep)
{
$coords = [];
$coords[] = [$pirep->dpt_airport->lon, $pirep->dpt_airport->lat];
// TODO: Add markers for the start/end airports
// TODO: Read from the ACARS data table
$feature = new Feature($line, [], 1);
$features = new FeatureCollection([$feature]);
$coords[] = [$pirep->arr_airport->lon, $pirep->arr_airport->lat];
$line = new LineString($coords);
$features = new FeatureCollection([
new Feature($line, [], 1)
]);
return [
'features' => $features,
//'center' => $center,
];
}