Re: PostGIS - Mailing list pgsql-general

From Michael Fuhr
Subject Re: PostGIS
Date
Msg-id 20061104041922.GA73674@winnie.fuhr.org
Whole thread Raw
In response to Re: PostGIS  ("Sandeep Kumar Jakkaraju" <sandeepkumar.jakkaraju@gmail.com>)
List pgsql-general
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

pgsql-general by date:

Previous
From: Alexander Staubo
Date:
Subject: Per-row security
Next
From: Shane Ambler
Date:
Subject: Re: Can PostgreSQL reside on the same server as MSDE?