R Spatial Lab Assignment #1

Task 1: Project Setup

This assignment uses R spatial tools to load, process, visualize, and save three spatial datasets for New York City: ZIP code postal areas, NYS health facilities, and NYS retail food stores.

Task 2: Read NYC Postal Areas Shapefile

nyc_zip_sf <- st_read("R-Spatial_I_Lab/nyc_zipcode/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `/Users/elinorgoldsmith-greenberg/Documents/GTECH 385 Assignments/R-Spatial/data/Section_07/R-Spatial_I_Lab/nyc_zipcode/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)
plot(st_geometry(nyc_zip_sf), main = "NYC Zip Codes")

Task 3: Read and Process NYS Health Facilities

health_df <- read_csv("R-Spatial_I_Lab/NYS_Health_Facility.csv",
                      show_col_types = FALSE)

health_sf <- health_df %>%
  filter(!is.na(`Facility Latitude`) & !is.na(`Facility Longitude`)) %>%
  filter(`Facility Longitude` != 0 & `Facility Latitude` != 0) %>%
  filter(`Facility Longitude` < -60 & `Facility Latitude` > 40) %>%
  st_as_sf(coords = c("Facility Longitude", "Facility Latitude"), crs = 4326)

print(health_sf)
## Simple feature collection with 3843 features and 34 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -79.6299 ymin: 40.51677 xmax: -72.17 ymax: 44.97849
## Geodetic CRS:  WGS 84
## # A tibble: 3,843 × 35
##    `Facility ID` `Facility Name`                 `Short Description` Description
##  *         <dbl> <chr>                           <chr>               <chr>      
##  1           204 Hospice at Lourdes              HSPC                Hospice    
##  2           620 Charles T Sitrin Health Care C… NH                  Residentia…
##  3          1156 East Side Nursing Home          NH                  Residentia…
##  4          2589 Wellsville Manor Care Center    NH                  Residentia…
##  5          3455 Harris Hill Nursing Facility, … NH                  Residentia…
##  6          3853 Garden City Surgi Center        DTC                 Diagnostic…
##  7          4249 Willcare                        CHHA                Certified …
##  8          4473 Good Shepherd Hospice           HSPC                Hospice    
##  9          6230 NYU Langone Rutherford          HOSP-EC             Hospital E…
## 10          6482 Endoscopy Center of Long Islan… DTC                 Diagnostic…
## # ℹ 3,833 more rows
## # ℹ 31 more variables: `Facility Open Date` <chr>, `Facility Address 1` <chr>,
## #   `Facility Address 2` <chr>, `Facility City` <chr>, `Facility State` <chr>,
## #   `Facility Zip Code` <chr>, `Facility Phone Number` <dbl>,
## #   `Facility Fax Number` <dbl>, `Facility Website` <chr>,
## #   `Facility County Code` <dbl>, `Facility County` <chr>,
## #   `Regional Office ID` <dbl>, `Regional Office` <chr>, …

Task 4: Read and Process NYS Retail Food Stores

retail_df <- read_csv("R-Spatial_I_Lab/nys_retail_food_store_xy.csv",
                      locale = locale(encoding = "latin1"),
                      show_col_types = FALSE)

retail_sf <- retail_df %>%
  filter(!is.na(X) & !is.na(Y)) %>%
  filter(`ï..County` %in% c("New York", "Kings", "Queens", "Bronx", "Richmond")) %>%
  st_as_sf(coords = c("X", "Y"), crs = 4326)

print(retail_sf)
## Simple feature collection with 11300 features and 16 fields
## Geometry type: POINT
## Dimension:     XY
## Bounding box:  xmin: -74.2484 ymin: 40.50782 xmax: -73.67061 ymax: 40.91008
## Geodetic CRS:  WGS 84
## # A tibble: 11,300 × 17
##    ï..County License.Number Operation.Type Establishment.Type Entity.Name       
##  * <chr>              <dbl> <chr>          <chr>              <chr>             
##  1 Bronx             734149 Store          JAC                7 ELEVEN FOOD STO…
##  2 Bronx             606221 Store          JAC                1001 SAN MIGUEL F…
##  3 Bronx             606228 Store          JAC                1029 FOOD PLAZA I…
##  4 Bronx             723375 Store          JAC                1078 DELI GROCERY…
##  5 Bronx             724807 Store          JAC                1086 LUNA DELI GR…
##  6 Bronx             712943 Store          JAC                109 AJ DELI GROCE…
##  7 Bronx             703060 Store          JAC                10 NEIGHBORHOOD C…
##  8 Bronx             609065 Store          JAC                1105 TINTON DELI …
##  9 Bronx             722972 Store          A                  1150 WEBSTER PHAR…
## 10 Bronx             609621 Store          JAC                1158 GROCERY & DE…
## # ℹ 11,290 more rows
## # ℹ 12 more variables: DBA.Name <chr>, Street.Number <chr>, Street.Name <chr>,
## #   Address.Line.2 <lgl>, Address.Line.3 <lgl>, City <chr>, State <chr>,
## #   Zip.Code <dbl>, Square.Footage <dbl>, Location <chr>, Coords <chr>,
## #   geometry <POINT [°]>

Task 5: Verify Datasets with mapview

mapview(nyc_zip_sf, layer.name = "NYC Zip Codes") +
  mapview(health_sf, layer.name = "Health Facilities", col.regions = "red") +
  mapview(retail_sf, layer.name = "Retail Food Stores", col.regions = "blue")

Task 6: Save to GeoPackage

st_write(nyc_zip_sf,
         dsn = "lab_07_data.gpkg",
         layer = "nyc_zip_codes",
         delete_layer = TRUE)
## Deleting layer `nyc_zip_codes' using driver `GPKG'
## Writing layer `nyc_zip_codes' to data source `lab_07_data.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.
st_write(health_sf,
         dsn = "lab_07_data.gpkg",
         layer = "health_facilities",
         delete_layer = TRUE)
## Deleting layer `health_facilities' using driver `GPKG'
## Writing layer `health_facilities' to data source 
##   `lab_07_data.gpkg' using driver `GPKG'
## Writing 3843 features with 34 fields and geometry type Point.
st_write(retail_sf,
         dsn = "lab_07_data.gpkg",
         layer = "retail_food_stores",
         delete_layer = TRUE)
## Deleting layer `retail_food_stores' using driver `GPKG'
## Writing layer `retail_food_stores' to data source 
##   `lab_07_data.gpkg' using driver `GPKG'
## Writing 11300 features with 16 fields and geometry type Point.
cat("All three layers saved to lab_07_data.gpkg")
## All three layers saved to lab_07_data.gpkg