library(sf)
## Linking to GEOS 3.11.0, GDAL 3.5.3, PROJ 9.1.0; sf_use_s2() is TRUE
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
library(tmap)
## The legacy packages maptools, rgdal, and rgeos, underpinning the sp package,
## which was just loaded, will retire in October 2023.
## Please refer to R-spatial evolution reports for details, especially
## https://r-spatial.org/r/2023/05/15/evolution4.html.
## It may be desirable to make the sf package available;
## package maintainers should consider adding sf to Suggests:.
## The sp package is now running under evolution status 2
##      (status 2 uses the sf package in place of rgdal)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tidycensus)


# 1. Download the "2017 BOND PROGRAM LINE " shapefile data from https://www.sanantonio.gov/GIS/GISData Links to an external site, and visualize the lines (in green) with bexar county census tracts as the background.  (2') 

bond_program <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Week 12/Assignment 5/BondProjectLines2017/BondProjectLines2017.shp")
## Reading layer `BondProjectLines2017' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Week 12/Assignment 5/BondProjectLines2017/BondProjectLines2017.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 69 features and 24 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: 2078269 ymin: 13665110 xmax: 2168346 ymax: 13783530
## Projected CRS: NAD83 / Texas South Central (ftUS)
bond_program <- st_transform(bond_program, crs=4326)

bexar_tracts<- tracts(state = "TX", county = "Bexar",cb=T)
## Retrieving data for the year 2021
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |===                                                                   |   5%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |=====================================================                 |  75%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================| 100%
buffer_distance <- 0.5 # adjust as needed
lines_polygon <- st_buffer(bond_program, dist = buffer_distance)

tm_shape(bexar_tracts) +
  tm_borders() +
  tm_shape(lines_polygon) +
  tm_borders(lwd = 2, col = "green") +  # Customize line appearance
  tm_layout(frame = FALSE)

# 2. Download the "LIBRARIES " shapefile data from https://www.sanantonio.gov/GIS/GISData Links to an external site. to an external site. and visualize the library point (in red) with bexar county census tracts as the background. Make the map interactive using tmap_mode("view") (2').
SA_Library <-st_read(dsn = "/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Week 12/Assignment 5/Libraries/Libraries.shp")
## Reading layer `Libraries' from data source 
##   `/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Week 12/Assignment 5/Libraries/Libraries.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 30 features and 9 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 2075828 ymin: 13669240 xmax: 2170361 ymax: 13779280
## Projected CRS: NAD83 / Texas South Central (ftUS)
SA_Library <- st_transform(SA_Library, crs=4326)

bexar_tracts<- tracts(state = "TX", county = "Bexar",cb=T)
## Retrieving data for the year 2021
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(bexar_tracts) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(SA_Library) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_layout(title = "San Antonio Libraries", frame = FALSE)
# 3. Read the raster file (LC09_028040_20230826.tif) which is the land surface temperature of Dallas. (1')
library(raster)
## Loading required package: sp
LST <- raster("/Users/gabbyrodriguez/Documents/UTSA MSURP/Urban Planning Methods/Week 12/Assignment 5/LC09_028040_20230826.tif")

LST <- setMinMax(LST)

LST
## class      : RasterLayer 
## dimensions : 7779, 7542, 58669218  (nrow, ncol, ncell)
## resolution : 30, 30  (x, y)
## extent     : 311265, 537525, 3076545, 3309915  (xmin, xmax, ymin, ymax)
## crs        : +proj=utm +zone=14 +datum=WGS84 +units=m +no_defs 
## source     : LC09_028040_20230826.tif 
## names      : LST 
## values     : 272.8423, 334.4377  (min, max)
# 4. Convert the Kelvin into Celsius (C = K - 273.15) and then plot the raster data. (1')
LST <- LST - 273.15
LST
## class      : RasterLayer 
## dimensions : 7779, 7542, 58669218  (nrow, ncol, ncell)
## resolution : 30, 30  (x, y)
## extent     : 311265, 537525, 3076545, 3309915  (xmin, xmax, ymin, ymax)
## crs        : +proj=utm +zone=14 +datum=WGS84 +units=m +no_defs 
## source     : memory
## names      : LST 
## values     : -0.3076831, 61.28771  (min, max)
plot(LST, 
     main="LST of Dallas")

# 5. Only visualize the areas with temperatures higher than 40 Degree Celsius (1').
image(LST, zlim=c(40,100))