R Spatial Lab Assignment # 7

Task 1. Read NYS data: zip codes, health facility, and retail food store.

data_dir<- file.path(getwd(), "Data/R-Spatial_I_Lab")
zip_code<- st_read(file.path(data_dir, "ZIP_CODE_040114", "ZIP_CODE_040114.shp"))
health<- read_csv(file.path(data_dir, "NYS_Health_Facility.csv"), show_col_types=F)
food<- read_csv(file.path(data_dir, "nys_retail_food_store_xy.csv"), show_col_types=F) 

Task 2.Create sf objects from geographic coordinates

# filter out data without coordinates
# st_as_sf(na.fail = True) didn't work when I plot the data
health <- health[!is.na(health$`Facility Longitude`) & !is.na(health$`Facility Latitude`), ]
food <- food[!is.na(food$X) & !is.na(food$Y), ]

m_health<- st_as_sf(health, coords = c("Facility Longitude", "Facility Latitude"), crs = 4326)
m_food<- st_as_sf(food, coords = c("X", "Y"), crs = 4326)

Task3. Plot data.

plot(zip_code["ZIPCODE"])

# mapview(m_health, popup= "Facility ID", legend=FALSE)
# remove incorrect coordinates
remove_id<- c(5764, 10306, 10324, 10337, 10383)
m_health_filter<- m_health[!m_health$`Facility ID` %in% remove_id,]

# mapview don't work with knitr?
mapview(m_health_filter)
mapview(m_food, popup= "Facility ID")

Task 4.Save sf objects in a single GeoPackage file/database.

st_write(zip_code, 
         dsn = "./Data/R-Spatial_I_Lab/nys_data.gpkg", 
         layer="zip_code",
         delete_layer = TRUE)

st_write(m_health_filter,          
         dsn = "./Data/R-Spatial_I_Lab/nys_data.gpkg", 
         layer='NYS_Health_Facility',
         delete_layer = TRUE)

st_write(m_food,          
         dsn = "./Data/R-Spatial_I_Lab/nys_data.gpkg", 
         layer="NYS_Retail_Food_Stores",
         delete_layer = TRUE)