pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Michael Fuhr <mike@fuhr.org>
To: Sandeep Kumar Jakkaraju <sandeepkumar.jakkaraju@gmail.com>
Cc: b.wood@niwa.co.nz, Postgresql <pgsql-general@postgresql.org>
Subject: Re: PostGIS
Date: Fri, 3 Nov 2006 21:19:22 -0700
Message-ID: <20061104041922.GA73674@winnie.fuhr.org> (raw)
In-Reply-To: <c653c98f0611020741t3ae8b1a0ua470a08a3c3be3e2@mail.gmail.com>
References: <013e01c6f390$7d474220$8e904618@owner>
	<4F3EB224-4B1A-4E1D-A364-F4477B21E62E@me3.com>
	<c653c98f0610190901v3e149482je80db02974f698b@mail.gmail.com>
	<454689A5.8070306@niwa.co.nz>
	<c653c98f0611020741t3ae8b1a0ua470a08a3c3be3e2@mail.gmail.com>

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



view thread (48+ messages)  latest in thread

Message-ID: <20061104041922.GA73674@winnie.fuhr.org>
Permalink:  ../20061104041922.GA73674@winnie.fuhr.org/
Also on:    postgresql.org/message-id/20061104041922.GA73674@winnie.fuhr.org

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: mike@fuhr.org, sandeepkumar.jakkaraju@gmail.com
  Subject: Re: PostGIS
  In-Reply-To: <20061104041922.GA73674@winnie.fuhr.org>
  References: <013e01c6f390$7d474220$8e904618@owner> <4F3EB224-4B1A-4E1D-A364-F4477B21E62E@me3.com> <c653c98f0610190901v3e149482je80db02974f698b@mail.gmail.com> <454689A5.8070306@niwa.co.nz> <c653c98f0611020741t3ae8b1a0ua470a08a3c3be3e2@mail.gmail.com> <20061104041922.GA73674@winnie.fuhr.org>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox