How to get the nearby cities of a city(geonameid) in python?

shaikh picture shaikh · Feb 1, 2012 · Viewed 12.9k times · Source

Given a radius of 100 KM from the base city(geonameid) i need to find the nearby cities.

My script is in python. Is there any API to find it out ?

Thanks

Answer

MMM picture MMM · Feb 1, 2012

If you have a database of cities, have a look at this example which shows how to build SQL queries for that particular task.

SELECT id, ( 3959 * acos( cos( radians(37) ) * cos( radians( lat ) ) * cos( radians( lng ) - radians(-122) ) + sin( radians(37) ) * sin( radians( lat ) ) ) ) AS distance FROM markers HAVING distance < 25 ORDER BY distance LIMIT 0 , 20;

So if you have your own database or cities or you are willing to create a database which has locations and coordinates of certain cities (might be available somewhere on the internet) then this solution will work for you.

Alternatively if you don't have a database and you want to use a API like Google Maps, have a look at The Google Geacoding API and Reverse Geocoding in particular. A request like:

http://maps.googleapis.com/maps/api/geocode/json?latlng=40.7,-73.9&sensor=false

Will give you possible addresses for the longitude and latitude you've provided it with.

Unfortunately the Google Places API (nearby places) only works for things like restaurants, shops, bars etc..