#1. Redo the analysis for mapping Hispanic population and add histogram in the map (2').
library(tidycensus)
## Warning: package 'tidycensus' was built under R version 4.2.3
library(sf)
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(tmap)
## Warning: package 'tmap' was built under R version 4.2.3
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tigris)
## To enable caching of data, set `options(tigris_use_cache = TRUE)`
## in your R script or .Rprofile.
## 
## Attaching package: 'tigris'
## The following object is masked from 'package:tidycensus':
## 
##     fips_codes
census_api_key("edd28860119f5a254a2915190d528839b25a475e",install="TRUE", overwrite = TRUE)
## Your original .Renviron will be backed up and stored in your R HOME directory if needed.
## Your API key has been stored in your .Renviron and can be accessed by Sys.getenv("CENSUS_API_KEY"). 
## To use now, restart R or run `readRenviron("~/.Renviron")`
## [1] "edd28860119f5a254a2915190d528839b25a475e"
var <- c('B19013_001E','B03002_001E','B03002_004E','B01001_002E','B01001_026E','B01003_001E','B01002_001E')
Travis_County <- get_acs(geography = "tract", 
                         variables = var, 
                         county = "Travis",
                         state = "TX",
                         output="wide", 
                         geometry = TRUE)
## Getting data from the 2017-2021 5-year ACS
## Downloading feature geometry from the Census website.  To cache shapefiles for use in future sessions, set `options(tigris_use_cache = TRUE)`.
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=                                                                     |   1%
  |                                                                            
  |=                                                                     |   2%
  |                                                                            
  |==                                                                    |   2%
  |                                                                            
  |==                                                                    |   3%
  |                                                                            
  |==                                                                    |   4%
  |                                                                            
  |===                                                                   |   4%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |==========                                                            |  15%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |=====================                                                 |  31%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |===============================                                       |  44%
  |                                                                            
  |===============================                                       |  45%
  |                                                                            
  |================================                                      |  46%
  |                                                                            
  |=================================                                     |  47%
  |                                                                            
  |=================================                                     |  48%
  |                                                                            
  |==================================                                    |  48%
  |                                                                            
  |==================================                                    |  49%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |====================================                                  |  51%
  |                                                                            
  |====================================                                  |  52%
  |                                                                            
  |=====================================                                 |  53%
  |                                                                            
  |======================================                                |  54%
  |                                                                            
  |======================================                                |  55%
  |                                                                            
  |=======================================                               |  56%
  |                                                                            
  |========================================                              |  57%
  |                                                                            
  |========================================                              |  58%
  |                                                                            
  |=========================================                             |  59%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |===========================================                           |  61%
  |                                                                            
  |===========================================                           |  62%
  |                                                                            
  |============================================                          |  62%
  |                                                                            
  |============================================                          |  63%
  |                                                                            
  |=============================================                         |  64%
  |                                                                            
  |=============================================                         |  65%
  |                                                                            
  |==============================================                        |  65%
  |                                                                            
  |==============================================                        |  66%
  |                                                                            
  |===============================================                       |  67%
  |                                                                            
  |===============================================                       |  68%
  |                                                                            
  |================================================                      |  68%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  79%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |========================================================              |  81%
  |                                                                            
  |=========================================================             |  82%
  |                                                                            
  |==========================================================            |  83%
  |                                                                            
  |==========================================================            |  84%
  |                                                                            
  |===========================================================           |  84%
  |                                                                            
  |===========================================================           |  85%
  |                                                                            
  |============================================================          |  86%
  |                                                                            
  |=============================================================         |  86%
  |                                                                            
  |=============================================================         |  87%
  |                                                                            
  |==============================================================        |  88%
  |                                                                            
  |==============================================================        |  89%
  |                                                                            
  |===============================================================       |  89%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |================================================================      |  91%
  |                                                                            
  |================================================================      |  92%
  |                                                                            
  |=================================================================     |  92%
  |                                                                            
  |=================================================================     |  93%
  |                                                                            
  |==================================================================    |  94%
  |                                                                            
  |==================================================================    |  95%
  |                                                                            
  |===================================================================   |  95%
  |                                                                            
  |===================================================================   |  96%
  |                                                                            
  |====================================================================  |  97%
  |                                                                            
  |===================================================================== |  98%
  |                                                                            
  |===================================================================== |  99%
  |                                                                            
  |======================================================================|  99%
  |                                                                            
  |======================================================================| 100%
names(Travis_County)[3] <-"HHIncome"
names(Travis_County)[5] <-"Hispanic"
names(Travis_County)[7] <-"Black"
names(Travis_County)[9] <-"Male"
names(Travis_County)[11] <-"Female"
names(Travis_County)[13] <-"Totalpop"
names(Travis_County)[15] <- "MDage"

Travis_County$B19013_001M <- NULL
Travis_County$B03002_001M <- NULL
Travis_County$B03002_004M <- NULL
Travis_County$B01001_002M <- NULL
Travis_County$B01001_026M <- NULL
Travis_County$B01003_001M <- NULL
Travis_County$B01002_001M <- NULL

#Q2-4 Histogram + Boarders + Compass + Color interval + tmap
tmap_mode("plot")
## tmap mode set to plotting
tm_shape(Travis_County) +
  tm_fill(col = "Hispanic",palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.7) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = TRUE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

