knitr::opts_chunk$set(echo = TRUE)

#Project 2
library(sf)
## Linking to GEOS 3.11.2, GDAL 3.6.2, PROJ 9.2.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)
library(raster)
## Loading required package: sp
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:raster':
## 
##     intersect, select, union
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
#Q1

var=c('B03002_012E','B03002_003E','B03002_004E','B03002_006E','B01001_001E') 
Victoria_segregation <- get_acs(geography = "tract", variables = var, county = "Victoria",
                                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%
  |                                                                            
  |====                                                                  |   5%
  |                                                                            
  |====                                                                  |   6%
  |                                                                            
  |=====                                                                 |   6%
  |                                                                            
  |=====                                                                 |   7%
  |                                                                            
  |=====                                                                 |   8%
  |                                                                            
  |======                                                                |   8%
  |                                                                            
  |======                                                                |   9%
  |                                                                            
  |=======                                                               |   9%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |========                                                              |  11%
  |                                                                            
  |========                                                              |  12%
  |                                                                            
  |=========                                                             |  12%
  |                                                                            
  |=========                                                             |  13%
  |                                                                            
  |==========                                                            |  14%
  |                                                                            
  |===========                                                           |  15%
  |                                                                            
  |===========                                                           |  16%
  |                                                                            
  |============                                                          |  17%
  |                                                                            
  |=============                                                         |  18%
  |                                                                            
  |=============                                                         |  19%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |===============                                                       |  21%
  |                                                                            
  |===============                                                       |  22%
  |                                                                            
  |================                                                      |  22%
  |                                                                            
  |================                                                      |  23%
  |                                                                            
  |=================                                                     |  24%
  |                                                                            
  |=================                                                     |  25%
  |                                                                            
  |==================                                                    |  25%
  |                                                                            
  |==================                                                    |  26%
  |                                                                            
  |===================                                                   |  27%
  |                                                                            
  |====================                                                  |  28%
  |                                                                            
  |====================                                                  |  29%
  |                                                                            
  |=====================                                                 |  29%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |======================                                                |  31%
  |                                                                            
  |======================                                                |  32%
  |                                                                            
  |=======================                                               |  33%
  |                                                                            
  |========================                                              |  34%
  |                                                                            
  |========================                                              |  35%
  |                                                                            
  |=========================                                             |  35%
  |                                                                            
  |=========================                                             |  36%
  |                                                                            
  |==========================                                            |  37%
  |                                                                            
  |===========================                                           |  38%
  |                                                                            
  |===========================                                           |  39%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |=============================                                         |  41%
  |                                                                            
  |=============================                                         |  42%
  |                                                                            
  |==============================                                        |  43%
  |                                                                            
  |==============================                                        |  44%
  |                                                                            
  |===============================                                       |  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%
  |                                                                            
  |================================================                      |  69%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |=================================================                     |  71%
  |                                                                            
  |==================================================                    |  71%
  |                                                                            
  |==================================================                    |  72%
  |                                                                            
  |===================================================                   |  72%
  |                                                                            
  |===================================================                   |  73%
  |                                                                            
  |====================================================                  |  74%
  |                                                                            
  |====================================================                  |  75%
  |                                                                            
  |=====================================================                 |  76%
  |                                                                            
  |======================================================                |  77%
  |                                                                            
  |======================================================                |  78%
  |                                                                            
  |=======================================================               |  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(Victoria_segregation)[3] <- 'Hispanic'

tm_shape(Victoria_segregation) +
  tm_fill(col = "Hispanic",legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_layout(legend.outside = TRUE,frame = FALSE)

#Q2 Border Census Tracts

tm_shape(Victoria_segregation) +
  tm_fill(col = "Hispanic",legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_layout(legend.outside = TRUE,frame = FALSE)

#Q3 Compass and Scale Bar

tm_shape(Victoria_segregation) +
  tm_fill(col = "Hispanic",legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = FALSE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))

#Q4 Color Intervals


tm_shape(Victoria_segregation) +
  tm_fill(col = "Hispanic",palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = FALSE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  

#5 Interactive Map

tm_shape(Victoria_segregation) +
  tm_fill(col = "Hispanic",palette = "Reds", legend.hist = TRUE)+ 
  tm_layout(title = "Hispanic Pop")+ 
  tm_borders(alpha=.4) +
  tm_compass(type = "8star", 
             position = c("RIGHT", "BOTTOM"),
             show.labels = 2,
             text.size = 0.6) +
  tm_layout(legend.outside = TRUE,frame = FALSE)+
  tm_scale_bar(position = c("RIGHT", "BOTTOM"))+
  tmap_mode("view")
## tmap mode set to interactive viewing
## Compass not supported in view mode.
#6 Find county shapefile creating skeleton of county , base layer

library(tigris)
Victoria_ct <-tracts(state = "TX", county = "Victoria",cb=T)
## Retrieving data for the year 2021
# 7 Find any point shapefile data within the county and make a map with census tract as the background

Vic_Points<-st_read(dsn="C:/Users/Mario/Documents/Wei CLASS Folder/VIC_points/poi_point.shp")
## Reading layer `poi_point' from data source 
##   `C:\Users\Mario\Documents\Wei CLASS Folder\VIC_points\poi_point.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 150 features and 22 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -97.19454 ymin: 28.56333 xmax: -96.79105 ymax: 29.0672
## Geodetic CRS:  WGS 84
tm_shape(Victoria_ct) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(Vic_Points) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_basemap() +
  tm_layout(title = "Victoria County Points of Interest", frame = FALSE)
# 8 Find any line shapefile data within the county and make a map with census tract as the background

  # Sent separatly 

# 9  Find any polygon shapefile data within the county and make a map with census tract as the background (2').
    # I could not find a polygon shapefile for Victoria County. 

# 10 Over-Lay function 533:29 22:10 27:30
Vic_ct2<-st_transform(Victoria_ct,crs = 4326)
Vic_points2<- st_transform(Vic_Points,crs = 4326)

landmark_Vic_ct <- st_intersection(Vic_points2, Vic_ct2)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#11 Get Digital Elevation Model (DEM) data of a selected area of the county through this video:
    #C:/Users/Mario/Documents/Wei CLASS Folder/Victoria Dem File

#12 Read the DEM data and get the min and max of elevations (2').

DEM_Vic <- raster('C:/Users/Mario/Documents/Wei CLASS Folder/Victoria Dem File.tif')
DEM_Vic <- setMinMax(DEM_Vic)
cellStats(DEM_Vic, max)
## [1] 172.27
cellStats(DEM_Vic, min)
## [1] -0.2327512
#13 Make a map that shows areas with elevation lower than 350m (1').

plot(DEM_Vic < 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_Vic, 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_Vic, opt = "aspect", unit = "degrees") > 22.5 &
       terrain(DEM_Vic, opt = "aspect", unit = "degrees") < 157.5, 
     main = "Aspect criteria")