Speed cameras in Toronto

Project
R
Leaflet
Creating a Leaflet map of all the speed cameras in Toronto
Author

Mark Edney

Published

February 16, 2022

Photo by Sepideh Golchin Rad on Unsplash

This project was originally written on 02/01/2021 as part of the Data Products course for the Data Science Specialization from Johns Hopkins University on Coursera

Objective

This report plots the speed cameras in the Greater Toronto Area from the data provided by Open Toronto, which can be found here.

Initialization

The following code is used to initialize the required libraries.

install.packages("opendatatoronto", repos = "https://cran.us.r-project.org", dependencies = TRUE)
package 'opendatatoronto' successfully unpacked and MD5 sums checked

The downloaded binary packages are in
    C:\Users\Mark\AppData\Local\Temp\RtmpglvqBO\downloaded_packages
library(opendatatoronto)
library(dplyr)
library(leaflet)

The following code is provided by the Open Toronto site to download the dataset.

# get package
package <- show_package("a154790c-4a8a-4d09-ab6b-535ddb646770")

# get all resources for this package
resources <- list_package_resources("a154790c-4a8a-4d09-ab6b-535ddb646770")

# identify datastore resources; by default, Toronto Open Data sets datastore resource format to CSV for non-geospatial and GeoJSON for geospatial resources
datastore_resources <- filter(resources, tolower(format) %in% c('csv', 'geojson'))

# load the first datastore resource as a sample
data <- filter(datastore_resources, row_number()==1) %>% get_resource()

Plotting the Data

The geometry in the dataset can be used directly with Leaflet, and the longitude and latitude do not need to be separated.

df <- data$geometry

Custom icons for the speed cameras can be used with the following code:

cameraicon <- makeIcon(
        iconUrl = "https://www.flaticon.com/svg/static/icons/svg/2164/2164608.svg",
        iconWidth = 35, iconHeight = 35
)

Finally, all the data and options can be passed to the leaflet function.

plt <- df %>%
        leaflet() %>%
        addTiles() %>%
        addMarkers(icon = cameraicon, clusterOptions = markerClusterOptions(), popup = data$location)