library(tidycensus)
library(tmap)
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
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(raster)
## Loading required package: sp
census_api_key("0d539976d5203a96fa55bbf4421110d4b3db3648", overwrite = TRUE)
## To install your API key for use in future sessions, run this function with `install = TRUE`.
variables <- load_variables(2020, "acs5", cache = TRUE)


#Q1 

var <- c(total_pop = "B03002_001",
         hsipanic = "B03002_012") 
st <-"TX"
ct <-"Harris County"

harriss <- get_acs(geography = "tract", variables = var, count=ct,
                   state = st,output="wide", year = 2021, 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%  |                                                                              |=====                                                                 |   7%  |                                                                              |=====                                                                 |   8%  |                                                                              |======                                                                |   8%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |   9%  |                                                                              |=======                                                               |  10%  |                                                                              |========                                                              |  11%  |                                                                              |========                                                              |  12%  |                                                                              |=========                                                             |  12%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  14%  |                                                                              |===========                                                           |  15%  |                                                                              |===========                                                           |  16%  |                                                                              |============                                                          |  17%  |                                                                              |=============                                                         |  19%  |                                                                              |==============                                                        |  20%  |                                                                              |===============                                                       |  21%  |                                                                              |===============                                                       |  22%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  25%  |                                                                              |==================                                                    |  26%  |                                                                              |====================                                                  |  28%  |                                                                              |=====================                                                 |  30%  |                                                                              |======================                                                |  32%  |                                                                              |=======================                                               |  33%  |                                                                              |=========================                                             |  35%  |                                                                              |==========================                                            |  37%  |                                                                              |===========================                                           |  39%  |                                                                              |============================                                          |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  45%  |                                                                              |=================================                                     |  47%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  50%  |                                                                              |=====================================                                 |  53%  |                                                                              |======================================                                |  55%  |                                                                              |=======================================                               |  56%  |                                                                              |========================================                              |  57%  |                                                                              |==========================================                            |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |===========================================                           |  61%  |                                                                              |============================================                          |  63%  |                                                                              |==============================================                        |  66%  |                                                                              |===============================================                       |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |==================================================                    |  71%  |                                                                              |===================================================                   |  73%  |                                                                              |=====================================================                 |  75%  |                                                                              |======================================================                |  77%  |                                                                              |=======================================================               |  79%  |                                                                              |=========================================================             |  81%  |                                                                              |==========================================================            |  83%  |                                                                              |============================================================          |  85%  |                                                                              |=============================================================         |  87%  |                                                                              |==============================================================        |  89%  |                                                                              |================================================================      |  91%  |                                                                              |=================================================================     |  93%  |                                                                              |==================================================================    |  95%  |                                                                              |====================================================================  |  97%  |                                                                              |===================================================================== |  99%  |                                                                              |======================================================================| 100%
harriss$hispanic_pct <- harriss$hsipanicE/harriss$total_popE



tm_shape(harriss) +
  tm_polygons(col = "hispanic_pct", palette = "YlOrRd", title = "Hispanic %") +
  tm_compass(type = "8star", position = c("right", "top"), size = 1) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_layout(legend.position = c("center", "bottom"),
            legend.outside = TRUE,
            legend.outside.position = "right")

#Q2

Harris_tracts<- tracts(state = "TX", county = "Harris County", cb=T)
## Retrieving data for the year 2022
##   |                                                                              |                                                                      |   0%  |                                                                              |=                                                                     |   1%  |                                                                              |==                                                                    |   3%  |                                                                              |===                                                                   |   5%  |                                                                              |=====                                                                 |   7%  |                                                                              |======                                                                |   9%  |                                                                              |=======                                                               |  11%  |                                                                              |=========                                                             |  13%  |                                                                              |==========                                                            |  15%  |                                                                              |============                                                          |  16%  |                                                                              |=============                                                         |  18%  |                                                                              |==============                                                        |  20%  |                                                                              |================                                                      |  23%  |                                                                              |=================                                                     |  25%  |                                                                              |===================                                                   |  27%  |                                                                              |====================                                                  |  29%  |                                                                              |=====================                                                 |  31%  |                                                                              |=======================                                               |  33%  |                                                                              |========================                                              |  34%  |                                                                              |=========================                                             |  36%  |                                                                              |===========================                                           |  38%  |                                                                              |============================                                          |  41%  |                                                                              |==============================                                        |  43%  |                                                                              |===============================                                       |  44%  |                                                                              |=================================                                     |  46%  |                                                                              |==================================                                    |  48%  |                                                                              |===================================                                   |  50%  |                                                                              |=====================================                                 |  52%  |                                                                              |======================================                                |  54%  |                                                                              |========================================                              |  57%  |                                                                              |=========================================                             |  59%  |                                                                              |==========================================                            |  60%  |                                                                              |============================================                          |  62%  |                                                                              |=============================================                         |  64%  |                                                                              |==============================================                        |  66%  |                                                                              |================================================                      |  68%  |                                                                              |=================================================                     |  70%  |                                                                              |===================================================                   |  72%  |                                                                              |====================================================                  |  74%  |                                                                              |=====================================================                 |  76%  |                                                                              |=======================================================               |  78%  |                                                                              |========================================================              |  80%  |                                                                              |==========================================================            |  82%  |                                                                              |===========================================================           |  84%  |                                                                              |============================================================          |  86%  |                                                                              |==============================================================        |  88%  |                                                                              |===============================================================       |  90%  |                                                                              |================================================================      |  92%  |                                                                              |==================================================================    |  94%  |                                                                              |===================================================================   |  96%  |                                                                              |===================================================================== |  98%  |                                                                              |======================================================================| 100%
Community_Centers<-st_read(dsn = "C:/Users/عمر/Desktop/Fall 2024/Rstudio/project 2/Harris_County_Community_Centers/Harris_County_Community_Centers.shp")
## Reading layer `Harris_County_Community_Centers' from data source 
##   `C:\Users\عمر\Desktop\Fall 2024\Rstudio\project 2\Harris_County_Community_Centers\Harris_County_Community_Centers.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 94 features and 17 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: 2960593 ymin: 13752700 xmax: 3235328 ymax: 13969850
## Projected CRS: NAD83 / Texas South Central (ftUS)
tm_shape(Harris_tracts) +
  tm_borders() +
  tm_shape(Community_Centers) +
  tm_dots(col = "blue", size = 0.1, alpha = 0.5) +
  tm_layout(frame = FALSE)

#Q3

water_q<-st_read(dsn = "C:/Users/عمر/Desktop/Fall 2024/Rstudio/project 2/tl_rd22_48201_linearwater/tl_rd22_48201_linearwater.shp")
## Reading layer `tl_rd22_48201_linearwater' from data source 
##   `C:\Users\عمر\Desktop\Fall 2024\Rstudio\project 2\tl_rd22_48201_linearwater\tl_rd22_48201_linearwater.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 4166 features and 5 fields
## Geometry type: LINESTRING
## Dimension:     XY
## Bounding box:  xmin: -95.96073 ymin: 29.49734 xmax: -94.90849 ymax: 30.17061
## Geodetic CRS:  NAD83
tm_shape(Harris_tracts) +
  tm_borders() +  
  tm_shape(water_q) +
  tm_lines(col = "blue", lwd = 2) +  
  tm_layout(frame = FALSE)

#Q4

zipcode <-st_read(dsn = "C:/Users/عمر/Desktop/Fall 2024/Rstudio/project 2/Harris_County_Zipcodes/Harris_County_Zipcodes.shp")
## Reading layer `Harris_County_Zipcodes' from data source 
##   `C:\Users\عمر\Desktop\Fall 2024\Rstudio\project 2\Harris_County_Zipcodes\Harris_County_Zipcodes.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 155 features and 10 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 2928863 ymin: 13748310 xmax: 3265257 ymax: 13992550
## Projected CRS: NAD83 / Texas South Central (ftUS)
tmap_options(check.and.fix = TRUE)

tm_shape(Harris_tracts) +
  tm_borders() +
  tm_shape(zipcode) +
  tm_polygons(col = "green") +
  tm_layout(frame = FALSE)

#Q5

water_q <- st_make_valid(water_q)
zipcode <- st_make_valid(zipcode)

water_q <- st_transform(water_q, crs = 4326)
zipcode <- st_transform(zipcode, crs = 4326)

overlay_result  <- st_intersection(water_q, zipcode)
## Warning: attribute variables are assumed to be spatially constant throughout
## all geometries
#Q6
DEM <- raster('C:/Users/عمر/Desktop/Fall 2024/Rstudio/project 2/USGS_13_n30w096_20240229.tif')
DEM
## class      : RasterLayer 
## dimensions : 10812, 10812, 116899344  (nrow, ncol, ncell)
## resolution : 9.259259e-05, 9.259259e-05  (x, y)
## extent     : -96.00056, -94.99944, 28.99944, 30.00056  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=NAD83 +no_defs 
## source     : USGS_13_n30w096_20240229.tif 
## names      : Layer_1
#Q7
DEM <- setMinMax(DEM)
cellStats(DEM, min)
## [1] -12.24206
cellStats(DEM, max)
## [1] 80.40253
#Q8

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

#Q9


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

#Q10

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

#Q11

#In this project, I analyzed different data layers to understand the characteristics of Harris County. First, I created a map showing the percentage of the Hispanic population. Next, I mapped community centers such as libraries and other facilities. I also visualized linear water and  ZIP code boundaries. I also did an overlay analysis for linerwater and ZIP code regions. I used a Digital Elevation Model (DEM) to study the county's topography. I identified areas with elevations below 350 meters and slopes less than 3 degrees, I also created a map showing the direction of the terrain, focusing on areas facing between 22.5° and 157.5°. 

##Q11 In this project, I analyzed different data layers to understand the characteristics of Harris County. First, I created a map showing the percentage of the Hispanic population. Next, I mapped community centers such as libraries and other facilities. I also visualized linear water and ZIP code boundaries. I also did an overlay analysis for linerwater and ZIP code regions. I used a Digital Elevation Model (DEM) to study the county’s topography. I identified areas with elevations below 350 meters and slopes less than 3 degrees, I also created a map showing the direction of the terrain, focusing on areas facing between 22.5° and 157.5°.