/** * Calculate distance between two coord sets * @author zbrown * * @param ApiRequestObject $apiRequest * @return array */ public function calculateDistance(ApiRequestObject $apiRequest) { $primaryLatitude = $apiRequest->getLatitude(); $primaryLongitude = $apiRequest->getLongitude(); $secondaryLatitude = $apiRequest->getSecondaryLatitude(); $secondaryLongitude = $apiRequest->getSecondaryLongitude(); $metricUnit = $apiRequest->getMetricUnit(); $roundMath = $apiRequest->getRoundMath(); $payload = array(); $theta = $primaryLongitude - $secondaryLongitude; $dist = sin(deg2rad($primaryLatitude)) * sin(deg2rad($secondaryLatitude)) + cos(deg2rad($primaryLatitude)) * cos(deg2rad($secondaryLatitude)) * cos(deg2rad($theta)); $dist = acos($dist); $dist = rad2deg($dist); $miles = $dist * 60 * 1.1515; $unit = strtolower($metricUnit); if ($unit == 'k') { $distance = $miles * 1.609344; $payload['unitName'] = 'kilometers'; } else { if ($unit == 'n') { $distance = $miles * 0.8683999999999999; $payload['unitName'] = 'nautical miles'; } else { $distance = $miles; $payload['unitName'] = 'miles'; } } if (!empty($roundMath)) { $final = round($distance, $roundMath); } else { $final = $distance; } $payload = ['distance' => $final, 'metric' => strtoupper($apiRequest->getMetricUnit())]; return $payload; }
/** * Detect country of coords * @author zbrown * * @param ApiRequestObject $apiRequest * @return bool * @throws \Doctrine\DBAL\DBALException */ public function detectCountry(ApiRequestObject $apiRequest) { $qb = $this->getDoctrine()->getEntityManager()->getConnection(); $q = $qb->prepare('SELECT name AS country, sovereign, formal, economy_level AS economy, income_level AS income, MBRContains(GeomFromText(coordinates_wkt), POINT(":lat", ":lon")) AS contain FROM countries HAVING contain > 0 ORDER BY contain DESC LIMIT 1'); $q->bindValue('lat', $apiRequest->getLatitude()); $q->bindValue('lon', $apiRequest->getLongitude()); $q->execute(); $result = $q->fetchAll(); if (empty($result)) { return 'Unable to detect country.'; } else { return $result; } }
/** * Select airport data within constraints * @author zbrown * * @param $lat * @param $lon * @param $limit * @param $max * @return bool * @throws \Doctrine\DBAL\DBALException */ private function findNearAirport(ApiRequestObject $apiRequest) { $latitude = $apiRequest->getLatitude(); $longitude = $apiRequest->getLongitude(); $limit = $apiRequest->getLimit(); $max = $apiRequest->getMaximum(); $qb = $this->getDoctrine()->getEntityManager()->getConnection(); $q = $qb->prepare('SELECT name, type, icao_code, iata_code, X(GeomFromText(coordinates_wkt)) AS latitude, Y(GeomFromText(coordinates_wkt)) AS longitude, SQRT( POW(69.1 * (X(GeomFromText(coordinates_wkt)) - :lat), 2) + POW(69.1 * (:lon - Y(GeomFromText(coordinates_wkt))) * COS(X(GeomFromText(coordinates_wkt)) / 57.3), 2)) AS distance FROM airports HAVING distance < :max ORDER BY distance ASC LIMIT ' . $limit); $q->bindValue('lat', $latitude); $q->bindValue('lon', $longitude); $q->bindValue('max', $max); return $q->execute(); }
/** * Get nearby Ports * @author zbrown * * @param ApiRequestObject $apiRequest * @return bool * @throws \Doctrine\DBAL\DBALException */ public function getNearbyPorts(ApiRequestObject $apiRequest) { $qb = $this->getDoctrine()->getEntityManager()->getConnection(); $q = $qb->prepare('SELECT name, X(GeomFromText(coordinates_wkt)) AS latitude, Y(GeomFromText(coordinates_wkt)) AS longitude, SQRT( POW(69.1 * (X(GeomFromText(coordinates_wkt)) - :lat), 2) + POW(69.1 * (:lon - Y(GeomFromText(coordinates_wkt))) * COS(X(GeomFromText(coordinates_wkt)) / 57.3), 2)) AS distance FROM ports HAVING distance < :max ORDER BY distance ASC LIMIT ' . $apiRequest->getLimit()); $q->bindValue('lat', $apiRequest->getLatitude()); $q->bindValue('lon', $apiRequest->getLongitude()); $q->bindValue('max', $apiRequest->getMaximum()); $q->execute(); $result = $q->fetchAll(); if (empty($result)) { return 'No ports found near coordinate set.'; } else { return $result; } }