<?php
namespace Nuark\KRoute {
class Math
{
public static function toRadian(float $x)
{
return floatval($x * M_PI / 180.0);
}
}
class Map
{
public const EARTH_RADIUS = 6371000;
public static function pointIsInCircle(PointF $pointForCheck, PointF $center, $radius) {
if (getDistanceBetweenTwoPoints($pointForCheck, $center) <= $radius) {
return true;
}
else {
return false;
}
}
public static function getDistanceBetweenTwoPoints(PointF $p1, PointF $p2) {
$dLat = Math::toRadian($p2->x - $p1->x);
$dLon = Math::toRadian($p2->y - $p1->y);
$lat1 = Math::toRadian($p1->x);
$lat2 = Math::toRadian($p2->x);
$a = sin($dLat / 2) * sin($dLat / 2) + sin($dLon / 2) * sin($dLon / 2)
* cos($lat1) * cos($lat2);
$c = 2 * atan2(sqrt(a), sqrt(1-a));
$d = Map::EARTH_RADIUS * c;
}
public static function calculateDerivedPosition(PointF $point, float $range, float $bearing) {
$latA = Math::toRadian($point->x);
$lonA = Math::toRadian($point->y);
$angularDistance = $range / Map::EARTH_RADIUS;
$trueCourse = Math::toRadian($bearing);
$lat = asin(
sin($latA) * cos($angularDistance) + cos($latA) * sin($angularDistance) * cos($trueCourse)
);
$dlon = atan2(
sin($trueCourse) * sin($angularDistance) * cos($latA),
cos($angularDistance) - sin($latA) * sin($lat)
);
$lon = (($lonA + $dlon + M_PI) % (M_PI * 2)) - M_PI;
$lat = Math::toRadian($lat);
$lon = Math::toRadian($lon);
$newPoint = new PointF(floatval($lat), floatval($lon));
return $newPoint;
}
}
class PointF {
public function __construct(float $x, float $y) {
$this->x = floatval($x);
$this->y = floatval($y);
}
}
class Database {
public function __construct($path) {
$this->connection = new \PDO("sqlite:$path");
}
public function getData($x, $y, $radius) {
$center = new PointF($x, $y);
$mult = 1.1;
$insert = "SELECT * FROM Places ".
"WHERE latitude > :p3x AND latitude < :p1x AND longitude < :p2y AND longitude > :p4y";
$stmt = $this->connection->prepare($insert);
$p1 = Map::calculateDerivedPosition($center, $mult * $radius, 0);
$p2 = Map::calculateDerivedPosition($center, $mult * $radius, 90);
$p3 = Map::calculateDerivedPosition($center, $mult * $radius, 180);
$p4 = Map::calculateDerivedPosition($center, $mult * $radius, 270);
$stmt->bindParam(':p3x', $p3->x);
$stmt->bindParam(':p1x', $p1->x);
$stmt->bindParam(':p2y', $p2->y);
$stmt->bindParam(':p4y', $p4->y);
var_dump(':p3x '.$p3->x);
var_dump(':p1x '.$p1->x);
var_dump(':p2y '.$p2->y);
var_dump(':p4y '.$p4->y);
$stmt->execute();
$data = $stmt->fetchAll(\PDO::FETCH_ASSOC);
return $data;
}
}
}