R – Find the nearest city for some coordinates

During the development of a web application, I had the need to make some graphs where mapping couples of latitudes and longitudes. There were many of those couples, more than 500000, making the graphs very ugly!

The solution was to find which is the biggest city nearest every coordinates, so I can highligths the numbers only on the biggest city (with more than 50000 inhabitants).

With google I find an R package: SearchTrees

We start retrieving a database with all the world cities. I found it on this site: http://www.maxmind.com/en/worldcities

On this page you can find a link to download the database. Unzip and put it in the directory from which you start R (or the base directory of the RStudio project).

Now we have the SearchTree object which allow us to perform searches with kNN algorithm, an algorithm which can find the number of entries (k) nearest the given value.

Lets see which results it gives.

I aspect to see the two cities (k=2) neasrest Cesano Boscone with a number of inhabitants equal or greater than 50000.

The numbers in the matrix are the index of the dataframe we use for tree creation (subCities).

Easy, isn’t it? 🙂

Posted in R, Tutorial.