library(tmap)  # Load tmap for creating interactive and static maps
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
library(tidycensus)  # Load tidycensus for accessing U.S. Census Bureau data directly
library(tidycensus)
library(sf)
## Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.3.1; 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)
library(raster)
## Loading required package: sp
#Hispanic population= B03002_012E
#total population=B01003_001E


var=c('B03002_012E','B01003_001E') 

Travis_segregation <- get_acs(geography = "tract", variables = var, county = "Travis",
                              state = "TX",output="wide", geometry = TRUE)
## Getting data from the 2018-2022 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%  |                                                                              |===                                                                   |   5%  |                                                                              |====                                                                  |   5%  |                                                                              |====                                                                  |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |========                                                              |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |==========                                                            |  15%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  18%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |====================                                                  |  28%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  32%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |============================                                          |  40%  |                                                                              |===============================                                       |  44%  |                                                                              |===============================                                       |  45%  |                                                                              |===================================                                   |  50%  |                                                                              |===================================                                   |  51%  |                                                                              |=====================================                                 |  53%  |                                                                              |========================================                              |  57%  |                                                                              |===========================================                           |  61%  |                                                                              |==============================================                        |  65%  |                                                                              |===============================================                       |  67%  |                                                                              |============================================================          |  86%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  94%  |                                                                              |==================================================================    |  95%  |                                                                              |===================================================================   |  95%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
names(Travis_segregation)[3] <- 'Hispanic'
names(Travis_segregation)[5] <- 'TotalPop'


Travis_segregation$B01003_001M<- NULL
Travis_segregation$B03002_012M<- NULL


Travis_segregation$pct_Hispanic <- 100*Travis_segregation$Hispanic/Travis_segregation$TotalPop
tm_shape(Travis_segregation) +tm_fill(col = "pct_Hispanic")+ tm_layout(title = "Hispanic Percent")+
  #add compass
  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"))

