2013-02-05 38 views
5

我試圖計算Oracle數據庫中的兩個點之間的距離,如何使用Oracle的sdo_distance

Point A is 40.716715, -74.033907 
Point B is 40.716300, -74.033900 

使用這個SQL語句:

SELECT sdo_geom.sdo_distance(sdo_geom.sdo_geometry(2001 ,8307 ,sdo_geom.sdo_point_type(40.716715, -74.033907 , NULL) ,NULL ,NULL) 
          ,sdo_geom.sdo_geometry(2001 ,8307 ,sdo_point_type(40.716300,-74.033901, NULL) ,NULL ,NULL) ,0.0001 ,'unit=M') distance_in_m 
          from DUAL; 

結果是12.7646185977151

在使用Apple CoreLocation API時:

CLLocation* pa = [[CLLocation alloc] initWithLatitude:40.716715 longitude:-74.033907]; 
CLLocation* pa2 = [[CLLocation alloc] initWithLatitude:40.716300 longitude:-74.033900]; 
CLLocationDistance dist = [pa distanceFromLocation:pa2]; 

結果是46.0888946842423

自己實現

double dinstance_m(double lat1, double long1, double lat2, double long2){ 
double dlong = (long2 - long1) * d2r; 
double dlat = (lat2 - lat1) * d2r; 
double a = pow(sin(dlat/2.0), 2) + cos(lat1*d2r) * cos(lat2*d2r) * pow(sin(dlong/2.0), 2); 
double c = 2 * atan2(sqrt(a), sqrt(1-a)); 
double d = 6367 * c; 

return d * 1000.; 
} 

結果是46.120690774231

Oracle的實現顯然是關閉的,但我不明白爲什麼。任何幫助將不勝感激。

回答

5

嘗試扭轉縱座標的順序,如:

SELECT sdo_geom.sdo_distance(sdo_geom.sdo_geometry(2001, 8307, sdo_geom.sdo_point_type(-74.033907, 40.716715, NULL), NULL, NULL), 
          sdo_geom.sdo_geometry(2001, 8307, sdo_point_type(-74.033901, 40.716300, NULL), NULL, NULL), 0.0001, 'unit=M') distance_in_m 
          from DUAL; 

我得到46.087817955912。

使用SDO_GEOMETRY,座標以X,Y(經度,緯度)順序列出。