Playing with Leaflet (and Radar locations)

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

library(leaflet)

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

download.file("http://carte-gps-gratuite.fr/radars/zones-de-danger-destinator.zip","radar.zip")
unzip("radar.zip")
 
 ext_radar=function(nf){
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[!is.na(z)])})
  radar <- radar[,c(1,2,4)]
  names(radar) <- c("lon", "lat", "type")
  return(radar)}
 
L=list.files("./destinator/")
nl=nchar(L)
id=which(substr(L,4,8)=="Radar" & substr(L,nl-2,nl)=="csv")
 
radar_E=NULL
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 <- "http://evadeo.typepad.fr/.a/6a00d8341c87ef53ef01310f9238e6970c-800wi"
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))
m

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())
m

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.


OpenEdition suggests that you cite this post as follows:
Arthur Charpentier (September 30, 2015). Playing with Leaflet (and Radar locations). Freakonometrics. Retrieved November 3, 2024 from https://doi.org/10.58079/ov0l


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.