texas_counties<-counties(state = "Texas",cb=T)
## Retrieving data for the year 2022
##   |                                                                              |                                                                      |   0%  |                                                                              |                                                                      |   1%  |                                                                              |=                                                                     |   1%  |                                                                              |==                                                                    |   2%  |                                                                              |==                                                                    |   3%  |                                                                              |====                                                                  |   5%  |                                                                              |=====                                                                 |   6%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |===========                                                           |  16%  |                                                                              |================                                                      |  23%  |                                                                              |==================                                                    |  25%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  31%  |                                                                              |=======================                                               |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  35%  |                                                                              |=========================                                             |  36%  |                                                                              |============================                                          |  39%  |                                                                              |============================                                          |  40%  |                                                                              |=============================                                         |  42%  |                                                                              |==============================                                        |  43%  |                                                                              |=================================                                     |  47%  |                                                                              |=================================                                     |  48%  |                                                                              |==================================                                    |  48%  |                                                                              |===========================================                           |  61%  |                                                                              |================================================                      |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |===================================================                   |  72%  |                                                                              |=========================================================             |  81%  |                                                                              |==========================================================            |  82%  |                                                                              |==========================================================            |  83%  |                                                                              |=============================================================         |  87%  |                                                                              |=============================================================         |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  91%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |====================================================================  |  97%  |                                                                              |====================================================================  |  98%  |                                                                              |===================================================================== |  98%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
Travis<-texas_counties[texas_counties$NAME=="Travis",]
Travis<-st_transform(Travis,crs = 4326) #define the coordinate reference system
#get texas landmarks
tx_landmarks <- landmarks(state = "TX",type = 'point')
## Retrieving data for the year 2022
## Downloading: 65 kB     Downloading: 65 kB     Downloading: 65 kB     Downloading: 65 kB     Downloading: 65 kB     Downloading: 65 kB     Downloading: 130 kB     Downloading: 130 kB     Downloading: 130 kB     Downloading: 130 kB     Downloading: 130 kB     Downloading: 130 kB     Downloading: 180 kB     Downloading: 180 kB     Downloading: 190 kB     Downloading: 190 kB     Downloading: 190 kB     Downloading: 190 kB     Downloading: 190 kB     Downloading: 190 kB     Downloading: 220 kB     Downloading: 220 kB     Downloading: 220 kB     Downloading: 220 kB     Downloading: 220 kB     Downloading: 220 kB     Downloading: 240 kB     Downloading: 240 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 250 kB     Downloading: 280 kB     Downloading: 280 kB     Downloading: 280 kB     Downloading: 280 kB     Downloading: 280 kB     Downloading: 280 kB     Downloading: 300 kB     Downloading: 300 kB     Downloading: 350 kB     Downloading: 350 kB     Downloading: 350 kB     Downloading: 350 kB     Downloading: 370 kB     Downloading: 370 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 380 kB     Downloading: 400 kB     Downloading: 400 kB     Downloading: 400 kB     Downloading: 400 kB     Downloading: 410 kB     Downloading: 410 kB     Downloading: 410 kB     Downloading: 410 kB     Downloading: 410 kB     Downloading: 410 kB     Downloading: 430 kB     Downloading: 430 kB     Downloading: 430 kB     Downloading: 430 kB     Downloading: 440 kB     Downloading: 440 kB     Downloading: 440 kB     Downloading: 440 kB     Downloading: 440 kB     Downloading: 440 kB     Downloading: 460 kB     Downloading: 460 kB     Downloading: 470 kB     Downloading: 470 kB     Downloading: 510 kB     Downloading: 510 kB     Downloading: 510 kB     Downloading: 510 kB     Downloading: 540 kB     Downloading: 540 kB     Downloading: 540 kB     Downloading: 540 kB     Downloading: 540 kB     Downloading: 540 kB     Downloading: 560 kB     Downloading: 560 kB     Downloading: 570 kB     Downloading: 570 kB     Downloading: 570 kB     Downloading: 570 kB     Downloading: 570 kB     Downloading: 570 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 600 kB     Downloading: 620 kB     Downloading: 620 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 640 kB     Downloading: 650 kB     Downloading: 650 kB     Downloading: 650 kB     Downloading: 650 kB     Downloading: 690 kB     Downloading: 690 kB     Downloading: 700 kB     Downloading: 700 kB     Downloading: 700 kB     Downloading: 700 kB     Downloading: 700 kB     Downloading: 700 kB     Downloading: 740 kB     Downloading: 740 kB     Downloading: 780 kB     Downloading: 780 kB     Downloading: 780 kB     Downloading: 780 kB     Downloading: 790 kB     Downloading: 790 kB     Downloading: 830 kB     Downloading: 830 kB     Downloading: 830 kB     Downloading: 830 kB     Downloading: 830 kB     Downloading: 830 kB     Downloading: 870 kB     Downloading: 870 kB     Downloading: 900 kB     Downloading: 900 kB     Downloading: 900 kB     Downloading: 900 kB     Downloading: 900 kB     Downloading: 900 kB     Downloading: 910 kB     Downloading: 910 kB     Downloading: 910 kB     Downloading: 910 kB     Downloading: 910 kB     Downloading: 910 kB     Downloading: 930 kB     Downloading: 930 kB     Downloading: 970 kB     Downloading: 970 kB     Downloading: 970 kB     Downloading: 970 kB     Downloading: 1 MB     Downloading: 1 MB     Downloading: 1.1 MB     Downloading: 1.1 MB     Downloading: 1.1 MB     Downloading: 1.1 MB     Downloading: 1.1 MB     Downloading: 1.1 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.2 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.3 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.4 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.5 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.6 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.7 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB     Downloading: 1.8 MB
tx_landmarks<-st_transform(tx_landmarks,crs = 4326) #define the coordinate reference system


