freshJet Posted January 30, 2014 Report Posted January 30, 2014 I am trying to return the total distance of a route, rather than the great circle distance. I have a complicated method, but I'm not convinced with the result: function getFPDistance(){ $schedule = SchedulesData::getScheduleDetailed($_GET['plan']); $params->deplat = $schedule->deplat; $params->deplng = $schedule->deplng; $params->arrlat = $schedule->arrlat; $params->arrlng = $schedule->arrlng; $params->route = $_POST['route']; $routedata = NavData::parseRoute($params); $depairport = OperationsData::getAirportInfo($schedule->depicao); $arrairport = OperationsData::getAirportInfo($schedule->arricao); $end = (count($routedata) - 1); $total = 0; $a = -1; $b = 0; # Put lat/lng of each waypoint into array $lat = array(); $lng = array(); foreach($routedata as $wp){ $lat[] = $wp->lat; $lng[] = $wp->lng; } # Calculate the distance between each waypoint in the array for($i=0; $i<$end; $i++){ $a++; $b++; $dist1 = distance($lat[$a], $lng[$a], $arrairport->lat, $arrairport->lng); $dist2 = distance($lat[$b], $lng[$b], $arrairport->lat, $arrairport->lng); $dist = $dist1 - $dist2; $total = $total + $dist; } # Get the distance between the departure airport and the first waypoint $dep_to_first = distance($depairport->lat, $depairport->lng, $lat[0], $lng[0]); # Get the distance between the last waypoint and arrival airport $last_to_arr = distance($lat[$end], $lng[$end], $arrairport->lat, $arrairport->lng); # Add these to the total $total = $total + $dep_to_first + $last_to_arr; return round($total); } For EGCC-GCTS, this returns 1650nm, with GC distance being 1648nm, route shown below. NOKIN UN862 LAMAT UN90 NOTRO UM30 SALCO UN864 MONTO UN857 TERTO Any ideas? Quote
Tom Posted January 30, 2014 Report Posted January 30, 2014 I put the route into a mapper and it suggests the route length should be 1780.6nm. You seem to be calculating the distance between each waypoint and the destination airport, and subtracting the distance of the previous waypoint and the destination airport. Strange method... Assuming your logic for $end etc is correct this might work better: function getFPDistance(){ $schedule = SchedulesData::getScheduleDetailed($_GET['plan']); $params->deplat = $schedule->deplat; $params->deplng = $schedule->deplng; $params->arrlat = $schedule->arrlat; $params->arrlng = $schedule->arrlng; $params->route = $_POST['route']; $routedata = NavData::parseRoute($params); $depairport = OperationsData::getAirportInfo($schedule->depicao); $arrairport = OperationsData::getAirportInfo($schedule->arricao); $end = (count($routedata) - 1); // Put lat/lng of each waypoint into array $lat = $lng = array(); foreach($routedata as $wp){ $lat[] = $wp->lat; $lng[] = $wp->lng; } // Distance from departure airport to first waypoint $total = distance($depairport->lat, $depairport->lng, $lat[0], $lng[0]); // Distance between each waypoint for($i=1; $i<$end; $i++){ $total += distance($lat[$i], $lng[$i], $lat[$i - 1], $lng[$i - 1]); } // Distance from last waypoint to arrival airport $total += distance($lat[$end], $lng[$end], $arrairport->lat, $arrairport->lng); return round($total); } 1 Quote
freshJet Posted January 30, 2014 Author Report Posted January 30, 2014 Yeah, that's what I was trying to do. It seemed logical at the time Your version returns 1100nm. EDIT: Fixed, thanks. I changed: for($i=1; $i<$end; $i++){ to: for($i=1; $i<=$end; $i++){ Quote
Tom Posted January 30, 2014 Report Posted January 30, 2014 And try with $end = count($routedata); instead? ^^ use that instead for simplicity - there's no point subtracting 1 and then doing <= Quote
freshJet Posted January 30, 2014 Author Report Posted January 30, 2014 And try with $end = count($routedata); instead? That would also have worked actually. Thanks again. Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.