Convert (lat,lon) to city close-by

Using the GeoNames database we can create a tool that provides the city closest to a given latitude, longitude. geonamesHere you find a script including documentation to create and query a mysql table. It also includes the cities15000.txt file downloaded from GeoNames (it is licenses under the Creative Commons License 3.0).

It is used like so:

geonames % ./closest_place.sh 52.0 7.0
asciiname name latitude longitude distance population country timezone
Enschede Enschede 52.2183300 6.8958300 25.263898579933397 153655 NL Europe/Amsterdam

See the script on how to change the output. Add --skip-column-namesto get rid of the table header. Change the SQL query to reduce the fields that are being returned, or to adapt the formatting.

Update: Perl version of the code, without the need for a database can be found in the archiveas well. To be used like so (200ms CPU time when using cities15000.txt):

geonames % ./closest_place.pl 52.0 7.0
Enschede

Leave a Reply

Your email address will not be published. Required fields are marked *