I just got a speeding ticket by the mail. I’m pissed off, mostly at myself.

Yet, I now have two questions regarding the relevance and efficiency of photo radars:

  • Relevance: Are photo radars optimally located in areas with lots of crashes?
  • Efficiency: Do photo radars reduce crash frequency?

Let’s figure this out by look at some #opendata from Montreal.

Disclaimer: I am not a domain expert, I didnt even bother to do a litterature review. I’m just a pissed off dude killing an evening while the kids are asleep.

The data

We combine two sources:

The code

As usual, the code is on my github.

The most useful code snippet I will come back here for is the combination of split() and map() to perform rowwise computation on sf objects. I use it to find the crashes that happened within 100 meters of a photo radar:

get_accidents_proches <- function(radar){
  distances <- as.numeric(st_distance(radar, accidents))
  accidents_proches  <- distances<100
  if(sum(accidents_proches) > 0){
    data <- accidents
    data$distance_du_radar <- distances
    data[accidents_proches,]
  } else{NULL}
}



radars %>% 
mutate(accidents_proches = split(., 1:nrow(.))  %>% 
         map(get_accidents_proches)
) 

note that you can sometime map geometry directly:

radars %>% mutate(
  bb = map(geometry, st_bbox)
)

I also like this function from data.table that allows me to bind the rows of sfdata.frames:

sf::st_as_sf(data.table::rbindlist(pouet$accidents_proches))

The following code is used to wrap the text in the facet titles :

facet_wrap(~ descriptio, labeller = label_wrap_gen(width = 30))

The results

Relevance

Here is a heatmap of the 171 260 crashes that occurred in Montreal between 2012 and 2018. The camera markers show the locations of the 5 fixed photo radars. I believe we can agree that some locations with tons of accidents were ignored.