问题
Here is a pic of what I basically want to achieve:
So as the title says, I want to merge long/lat points which they're radius (of 25Kms for example) touch inside a bounding box of long/lat points.
Here is my very simple DB structure:
+-------+-------------+------------+
| id | long | lat |
+-------+-------------+------------+
| 1 | -90.27137 | 50.00702 |
| 2 | -92.27137 | 52.00702 |
| 3 | -87.27137 | 48.00702 |
| 4 | -91.27137 | 51.00702 |
+-------+-------------+------------+
Here is my query so far:
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
So my query just merging all points inside an imaginary bounding box without considering radius.
I know that I would prorably have to work with the Haversine formula but I'm crap at maths and MySQL which make things a little bit difficult. Indeed, I could eventually merge points if I had just one radius but each points have its own radius and I'm struggling.
This is for a student project and any help will be much much apreciated.
References:
-My query on SQL Fiddle: http://sqlfiddle.com/#!2/3a42b/2 ( contains a SQL Fiddle exemple for the Haversine Formula in comment )
-The Haversine formula in MySQL query: (work for checking all points inside a given radius)
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
;
回答1:
This operation may be too complicated to execute without the aid of PHP or another programming language. Here's how you could do it in 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>";
}
?>
The output of this code on the data you provided is:
Removing point id:1
Removing point id:2
Remaining points:
id=3
id=4
Note that this just removes the overlapping points, it doesn't do any averaging of positions. You could easily add that though. Hope this helps.
来源:https://stackoverflow.com/questions/23236605/merge-long-lat-points-within-a-bounding-box-depending-on-a-radius-in-mysql