#replace my path with yours to the Lab 5 folder
# setwd("/Users/xfliang/University of Michigan Dropbox/Xiaofan Liang/UM_Teaching/URP535_Urban_Informatics/W25/Lab/Lab10/")
setwd("G:/My Drive/MISC/MASTER/2023-24/Umich/Winter 25/URP 535/Week 10/Lab10")
#install.packages('tidyverse')
library(tidyverse)
## Warning: package 'readr' was built under R version 4.4.3
## Warning: package 'dplyr' was built under R version 4.4.3
#install.packages('tmap')
library(tmap)
#install.packages('sf')
library(sf)
#install.packages('tmap')
library(osmdata)
#install.packages('tmap')
library(osmdata)
#install.packages('leaflet')
library(leaflet) # for visualizing leaflet maps
#install.packages("shiny")
library(shiny) # for writing a shiny app
## Warning: package 'shiny' was built under R version 4.4.3
# install.packages("rsconnect")
library(rsconnect)# for deploying a shiny app
# Get all food-related amenities in Ann Arbor
q <- opq(bbox = getbb("Ann Arbor, US")) %>%
add_osm_features(features = c(
"amenity" = "restaurant",
"amenity" = "bar",
"amenity" = "biergarden",
"amenity" = "fast_food",
"amenity" = "food_court",
"amenity" = "cafe",
"amenity" = "pub",
"amenity" = "ice_cream"
)) %>%
osmdata_sf()
# only take point geometry and drop entries that do not have values in name and amenity category
amenity_point <- q$osm_points %>%
select(osm_id, name, amenity, geometry) %>%
drop_na(name, amenity)
head(amenity_point)
## Simple feature collection with 6 features and 3 fields
## Geometry type: POINT
## Dimension: XY
## Bounding box: xmin: -83.74839 ymin: 42.25572 xmax: -83.68812 ymax: 42.28785
## Geodetic CRS: WGS 84
## osm_id name amenity geometry
## 305088494 305088494 Casey's Tavern pub POINT (-83.74434 42.28785)
## 541900696 541900696 Starbucks cafe POINT (-83.68812 42.25572)
## 560917683 560917683 Good Time Charley's bar POINT (-83.73485 42.27482)
## 695043656 695043656 Real Seafood Company restaurant POINT (-83.74839 42.27837)
## 748964467 748964467 Pita Kabob Grill restaurant POINT (-83.74112 42.27797)
## 749007070 749007070 Ashley's Ann Arbor pub POINT (-83.74095 42.2781)
Explore leaflet documentation - Add markers to leaflet page. Pick one feature to implement in addition to the amenity map in the lab (colored by amenity type), such as customizing marker icons, marker cluster, or customize the circle markers by radius.
custom_icons <- icons(
iconUrl = ifelse(amenity_point$amenity == "restaurant", "https://cdn-icons-png.flaticon.com/512/6643/6643359.png",
ifelse(amenity_point$amenity == "pub", "https://cdn-icons-png.flaticon.com/512/2504/2504294.png",
ifelse(amenity_point$amenity == "cafe", "https://cdn-icons-png.flaticon.com/512/2935/2935477.png",
ifelse(amenity_point$amenity == "bar", "https://cdn-icons-png.freepik.com/512/1748/1748081.png",
ifelse(amenity_point$amenity == "fast_food", "https://cdn-icons-png.flaticon.com/512/3703/3703377.png",
ifelse(amenity_point$amenity == "ice_cream", "https://cdn-icons-png.flaticon.com/512/3077/3077188.png",
"https://cdn-icons-png.flaticon.com/512/684/684908.png")))))), # Default marker
iconWidth = 30, iconHeight = 30
)
leaflet(data = amenity_point) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers(
lng = st_coordinates(amenity_point)[,1],
lat = st_coordinates(amenity_point)[,2],
popup = ~paste0("<b>", name, "</b><br>", amenity),
icon = custom_icons
) %>%
addLegend(
"bottomright",
colors = c("red", "blue", "green", "yellow", "pink", "purple"),
labels = c("Restaurant", "Pub", "Cafe", "Bar", "fast_food", "ice_cream"),
title = "Amenity Type",
opacity = 1
) %>%
setView(lng = -83.7430, lat = 42.2808, zoom = 12)