function test() { $db = db_connect(); $this->assertEqual(gcDistance($db, 1, 1), array(0, gcDuration(0))); }
function gcDistance($db, $src_apid, $dst_apid) { // Special case: loop flight to/from same airport if ($src_apid == $dst_apid) { $dist = 0; } else { $sql = "SELECT x,y FROM airports WHERE apid={$src_apid} OR apid={$dst_apid}"; // Handle both OO and procedural-style database handles, depending on what type we've got. if (get_class($db) == "PDO") { $sth = $db->prepare($sql); $sth->execute(); if ($sth->rowCount() != 2) { return array(null, null); } $coord1 = $sth->fetch(); $lon1 = $coord1["x"]; $lat1 = $coord1["y"]; $coord2 = $sth->fetch(); $lon2 = $coord2["x"]; $lat2 = $coord2["y"]; } else { $rs = mysql_query($sql, $db); if (mysql_num_rows($rs) != 2) { return array(null, null); } $row = mysql_fetch_assoc($rs); $lon1 = $row["x"]; $lat1 = $row["y"]; $row = mysql_fetch_assoc($rs); $lon2 = $row["x"]; $lat2 = $row["y"]; } $pi = 3.1415926; $rad = doubleval($pi / 180.0); $lon1 = doubleval($lon1) * $rad; $lat1 = doubleval($lat1) * $rad; $lon2 = doubleval($lon2) * $rad; $lat2 = doubleval($lat2) * $rad; $theta = $lon2 - $lon1; $dist = acos(sin($lat1) * sin($lat2) + cos($lat1) * cos($lat2) * cos($theta)); if ($dist < 0) { $dist += $pi; } $dist = floor($dist * 6371.2 * 0.621); } $duration = gcDuration($dist); return array($dist, $duration); }
$sql .= " ORDER BY src_date DESC, src_time DESC"; } // Execute! $result = mysql_query($sql, $db) or die('Error;Query ' . print_r($_GET, true) . ' caused database error ' . $sql . ', ' . mysql_error()); $first = true; if ($export == "export" || $export == "backup") { // Start with byte-order mark to try to clue Excel into realizing that this is UTF-8 print "Date,From,To,Flight_Number,Airline,Distance,Duration,Seat,Seat_Type,Class,Reason,Plane,Registration,Trip,Note,From_OID,To_OID,Airline_OID,Plane_OID\r\n"; } $gcmap_city_pairs = ''; // list of city pairs when doing gcmap export. while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) { $note = $row["note"]; if ($route) { $row["distance"] = gcPointDistance(array("x" => $row["sx"], "y" => $row["sy"]), array("x" => $row["dx"], "y" => $row["dy"])); $row["duration"] = gcDuration($row["distance"]); $row["code"] = $row["al_name"] . " (" . $row["code"] . ")"; $note = ""; if ($row["stops"] == "0") { $note = "Direct"; } else { $note = $row["stops"] . " stops"; } if ($row["codeshare"] == "Y") { $note = "Codeshare"; } } if ($first) { $first = false; } else { if ($export == "export" || $export == "backup") {