A
A
Andrey Gorbatsevich2019-06-07 17:43:34
PHP
Andrey Gorbatsevich, 2019-06-07 17:43:34

How to find the nearest objects knowing its latitude, longitude and search radius?

Found the answer on StackOverflow , translated from Java to PHP, but nothing works.

index.php
<?php
require_once("Classes.php");

use Nuark\KRoute\Database;

$db = new Database("./places.sqlite");
$data = $db->getData(56.0092305, 92.8393127, 1000);
var_dump($data);

classes.php
<?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;
        }
    }
}

Base content (1 test element)
5cfa773c4a627079057603.png

With those values ​​that are written in index.php, the left data is obtained, as a result - there is no selection from the database
5cfa77b697bf7058308106.png
. What am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konata Izumi, 2019-06-07
@Konata69lol

1. Take PHPStorm.
2. Go through the debugger from start to finish.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question