Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grid Interpolation #22

Closed
bransonf opened this issue Jul 9, 2019 · 3 comments
Closed

Grid Interpolation #22

bransonf opened this issue Jul 9, 2019 · 3 comments

Comments

@bransonf
Copy link
Contributor

bransonf commented Jul 9, 2019

I think interpolating to grids is a frequent enough use case to warrant its own function. Below is a reprex of what I'm imagining.

# Helper Function to make grid from shape
# Requires that data is projected and that units are known

vars <- c("B02001_001E","B02001_003E")

test <- tidycensus::get_acs("tract", vars, output = "wide", state = "MO", county = 510, geometry = TRUE) %>%
  dplyr::transmute(black_pop = B02001_003E/B02001_001E,
            tid = row_number()) %>%
  sf::st_transform(102696)

make_grid <- function(.data){

  # find units of object
  units <- st_crs(.data, parameters = TRUE)$units_gdal

  # error if not projected
  if(units == "degree"){
    stop("Data must be projected to interpolate to grid squares")
  }

  cellsize <- switch (units,
    "Foot_US" = c(3280.84,3280.84), # Feet to Sq KM
    "Meter" = c(1000, 1000)
  ) # Will return NULL if no matches, need to implement more possible GDAL Units

  # make grid and clip to full boundary of data
  grid <- sf::st_make_grid(.data, cellsize, square = TRUE) %>%
    sf::st_intersection(sf::st_union(.data))

  # coerce to sf/df and add unique ID
  grid %<>% sf::st_sf() %>% dplyr::mutate(gid = dplyr::row_number())

  return(grid)
}

grid <- make_grid(test)
out <- areal::aw_interpolate(grid, "gid", test, "tid", weight = "sum", output = "sf", intensive = "black_pop")

Definitely needs refinement to implement, and the reason aw_interpolate is external to the function is because I could not get NSE to play nicely from within the function. Very neat too is that you can change from square to hexagons by setting square = FALSE

@chris-prener
Copy link
Owner

I think I'd prefer to keep these separate, but I'd be open to included some wrapped tessellation tools - pass in a county shape file and convert it to a grid or hexagons, and then the end user could pipe that into areal::aw_interpolate(). How would that be @bransonf ?

@bransonf
Copy link
Contributor Author

In that case I'll make a PR in a few hours.

@chris-prener
Copy link
Owner

Sounds great @bransonf !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants