Received: from localhost (pluto.hub.org [200.46.204.4]) by postgresql.org (Postfix) with ESMTP id 74BB89FA27F for ; Sat, 4 Nov 2006 00:19:52 -0400 (AST) Received: from postgresql.org ([200.46.204.71]) by localhost (mx1.hub.org [200.46.204.4]) (amavisd-new, port 10024) with ESMTP id 93722-09 for ; Sat, 4 Nov 2006 00:19:50 -0400 (AST) X-Greylist: from auto-whitelisted by SQLgrey- Received: from tigger.fuhr.org (tigger.fuhr.org [63.214.45.158]) by postgresql.org (Postfix) with ESMTP id CFC099F9EF0 for ; Sat, 4 Nov 2006 00:19:38 -0400 (AST) Received: from winnie.fuhr.org (winnie.fuhr.org [10.1.0.1]) by tigger.fuhr.org (8.13.8/8.13.8) with ESMTP id kA44JOwv031880 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK); Fri, 3 Nov 2006 21:19:27 -0700 (MST) Received: from winnie.fuhr.org (localhost [127.0.0.1]) by winnie.fuhr.org (8.13.8/8.13.8) with ESMTP id kA44JO0b075758; Fri, 3 Nov 2006 21:19:24 -0700 (MST) (envelope-from mfuhr@winnie.fuhr.org) Received: (from mfuhr@localhost) by winnie.fuhr.org (8.13.8/8.13.8/Submit) id kA44JMeF075757; Fri, 3 Nov 2006 21:19:22 -0700 (MST) (envelope-from mfuhr) Date: Fri, 3 Nov 2006 21:19:22 -0700 From: Michael Fuhr To: Sandeep Kumar Jakkaraju Cc: b.wood@niwa.co.nz, Postgresql Subject: Re: PostGIS Message-ID: <20061104041922.GA73674@winnie.fuhr.org> References: <013e01c6f390$7d474220$8e904618@owner> <4F3EB224-4B1A-4E1D-A364-F4477B21E62E@me3.com> <454689A5.8070306@niwa.co.nz> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Virus-Scanned: Maia Mailguard 1.0.1 X-Archive-Number: 200611/212 X-Sequence-Number: 103308 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