2012-03-21 95 views
3

选择随机GPS点我想写一个PHP程序在我的数据库选择从400点16个随机GPS点与最小距离

(点表:ID - 标题 - 纬度 - 经度)。

LAT 37.9824
LON -87.5781547

的唯一要求16个的随机点,其每一个彼此点(发现在1KM范围的点)

它是一个至少为1公里系统,该系统选择每家药房之间最小距离为1公里的药房。我在数据库中有400家药店,每周我必须选择16家药店。我无法选择非常接近的两家药店。

示例:

如果程序返回3药店A B和C。

药房之间的ditance必须是:

A和B = 1 KM

A和C = 1 KM

B和C = 1 KM

+0

封闭,不是一个真正的问题由deceze,吉荣,衮,感知,道3小时前????? 对不起你们我认为这是一个问题,我忘了,这只是一个爆炸性新闻 – Fadel 2012-03-21 12:02:38

回答

0

让我们尝试使硬方式,因为你只有400个记录,它可能只需要几个小时......没有尝试过,但它可能会给你一个想法

$min =1; 
$n =16; 

$pharmas = fillUp(); 

// main function 
function fillUp(){ 
    $points = array(); 
    while(count($points)< $n){ 
     $tmp = getRandomPoint(); 
     if(checkAll($tmp, $points){ 
     $points[] = $tmp; 
     } 
} 
return $points; // after a few hours ?? 
} 

// get a random point 
// after all we might get lucky 
function getRandomPoint(){ 
//... 
// return array with ['latitude'] & ['longitude'] 
} 

// check that all points meet the requirements 
function checkAll($pt, $points){ 
    foreach($points as $point){ 
     if(distance($point, $pt) < $min { 
       return false; 
      } 
    } 
    return true; 
} 

// calculate the distance between 2 points 
function distance ($point1, $point2, $uom='km') { 
    // Use Haversine formula to calculate the great circle distance 
    //  between two points identified by longitude and latitude 
    switch (strtolower($uom)) { 
     case 'km' : 
      $earthMeanRadius = 6371.009; // km 
      break; 
     case 'm' : 
      $earthMeanRadius = 6371.009 * 1000; // km 
      break; 
     case 'miles' : 
      $earthMeanRadius = 3958.761; // miles 
      break; 
     case 'yards' : 
     case 'yds' : 
      $earthMeanRadius = 3958.761 * 1760; // miles 
      break; 
     case 'feet' : 
     case 'ft' : 
      $earthMeanRadius = 3958.761 * 1760 * 3; // miles 
      break; 
     case 'nm' : 
      $earthMeanRadius = 3440.069; // miles 
      break; 
    } 
    $deltaLatitude = deg2rad($point2['latitude'] - $point1['latitude']); 
    $deltaLongitude = deg2rad($point2['longitude'] - $point1['longitude']); 
    $a = sin($deltaLatitude/2) * sin($deltaLatitude/2) + 
      cos(deg2rad($point1['latitude'])) * cos(deg2rad($point2['latitude'])) * 
      sin($deltaLongitude/2) * sin($deltaLongitude/2); 
    $c = 2 * atan2(sqrt($a), sqrt(1 - $a)); 
    $distance = $earthMeanRadius * $c; 
    return $distance; 
} 
0

这里摘下帽子的答案:

我首先会创建一个视图,其中包含的对象列表与使用笛卡尔距离公式的距离接近,然后应用PHP代码来计算实际的球面距离。

@MY_LAT = 37.9824; 
@MY_LONG = -87.5781547; 

SELECT *, SQRT(
       ABS((latitude - @MY_LAT) * (latitude - @MY_LAT) + 
        (longitude - @MY_LONG) * (longitude - @MY_LONG))) 
      AS DIST 
FROM POINT_TABLE 
ORDER BY DIST ASC 

从该视图中选择前n行,以获得距离'兴趣点'最近的16个点。要检查点是你的参考点1公里范围内得到的结果后,你可以写一个小PHP代码段。这会帮助你与片段:

http://www.zipcodeworld.com/samples/distance.php.html

在这里,我用它只会减少的记录数你能适用于PHP球面距离公式为目的的查询笛卡尔距离公式.X