Read in shapefiles using sf starting with county boundary data

This chunk of code reads in the shapefile containing county boundaries data and transforms the data to the geographic coordinate system crs = 4326. The crs code transforms the county spatial data to latitude-longitudinal format. The following line sets the column names of the counties dataframe and cleans them up using the make_clean_names function. Head shows us the top rows of the counties dataframe.

counties <- read_sf('shapefiles/county_boundaries.shp') %>% 
  st_transform(crs = 4326) 
colnames(counties) <- make_clean_names(colnames(counties))
head(counties)
## Simple feature collection with 3 features and 32 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -75.69678 ymin: 39.80192 xmax: -74.95576 ymax: 40.44699
## Geodetic CRS:  WGS 84
## # A tibble: 3 x 33
##   objectid geoid namelsad       g_id  geography total_popu poverty_ra total_hous
##      <dbl> <chr> <chr>          <chr> <chr>          <dbl>      <dbl>      <dbl>
## 1       15 42101 Philadelphia ~ G421~ Philadel~    1555072       26.4     581050
## 2       42 42091 Montgomery Co~ G420~ Montgome~     812970        6.6     308626
## 3       45 42045 Delaware Coun~ G420~ Delaware~     561683       10.6     203817
## # i 25 more variables: snap_rate <dbl>, median_hou <dbl>, percent_wh <dbl>,
## #   percent_af <dbl>, percent_as <dbl>, percent_mu <dbl>, percent_hi <dbl>,
## #   median_age <dbl>, percent_en <dbl>, median_ren <dbl>, unemployme <dbl>,
## #   percent_di <dbl>, percent_un <dbl>, total_ho_1 <dbl>, occupied_h <dbl>,
## #   vacant_hou <dbl>, percent_va <dbl>, owner_occu <dbl>, renter_occ <dbl>,
## #   percent_re <dbl>, occ_hu_no_ve <dbl>, pct_no_vehi <dbl>, shape_st_ar <dbl>,
## #   shape_st_le <dbl>, geometry <POLYGON [°]>

Read in shapefiles using sf for Philadelphia City boundary data

This chunk of code reads in the shapefile containing Philadelphia City boundaries data and transforms the data to the geographic coordinate system crs = 4326. The crs code transforms the county spatial data to latitude-longitudinal format. The following line sets the column names of the counties dataframe and cleans them up using the make_clean_names function. Head shows us the top rows of the phila dataframe.

phila <- read_sf('shapefiles/city_boundary.shp') %>% 
  st_transform(crs = 4326)
colnames(phila) <- make_clean_names(colnames(phila))
head(phila)
## Simple feature collection with 1 feature and 6 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -75.2803 ymin: 39.86726 xmax: -74.95575 ymax: 40.13791
## Geodetic CRS:  WGS 84
## # A tibble: 1 x 7
##          area perimeter boundary boundary_i  shape_area shape_len
##         <dbl>     <dbl>    <dbl>      <dbl>       <dbl>     <dbl>
## 1 3973640269.   390420.        2          0 3970787926.   394714.
## # i 1 more variable: geometry <POLYGON [°]>

Read in shapefiles using sf for Philadelphia City boundary data

This chunk of code reads in the shapefile containing neighborhood boundaries data and transforms the data to the geographic coordinate system crs = 4326. The crs code transforms the county spatial data to latitude-longitudinal format. The following line sets the column names of the counties dataframe and cleans them up using the make_clean_names function. Head shows us the top rows of the neighborhoods dataframe.

neighborhoods <- read_sf('shapefiles/philly_neighborhoods.shp') %>% 
  st_transform(crs = 4326)
colnames(neighborhoods) <- make_clean_names(colnames(neighborhoods))
head(neighborhoods)
## Simple feature collection with 6 features and 1 field
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: -75.26443 ymin: 39.87824 xmax: -74.95576 ymax: 40.13799
## Geodetic CRS:  WGS 84
## # A tibble: 6 x 2
##   postorm_rg                                                            geometry
##   <chr>                                                            <POLYGON [°]>
## 1 Center City & Kensington/Port Richmond Areas         ((-75.14851 40.00899, -7~
## 2 North/NorthEast Philadelphia - Tacony/Frankford Are~ ((-75.10943 40.04587, -7~
## 3 NorthEast Philadelphia                               ((-74.99852 40.12819, -7~
## 4 NorthEast Philadelphia - Fox Chase & Pennypack Areas ((-75.01827 40.09707, -7~
## 5 NorthWest Philadelphia - Mt. Airy & Germantown Areas ((-75.21221 40.08603, -7~
## 6 South Philadelphia                                   ((-75.13626 39.94264, -7~

Read in raingage coordinate data and clean up data

Start by loading in the rg_coords.csv file and clean up the data in the following lines.

rg_coords <- read_csv(file.path("inputs", "rg_coords.csv"), show_col_types = F)
colnames(rg_coords) <- make_clean_names(colnames(rg_coords))

Mutate raingage coordinate data

Read in the rg_coords dataframe and assign the new resulting dataframe rg_coords1. Using the mutate function, remove *_* from the gauge column and assign the changed column to a new column named labels. Using the as.numeric function, remove “RG_” from the gauge column and keep the same column name - gauge. Using the if_else function, find instances in the gauge column with NA and replace these missing values with PHL in the labels column. Using the if_else function, replace values of 99 in the gauge column where the the labels column reads PHL.

rg_coords1 <- rg_coords %>% 
  mutate(labels = gsub("_","",gauge),
         gauge = as.numeric(gsub("RG_","",gauge)),
         labels = if_else(is.na(gauge), "PHL", labels),
         gauge  = if_else(labels == "PHL", 99, gauge))
## Warning: There was 1 warning in `mutate()`.
## i In argument: `gauge = as.numeric(gsub("RG_", "", gauge))`.
## Caused by warning:
## ! NAs introduced by coercion

Transform rg_coords1 dataframe to a spatial object sf

Following the edits made to rg_coords1, this line takes in the df as an input and converts it to a sf - simple features object. The line adds in two columns containing longitudinal and latitudinal coordinates. Like before, crs = 4326 sets the coordinate reference system. remove = FALSE ensures that the rest of the columns from rg_coords1 dataframe are brought over in the resulting spatial object.

raingages <- st_as_sf(rg_coords1, coords = c("long", "lat"), crs = 4326, remove = FALSE)

icon.cloud

Using the leaflet package, create a custom icon for a cloud marker on a leaflet map with the makeIcon function. iconUrl is responsible for setting the path to the SVG image file that will be used as a marker. Specify your prefered width and height for the icon.

icon.cloud <- makeIcon(iconUrl = 'inputs/default_cloud.svg',
         iconWidth = 22, iconHeight = 14)

Remove old df

Now that we have saved our rg_coord1 df to the raingages spatial object, we remove the rg_coord1 df using rm

rm(rg_coords1)

Save our work

Saves our current workspace including all objects and variables to the following file name.

save.image('PWD_spatial_data_RGs_sf.RData')