Re: haversine formula with postgreSQL

From: Jonathan <jharahush(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: haversine formula with postgreSQL
Date: 2009-09-17 20:55:19
Message-ID: 1fc16f45-f2c0-4a56-96e0-5a649f92b25f@o21g2000vbl.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

It's the whole query as far as I can tell. The app takes input from
the user --- the user enters an address and chooses a radius ("show me
all facilities within 5 miles of this address") and then the latitude
and longitude of the address and the radius is passed into the query
so that the database can grab all locations within a certain radius.

The example provided by Google is using MySQL. The query for MySQL
looks like this:

SELECT address, name, lat, lng, ( 3959 * acos( cos( radians('%s') ) *
cos( radians( lat ) ) * cos( radians( lng ) - radians('%s') ) + sin
( radians('%s') ) * sin( radians( lat ) ) ) ) AS distance FROM markers
HAVING distance < '%s' ORDER BY distance LIMIT 0 , 20

And I'm attempting to change it to work with Postgres and have done
this:

SELECT 'ID', 'FACILITY', 'ADDRESS', latitude, longitude, ( 3959 * acos
( cos( radians('%s') ) * cos( radians( latitude ) ) * cos( radians
( longitude ) - radians('%s') ) + sin( radians('%s') ) * sin( radians
( latitude ) ) ) ) AS distance FROM aaafacilities HAVING distance <
'%s' ORDER BY distance LIMIT 0 OFFSET 20

aaafacilities is my table name in my posgres database.

I'm sorry if this isn't enough info.. like I said, I'm new to this but
definitely interested in learning and figuring this out!

From what I can tell, the database is supposed to calculate and then
output the distance of each "match" but it seems like in the MySQL
example, it can do this without having an actual distance column in
the database.

Thanks again!

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2009-09-17 21:25:07 Re: 'Weird' errors
Previous Message Martin Gainty 2009-09-17 20:48:23 Re: 'Weird' errors