R Spaital Lab Assignment # 1

task 1: Read the NYC postal areas in Shapefiles into sf objects.

# read in zip code shapefile
nyc_zip <- st_read("R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\jdhoc\Desktop\Hunter\SPRING 2024\GEOG 39300 R\R-spatial\Session_7\R-Spatial_I_Lab\ZIP_CODE_040114\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)
# turn it into sf object
nyc_zip_sf <- st_geometry(nyc_zip)

task 2: Read and process the NYS health facilities spreadsheet data. Create sf objects from geographic coordinates.

# read in nys health facility data and clean column  names
nys_hf <- read_csv("R-Spatial_I_Lab/NYS_Health_Facility.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE) %>% 
  clean_names()

# remove any rows with NAs or 0 for lat or long
nys_hf <- nys_hf[!is.na(nys_hf$facility_longitude) &
                 !is.na(nys_hf$facility_latitude) &
                 nys_hf$facility_longitude != 0 &
                 nys_hf$facility_latitude != 0,]

# one facility (id 5764) has the long and lat switched, so switch them to be correct
correct_lat <- nys_hf[nys_hf$facility_id == 5764,]$facility_longitude
correct_long <- nys_hf[nys_hf$facility_id == 5764,]$facility_latitude
nys_hf[nys_hf$facility_id == 5764,]$facility_longitude <- correct_long
nys_hf[nys_hf$facility_id == 5764,]$facility_latitude <- correct_lat

# convert to an sf object and set the crs
nys_hf <- st_as_sf(nys_hf, 
                  coords = c("facility_longitude", "facility_latitude"))
st_crs(nys_hf) <- 4326

task 3: Read and process the NYS retail food stores data. Create sf objects from geographic coordinates for NYC.

# read in nys retail food stores data and clean column  names
nys_rfs <- read_csv("R-Spatial_I_Lab/NYS_Retail_Food_Stores.csv", 
                               show_col_types = FALSE, 
                               lazy = FALSE) %>% 
  clean_names()

# remove any rows with NA for location column
nys_rfs <- nys_rfs[!is.na(nys_rfs$location),]

# split the location string to get just the point in a new column
split_list <- strsplit(nys_rfs$location, split = '\n')
nys_rfs$point <- sapply(split_list, `[`, 3)

# remove any rows with NA for point column
nys_rfs <- nys_rfs[!is.na(nys_rfs$point),]

#remove parentheses, split the point into seperate long and lat
nys_rfs$point <- nys_rfs$point %>% str_sub(2,-2)
split_list_coords <- strsplit(nys_rfs$point, split = ',')
nys_rfs$longitude <- sapply(split_list_coords, `[`, 2)
nys_rfs$latitude <- sapply(split_list_coords, `[`, 1)

# filter for stores in nyc
nyc_rfs <- nys_rfs %>%
  dplyr::filter(county == "New York" | county == "Kings" | 
                county == "Queens" | county == "Bronx" | county == "Richmond")

# convert to an sf object and set the crs
nyc_rfs <- st_as_sf(nyc_rfs, 
                    coords = c("longitude", "latitude"))
st_crs(nyc_rfs) <- 4326

task 4: Use simple mapping method, either based on ggmap+ggplot or mapview, with a basemap to verify the above datasets in terms of their geometry locations.

# use mapview to map and verify datasets

mapview::mapview(nys_hf, zcol = 'ownership_type', layer.name = "NYS Health Facility Ownership Type")
mapview::mapview(nyc_rfs, zcol = 'operation_type', layer.name = "NYS Food Retail Operation Type")
# Use ggplot to map all the health facilities in NYS, colored by ownership type
# ggplot(data = nys_hf) +
#   geom_sf(aes(color = ownership_type)) +
#   coord_sf(default_crs = sf::st_crs(4326)) +
#   labs(title = "NYS Health Facilities by Ownership Type",
#        color = "Ownership Type")
# 
# ggplot(data = nys_rfs) +
#   geom_sf(aes(color = state)) +
#   coord_sf(default_crs = sf::st_crs(4326)) +
#   labs(title = "NYS Retail Food Stores", 
#        color = "xyz")

task 5: Save the three sf objects in a RData file or in a single GeoPackage file/database.

save(nyc_zip_sf, nys_hf, nyc_rfs,
     file = './data/assignment1_data.RData')