Setup

Load fishnetutils

library(fishnetutils)

Downloading GIS data for Brookline

We can use make_buffered_road to create a road-buffer for Norfolk County.

norfolk <- 
  make_buffered_road(state_name = 'MA', county_name = 'Norfolk', crs_in = 102286, 
                     buffer_width = 2640)

nf_roads <- tigris::roads('ma', 'norfolk')

nf_roads %>% sf::st_as_sfc() %>% sf::st_write(dsn = './norfolkroads2.gpkg', driver = 'GPKG')


plot(norfolk)

Creating the fishnet

Default cell size is 5280 feet (1 mile). Unit is determined by crs_to.

fishnet_unclipped <- create_fishnet_clip(norfolk, 
                                         crs_to = sf::st_crs(norfolk),
                                         cell_size = 5280/2)
fishnet_latlong <- fishnet_unclipped %>% sf::st_transform(x = ., 4326)
View(fishnet_unclipped %>%  sf::st_transform(x = ., 4326))
plot(fishnet_latlong)

Exporting files

Use the convenience functions for exporting:

TODO

sf::write_sf(fishnet_latlong,
             dsn = 'Norfolk_halfmile.geojson',
             driver = 'GeoJSON',
             update = TRUE)

Test: water buffering

water_only <-
  tigris::area_water('CA',
                     county = 'Mariposa',
                     class = 'sf',
                     refresh = TRUE) %>%
  sf::st_transform(crs = 3310) %>%
  sf::st_simplify(dTolerance = 300)

plot(water_only)

pts_output <-
  create_fishnet_clip(make_buffered_road('CA', 'Mariposa', 3310), crs_to = 3310)
points_no_water <- st_difference(pts_output, water_only)
# 
# # Scratch
# pacman::p_load('tigris', 'sf')
# 
# fips("NY",
#      county = c('Kings',
#                 'Queens',
#                 'New York',
#                 'Bronx County',
#                 'Richmond'))
# 
# 
# # OK now get all the shapefiles for each county
# 
# 
# bronx <- county_subdivisions(
#   class = "sf",
#   county = '005',
#   cb = TRUE,
#   state = '36'
# )
# 
# 
# 
# # sf::st_transform(bronx,crs=3628)
# # LI_FIPS <- 3628
# 
# # Get geographies
# ############################################################################
# 
# bronx <- county_subdivisions(
#   class = "sf",
#   county = '005',
#   cb = TRUE,
#   state = '36'
# )
# 
# # Make a clipping grid for each
# ############################################################################
# 
# #' make_clipped_grid
# #'
# #' @param shapes sf object to make a grid out of
# #' @param to_crs integer of EPSG CRS
# #' @param grid_size float representing grid size; takes unit of CRS
# #'
# #' @return sf object of grid items
# #' @export
# #'
# #' @examples
# make_clipped_grid <- function(shapes, to_crs, grid_size) {
#   # Perform CRS transformations first
#   shapes_transformed <- st_transform(shapes, crs = 3628)
# 
#   # Create grid points
#   grid_points <-
#     st_transform(shapes, crs = 3628) %>%
#     st_make_grid(x = .,
#                  # this is the unit
#                  cellsize = grid_size,
#                  what = "centers")
#   # Clip the grid points to the transformed input shape
#   pts_output <- st_intersection(grid_points, shapes_transformed)
#   return(pts_output)
# }
# 
# make_clipped_grid()
# 
# #' make_buffered_road
# #'
# #' @param state_name String, name abbrev of the state
# #' @param county_name String, county name
# #' @param crs_in Integer, CRS EPSG number of the local CRS for transforms
# #' @param buffer_size Integer, the size of the buffer
# #'
# #' @return `sf` with a buffer around the roads
# #' @export
# #'
# #' @examples
# make_buffered_road <-
#   function(state_name,
#            county_name,
#            crs_in,
#            buffer_size = 1000) {
#     ## Takes in specifications for roads in a county,
#     ## returned the buffered areas
#     shape <- tigris::roads(state = state_name,
#                            county = county_name,
#                            class = 'sf') %>%
#       sf::st_transform(., crs = crs_in) %>%
#       sf::st_buffer(x = ., dist = buffer_size) %>%
#       sf::st_combine(x = .) %>%
#       sf::st_union(x = . , by_feature = TRUE)
#     return(shape)
#   }
# 
# # make_buffered_road()
# 
# out <- make_grid(bronx, 3628, 5000 * 3)
# plot(out)
# 
# 
# 
# # Bronx 36005
# # Kings 36047
# # Queens 36081
# # Richmond 36085
# # NYC 36061
# county_subdivisions(
#   class = "sf",
#   county = '005',
#   cb = TRUE,
#   state = '36'
# ) %>%
#   make_grid(., 3628, 5000 * 3) %>%
#   st_transform(., crs = 4326) %>%
#   st_write(obj = .,
#            dsn = 'Bronx_36005.geojson',
#            update = TRUE)
# 

Batch processing

Load in the study area file

jobs <-
  readr::read_csv(
    file = file.path('./../data/nyc_study_area_counties.csv'),
    col_types = list(
      readr::col_character(),
      readr::col_character(),
      readr::col_character(),
      readr::col_character(),
      readr::col_number(),
      readr::col_character()
    )
  )

jobs
save_file(to_save = fishnet_latlong, geo_type = 'fishnet', format = 'GPKG',
          county_name = 'Norfolk', county_abbrev = 'Norfolk')
library('fishnetutils')
# apply(jobs, FUN=save_all_outputs)
apply(jobs, 1, FUN = save_all_outputs)

Create clipped fishnet

create_fishnet_clip(inputty = norfolk, crs_to = )


rl2999/fishnetutils documentation built on Feb. 25, 2020, 3:54 a.m.