library(sf)
library(readr)
library(dplyr)
library(ggplot2)
library(mapview)
unzip("Section_07/R-Spatial_I_Lab.zip", exdir = "Section_07/Week_07")
unzip("./Section_07/Week_07/ZIP_CODE_040114.zip", exdir = "./Section_07/Week_07/Shapes")
# Replace with your actual file path
nyc_sf_ZIP <- st_read("./Section_07/Week_07/Shapes/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\Alijah Anyagwosi\Downloads\SPRING 2025\RStudio\Section_07\Week_07\Shapes\ZIP_CODE_040114.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 263 features and 12 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 913129 ymin: 120020.9 xmax: 1067494 ymax: 272710.9
## Projected CRS: NAD83 / New York Long Island (ftUS)
nyc_sf_ZIP <- st_transform(nyc_sf_ZIP, crs = 4326)
health_data <- read.csv("./Section_07/Week_07/NYS_Health_Facility.csv")
health_data_new <- health_data %>%
  filter(!is.na(Facility.Longitude) & !is.na(Facility.Latitude))
health_sf <- st_as_sf(health_data_new, 
                          coords = c("Facility.Longitude", "Facility.Latitude"), 
                          crs = 4326)
rfs_xy_data <- read.csv("Section_07/Week_07/nys_retail_food_store_xy.csv", fileEncoding="latin1")
rfs_xy_new <- rfs_xy_data %>%
  filter(!is.na(Y) & !is.na(X))

rfs_xy_sf <- st_as_sf(rfs_xy_new, 
                          coords = c("Y", "X"), 
                          crs = 4326)
# Filter health facilities to only those within NYC ZIP code polygons
health_sf <- st_filter(health_sf, nyc_sf_ZIP, .predicate = st_within)
colnames(health_sf)
##  [1] "Facility.ID"                  "Facility.Name"               
##  [3] "Short.Description"            "Description"                 
##  [5] "Facility.Open.Date"           "Facility.Address.1"          
##  [7] "Facility.Address.2"           "Facility.City"               
##  [9] "Facility.State"               "Facility.Zip.Code"           
## [11] "Facility.Phone.Number"        "Facility.Fax.Number"         
## [13] "Facility.Website"             "Facility.County.Code"        
## [15] "Facility.County"              "Regional.Office.ID"          
## [17] "Regional.Office"              "Main.Site.Name"              
## [19] "Main.Site.Facility.ID"        "Operating.Certificate.Number"
## [21] "Operator.Name"                "Operator.Address.1"          
## [23] "Operator.Address.2"           "Operator.City"               
## [25] "Operator.State"               "Operator.Zip.Code"           
## [27] "Cooperator.Name"              "Cooperator.Address"          
## [29] "Cooperator.Address.2"         "Cooperator.City"             
## [31] "Cooperator.State"             "Cooperator.Zip.Code"         
## [33] "Ownership.Type"               "Facility.Location"           
## [35] "geometry"
colnames(rfs_xy_data)
##  [1] "ï..County"          "License.Number"     "Operation.Type"    
##  [4] "Establishment.Type" "Entity.Name"        "DBA.Name"          
##  [7] "Street.Number"      "Street.Name"        "Address.Line.2"    
## [10] "Address.Line.3"     "City"               "State"             
## [13] "Zip.Code"           "Square.Footage"     "Location"          
## [16] "Coords"             "Y"                  "X"
rfs_xy_new <- rfs_xy_data %>%
  filter(!is.na(X) & !is.na(Y))

rfs_xy_sf <- st_as_sf(rfs_xy_new, 
                          coords = c("X", "Y"), 
                          crs = 4326)
# Filter food stores to only those within NYC ZIP code polygons
rfs_xy_sf <- st_filter(rfs_xy_sf, nyc_sf_ZIP, .predicate = st_within)
#Merging both of the points on the single
mapview(nyc_sf_ZIP, alpha.regions = 0.3, legend = FALSE) + 
  mapview(health_sf, col.region = "blue", layer.name = "Health Facilities") + 
  mapview(rfs_xy_sf, col.region = "green", layer.name = "Food Stores")
save(nyc_sf_ZIP, health_sf, rfs_xy_sf, file = "Section_07/Week_07/nyc_spatial_data.RData")
st_write(nyc_sf_ZIP, "Section_07/Week_07/nyc_data.gpkg", layer = "zip_codes", delete_layer = TRUE)
## Deleting layer `zip_codes' using driver `GPKG'
## Writing layer `zip_codes' to data source 
##   `Section_07/Week_07/nyc_data.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(health_sf, "Section_07/Week_07/nyc_data.gpkg", layer = "health_facs", delete_layer = TRUE)
## Deleting layer `health_facs' using driver `GPKG'
## Writing layer `health_facs' to data source 
##   `Section_07/Week_07/nyc_data.gpkg' using driver `GPKG'
## Writing 1293 features with 34 fields and geometry type Point.
st_write(rfs_xy_sf, "Section_07/Week_07/nyc_data.gpkg", layer = "food_stores", delete_layer = TRUE)
## Deleting layer `food_stores' using driver `GPKG'
## Writing layer `food_stores' to data source 
##   `Section_07/Week_07/nyc_data.gpkg' using driver `GPKG'
## Writing 11306 features with 16 fields and geometry type Point.