查找附近点--Geohash方案讨论



基础数据中,一般保存了目标位置的经纬度;利用用户提供的经纬度,进行对比,从而获得是否在附近。

查找附近的XXX,由近到远返回结果,且结果中有与目标点的距离。

一、方案A:
=================================================================================================

点(纬度,经度),A($radLat1,$radLng1)、B($radLat2,$radLng2);

缺点:每次都会查询数据库,性能堪忧

通过余弦定理以及弧度计算方法,最终推导出来的算式A为:


$s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;




$s = 2*asin(sqrt(pow(sin(($radLat1-$radLat2)/2),2)+cos($radLat1)*cos($radLat2)*pow(sin(($radLng1-$radLng2)/2),2)))*$R;

其中 :
$radLat1、$radLng1,$radLat2,$radLng2 为弧度

2、通过测试两种算法,结果相同且都正确,但通过PHP代码测试,两点间距离,10W次性能对比,自行推导版本计算时长算式B较优,如下:

0.56368780136108float(431)
0.57460689544678float(431)
0.59051203727722float(431)

0.47404885292053float(431)
0.47808718681335float(431)
0.47946381568909float(431)




 
    //根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)
    public static function getDistance($lat1,$lng1,$lat2,$lng2)
    {
        //地球半径
        $R = 6378137;
 
        //将角度转为狐度
        $radLat1 = deg2rad($lat1);
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
 
        //结果
        $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;
 
        //精度
        $s = round($s* 10000)/10000;
 
        return  round($s);
    }
 
?>


4、在实际应用中,需要从数据库中遍历取出符合条件,以及排序等操作,

4.1、创建Mysql存储函数,并对经纬度字段建立索引



 
    //根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)
    public static function getDistance($lat1,$lng1,$lat2,$lng2)
    {
        //地球半径
        $R = 6378137;
 
        //将角度转为狐度
        $radLat1 = deg2rad($lat1);
        $radLat2 = deg2rad($lat2);
        $radLng1 = deg2rad($lng1);
        $radLng2 = deg2rad($lng2);
 
        //结果
        $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;
 
        //精度
        $s = round($s* 10000)/10000;
 
        return  round($s);
    }
 
?>



通过SQL,可设置距离以及排序;可搜索出符合条件的信息,以及有一个较好的排序

1
SELECT *,latitude,longitude,GETDISTANCE(latitude,longitude,30.663262,104.071619) AS distance FROM  mb_shop_ext where 1 HAVING distance


=================================================================================================

比如,成都永丰立交的编码是wm3yr31d2524

1、利用一个字段,即可存储经纬度;搜索时,只需一条索引,效率较高
2、编码的前缀可以表示更大的区域,查找附近的,非常方便。 SQL中,LIKE ‘wm3yr3%’,即可查询附近的所有地点。
3、通过编码精度可模糊坐标、隐私保护等。

1、geohash的编码算法

1.1、纬度范围(-90, 90)平分成两个区间(-90, 0)、(0, 90),如果目标纬度位于前一个区间,则编码为0,否则编码为1。
由于30.625265属于(0, 90),所以取编码为1。
然后再将(0, 90)分成 (0, 45), (45, 90)两个区间,而39.92324位于(0, 45),所以编码为0,
然后再将(0, 45)分成 (0, 22.5), (22.5, 45)两个区间,而39.92324位于(22.5, 45),所以编码为1,
依次类推可得永丰立交纬度编码为101010111001001000100101101010。

1.3、合并经纬度编码,从高到低,先取一位经度,再取一位纬度;得出结果 111001001100011111101011100011000010110000010001010001000100












11100 10011 00011 11110 10111 00011 00001 01100 00010 00101 00010 00100 => wm3yr31d2524
 
十进制  0   1   2   3   4   5   6   7   8   9   10  11  12  13  14  15
base32   0   1   2   3   4   5   6   7   8   9   b   c   d   e   f   g
十进制  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31
base32   h   j   k   m   n   p   q   r   s   t   u   v   w   x   y   z


2、策略

2、查找附近,利用 在SQL中 LIKE ‘wm3yr3%’;且此结果可缓存;在小区域内,不会因为改变经纬度,而重新数据库查询

3、PHP基类




 

 
class Geohash
{
    private $coding="0123456789bcdefghjkmnpqrstuvwxyz";
    private $codingMap=array();
 
