johnnyb
04-21-2006, 09:23 AM
Hi,
So, I have this table of postal codes, (what we call ZIP codes up here in the great white north), and their associated latitudes & longitudes. I am developing an application that searches for stores within X kilometers of a given postal code. First step: Make a query that will return all postal codes within x kilometers of that postal code. After that some joins will be easy.
Here's what I have:
SELECT postalcodes.postalcode,
(ACOS(
COS(RADIANS('.$point_item['Latitude'].'))
*COS(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*COS(RADIANS(postalcodes.Longitude))
+
COS(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*SIN(RADIANS(postalcodes.Longitude))
+
SIN(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
)
* 6378.15) AS distance
FROM postalcodes WHERE
(ACOS(
COS(RADIANS('.$point_item['Latitude'].'))
*COS(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*COS(RADIANS(postalcodes.Longitude))
+
COS(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*SIN(RADIANS(postalcodes.Longitude))
+
SIN(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
)
* 6378.15)
< 500
So, all of the big math gives me the distance, (great circle route even!). $point_item['latitude'] and $point_item['longitude'] are the lat & lon of the originating postal code. The <500 at the end means I am searching within 500 kilometers of the originating postal code.
So, I should have some results, right? But no :( My query returns 0 rows. Apparently I'm doing something wrong in the SQL, any pointers?
Also, I'd like to not have to run the first query to get the lat & lon of the originating postal code. I think I should be able to run it all in one query, I just don't know what that query is. Finally, I'd like to avoid typing the math twice if possible. I tried some subqueries in the first portion of the statement but was told by my server that I had an error in my SQL syntax.
Anyhow, if anyone knows what I'm doing wrong please let me know!
Thanks a lot.
John
So, I have this table of postal codes, (what we call ZIP codes up here in the great white north), and their associated latitudes & longitudes. I am developing an application that searches for stores within X kilometers of a given postal code. First step: Make a query that will return all postal codes within x kilometers of that postal code. After that some joins will be easy.
Here's what I have:
SELECT postalcodes.postalcode,
(ACOS(
COS(RADIANS('.$point_item['Latitude'].'))
*COS(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*COS(RADIANS(postalcodes.Longitude))
+
COS(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*SIN(RADIANS(postalcodes.Longitude))
+
SIN(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
)
* 6378.15) AS distance
FROM postalcodes WHERE
(ACOS(
COS(RADIANS('.$point_item['Latitude'].'))
*COS(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*COS(RADIANS(postalcodes.Longitude))
+
COS(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
*COS(RADIANS(postalcodes.Latitude))
*SIN(RADIANS(postalcodes.Longitude))
+
SIN(RADIANS('.$point_item['Latitude'].'))
*SIN(RADIANS('.$point_item['Longitude'].'))
)
* 6378.15)
< 500
So, all of the big math gives me the distance, (great circle route even!). $point_item['latitude'] and $point_item['longitude'] are the lat & lon of the originating postal code. The <500 at the end means I am searching within 500 kilometers of the originating postal code.
So, I should have some results, right? But no :( My query returns 0 rows. Apparently I'm doing something wrong in the SQL, any pointers?
Also, I'd like to not have to run the first query to get the lat & lon of the originating postal code. I think I should be able to run it all in one query, I just don't know what that query is. Finally, I'd like to avoid typing the math twice if possible. I tried some subqueries in the first portion of the statement but was told by my server that I had an error in my SQL syntax.
Anyhow, if anyone knows what I'm doing wrong please let me know!
Thanks a lot.
John