4

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

shaikh
  • 1,355
  • 4
  • 16
  • 30

3 Answers3

10

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..

MMM
  • 7,221
  • 2
  • 24
  • 42
  • 1
    Actually, Places includes also a bunch of types that are not just establishments, but also includes locality, country, administrative units, and a bunch more: http://code.google.com/apis/maps/documentation/places/supported_types.html – Mano Marks Feb 01 '12 at 15:58
  • 1
    @ManoMarks Yes but those types are "supported by the Places API when sending Place Search requests". – MMM Feb 01 '12 at 16:13
  • right, I'm not really sure what the point you're making is then when you say "Unfortunately the Google Places API (nearby places) only works for things like restaurants, shops, bars etc.." – Mano Marks Feb 01 '12 at 16:37
  • My point is exactly that, the API only "works for things like restaurants, shops, bars etc..", you cannot get nearby cities, countries using Places API, however you can **search** for _places_ using such administrative units. – MMM Feb 01 '12 at 17:00
  • But Places that are localities and admin units are exactly cities and countries. I think you're making a false distinction – Mano Marks Feb 01 '12 at 17:32
  • -1 The quoted article is inaccurate (it says it is going to use the "haversine" formula but the formula used in the quoted SQL is the "spherical law of cosines" formula) and simplistic (if you don't have software with those formulas built in and you have a largish database, the necessary full table scan is extremely inefficient; you would need to calculate a bounding box and feed it to a query that retrieved a subset of points, using an index on latitude or longitude). – John Machin Feb 01 '12 at 21:58
  • 1
    Geez, I'm quoting an official Google article, I appreciate your knowledge but I can't be blamed for trusting that company – MMM Feb 01 '12 at 23:48
4

Since you're using the Geonames webservice, it looks like there's functionality built in to it to find nearby place names:

from: http://www.geonames.org/export/web-services.html

Find nearby populated place / reverse geocoding

Webservice Type : REST Url : api.geonames.org/findNearbyPlaceName? Parameters : lat,lng, lang: language of returned 'name' element (the pseudo language code 'local' will return it in local language), radius: radius in km (optional), maxRows: max number of rows (default 10) style: SHORT,MEDIUM,LONG,FULL (default = MEDIUM), verbosity of returned xml document Result : returns the closest populated place for the lat/lng query as xml document. The unit of the distance element is 'km'. Example: http://api.geonames.org/findNearbyPlaceName?lat=47.3&lng=9&username=demo

This service is also available in JSON format : http://api.geonames.org/findNearbyPlaceNameJSON?lat=47.3&lng=9&username=demo

Mano Marks
  • 8,761
  • 3
  • 27
  • 28
0

I'm afraid neither the Geocoding API nor the Places API would help you the way you want.

Reverse geocoding will give you the city for a given latlng, but not the ones around that one. Places API not even that. These APIs are for different uses, but I guess you're not interested in that now.

Unless you find an API that does what you want, you'd need to build your own database and run queries like the one MMM points out. To build your database, you can use the Populated Places dataset from Natural Earth's 1:10m Cultural Vectors. You'd need some sort of GIS software to process it and extract from those populated places you'd call cities though.

miguev
  • 4,481
  • 21
  • 41
  • OP is actually using the Geonames database – Mano Marks Feb 01 '12 at 16:38
  • Even easier, I hadn't looked at Geonames until today. Fusion Tables can do the trick, just import your favorite citiesXXXX table and then query the Fusion Tables SQL API using an ST_INTERSECTS spatial condition. – miguev Feb 01 '12 at 17:01