    public function Geohash()
    {
        for($i=0; $i
        {
            $this->codingMap[substr($this->coding,$i,1)]=str_pad(decbin($i), 5, "0", STR_PAD_LEFT);
        }
 
    }
 
    public function decode($hash)
    {
        $binary="";
        $hl=strlen($hash);
        for($i=0; $i
        {
            $binary.=$this->codingMap[substr($hash,$i,1)];
        }
 
        $bl=strlen($binary);
        $blat="";
        $blong="";
        for ($i=0; $i
        {
            if ($i%2)
                $blat=$blat.substr($binary,$i,1);
            else
                $blong=$blong.substr($binary,$i,1);
 
        }
 
        $lat=$this->binDecode($blat,-90,90);
        $long=$this->binDecode($blong,-180,180);
 
        $latErr=$this->calcError(strlen($blat),-90,90);
        $longErr=$this->calcError(strlen($blong),-180,180);
 
        $latPlaces=max(1, -round(log10($latErr))) - 1;
        $longPlaces=max(1, -round(log10($longErr))) - 1;
 
        $lat=round($lat, $latPlaces);
        $long=round($long, $longPlaces);
 
        return array($lat,$long);
    }
 
    public function encode($lat,$long)
    {
        $plat=$this->precision($lat);
        $latbits=1;
        $err=45;
        while($err>$plat)
        {
            $latbits++;
            $err/=2;
        }
 
        $plong=$this->precision($long);
        $longbits=1;
        $err=90;
        while($err>$plong)
        {
            $longbits++;
            $err/=2;
        }
 
        $bits=max($latbits,$longbits);
 
        $longbits=$bits;
        $latbits=$bits;
        $addlong=1;
        while (($longbits+$latbits)%5 != 0)
        {
            $longbits+=$addlong;
            $latbits+=!$addlong;
            $addlong=!$addlong;
        }
 
        $blat=$this->binEncode($lat,-90,90, $latbits);
 
        $blong=$this->binEncode($long,-180,180,$longbits);
 
        $binary="";
        $uselong=1;
        while (strlen($blat)+strlen($blong))
        {
            if ($uselong)
            {
                $binary=$binary.substr($blong,0,1);
                $blong=substr($blong,1);
            }
            else
            {
                $binary=$binary.substr($blat,0,1);
                $blat=substr($blat,1);
            }
            $uselong=!$uselong;
        }
 
        $hash="";
        for ($i=0; $i
        {
            $n=bindec(substr($binary,$i,5));
            $hash=$hash.$this->coding[$n];
        }
 
        return $hash;
    }
 
    private function calcError($bits,$min,$max)
    {
        $err=($max-$min)/2;
        while ($bits--)
            $err/=2;
        return $err;
    }
 
    private function precision($number)
    {
        $precision=0;
        $pt=strpos($number,'.');
        if ($pt!==false)
        {
            $precision=-(strlen($number)-$pt-1);
        }
 
        return pow(10,$precision)/2;
    }
 
    private function binEncode($number, $min, $max, $bitcount)
    {
        if ($bitcount==0)
            return "";
        $mid=($min+$max)/2;
        if ($number>$mid)
            return "1".$this->binEncode($number, $mid, $max,$bitcount-1);
        else
            return "0".$this->binEncode($number, $min, $mid,$bitcount-1);
    }
 
    private function binDecode($binary, $min, $max)
    {
        $mid=($min+$max)/2;
 
        if (strlen($binary)==0)
            return $mid;
 
        $bit=substr($binary,0,1);
        $binary=substr($binary,1);
 
        if ($bit==1)
            return $this->binDecode($binary, $mid, $max);
        else
            return $this->binDecode($binary, $min, $mid);
    }
}
 
?>


三、测试



 
require_once('Mysql.class.php');
require_once('geohash.class.php');
 
//mysql
$conf = array(
 
    'host' => '127.0.0.1',
    'port' => 3306,
    'user' => 'root',
    'password' => '123456',
    'database' => 'mocube',
    'charset' => 'utf8',
    'persistent' => false
);
 
$mysql = new Db_Mysql($conf);
$geohash=new Geohash;
 
//经纬度转换成Geohash

 
//获取附近的信息
$n_latitude = $_GET['la'];
$n_longitude = $_GET['lo'];
 
//开始
$b_time = microtime(true);
 
//方案A,直接利用数据库存储函数,遍历排序

 
//方案B geohash求出附近,然后排序
 
//当前 geohash值
$n_geohash = $geohash->encode($n_latitude,$n_longitude);
 
//附近
$n = $_GET['n'];
$like_geohash = substr($n_geohash, 0, $n);
 
$sql = 'select * from mb_shop_ext where geohash like "'.$like_geohash.'%"';
 
echo $sql;
 
$data = $mysql->queryAll($sql);
 
//算出实际距离
foreach($data as $key=>$val)
{
    $distance = getDistance($n_latitude,$n_longitude,$val['latitude'],$val['longitude']);
 
    $data[$key]['distance'] = $distance;
 
    //排序列
    $sortdistance[$key] = $distance;
}
 
//距离排序
array_multisort($sortdistance,SORT_ASC,$data);
 
//结束
$e_time = microtime(true);
 
echo $e_time - $b_time;
 
var_dump($data);
 
//根据经纬度计算距离 其中A($lat1,$lng1)、B($lat2,$lng2)
function getDistance($lat1,$lng1,$lat2,$lng2)
{
    //地球半径
    $R = 6378137;
 
    //将角度转为狐度
    $radLat1 = deg2rad($lat1);
    $radLat2 = deg2rad($lat2);
    $radLng1 = deg2rad($lng1);
    $radLng2 = deg2rad($lng2);
 
    //结果
    $s = acos(cos($radLat1)*cos($radLat2)*cos($radLng1-$radLng2)+sin($radLat1)*sin($radLat2))*$R;
 
    //精度
    $s = round($s* 10000)/10000;
 
    return  round($s);
}
 
?>



方案B的亮点在于:
1、搜索结果可缓存,重复使用,不会因为用户有小范围的移动,直接穿透数据库查询。
2、先缩小结果范围,再运算、排序,可提升性能。

在实际应用场景中,方案B数据库搜索可内存缓存;且如数据量更大,方案B结果会更优。

0.016560077667236
0.032402992248535
0.040318012237549

0.0079810619354248
0.0079669952392578
0.0064868927001953

两种方案,根据应用场景以及负载情况合理选择,当然推荐方案B;
不管哪种方案,都记得,给列加上索引,利于数据库检索。

猜你喜欢

转载自anduo.iteye.com/blog/1883038