2

ここに私が基本的に達成したいものの写真があります:

タイトルが示すように、半径(たとえば25Kms)である長点/緯度点を、長点/緯度点のバウンディングボックス内にマージしたいと考えています。

これが私の非常に単純なDB構造です。

+-------+-------------+------------+
| 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 が苦手で、物事が少し難しくなっています。確かに、半径が 1 つしかない場合でも最終的にポイントをマージできますが、各ポイントには独自の半径があり、苦労しています。

これは学生のプロジェクトのためのものであり、どんな助けも非常に高く評価されます。

参考文献:

- SQL Fiddle に関する私のクエリ: http://sqlfiddle.com/#!2/3a42b/2 (コメントに Haversine Formula の 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 に答える