2

这是我基本上想要实现的图片:

所以正如标题所说,我想合并长/纬度点,它们的半径(例如 25 公里)在长/纬度点的边界框内触摸。

这是我非常简单的数据库结构:

+-------+-------------+------------+
| id    |        long |        lat |
+-------+-------------+------------+
|     1 |   -90.27137 |   50.00702 |
|     2 |   -92.27137 |   52.00702 |
|     3 |   -87.27137 |   48.00702 |
|     4 |   -91.27137 |   51.00702 |
+-------+-------------+------------+

到目前为止,这是我的查询:

set @bottom_lat = -100.27137;
set @bottom_lon = 40.00702;

set @top_lat = -80.27137 ;
set @top_lon = 60.00702 ;
;

SELECT AVG(latitude), AVG(longitude)
FROM destination
WHERE latitude > @bottom_lat AND longitude > @bottom_lon AND latitude < @top_lat AND longitude < @top_lon

所以我的查询只是合并一个假想的边界框内的所有点,而不考虑半径。

我知道我可能不得不使用 Haversine 公式,但我在数学和 MySQL 方面很糟糕,这让事情变得有点困难。事实上,如果我只有一个半径,我最终可以合并点,但每个点都有自己的半径,我正在努力。

这是针对学生项目的,任何帮助都会非常感激。

参考:

-我对 SQL Fiddle 的查询:http ://sqlfiddle.com/#!2/3a42b/2 (在评论中包含一个用于 Haversine 公式的 SQL Fiddle 示例)

- MySQL 查询中的 Haversine 公式:(用于检查给定半径内的所有点)

SELECT*, ( 6371* acos( cos( radians(
@my_lat) ) * cos( radians( 
destination.latitude ) ) * cos( radians( 
destination.longitude ) - radians(
@my_lon) ) + sin( radians(
@my_lat) ) * sin( radians( 
destination.latitude ) ) ) ) AS distance 
FROM destination
ORDER BY distance limit 1
;
4

1 回答 1

0

如果没有 PHP 或其他编程语言的帮助,此操作可能太复杂而无法执行。以下是您可以在 PHP 中执行此操作的方法:

<?
    $link = mysqli_connect("host", "user", "pass", "database");

    // Grab all the points from the db and push them into an array
    $sql = "SELECT * FROM data";
    $res = $link->query($sql);
    $arr = array();
    for($i = 0; $i < mysqli_num_rows($res); $i++){
        array_push($arr, mysqli_fetch_assoc($res));
    }

    // Cycle through the point array, eliminating those points that "touch" 
    $rad = 1000; //radius in KM
    for($i = 0; $i < count($arr); ++$i){
        $lat1 = $arr[$i]['lat'];
        $lon1 = $arr[$i]['long'];
        for($j = 0; $j<count($arr); ++$j){
            if($i != $j && isset($arr[$i]) && isset($arr[$j])){ // do not compare a point to itself
                $lat2 = $arr[$j]['lat'];
                $lon2 = $arr[$j]['long'];
                // get the distance between each pair of points using the haversine formula
                $dist = acos( sin($lat1*pi()/180)*sin($lat2*pi()/180) + cos($lat1*pi()/180)*cos($lat2*pi()/180)*cos($lon2*PI()/180-$lon1*pi()/180) ) * 6371;
                if($dist < $rad){
                    echo "Removing point id:".$arr[$i]['id']."<br>";
                    unset($arr[$i]);
                }
            }
        }
    }

    //display results
    echo "Remaining points:<br>";
    foreach($arr as $val){
        echo "id=".$val['id']."<br>";
    }
?>

此代码对您提供的数据的输出是:

    Removing point id:1
    Removing point id:2
    Remaining points:
    id=3
    id=4

请注意,这只是删除了重叠点,它不会对位置进行任何平均。你可以很容易地添加它。希望这可以帮助。

于 2015-12-13T19:29:25.783 回答