rm(list = ls())
ChicagoLink="https://github.com/Spatial-Data-Analytics-2025/Homework2/raw/refs/heads/main/chicago_spatial_data.gpkg"
library(sf)
## Linking to GEOS 3.13.0, GDAL 3.10.1, PROJ 9.5.1; sf_use_s2() is TRUE
st_layers(ChicagoLink)
gpklink='chicago_spatial_data.gpkg'
library(sf)
fuel_stations = read_sf(gpklink, layer = "fuel_stations")
schools = read_sf(gpklink, layer = "schools")
good_buffers = read_sf(gpklink, layer = "good_buffers")
good_routes = read_sf(gpklink, layer = "good_routes")
good_centers <- read_sf(gpklink, layer = "good_centers")
relocate_points <- read_sf(gpklink, layer = "relocate_points")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.4.3
Routes_Map = ggplot() +
theme_bw() +
geom_sf(data = good_routes, color = 'Blue') +
ggtitle("Good Routes for Gas Stations")
Routes_Map

saveRDS(Routes_Map, file='Routes_Map.rds')
library(ggplot2)
library(sf)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
good_centers <- st_transform(good_centers, 4326)
map_relocate_final <- ggplot() +
geom_sf(data = good_routes, color = "blue", size = 0.3, show.legend = FALSE) +
geom_sf(data = good_centers, aes(shape = "Potential Fuel Stations"), color = "red", size = 3) +
geom_sf(data = fuel_stations, aes(shape = "Fuel Station"), color = "black", size = 1) +
geom_sf(data = schools, aes(shape = "School"), color = "green", size = 0.8) +
geom_sf(data = relocate_points, aes(shape = "Stations to Relocate"), color = "magenta", size = 3) +
# Legend setup
scale_shape_manual(
name = "Legend",
values = c(
"Fuel Station" = 16,
"School" = 16,
"Potential Fuel Stations" = 5,
"Stations to Relocate" = 4
)
) +
theme_bw() +
ggtitle("Map of Stations to Relocate") +
theme(legend.position = "right")
map_relocate_final

saveRDS(map_relocate_final, file = "map_of_stations_to_relocate_final.rds")
library(ggplot2)
library(sf)
library(ggspatial) # for annotation_map_tile
## Warning: package 'ggspatial' was built under R version 4.4.3
library(dplyr)
gpklink <- "chicago_spatial_data.gpkg"
relocate_points <- read_sf(gpklink, layer = "relocate_points") |> st_transform(4326)
schools <- read_sf(gpklink, layer = "schools") |> st_transform(4326)
buffer_75m <- st_buffer(relocate_points, 75)
nearby_schools <- st_intersection(schools, buffer_75m)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
Relocation_Map <- ggplot() +
annotation_map_tile(type = "osm", zoom = 13) + # OpenStreetMap tiles
geom_sf(data = buffer_75m, fill = "red", alpha = 0.2, color = NA) +
geom_sf(data = nearby_schools, color = "yellow", size = 2, shape = 17) +
geom_sf(data = relocate_points, color = "red", size = 1, shape = 5) +
ggtitle("Relocation Map") +
theme_minimal()
# Display
Relocation_Map
## Zoom: 13

# Save for flexdashboard
saveRDS(Relocation_Map, "Relocation_Map.rds")