- 함수 생성

-- $1 : lat (기준점:위도)

-- $2 : lng  (기준점:경도)

-- $3: lat (target : 위도)

-- $4 : lng (target : 경도)

create function fn_geo_distance(double precision, double precision, double precision, double precision) returns double precision as $$

select 

   ( 

(

   acos(

   sin( $1 * PI() /180.0 ) * 

   sin( $3 * PI() /180.0 ) + 

   cos( $1 * PI() /180.0 )  * 

   cos( $3 * PI() /180.0 ) * 

   cos( ($2 - $4) * PI() /180.0  ) 

   ) 

) *  180.0 / PI() 

   ) * 60*1.1515*1.609344*1000 

)

$$ LANGUAGE SQL;


- 사용 예

select fn_geo_distance(37.51084072964695, 127.01951086521149, cast(lat as double precision), cast(lng as double precision)) as distance


ps

 - 계산 알고리즘은 이해 못함. (ㅠㅠ)

'DBMS > postgresql' 카테고리의 다른 글

[poostgresql] - postgis 사용하여 반경 검색하기  (0) 2015.01.09
[postgresql] - OIDS 란?  (0) 2015.01.02
Posted by 짱가쟁이