On Thu, Nov 02, 2006 at 09:11:58PM +0530, Sandeep Kumar Jakkaraju wrote:
> Like We have used the Haversine formula ...given on most of the websites ...
> to calculate the distance between to lat/lon points ..
You can examine the PostGIS source code to see how distance_sphere()
works; the underlying function is LWGEOM_distance_sphere() in
lwgeom/lwgeom_spheroid.c. The algorithm appears to be Haversine.
> which doesnt match with the distance given by distance_sphere() method
Could you post an example showing how you're calling distance_sphere(),
the output you get, and the output you get from other Haversine
functions? Several possibilities might explain the discrepancy:
1. If the difference is small then distance_sphere() might be using
a different value for the earth's radius than some other Haversine
function. The earth isn't a perfect sphere so simple functions use
approximations of the earth's radius and different functions might
use different values. distance_sphere() uses 6370986.884258304,
which is a reasonable (if overly precise) approximation of the
earth's radius in meters.
2. Make sure you're comparing the same units. A Haversine function
that returns distance in feet or miles won't return the same distance
as distance_sphere(), which returns meters.
3. Make sure your PostGIS geometries have coordinates in the correct
order: (X Y) which is (longitude latitude). This is a common cause
of problems for people accustomed to thinking (latitude longitude)
and who therefore create geometries as (Y X).
--
Michael Fuhr