# use clip analysis to have the tx_landmarks in Travis county
clipped_landmark_Travis <- st_intersection(tx_landmarks, Travis)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#plot the landmark for Travis
tm_shape(Travis) +
  tm_borders() +  # Add borders for polygons (customize as needed)
  tm_shape(Travis_segregation) +
  tm_dots(size = 0.02, col = "red", alpha = 0.7) +  # Customize point appearance
  tm_basemap() +
  tm_layout(title = "Travis Landmarks", frame = FALSE)

bike_paths<-st_read(dsn = "C:/Users/dagob/OneDrive/Desktop/Data/Project 2/Park_Trails_and_Roads/Park_Trails_and_Roads.shp")
## Reading layer `Park_Trails_and_Roads' from data source 
##   `C:\Users\dagob\OneDrive\Desktop\Data\Project 2\Park_Trails_and_Roads\Park_Trails_and_Roads.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 364 features and 14 fields
## Geometry type: MULTILINESTRING
## Dimension:     XY
## Bounding box:  xmin: 2989166 ymin: 10035310 xmax: 3194239 ymax: 10143610
## Projected CRS: NAD83 / Texas Central (ftUS)
tmap_options(check.and.fix = TRUE)
buffer_distance <- 0.01 # adjust as needed
lines_polygon <- st_buffer(bike_paths, dist = buffer_distance)

tm_shape(Travis) +
  tm_borders(alpha=.4,lwd = 2, col = "red") +
  tm_shape(lines_polygon) +
  tm_borders(lwd = 2, col = "red") +  # Customize line appearance
  tm_layout(frame = FALSE)

Park<-st_read(dsn = "C:/Users/dagob/OneDrive/Desktop/Data/Project 2/parks_public/Parks_Public.shp")
## Reading layer `Parks_Public' from data source 
##   `C:\Users\dagob\OneDrive\Desktop\Data\Project 2\Parks_Public\Parks_Public.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 31 features and 20 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2988791 ymin: 10023720 xmax: 3194489 ymax: 10162930
## Projected CRS: NAD83 / Texas Central (ftUS)
tm_shape(Travis) +
  tm_borders(alpha=.4,lwd = 2, col = "red") +
  tm_shape(Park) +
  tm_polygons(col="green")
## Warning: The shape Park is invalid (after reprojection). See sf::st_is_valid

  tm_layout(frame = FALSE)
## $tm_layout
## $tm_layout$frame
## [1] FALSE
## 
## $tm_layout$style
## [1] NA
## 
## 
## attr(,"class")
## [1] "tm"
  tm_shape(Travis) +
    tm_borders(alpha=.4,lwd = 2, col = "red") +
    tm_shape(lines_polygon) +
    tm_borders(lwd = 2, col = "red") +  # Customize line appearance
    tm_layout(frame = FALSE)+
    tm_shape(Park) +
    tm_polygons(col="green")
## Warning: The shape Park is invalid (after reprojection). See sf::st_is_valid

  tm_layout(frame = FALSE)
## $tm_layout
## $tm_layout$frame
## [1] FALSE
## 
## $tm_layout$style
## [1] NA
## 
## 
## attr(,"class")
## [1] "tm"
  wd <- ("C:/Users/dagob/OneDrive/Desktop/Data/Project 2/")
  setwd(wd)
  DEM <- raster(paste0(wd, "USGS_13_n31w098_20211103.tif"))
  DEM <- setMinMax(DEM)
  
  min<- minValue(DEM)
  max<- maxValue(DEM)
  
  plot(DEM < 350, main = "Elevation criteria",
       col = c("#ffffff", "#0000ff"))

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

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

  #This script analyzes and visualizes geospatial data for Travis County, Texas, using R packages like tidycensus, 
  #tmap, sf, and raster. It retrieves census data to calculate and map the percentage of Hispanic residents. Travis 
  #County boundaries and landmarks are processed and clipped for focused analysis, while bike paths and parks are 
  #visualized using shapefiles. Digital Elevation Model (DEM) data is used for terrain analysis, highlighting areas 
  #by elevation, slope, and aspect. The workflow integrates demographic, infrastructure, and terrain data, producing 
  #thematic maps and insights into the county's spatial characteristics.