Site icon R-bloggers

Speed cameras in Toronto

[This article was first published on The Data Sandbox, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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

< section id="objective" class="level2">

Objective

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

< section id="initialization" class="level2">

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\RtmpGUv5NT\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()
< section id="plotting-the-data" class="level2">

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)
To leave a comment for the author, please follow the link and comment on their blog: The Data Sandbox.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.