Re: Zip code



>I have a database of zipcodes with latitude and longitude. I also have the
>method of calculating the distance between two zipcodes. What I want to
>know is if there is an efficient algorithm for obtaining the zip codes
>within a specified distance of the first zipcode without having to retrieve
>and calculate for every record in the database.

How about a single select statement?

In this case I use a table called cities, where I have the fields city,
latitude and longitude. I have indexed the lat / long for improved speed.

$sqlstr="select city, ((acos(sin((latitude)*(pi()/180)) *
sin((".$latitude.")*(pi()/180)) + cos((latitude)*(pi()/180)) *
cos((".$latitude.")*(pi()/180)) * cos((longitude -
".$longitude.")*(pi()/180))))*(180/pi())* 60 * 1.1515 * 1.609344) dist,
latitude, longitude from cities order by dist limit 30";

On my server this query returns results in under a second when the table is
indexed correctly.

This query is only returning the 30 closest cities to a latitude longitude,
but perhaps you can search where the dist is less than so many kilometers.

oh the *1.609344 at the end of the equation converts miles to kilometers.

I was originally thinking something much more complex was necessary, but it
isn't.

Hope it helps!

OH - where did you get the lat long database?

Michael


.



Relevant Pages

  • Re: Quick and dirty spatial index
    ... Each location has a latitude and longitude. ... this particular database does not have the ability to ... create spatial indexes. ...
    (comp.infosystems.gis)
  • Re: Quick and dirty spatial index
    ... Each location has a latitude and longitude. ... this particular database does not have the ability to create spatial indexes. ... strange tesselation of the plane, ...
    (comp.infosystems.gis)
  • Re: Adjacency List and nested set -- J celko example -- Guru needed
    ... Is Elmira the CEO of the company? ... > while (latitude, longitude) tells you global location precisely. ... > are artificial aspects of the database mode and you will never find them ...
    (microsoft.public.sqlserver.programming)
  • Re: Quick and dirty spatial index
    ... Each location has a latitude and longitude. ... this particular database does not have the ability to ... create spatial indexes. ...
    (comp.infosystems.gis)
  • Re: retrieving information from website using Java
    ... that accepts an address and provides the latitude and longitude ... I have a very basic question regd retrieving information from website using ... Basically I am working on the development of a database where a user enters ...
    (comp.lang.java.help)