Visualize geographical areas around points of interest with leaflet

Using the leaflet package to view a zone intersection Aim: Draw circles around points of interest

This article is for me the opportunity to share something I discovered on R using the leaflet package: the possibility to draw a color area around a given GPS point.
Utility: Visualize an area that meets one or more location criteria to find a house, school or other…

For information, it is possible to ask R to give you the GPS coordinates of a point but for this purpose you must use the geocode() function of the ggmap package.
Unfortunately since July 2018, to use it you need to create a Google Cloud account by entering your credit card number (sic). So I decided to do without it here.
For the code, you just have to put the place between quotation marks like this geocode("gare du Mans").
However, if you want to use ggmap and you have a problem loading the map, I will detail in the last part how to fix the problem.

Import a base map with leaflet

To view maps under R there are several tools.
The leaflet package is very useful because it allows you to zoom in/out but also to move around on the map.

I start by loading the background map, focused on my main point of interest, here Le Mans station (longitude = 0.192612, latitude = 47.995675), to which I add a yellow circle with a radius of 10 km.

library(leaflet)

central_point_coordinates <- c(lng = 0.192612, lat = 47.995675)

distance_max <- 10

map_background <- leaflet() %>%
setView(lng = central_point_coordinates, lat = central_point_coordinates, zoom = 10) %>%
addCircles(lng = as.numeric(central_point_coordinates), lat = as.numeric(central_point_coordinates), radius = distance_max * 1000, color = "yellow")

map_background

A first zone is good but generally, we are interested in several places. You can add to this map, the desired number of points of interest.

Add points of interest with their zones

A second one:

coordinates_point_two <- c(0.139636, 47.921906)

distance_max_two <- 10

map <- map_background %>%
addCircles(lng = coordinates_point_two, lat = coordinates_point_two, radius = distance_max_two * 1000, color = "red")

map

And one last one.

coordinates_point_three <- c(lng = 0.07, lat = 48.02)

distance_max_three <- 8

map_with_3 <- map_background %>%
addCircles(lng = as.numeric(coordinates_point_three), lat = as.numeric(coordinates_point_three), radius = distance_max_three * 1000, color = "blue")

map_with_3
map <- map %>%
addCircles(lng = as.numeric(coordinates_point_three), lat = as.numeric(coordinates_point_three), radius = distance_max_three * 1000, color = "blue")

map

I don’t know if you noticed it, but there is a small difference between adding point 2 and point 3, because when you give names to values, the addCircles() function needs a numeric value only, so I added as.numeric() in the line of code.

It’s over for this article, hoping it will be useful to you.
As mentioned at the beginning of the article, I will explain below how to create a Google Cloud account and update ggmap.

Trick: Google Cloud Account and API use

If you haven’t used ggmap since July 2018, you may not have realized the problem.

Error in download.file(url, destfile = tmp, quiet = !messaging, mode = “wb”) : impossible to open the URL at http://maps.googleapis.com/maps/api/staticmap?center=gare+du+du+Mans&zoom=13&size=640x640&scale=2&maptype=terrain&language=en-EN&sensor=false

Google has decided to “close” the use of APIs, so now you need a code / key to do it. To be able to obtain this key, you must first create an account by entering your credit card number…

To create the account, go to Google Cloud Platform.
Once this is done, you need to reinstall ggmap using the @dkahle code available here.
devtools::install_github("dkahle/ggmap", ref = "tidyup")
Restart R
And then
library(ggmap)
And that’s it, it took me a while to do much so I put it here!

Small bonus: How to use the package opencage to find GPS coordinates

Following Maëlle Salmon’s tweet, I decided to add this part to the article.

Indeed, Maëlle pointed out to me that Open Cage Data was a very good alternative to no longer using geocode(), so I take this opportunity to pass on the information here!

First step, as for Google Cloud you need to create an account here to get a key but no need to enter your credit card number!

Then it is necessary to register the key in the environment of R by doing usethis::edit_r_environ() which opens a file .Renviron.
In this file you must add OPENCAGE_KEY="copy-paste-your-key".
Caution the file must always end with a blank line! So if your file is blank, first line you put the key in and tap on enter to have a second blank line.

Now it is necessary to restart R to be able to use the database.

I’m not going to detail all the features here, just tell you how to get the coordinates. For the rest, I refer you to the GitHub page of Open Cage Data which is much more accurate.

To obtain coordinates
Install the package
library("devtools")
install_github("ropensci/opencage")