Re: calculating distance between longitude and latitude

From: Merlin Moncure <mmoncure(at)gmail(dot)com>
To: Geoffrey <lists(at)serioustechnology(dot)com>
Cc: pgsql-general(at)postgresql(dot)org
Subject: Re: calculating distance between longitude and latitude
Date: 2010-06-10 16:24:29
Message-ID: AANLkTimvprPanJ48_uzuWoHJfGsX8ihXO7bLStFfjWf8@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, Jun 9, 2010 at 3:02 PM, Geoffrey <lists(at)serioustechnology(dot)com> wrote:
> Does postgresql have functions to calculate the distance between two sets of
> longitude and latitude.

for posterity, if you are not:
*) very interested in high performance (that is, ok w/sql implementation)
*) needing super accurate results (ok with GC distance)
*) wanting to deal with dependencies (postgis, earthdistance)
*) interested in gist for indexed spacial searches

try this:

create or replace function gc_dist(_lat1 float8, _lon1 float8, _lat2
float8, _lon2 float8) returns float8 as
$$
select ACOS(SIN($1)*SIN($3)+COS($1)*COS($3)*COS($4-$2))*6371;
$$ language sql immutable;

I took that from here:
http://www.movable-type.co.uk/scripts/latlong.html

postgres=# select gc_dist(42, -74, 29, -81);
gc_dist
------------------
3725.88928230352

results in km. for serious stuff postgis is definitely the way to go.

merlin

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Justin Graf 2010-06-10 16:33:57 Re: Where has ms2pg gone?
Previous Message Paul Baker 2010-06-10 15:46:26 Postgresql packages in Solaris