library(leaflet)
library(rgdal)
Creating a map of the Seaport with subway and commuter rail lines and bus routes.
# Setting variables for map.
myLAT <- 42.346308
myLNG <- -71.042144
myZOOM <- 14
myTILES <- "CartoDB.Positron"
# Import subway lines shape file
subway <- readOGR(dsn="transitlayers/mbtarapidtransit", layer="MBTA_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/nadiahalder/Desktop/Seaport Project/transitlayers/mbtarapidtransit", layer: "MBTA_ARC"
## with 138 features
## It has 4 fields
subway <- spTransform(subway, CRS("+proj=longlat +ellps=GRS80"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum Unknown based on GRS80 ellipsoid in CRS definition
# Color subway lines
levels(factor(subway$LINE))
## [1] "BLUE" "GREEN" "ORANGE" "RED" "SILVER"
# Map colors to the different subway lines
pal <- colorFactor(palette = c('blue','green','orange','red','grey'),
domain = c('BLUE','GREEN','ORANGE','RED','SILVER'))
# Define new MBTA icon
mbtaIcon <- makeIcon(
iconUrl = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/64/MBTA.svg/240px-MBTA.svg.png",
iconWidth = 15, iconHeight = 15
)
# Load the MBTA stops shapefile
stops <- readOGR(dsn="transitlayers/mbtarapidtransit", layer="MBTA_NODE")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/nadiahalder/Desktop/Seaport Project/transitlayers/mbtarapidtransit", layer: "MBTA_NODE"
## with 166 features
## It has 4 fields
stops <- spTransform(stops, CRS("+proj=longlat +ellps=GRS80"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum Unknown based on GRS80 ellipsoid in CRS definition
# Import rail lines
trains <- readOGR(dsn="transitlayers/trains", layer="TRAINS_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/nadiahalder/Desktop/Seaport Project/transitlayers/trains", layer: "TRAINS_ARC"
## with 6595 features
## It has 28 fields
## Integer64 fields read as strings: TRACK LINE_CODE ASSET_ID
trains <- spTransform(trains, CRS("+proj=longlat +ellps=GRS80"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum Unknown based on GRS80 ellipsoid in CRS definition
# Subset to only include commuter rail
trains <- subset(trains, COMMRAIL == "Y", na.rm=TRUE)
# Import rail stations
trainstops <- readOGR(dsn="transitlayers/trains", layer="TRAINS_NODE")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/nadiahalder/Desktop/Seaport Project/transitlayers/trains", layer: "TRAINS_NODE"
## with 387 features
## It has 6 fields
trainstops <- spTransform(trainstops, CRS("+proj=longlat +ellps=GRS80"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum Unknown based on GRS80 ellipsoid in CRS definition
# Subset to only include commuter rail stations
trainstops <- subset(trainstops, trainstops@data$C_RAILSTAT == "Y", na.rm=TRUE)
# Make a purple circle icon for the commuter rail stations
crIcon <- makeIcon(
iconUrl = "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcT-a648p1qz2nfaR26UBVe3A6bVtD1aZ5Yl5A&usqp=CAU", iconWidth = 15, iconHeight = 15)
# Import bus routes shapefile
bus <- readOGR(dsn="transitlayers/mbtabus", layer="MBTABUSROUTES_ARC")
## OGR data source with driver: ESRI Shapefile
## Source: "/Users/nadiahalder/Desktop/Seaport Project/transitlayers/mbtabus", layer: "MBTABUSROUTES_ARC"
## with 923 features
## It has 11 fields
## Integer64 fields read as strings: CTPS_ROU_2
bus <- spTransform(bus, CRS("+proj=longlat +ellps=GRS80"))
## Warning in showSRID(uprojargs, format = "PROJ", multiline = "NO"): Discarded
## datum Unknown based on GRS80 ellipsoid in CRS definition
# Subset the bus routes into "key routes" and "non-key-routes"
keybuslist <- as.character(c(1,15,22,23,28,32,39,57,66,71,73,77,111,116,117))
bus.key <- subset(bus, MBTA_ROUTE %in% keybuslist, na.rm=TRUE)
bus.nokey <- subset(bus, !(MBTA_ROUTE %in% keybuslist), na.rm=TRUE)
# Create the map
mymap <- leaflet() %>%
addProviderTiles(myTILES) %>%
setView(myLNG, myLAT, zoom = myZOOM) %>%
addPolylines(data = bus.nokey, color="black", weight=1) %>%
addPolylines(data = bus.key, color="yellow", weight=2) %>%
addPolylines(data = trains, color="purple", weight=2) %>%
addPolylines(data = subway, color=~pal(LINE)) %>%
addMarkers(data = stops, icon=mbtaIcon) %>%
addMarkers(data = trainstops, icon=crIcon)
#Open map
mymap