用SQLite计算大圆距离

Ali*_*xel 7 php sqlite pdo geospatial haversine

这是我的问题,我有一个包含位置和纬度/经度的SQLite表.基本上我需要:

SELECT location, HAVERSINE(lat, lon) AS distance FROM location ORDER BY distance ASC;
Run Code Online (Sandbox Code Playgroud)

HAVERSINE()是一个PHP函数,应该返回给定一对纬度和经度值的大圆距离(以英里或公里为单位).其中一对应由PHP提供,另一对应由locations表中可用的每个纬度/经度行提供.

由于SQLite没有任何Geo Spatial扩展(AFAIK SpatiaLite存在但仍然......)我猜测最好的方法是使用自定义函数和其中一个PDO方法:

我认为对于这种情况PDO::sqliteCreateFunction()就足够了,但是我对这个函数的有限经验可以简化为类似于PHP手册中提供的用例:

$db = new PDO('sqlite:geo.db');

function md5_and_reverse($string) { return strrev(md5($string)); }

$db->sqliteCreateFunction('md5rev', 'md5_and_reverse', 1);
$rows = $db->query('SELECT md5rev(filename) FROM files')->fetchAll();
Run Code Online (Sandbox Code Playgroud)

我在弄清楚如何获得一个SQLite用户定义函数来同时处理来自PHP和表数据的数据时遇到了一些麻烦.如果有人能帮助我解决这个问题同时也理解SQLite UDF(一个大赢家),我将不胜感激. SQLite IMO)好一点.

提前致谢!

Ali*_*xel 9

到目前为止,我只能想到这个解决方案:

$db = new PDO('sqlite:geo.db');

$db->sqliteCreateFunction('ACOS', 'acos', 1);
$db->sqliteCreateFunction('COS', 'cos', 1);
$db->sqliteCreateFunction('RADIANS', 'deg2rad', 1);
$db->sqliteCreateFunction('SIN', 'sin', 1);
Run Code Online (Sandbox Code Playgroud)

然后执行以下冗长的查询:

SELECT "location",
       (6371 * ACOS(COS(RADIANS($latitude)) * COS(RADIANS("latitude")) * COS(RADIANS("longitude") - RADIANS($longitude)) + SIN(RADIANS($latitude)) * SIN(RADIANS("latitude")))) AS "distance"
FROM "locations"
HAVING "distance" < $distance
ORDER BY "distance" ASC
LIMIT 10;
Run Code Online (Sandbox Code Playgroud)

如果有人能想到更好的解决方案,请告诉我.


我刚刚发现了这个有趣的链接,明天我会试试.


dju*_*nod 3

来自您的“有趣的链接”。

function sqlite3_distance_func($lat1,$lon1,$lat2,$lon2) {
    // convert lat1 and lat2 into radians now, to avoid doing it twice below
    $lat1rad = deg2rad($lat1);
    $lat2rad = deg2rad($lat2);
    // apply the spherical law of cosines to our latitudes and longitudes, and set the result appropriately
    // 6378.1 is the approximate radius of the earth in kilometres
    return acos( sin($lat1rad) * sin($lat2rad) + cos($lat1rad) * cos($lat2rad) * cos( deg2rad($lon2) - deg2rad($lon1) ) ) * 6378.1;
}

$db->sqliteCreateFunction('DISTANCE', 'sqlite3_distance_func', 4);
Run Code Online (Sandbox Code Playgroud)

然后执行查询:

"SELECT * FROM location ORDER BY distance(latitude,longitude,{$lat},{$lon}) LIMIT 1"
Run Code Online (Sandbox Code Playgroud)

编辑(通过 QOP):我终于再次需要这个,这个解决方案效果很好,我只是最终修改了代码一点,它有点不那么冗长,并且可以优雅地处理非数字值,这里是:

$db->sqliteCreateFunction('distance', function () {
    if (count($geo = array_map('deg2rad', array_filter(func_get_args(), 'is_numeric'))) == 4) {
        return round(acos(sin($geo[0]) * sin($geo[2]) + cos($geo[0]) * cos($geo[2]) * cos($geo[1] - $geo[3])) * 6378.14, 3);
    }

    return null;
}, 4);
Run Code Online (Sandbox Code Playgroud)