diff --git a/1_fetch.R b/1_fetch.R index 55eb284..b27b45c 100644 --- a/1_fetch.R +++ b/1_fetch.R @@ -15,8 +15,20 @@ p1_targets <- list( tar_target( p1_conus_states_sf, generate_conus_states_sf(p1_proj_str) - ) + ), # Get basins - + # TODO: add more than the one IWS basin and propogate these + # labels through to the SVG additions. + tar_target( + p1_huc8s, c("07120001", "07120002", "07120003") + ), + tar_target( + p1_huc8s_sf, + get_huc8(id = p1_huc8s) %>% + st_buffer(0) %>% + st_union() %>% + st_make_valid() %>% + st_transform(p1_proj_str) + ) ) diff --git a/1_fetch/src/maps_to_sf.R b/1_fetch/src/maps_to_sf.R index 64583d2..82b4596 100644 --- a/1_fetch/src/maps_to_sf.R +++ b/1_fetch/src/maps_to_sf.R @@ -33,6 +33,7 @@ generate_conus_states_sf <- function(proj_str) { # polygon and gets dropped somewhere along the way. co_sf <- maps::map("state", "colorado", fill = TRUE, plot=FALSE) %>% st_as_sf() %>% + # st_buffer(0) %>% # Hmm thought it would fix the weird line but doesn't st_transform(proj_str) maps::map("state", fill = TRUE, plot=FALSE) %>% diff --git a/2_process.R b/2_process.R index a49607a..00338fc 100644 --- a/2_process.R +++ b/2_process.R @@ -6,6 +6,9 @@ source("2_process/src/coords_to_svg_path.R") p2_targets <- list( tar_target(svg_width, 1000), + tar_target(p2_view_bbox, st_bbox(p1_conus_states_sf)), + + # Prepare CONUS states for SVG tar_target( p2_conus_states_names, @@ -18,8 +21,7 @@ p2_targets <- list( p2_conus_states_coords, p1_conus_states_sf %>% filter(ID %in% p2_conus_states_names) %>% - sf_to_coords(svg_width, - view_bbox = st_bbox(p1_conus_states_sf)), + sf_to_coords(svg_width, view_bbox = p2_view_bbox), pattern = map(p2_conus_states_names) ), @@ -27,5 +29,18 @@ p2_targets <- list( p2_conus_states_paths, coords_to_svg_path(p2_conus_states_coords, close_path = TRUE), pattern = map(p2_conus_states_coords) + ), + + # Prepare HUCs for SVG + + tar_target( + p2_huc8s_coords, + p1_huc8s_sf %>% + sf_to_coords(svg_width, view_bbox = p2_view_bbox) + ), + + tar_target( + p2_huc8s_paths, + coords_to_svg_path(p2_huc8s_coords, close_path = TRUE) ) ) diff --git a/3_build.R b/3_build.R index 8171eea..a5ae6ac 100644 --- a/3_build.R +++ b/3_build.R @@ -11,6 +11,8 @@ p3_targets <- list( format = "file" ), + # Add states + # TODO: groups don't seem to actually be working tar_target( g_conus_state_svg, add_grp(out_svg = "3_build/tmp/g_conus_state.svg", @@ -23,13 +25,32 @@ p3_targets <- list( state_paths_svg, add_child_paths(out_svg = "3_build/tmp/state_paths.svg", in_svg = g_conus_state_svg, - paths = p2_conus_states_paths), + paths = p2_conus_states_paths, + path_nms = sprintf('state-%s', p2_conus_states_names)), + format = "file" + ), + + # Add in HUCs + tar_target( + g_huc8s_svg, + add_grp(out_svg = "3_build/tmp/g_huc8s.svg", + in_svg = state_paths_svg, + grp_nm = 'huc8s', trans_x = 0, trans_y = 0), + format = "file" + ), + + tar_target( + huc8s_paths_svg, + add_child_paths(out_svg = "3_build/tmp/huc8s_paths.svg", + in_svg = g_huc8s_svg, + paths = p2_huc8s_paths, + path_nms = rep('huc8s', length(p2_huc8s_paths))), format = "file" ), tar_target( map_svg, - build_final_svg("3_build/out/map.svg", state_paths_svg), + build_final_svg("3_build/out/map.svg", huc8s_paths_svg), format = "file" ) diff --git a/3_build/src/svg_xml_helpers.R b/3_build/src/svg_xml_helpers.R index 9fb924c..9de9193 100644 --- a/3_build/src/svg_xml_helpers.R +++ b/3_build/src/svg_xml_helpers.R @@ -25,12 +25,12 @@ add_grp <- function(out_svg, in_svg, grp_nm, trans_x, trans_y) { return(out_svg) } -add_child_paths <- function(out_svg, in_svg, paths) { +add_child_paths <- function(out_svg, in_svg, paths, path_nms) { svg_state <- read_xml(in_svg) - for(path_i in paths) { - xml_add_child(svg_state, 'path', d = path_i, - class='conus-state', - style="stroke:#9fabb7;stroke-width:0.5;fill:green") + for(i in 1:length(paths)) { + xml_add_child(svg_state, 'path', d = paths[i], + class = path_nms[i], + style = "stroke:#9fabb7;stroke-width:0.5;fill:none") } write_xml(svg_state, out_svg) return(out_svg) diff --git a/_targets.R b/_targets.R index bc95bd9..30e0390 100644 --- a/_targets.R +++ b/_targets.R @@ -2,6 +2,7 @@ library(targets) tar_option_set(packages = c( "maps", + "nhdplusTools", "rmapshaper", "sf", "tidyverse",