Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Oracle's sdo_distance

I am trying to calculate a distance between two points in Oracle DB,

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

using this sql statement:

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;

Result is 12.7646185977151

While doing it using Apple's 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];

Result is 46.0888946842423

Own implementation

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.;
}

Result is 46.120690774231

Oracle's implementation is clearly off, but I don't see why. Any help would be greatly appreciated.

like image 461
bioffe Avatar asked Feb 05 '13 23:02

bioffe


1 Answers

Try reversing the order of the ordinates, as in:

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;

I get 46.087817955912.

With SDO_GEOMETRY, ordinates are listed in X, Y (longitude, latitude) order.

like image 100
Brian Camire Avatar answered Oct 23 '22 08:10

Brian Camire