haversine formula with postgreSQL

From: Jonathan <jharahush(at)gmail(dot)com>
To: pgsql-general(at)postgresql(dot)org
Subject: haversine formula with postgreSQL
Date: 2009-09-17 19:16:34
Message-ID: bfb71d4a-1ff7-4114-a531-24e0fea7e60d@r36g2000vbn.googlegroups.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi!

I am looking at the PHP/MySQL Google Maps API store locator example
here:

http://code.google.com/apis/maps/articles/phpsqlsearch.html

And I'm trying to get this to work with PostgreSQL instead of MySQL.

I've (slightly) modified the haversine formula part of my PHP script
but I keep getting this error:

Invalid query: ERROR: column "distance" does not exist LINE
1: ...ude ) ) ) ) AS distance FROM aaafacilities HAVING distance <...
^

I'm new to this, but it doesn't look like I need to create a column in
my table for distance, or at least the directions didn't say to create
a distance column.

Here is my PHP with SQL:
$query = sprintf("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 dist LIMIT 0 OFFSET 20",
pg_escape_string($center_lat),
pg_escape_string($center_lng),
pg_escape_string($center_lat),
pg_escape_string($radius));

Does anyone have any ideas on how I can get this to work? I'm not
sure what is wrong, since it doesn't seem like I need to create a
distance column and when I do create one, I get this:

Invalid query: ERROR: column "aaafacilities.latitude" must appear in
the GROUP BY clause or be used in an aggregate function

Thanks for any comments or suggestions. I appreciate it. I'm new to
this.

Jonathan Harahush

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Alan McKay 2009-09-17 19:19:22 Re: limiting query time and/or RAM
Previous Message Scott Marlowe 2009-09-17 19:10:31 Re: limiting query time and/or RAM