How to use Oracle sdo_distance

I am trying to calculate the 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 12.7646185977151

By doing this using the 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];

Result 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 46.120690774231

The implementation of Oracle is clearly disabled, but I do not understand why. Any help would be greatly appreciated.

+5
source share
1 answer

Try changing 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.

Using SDO_GEOMETRY, the ordinates are listed in X, Y (longitude, latitude).

+5
source

All Articles