|
| 1 | +## load libraries ---- |
| 2 | +library(dplyr) |
| 3 | +library(tidyverse) |
| 4 | +library(magrittr) |
| 5 | +library(sf) |
| 6 | +library(raster) |
| 7 | +library(parallel) |
| 8 | +library(argparse) |
| 9 | +print("This is the sf package version we are using:") |
| 10 | +print(packageVersion("sf")) |
| 11 | + |
| 12 | +## define parser arguments ---- |
| 13 | +parser <- ArgumentParser() |
| 14 | +parser$add_argument("-y", "--year", default=2006, |
| 15 | + help="Year to run", type="integer") |
| 16 | +parser$add_argument("-c", "--cores", default=24, |
| 17 | + help="Number of cores", type="integer") |
| 18 | +args = parser$parse_args() |
| 19 | +print("use R script get_weights_par") |
| 20 | +# args = list() |
| 21 | +# args$year = 2006 |
| 22 | +# args$cores = 24 |
| 23 | + |
| 24 | +## read functions ---- |
| 25 | +source("../../lib/get_weights_par.R") |
| 26 | + |
| 27 | +print("load data") |
| 28 | +## Load grid and zip sf objects ---- |
| 29 | +grid_sf = read_rds("../data/intermediate/scratch/grid_sf.rds") %>% |
| 30 | + rename(grid_id = ID) |
| 31 | +zip_sf_list = read_rds("../data/intermediate/scratch/zip_sf_list.rds") |
| 32 | +zip_sf = zip_sf_list[[as.character(args$year)]] %>% |
| 33 | + rename(zip_id = ZIP) |
| 34 | +rm(zip_sf_list) |
| 35 | +x_poly_sf = zip_sf |
| 36 | +y_poly_sf = grid_sf |
| 37 | +x_id = "zip_id" |
| 38 | +y_id = "grid_id" |
| 39 | +cores = args$cores |
| 40 | + |
| 41 | +## crop polygons within same bounding box ---- |
| 42 | +x_poly_sf <- st_make_valid(x_poly_sf) |
| 43 | +x_poly_sf <- st_crop(x_poly_sf, st_bbox(y_poly_sf)) |
| 44 | + |
| 45 | +# assign 1s to zipcode polygons ---- |
| 46 | +x_poly_sf$w <- 1 |
| 47 | + |
| 48 | + |
| 49 | +x_to_y <- data.frame() |
| 50 | + |
| 51 | +# error zipcodes |
| 52 | +for(i in c("03281")) { |
| 53 | + x_i_sf <- dplyr::select(x_poly_sf[x_poly_sf[[x_id]] == i, ], c("w", "geometry")) |
| 54 | + y_i_sf <- dplyr::select(st_crop(y_poly_sf, extent(x_i_sf)), c(y_id, "geometry")) |
| 55 | + |
| 56 | + # y_i_w <- st_drop_geometry(st_interpolate_aw(x_i_sf, y_i_sf, extensive = T)) |
| 57 | + tryCatch({ |
| 58 | + y_i_w <- st_drop_geometry(st_interpolate_aw(x_i_sf, y_i_sf, extensive = T)) |
| 59 | + |
| 60 | + x_to_y_i <- data.frame( |
| 61 | + x_id = i, |
| 62 | + y_id = y_i_sf[[y_id]][as.numeric(rownames(y_i_w))], |
| 63 | + w = y_i_w$w) |
| 64 | + |
| 65 | + x_to_y <- rbind(x_to_y, x_to_y_i) |
| 66 | + |
| 67 | + }, error=function(e){ |
| 68 | + print("An error occurred while calculating the weights.") |
| 69 | + print(i) |
| 70 | + }) |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +## example successful one |
| 75 | +test_x <- dplyr::select(x_poly_sf[x_poly_sf[[x_id]] == "01604", ], c("w", "geometry")) |
| 76 | +test_y <- dplyr::select(st_crop(y_poly_sf, extent(test_x)), c(y_id, "geometry")) |
| 77 | + |
| 78 | +test_interpolate <- st_interpolate_aw(test_x, test_y, extensive = T) |
| 79 | + |
| 80 | +############ |
| 81 | +# The success one |
| 82 | +plot(test_x[[2]]) |
| 83 | +plot(test_y[[2]], add = TRUE) |
| 84 | +plot(test_interpolate[[2]]) |
| 85 | + |
| 86 | + |
| 87 | +# The error one |
| 88 | +plot(x_i_sf[[2]]) |
| 89 | +plot(y_i_sf[[2]], add = TRUE) |
| 90 | +st_interpolate_aw(x_i_sf, y_i_sf, extensive = T) |
| 91 | + |
| 92 | + |
0 commit comments