Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to find nearest cities in a given radius?

Do you know some utility or a web site where I can give US city,state and radial distance in miles as input and it would return me all the cities within that radius?

Thanks!

like image 941
Jeremy Thomson Avatar asked Mar 12 '09 20:03

Jeremy Thomson


4 Answers

Here is how I do it.

You can obtain a list of city, st, zip codes and their latitudes and longitudes. (I can't recall off the top of my head where we got ours)

edit: http://geonames.usgs.gov/domestic/download_data.htm like someone mentioned above would probably work.

Then you can write a method to calculate the min and max latitude and longitudes based on a radius, and query for all cities between those min and max. Then loop through and calculate the distance and remove any that are not in the radius

double latitude1 = Double.parseDouble(zipCodes.getLatitude().toString());
double longitude1 = Double.parseDouble(zipCodes.getLongitude().toString());

//Upper reaches of possible boundaries
double upperLatBound = latitude1 + Double.parseDouble(distance)/40.0;
double lowerLatBound = latitude1 - Double.parseDouble(distance)/40.0;

double upperLongBound = longitude1 + Double.parseDouble(distance)/40.0;
double lowerLongBound = longitude1 - Double.parseDouble(distance)/40.0;

//pull back possible matches
SimpleCriteria zipCriteria = new SimpleCriteria();
zipCriteria.isBetween(ZipCodesPeer.LONGITUDE, lowerLongBound, upperLongBound);
zipCriteria.isBetween(ZipCodesPeer.LATITUDE, lowerLatBound, upperLatBound);
List zipList = ZipCodesPeer.doSelect(zipCriteria);
ArrayList acceptList = new ArrayList();

if(zipList != null)
{
    for(int i = 0; i < zipList.size(); i++)
    {
        ZipCodes tempZip = (ZipCodes)zipList.get(i);
        double tempLat = new Double(tempZip.getLatitude().toString()).doubleValue();
        double tempLon = new Double(tempZip.getLongitude().toString()).doubleValue();
        double d = 3963.0 * Math.acos(Math.sin(latitude1 * Math.PI/180) * Math.sin(tempLat * Math.PI/180) + Math.cos(latitude1 * Math.PI/180) * Math.cos(tempLat * Math.PI/180) *  Math.cos(tempLon*Math.PI/180 -longitude1 * Math.PI/180));

        if(d < Double.parseDouble(distance))
        {
            acceptList.add(((ZipCodes)zipList.get(i)).getZipCd());  
        }
    }
}

There's an excerpt of my code, hopefully you can see what's happening. I start out with one ZipCodes( a table in my DB), then I pull back possible matches, and finally I weed out those who are not in the radius.

like image 169
Sheldon Ross Avatar answered Nov 07 '22 13:11

Sheldon Ross


Oracle, PostGIS, mysql with GIS extensions, sqlite with GIS extensions all support this kind of queries.

If you don't have the dataset look at:

http://www.geonames.org/

like image 22
Vasil Avatar answered Nov 07 '22 12:11

Vasil


Take a look at this web service advertised on xmethods.net. It requires a subscription to actually use, but claims to do what you need.

The advertised method in question's description:

GetPlacesWithin Returns a list of geo places within a specified distance from a given place. Parameters: place - place name (65 char max), state - 2 letter state code (not required for zip codes), distance - distance in miles, placeTypeToFind - type of place to look for: ZipCode or City (including any villages, towns, etc).

http://xmethods.net/ve2/ViewListing.po?key=uuid:5428B3DD-C7C6-E1A8-87D6-461729AF02C0

like image 30
sgriffinusa Avatar answered Nov 07 '22 13:11

sgriffinusa


You can obtain a pretty good database of geolocated cities/placenames from http://geonames.usgs.gov - find an appropriate database dump, import it into your DB, and performing the kind of query your need is pretty straightforward, particularly if your DBMS supports some kind of spatial queries (e.g. like Oracle Spatial, MySQL Spatial Extensions, PostGIS or SQLServer 2008)

See also: how to do location based search

like image 35
Paul Dixon Avatar answered Nov 07 '22 12:11

Paul Dixon