# Please download the storm channels data ("STORM CHANNELS") and Corridor Plans ("CORRIDOR PLANS") from: https://www.sanantonio.gov/GIS/GISDataLinks to an external site. 

# Transform the coordinate reference systems of both datasets to crs = 4326 

library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; sf_use_s2() is TRUE
library(ggplot2)

storm_channels <- st_read("C:/Users/cruzs/Downloads/StormChannels")
## Reading layer `StormChannels' from data source 
##   `C:\Users\cruzs\Downloads\StormChannels' using driver `ESRI Shapefile'
## Simple feature collection with 12042 features and 17 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: 2024999 ymin: 13600410 xmax: 2249487 ymax: 13824430
## Projected CRS: NAD83 / Texas South Central (ftUS)
corridor_plans <- st_read("C:/Users/cruzs/Downloads/CorridorPlans")
## Reading layer `CorridorPlans' from data source 
##   `C:\Users\cruzs\Downloads\CorridorPlans' using driver `ESRI Shapefile'
## Simple feature collection with 12 features and 4 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 2058688 ymin: 13665110 xmax: 2178301 ymax: 13793820
## Projected CRS: NAD83 / Texas South Central (ftUS)
storm_channels <- st_transform(storm_channels, crs = 4326)
corridor_plans <- st_transform(corridor_plans, crs = 4326)

# To find what parts of storm channels are within the corridor plan area, please intersect corridor plans with storm channels 

intersected_storm_channels <- st_intersection(storm_channels, corridor_plans)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
# Visualize the intersected storm channels with a background of Bexar County census tracts  

library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
bexar_tracts <- tracts(state = "TX", county = "Bexar", cb = TRUE)
## Retrieving data for the year 2022
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |=                                                                     |   2%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   4%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |=============                                                         |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |========================                                              |  35%  |                                                                              |==========================                                            |  38%  |                                                                              |===========================                                           |  39%  |                                                                              |=============================                                         |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |================================                                      |  45%  |                                                                              |================================                                      |  46%  |                                                                              |===================================                                   |  51%  |                                                                              |====================================                                  |  52%  |                                                                              |=====================================                                 |  52%  |                                                                              |=====================================                                 |  54%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |=========================================                             |  58%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  59%  |                                                                              |===========================================                           |  61%  |                                                                              |===========================================                           |  62%  |                                                                              |============================================                          |  63%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  65%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  67%  |                                                                              |===============================================                       |  68%  |                                                                              |=================================================                     |  69%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |==================================================                    |  72%  |                                                                              |=====================================================                 |  75%  |                                                                              |=====================================================                 |  76%  |                                                                              |======================================================                |  77%  |                                                                              |========================================================              |  80%  |                                                                              |=========================================================             |  81%  |                                                                              |=========================================================             |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |===========================================================           |  84%  |                                                                              |===============================================================       |  91%  |                                                                              |================================================================      |  92%  |                                                                              |=================================================================     |  92%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  95%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================|  99%  |                                                                              |======================================================================| 100%
bexar_tracts <- st_transform(st_as_sf(bexar_tracts), crs = 4326)

ggplot() +
  geom_sf(data = bexar_tracts, fill = "lightblue", color = "navyblue") +
  geom_sf(data = intersected_storm_channels, color = "orange") +
  labs(title = "Intersected Storm Channels within Corridor Plans in Bexar County",
       subtitle = "Background: Bexar County Census Tracts",
       caption = "Data source: San Antonio GIS") +
  theme_minimal()

# Read the raster file (LC09_028040_20230826.tif) Download LC09_028040_20230826.tif). Open this document with ReadSpeaker docReader, which is the land surface temperature of Dallas. 

library(raster)
## Loading required package: sp
library(ggplot2)
library(rasterVis)
## Warning: package 'rasterVis' was built under R version 4.4.2
## Loading required package: lattice
lst_raster <- raster("C:/Users/cruzs/Downloads/LC09_028040_20230826.tif")

# Convert the Kelvin into Celsius (C = K - 273.15) and then plot the raster data. 

lst_celsius <- lst_raster - 273.15


gplot(lst_celsius) +
  geom_tile(aes(fill = value)) +
  scale_fill_viridis_c(option = "C", name = "Temperature (°C)") +
  labs(
    title = "Land Surface Temperature in Dallas in Celsius (°C)",
    x = "Longitude",
    y = "Latitude",
  ) +
  theme_minimal()

# Only visualize the areas with temperatures higher than 40 Degree Celsius 

lst_above_40 <- calc(lst_celsius, fun = function(x) { ifelse(x > 40, x, NA) })

gplot(lst_above_40) +
  geom_tile(aes(fill = value)) +
  scale_fill_viridis_c(option = "C", name = "Temperature (°C)", na.value = "transparent") +
  labs(
    title = "Land Surface Temperature > 40°C in Dallas",
    x = "Longitude",
    y = "Latitude",
  ) +
  theme_minimal()