This assignment focuses on creating, loading and displaying spatial objects in r.
Check out the Source Code from the top-right corner
</>Code
menu.
In the following R code chunk, load_packages
is the code
chunk name. include=FALSE
suggests that the code chunk will
run, but the code itself and its outputs will not be included in the
rendered HTML. echo=TRUE
in the following code chunk
suggests that the code and results from running the code will be
included in the rendered HTML.
Tasks for the first lab are:
1. Set up a R project for the R-Spatial section. Read the NYC postal areas in Shapefiles into sf objects.
2. Read and process the NYS health facilities spreadsheet data. Create sf objects from geographic coordinates.
3.Read and process the NYS retail food stores data. Create sf objects from geographic coordinates for NYC.
4.Use simple mapping method such as mapview with a basemap to verify the above datasets in terms of their geometry locations.
5.Save the three sf objects in a RData file or in a single GeoPackage file/database.
Loading in the library. Setting up the R project for the R-spatial section. Working directory was set prior to loading files into R.
library(sf)
library(mapview)
library(readxl)
library(readr)
library(dplyr)
Reading the NYC postal areas in Shapefiles to sf objects. With the ‘shp’ file extension, sf knows it is a Shapefile.
zipcode_sf <- st_read("C:/Users/campb/Downloads/R-Spatial_I_Lab/ZIP_CODE_040114/ZIP_CODE_040114.shp")
## Reading layer `ZIP_CODE_040114' from data source
## `C:\Users\campb\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)
reading the NYS Health Facility csv file into the code.Creating sf objects from geographic coordinates.Missing values in coordinates are not allowed. Cleaning the data meant removing rows with missing values in the coordinates.
#reading the NYS Health Facility csv file into the code.
NYS_healthfacility_df <- read_csv("NYS_Health_Facility.csv")
## Rows: 3990 Columns: 36
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (28): Facility Name, Short Description, Description, Facility Open Date,...
## dbl (8): Facility ID, Facility Phone Number, Facility Fax Number, Facility ...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Missing values in coordinates not allowed. Cleaning the data.
# Remove rows with missing values in the coordinates.
NYS_healthfacility_df_clean <- na.omit(NYS_healthfacility_df[, c("Facility Longitude", "Facility Latitude")])
# Convert to Simple Features (SF).
NYS_healthfacility_sf <- st_as_sf(NYS_healthfacility_df_clean,
coords = c("Facility Longitude", "Facility Latitude"))
Reading the NYS Food Retail Stores file into the code. Creating sf object from geographic coordinates.
NYS_Retail_Food_Stores_df <- read_csv("C:/Users/campb/Downloads/R-Spatial_I_Lab/nys_retail_food_store_xy.csv",
locale = locale(encoding = "latin1"))
## Rows: 29389 Columns: 18
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (11): ï..County, Operation.Type, Establishment.Type, Entity.Name, DBA.Na...
## dbl (4): License.Number, Zip.Code, Y, X
## num (1): Square.Footage
## lgl (2): Address.Line.2, Address.Line.3
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#Creating sf objects from geographic coordinates.
#NYS_RetailFood_sf <- st_as_sf(NYS_Retail_Food_Stores_df, coords = c("X", "Y"))
#Missing values in coordinates not allowed. Cleaning the data.
# Remove rows with missing values in the coordinates.
NYS_Retail_Food_Stores_df_clean <- na.omit(NYS_Retail_Food_Stores_df[, c("X", "Y")])
#Convert to Simple Features (SF).
NYS_RetailFood_sf <- st_as_sf(NYS_Retail_Food_Stores_df_clean,
coords = c("X", "Y"))
Using simple mapping methods such as mapview with a basemap to verify the datasets.
#1, Zipcodes
mapview(zipcode_sf, zcol = "CTY_FIPS", layer.name = "NYC Boroughs by zipcode")
#2, Health Facilities
st_crs(NYS_healthfacility_sf) <- 4326
st_crs(NYS_healthfacility_sf)
## Coordinate Reference System:
## User input: EPSG:4326
## wkt:
## GEOGCRS["WGS 84",
## ENSEMBLE["World Geodetic System 1984 ensemble",
## MEMBER["World Geodetic System 1984 (Transit)"],
## MEMBER["World Geodetic System 1984 (G730)"],
## MEMBER["World Geodetic System 1984 (G873)"],
## MEMBER["World Geodetic System 1984 (G1150)"],
## MEMBER["World Geodetic System 1984 (G1674)"],
## MEMBER["World Geodetic System 1984 (G1762)"],
## MEMBER["World Geodetic System 1984 (G2139)"],
## MEMBER["World Geodetic System 1984 (G2296)"],
## ELLIPSOID["WGS 84",6378137,298.257223563,
## LENGTHUNIT["metre",1]],
## ENSEMBLEACCURACY[2.0]],
## PRIMEM["Greenwich",0,
## ANGLEUNIT["degree",0.0174532925199433]],
## CS[ellipsoidal,2],
## AXIS["geodetic latitude (Lat)",north,
## ORDER[1],
## ANGLEUNIT["degree",0.0174532925199433]],
## AXIS["geodetic longitude (Lon)",east,
## ORDER[2],
## ANGLEUNIT["degree",0.0174532925199433]],
## USAGE[
## SCOPE["Horizontal component of 3D system."],
## AREA["World."],
## BBOX[-90,-180,90,180]],
## ID["EPSG",4326]]
mapview(NYS_healthfacility_sf, layer.name = "Health Facilities")
## Warning in cbind(`Feature ID` = fid, mat): number of rows of result is not a
## multiple of vector length (arg 1)
Saving the data.
save(zipcode_sf, NYS_healthfacility_sf, NYS_RetailFood_sf, file = "assignment7_data.RData")
load("assignment7_data.RData")