R Spaital Lab Assignment # 1

task 1:

load in dataframes and convert to sf

# read in zip codes as sf
nyc_zip_sf <- st_read("ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source 
##   `C:\Users\andyx\Downloads\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)
# read in health facilities filter out NA coord rows
#also filtered out rows where coords were 0 or latitude and longitude values were switched
health_1 <- read_csv(file.path(getwd(),"NYS_Health_Facility.csv" 
                               ),show_col_types = FALSE)
health_2 <- health_1 %>%
  filter(!is.na(`Facility Longitude`) & !is.na(`Facility Latitude`)
         & `Facility Longitude` != 0 & `Facility Latitude` != 0
         & `Facility Latitude` >0)
health_sf <- st_as_sf(health_2,
                      coords = c("Facility Longitude", "Facility Latitude"),
                      crs=4326)

# read in food stores 
food <- read_csv(file.path(getwd(), "nys_retail_food_store_xy.csv"
                           ),show_col_types = FALSE)
food1 <- food %>% 
  filter(!is.na(X) & !is.na(Y)) %>%
  filter(County %in% c("Bronx", "Kings", "New York", "Queens", "Richmond"))
  
food_stores_sf <- st_as_sf(food1,
                           coords = c("X", "Y"),
                           crs = 4326)

task 2:

use mapview to view each sf with basemap

zip codes map

using mapview

mapview(nyc_zip_sf, zcol = "COUNTY", layer.name = "nyc zip codes")

health facilities map

using ggmap

#api key and basemap for ny state
register_stadiamaps(key="77550fe1-8725-4db8-b595-4408807c9c2a", write=FALSE)
health_sf %>% st_bbox() %>% as.vector() %>% get_stadiamap(zoom=10, messaging=FALSE)->basemap
## ℹ © Stadia Maps © Stamen Design © OpenMapTiles © OpenStreetMap contributors.
## ℹ 396 tiles needed, this may take a while (try a smaller zoom?)
#add lon lat columns back to sf dataframe
health_coords <- st_coordinates(health_sf)
lon <- health_coords[, "X"]
lat <- health_coords[, "Y"]
health_sf$Longitude <- lon
health_sf$Latitude <- lat
#plot points colored on facility type
ggmap(basemap) + 
  geom_point(aes(x=Longitude, y=Latitude, color=Description),
             data= health_sf,
             size=1)

food stores map

using mapview

food_stores_sf %>% mapview()

save sf objects into files

Save each sf object as geopackages into current working directory

dsn <- './ny_data.gpkg'
st_write(health_sf,
         dsn = dsn,
         layer = 'health_facilities',
         delete_layer=TRUE)
## Writing layer `health_facilities' to data source `./ny_data.gpkg' using driver `GPKG'
## Writing 3843 features with 36 fields and geometry type Point.
st_write(food_stores_sf,
         dsn = dsn,
         layer = 'food_stores',
         delete_layer=TRUE)
## Deleting layer `food_stores' failed
## Writing layer `food_stores' to data source `./ny_data.gpkg' using driver `GPKG'
## Writing 11300 features with 16 fields and geometry type Point.
st_write(nyc_zip_sf,
         dsn = dsn,
         layer = 'nyc_zip_codes',
         delete_layer=TRUE)
## Deleting layer `nyc_zip_codes' failed
## Writing layer `nyc_zip_codes' to data source `./ny_data.gpkg' using driver `GPKG'
## Writing 263 features with 12 fields and geometry type Polygon.