Playing with Leaflet (and Radar locations)

Yesterday, my friend Fleur did show me some interesting features of the leaflet package, in R.


In order to illustrate, consider locations of (fixed) radars, in several European countries. To get the data, use

radar=read.table(file=paste("destinator/",nf,sep=""), sep = ",", header = FALSE, stringsAsFactors = FALSE)
 radar$type <- sapply(radar$V3, function(x) {z=as.numeric(unlist(strsplit(x, " ")[[1]])); return(z[!])})
  radar <- radar[,c(1,2,4)]
  names(radar) <- c("lon", "lat", "type")
id=which(substr(L,4,8)=="Radar" & substr(L,nl-2,nl)=="csv")
for(i in id) radar_E=rbind(radar_E,ext_radar(L[i]))

(to be honest, if you run that code, you will get several countries, but not France… but if you want to add it, you should be able to do so…). The first tool is based on popups. If you click on a point on the map, you get some information, such as the speed limit where you can find a radar. To get a nice pictogram, use

fileUrl <- ""
download.file(fileUrl,"radar.png", mode = 'wb')
RadarICON <- makeIcon(  iconUrl = fileUrl,   iconWidth = 20, iconHeight = 20)

And then, use to following code get a dynamic map, mentionning the variable that should be used for the popup

m <- leaflet(data = radar_E) 
m <- m %>% addTiles() 
m <- m %>% addMarkers(~lon, ~lat, icon = RadarICON, popup = ~as.character(type))

Because the picture is a bit heavy, with almost 20K points, let us focus only on France,

The other interesting (not to say amazing) tool is a tool that creates clusters.

We have here the number of radars in a neighborhood of the point we see on the map. So far, it looks like polygons used are abitrary…

The code is extremely simple

m <- leaflet(radar_E) 
m <- m %>% addTiles()
m <- m %>% addMarkers(~lon, ~lat, clusterOptions = markerClusterOptions())

I still wonder how we can change the polygons (e.g. using more standard administrative regions). But I have to admit that this option is awesome.

3 thoughts on “Playing with Leaflet (and Radar locations)”

  1. Bonjour,

    merci beaucoup pour ce blog trés intéressant.
    Notamment les post sur leaflet que je commence juste à utiliser (mes précédentes cartes étaient faites avec ggplot2)

    Jean Jacques
    (ancien habitant de Rennes maintenant à Pessac 😉 )

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.