• Sphinx的GEO距离搜索 [转载]


    近项目用sphinx做地理搜索,可是结果总是不对,明明很近却搜不到

    结果检查sphinx源文件:

    static inline double sphSqr ( double v )
    
    {
    
    return v*v;
    
    }
    
    float ExprGeodist_t::Eval ( const CSphMatch & tMatch ) const
    
    {
    
    const double R = 6384000;
    
    float plat = tMatch.GetAttrFloat ( m_tGeoLatLoc );
    
    float plon = tMatch.GetAttrFloat ( m_tGeoLongLoc );
    
    double dlat = plat - m_fGeoAnchorLat;
    
    double dlon = plon - m_fGeoAnchorLong;
    
    double a = sphSqr ( sin ( dlat/2 ) ) + cos(plat)*cos(m_fGeoAnchorLat)*sphSqr(sin(dlon/2));
    
    double c = 2*asin ( Min ( 1, sqrt(a) ) );
    
    return (float)(R*c);
    
    }
    

      

    附上解决办法:

    $search->SetGeoAnchor('latitude', 'longitude', (float) deg2rad($_latitude), (float) deg2rad($_longitude));

    传入坐标的时候把坐标角度转换成弧度即可

    PHP版弧度计算:

    function sphSqr($v){

    return $v*$v;

    }

    function cacu_geo($loc_lat,$loc_lng,$geo_lat,$geo_lng){

    $R = 6384000;

    $plat = (float)$loc_lat;

    $plon = (float)$loc_lng;

    $dlat = $plat - $geo_lat;

    $dlon = $plon - $geo_lng;

    $a = sphSqr ( sin ( $dlat/2 ) ) + cos($plat)*cos($geo_lat)*sphSqr(sin($dlon/2));

    $c = 2*asin ( min ( 1, sqrt($a) ) );

    return (float)($R*$c);

    }

    MYSQL版本坐标计算(角度):

    SELECT 6384000 * 2 * ASIN(SQRT(

    POWER(SIN((31.218992 - abs(31.218992)) * pi()/180 / 2), 

    2) +  COS(31.218992 * pi()/180 ) * COS(abs(31.218992) * 

    pi()/180) *  POWER(SIN((121.431241 - 121.431480) * 

    pi()/180 / 2), 2) )) as  distance

    PHP版本坐标计算(角度):

    function rad($d)

    {

        return $d * 3.1415926535898 / 180.0;

    }

    function GetDistance($lat1, $lng1, $lat2, $lng2)

    {

        $EARTH_RADIUS = 6384.000;

        $radLat1 = rad($lat1);

        $radLat2 = rad($lat2);

        $a = $radLat1 - $radLat2;

        $b = rad($lng1) - rad($lng2);

        $s = 2 * asin(sqrt(pow(sin($a / 2), 2) + cos($radLat1) * cos($radLat2) * pow(sin($b / 2), 2)));

        $s = $s * $EARTH_RADIUS;

        $s = round($s * 10000) / 10000;

        return $s;

    }

  • 相关阅读:
    自然语言处理3.4——使用正则表达式检测词组搭配
    自然语言处理3.3——使用Unicode进行文字处理
    自然语言处理3.1——从网络和硬盘访问文本
    自然语言处理2.3——词典资源
    自然语言处理2.2——条件频率分布
    自然语言处理——NLTK中文语料库语料库
    自然语言处理2.1——NLTK文本语料库
    【转载】使用LFM(Latent factor model)隐语义模型进行Top-N推荐
    Ajax (jquery)实现智能提示搜索框(in Django)
    python操作mysql数据库
  • 原文地址:https://www.cnblogs.com/jamesbd/p/4698777.html
Copyright © 2020-2023  润新知