Skip to content

Commit

Permalink
add a few HUC8s
Browse files Browse the repository at this point in the history
  • Loading branch information
lindsayplatt committed Nov 22, 2021
1 parent a14a972 commit 0fef5a9
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 11 deletions.
16 changes: 14 additions & 2 deletions 1_fetch.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
)
1 change: 1 addition & 0 deletions 1_fetch/src/maps_to_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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) %>%
Expand Down
19 changes: 17 additions & 2 deletions 2_process.R
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -18,14 +21,26 @@ 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)
),

tar_target(
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)
)
)
25 changes: 23 additions & 2 deletions 3_build.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
)

Expand Down
10 changes: 5 additions & 5 deletions 3_build/src/svg_xml_helpers.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ library(targets)

tar_option_set(packages = c(
"maps",
"nhdplusTools",
"rmapshaper",
"sf",
"tidyverse",
Expand Down

0 comments on commit 0fef5a9

Please sign in to comment.