#Load Libraries-----
library(leaflet)
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.8.2, PROJ 9.3.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(here)
## here() starts at C:/Users/rwetzel/OneDrive - Granger Waste Services/Documents/Richard Wetzel/Administrative/MGIS/588/Lab 4

Introduction

This exercise is focused on building an interactive map using the Leaflet package in R. For my project, I chose to map arenas for both professional hockey leagues in North America: the National Hockey League (NHL) and the Professional Women’s Hockey League (PWHL). The NHL has been operating since 1917 when it began with the original six teams: the Detroit Red Wings, Chicago Blackhawks, New York Rangers, Toronto Maple Leafs, Montreal Canadiens, and the Boston Bruins. The PWHL is currently in its inaugural season, also featuring six teams: Boston, Montreal, Toronto, New York, Ottawa, and Minnesota. As the PWHL was stood up quickly, teams do not have logos or team names other and are instead go by the league and city, for example PWHL Boston.

#Set working directory where data and images are stored
setwd("C:/Users/rwetzel/OneDrive - Granger Waste Services/Documents/Richard Wetzel/Administrative/MGIS/588/Lab 4/")

#Read in data
arenas = st_read("NHL_Arenas.shp", quiet=TRUE)

Data Preparation

The data used in this map did not require much processing in order for it to be mapped. The only additional processing necessary was the creation of the dialog to be used for the popup box whenever that icon is clicked on the map. Using the paste0() function, a string is created that will identify the arena’s opening date, it’s capacity, and the team(s) that play there. Only one arena, the Xcel Energy Center in Minnesota, hosts two teams as both the Minnesota Wild of the NHL and PWHL Minnesota play their games there.

#Create the information used in the popup field using the paste0 function.
arenas = arenas %>%
  mutate(pup = paste0(Arena, " opened in ", Opened, " and has a capacity of ", Capacity,".  It is home to the ",Team,"."))

Mapping

Arenas are symbolized according to the league that the arena’s occupants play in. Ideally, a separate symbol, or combination of the two league logos, would be used for Xcel Energy Center as they host both an NHL and PWHL team, but I am not a graphic designer and could not locate a suitable alternative.

#Create a list of Icons displaying the league logos for display in the map. 
#Xcel Energy Center (in Minnesota) is home to both an NHL and PWHL team. Since I
#am not a graphic designer, I am using the NHL logo rather than trying to create 
#a composite logo for that site. 
#Ensure that the names of the icons match the field, in this case "League", that 
#will be used to specify which logo is used for each site. 
logoIcons <- iconList(
  PWHL = makeIcon(iconUrl = 'PWHL_Logo.svg',
                       iconWidth=95,
                       iconHeight =100),
  NHL = makeIcon(iconUrl = 'NHL_Shield.svg',
                      iconWidth=25,
                      iconHeight =25)
)

#Create the map
leaflet()%>%
  addTiles()%>% #Leaving the arguments blank defaults to OpenStreetMap.
  setView(lat = 40.302885,lng = -98.0536854,zoom = 4)%>% #These coordinates are geographic centroid of the arena dataset
  addMarkers(data=arenas, icon=~logoIcons[League], popup= ~pup, label = ~Arena)