#5
tmap_mode("view")
## tmap mode set to interactive viewing
tm_shape(Travis_County) +
  tm_fill(col = "Hispanic",palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.7) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = TRUE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))
## Compass not supported in view mode.
#6. Retrieve the census tract shapefile data of the county (1').
Travis_tracts<- tracts(state = "TX", county = "Travis",cb=T)
## Retrieving data for the year 2021
#st_write(Travis_tracts, "C:/Users/salom/OneDrive/School_Stuff/UTSA/Project 2/Travis.shp")

#7. Find any point shapefile data within the county and make a map with census tract as the background (2').
Travis_parks <- st_read(dsn = "C:/Users/salom/Downloads/Park_Facilities/Park_Facilities.shp")
## Reading layer `Park_Facilities' from data source 
##   `C:\Users\salom\Downloads\Park_Facilities\Park_Facilities.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 707 features and 8 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 2991024 ymin: 10034790 xmax: 3194189 ymax: 10144570
## Projected CRS: NAD83 / Texas Central (ftUS)
tmap_mode("plot")
## tmap mode set to plotting
tm_shape(Travis_tracts) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(Travis_parks) +
  tm_dots(size = 0.05, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_basemap() +
  tm_layout(main.title = "Travis Park Facilities", main.title.position = "center", frame = TRUE)

Travis_parks <-st_transform(Travis_parks,crs = 4326)

#8. Find any line shapefile data within the county and make a map with census tract as the background (2').
Bike_paths <- st_read(dsn = "C:/Users/salom/OneDrive/School_Stuff/UTSA/Project 2/Bike_paths.shp")
## Reading layer `Bike_paths' from data source 
##   `C:\Users\salom\OneDrive\School_Stuff\UTSA\Project 2\Bike_paths.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 17753 features and 13 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: -98.17051 ymin: 29.99595 xmax: -97.3698 ymax: 30.65473
## Geodetic CRS:  WGS 84
buffer_distance <- 0.01 # adjust as needed
lines_polygon <- st_buffer(Bike_paths, dist = buffer_distance)

tmap_mode("plot")
## tmap mode set to plotting
tm_shape(lines_polygon) +
  tm_borders() +
  tm_layout(frame = FALSE)

tm_shape(Travis_tracts) +
  tm_borders() +
  tm_shape(lines_polygon) +
  tm_borders(lwd = 2, col = "green") +  # Customize line appearance
  tm_layout(main.title = "Travis County Bike Facilities", main.title.position = "center", frame = TRUE)

#9. Find any polygon shapefile data within the county and make a map with census tract as the background (2').

Travis_prec <- st_read(dsn = "C:/Users/salom/Downloads/Commissioner_Precincts/Commissioner_Precincts.shp")
## Reading layer `Commissioner_Precincts' from data source 
##   `C:\Users\salom\Downloads\Commissioner_Precincts\Commissioner_Precincts.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 4 features and 10 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 2977919 ymin: 9982435 xmax: 3230644 ymax: 10200210
## Projected CRS: NAD83 / Texas Central (ftUS)
tmap_mode("plot")
## tmap mode set to plotting
tm_shape(Travis_prec) + 
  tm_borders() + 
  tm_fill(col = "PRECINCT", palette = "Blues") + 
  tm_layout(main.title = "Commissioner Precincts", main.title.position = "center")+
  tm_shape(Travis_tracts) +
  tm_borders(col = "black", alpha = 1)

#10. Have overlay analysis between the point shapefile and the county's census tract shapefile so as to find the census tract ID of each point (2').
Travis_parks <-st_transform(Travis_parks,crs = 4326)
Travis_tracts <-st_transform(Travis_tracts,crs = 4326)

Park_travis_ct <- st_intersection(Travis_parks,Travis_tracts)
## Warning: attribute variables are assumed to be spatially constant throughout all
## geometries
plot(Park_travis_ct)
## Warning: plotting the first 9 out of 21 attributes; use max.plot = 21 to plot
## all

#12. Read the DEM data and get the min and max of elevations (2').
library(raster)
## Loading required package: sp
DEM_ATX <- raster('C:/Users/salom/Downloads/USGS_13_n31w098_20211103.tif')
DEM_ATX <- setMinMax(DEM_ATX)
cellStats(DEM_ATX, max)
## [1] 398.0261
cellStats(DEM_ATX, min)
## [1] 79.33839
#13. Make a map that shows areas with elevation lower than 350m (1').

plot(DEM_ATX < 350, main = "Elevation criteria",
     col = c("#ffffff", "#0000ff"))

#14. Make a map that shows the slop degree of topography map is less than 3 (1').

plot(terrain(DEM_ATX, opt = "slope", unit = "degrees") < 3, 
     main = "Slope criteria",
     col = c("#ffffff", "#ff9900"))

#15. Make a map that shows the aspect criteria of topography map is less than between 22.5 and 157.5 (2').

plot(terrain(DEM_ATX, opt = "aspect", unit = "degrees") > 22.5 &
       terrain(DEM_ATX, opt = "aspect", unit = "degrees") < 157.5, 
     main = "Aspect criteria")

#16. Submit your code and result to Rpubs using R markdown (1').