From 4f94249b23c4e4f65d55ed611ee7bd66fbadcc02 Mon Sep 17 00:00:00 2001 From: dylanbenzi Date: Wed, 24 Dec 2025 17:04:39 -0500 Subject: [PATCH] update growth map to use indirect landfall states --- app/queries.R | 213 +++++++++++++++++++++++++++++++------------------- 1 file changed, 131 insertions(+), 82 deletions(-) diff --git a/app/queries.R b/app/queries.R index 6c52079..7c88b61 100644 --- a/app/queries.R +++ b/app/queries.R @@ -30,6 +30,8 @@ fips.counties <- tbl(con, I("fips.counties")) fips.states <- tbl(con, I("fips.states")) gis.affected_area_landfalls <- tbl(con, I("gis.affected_area_landfalls")) +gis.indirect_landfalls <- tbl(con, I("gis.indirect_landfalls")) +gis.us_states_boundary <- tbl(con, I("gis.us_states_boundary")) hurdat.best_track <- tbl(con, I("hurdat.best_track")) hurdat.hurdat_storms <- tbl(con, I("hurdat.hurdat_storms")) @@ -54,6 +56,7 @@ view.all_lf_type_landfalls <- tbl(con, "all_lf_type_landfalls") view.lf_id_location <- tbl(con, "lf_id_location") view.lf_landfall_gis <- tbl(con, "lf_landfall_gis") view.best_track_summary <- tbl(con, "best_track_summary") +view.yearly_state_metrics <- tbl(con, "yearly_state_metrics") #qry <- econ.storm_base_loss %>% # left_join(view.hurdat_track, by = c("storm_basin", "storm_year", "storm_name")) @@ -417,91 +420,137 @@ get_hurdat_track <- function(storm) { get_normalized_metric_growth <- function(storm, full_lf_id) { lf_id_parts <- split_full_lf_id(full_lf_id) - affected_counties <- gis.affected_area_landfalls %>% - filter( - storm_basin == storm$storm_basin, - storm_year == storm$storm_year, - storm_name == storm$storm_name, - lf_type == lf_id_parts$lf_type, - lf_id == lf_id_parts$lf_id - ) %>% - select(state_fips, county_fips) + if (lf_id_parts$lf_type == 'LF') { + affected_counties <- gis.affected_area_landfalls %>% + filter( + storm_basin == storm$storm_basin, + storm_year == storm$storm_year, + storm_name == storm$storm_name, + lf_type == lf_id_parts$lf_type, + lf_id == lf_id_parts$lf_id + ) %>% + select(state_fips, county_fips) - baseline_metrics <- metrics.pop_and_housing %>% - filter( - year == storm$storm_year - ) %>% - inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% - select( - state_fips, - county_fips, - baseline_population = population, - baseline_housing = housing_units - ) - - normalized_metrics <- metrics.pop_and_housing %>% - filter( - year >= storm$storm_year - ) %>% - inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% - inner_join(baseline_metrics, by = c("state_fips", "county_fips")) %>% - mutate( - normalized_population = as.numeric(population) / - as.numeric(baseline_population), - normalized_housing = as.numeric(housing_units) / - as.numeric(baseline_housing) - ) %>% - select( - state_fips, - county_fips, - year, - population, - housing_units, - normalized_population, - normalized_housing - ) - - query <- normalized_metrics %>% - inner_join( - public.counties, - by = c("state_fips" = "statefp", "county_fips" = "countyfp") - ) %>% - mutate( - geom_wkt = sql( - "ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(geom, 4326), .001))" + baseline_metrics <- metrics.pop_and_housing %>% + filter( + year == storm$storm_year + ) %>% + inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% + select( + state_fips, + county_fips, + baseline_population = population, + baseline_housing = housing_units ) - ) %>% - select( - state_fips, - county_fips, - year, - population, - housing_units, - normalized_population, - normalized_housing, - geom_wkt - ) %>% - arrange(state_fips, county_fips, year) - #query <- normalized_metrics %>% - # inner_join( - # public.counties, - # by = c("state_fips" = "statefp", "county_fips" = "countyfp") - # ) %>% - # mutate( - # geom_wkt = sql("ST_AsText(ST_Transform(geom, 4326))") - # ) %>% - # select( - # state_fips, - # county_fips, - # year, - # population, - # housing_units, - # normalized_population, - # normalized_housing, - # geom_wkt - # ) %>% - # arrange(state_fips, county_fips, year) + normalized_metrics <- metrics.pop_and_housing %>% + filter( + year >= storm$storm_year + ) %>% + inner_join(affected_counties, by = c("state_fips", "county_fips")) %>% + inner_join(baseline_metrics, by = c("state_fips", "county_fips")) %>% + mutate( + normalized_population = as.numeric(population) / + as.numeric(baseline_population), + normalized_housing = as.numeric(housing_units) / + as.numeric(baseline_housing) + ) %>% + select( + state_fips, + county_fips, + year, + population, + housing_units, + normalized_population, + normalized_housing + ) + + query <- normalized_metrics %>% + inner_join( + public.counties, + by = c("state_fips" = "statefp", "county_fips" = "countyfp") + ) %>% + mutate( + geom_wkt = sql( + "ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(geom, 4326), .001))" + ) + ) %>% + select( + state_fips, + county_fips, + year, + population, + housing_units, + normalized_population, + normalized_housing, + geom_wkt + ) %>% + arrange(state_fips, county_fips, year) + } else { + affected_state <- gis.indirect_landfalls %>% + filter( + storm_basin == storm$storm_basin, + storm_year == storm$storm_year, + storm_name == storm$storm_name, + lf_type == lf_id_parts$lf_type, + lf_id == lf_id_parts$lf_id + ) %>% + select( + state_fips + ) + + baseline_metrics <- view.yearly_state_metrics %>% + filter(year == storm$storm_year) %>% + inner_join(affected_state, by = "state_fips") %>% + select( + state_fips, + baseline_population = state_population, + baseline_housing = state_housing_units + ) + + normalized_metrics <- view.yearly_state_metrics %>% + filter( + year >= storm$storm_year + ) %>% + inner_join(affected_state, by = "state_fips") %>% + inner_join(baseline_metrics, by = "state_fips") %>% + mutate( + normalized_population = as.numeric(state_population) / + as.numeric(baseline_population), + normalized_housing = as.numeric(state_housing_units) / + as.numeric(baseline_housing) + ) %>% + select( + state_fips, + year, + population = state_population, + housing_units = state_housing_units, + normalized_population, + normalized_housing + ) + + query <- normalized_metrics %>% + inner_join( + gis.us_states_boundary %>% + rename(state_fips = statefp), + by = "state_fips" + ) %>% + mutate( + geom_wkt = sql( + "ST_AsText(ST_SimplifyPreserveTopology(ST_Transform(wkb_geometry, 4326), .001))" + ) + ) %>% + select( + state_fips, + year, + population, + housing_units, + normalized_population, + normalized_housing, + geom_wkt + ) %>% + arrange(state_fips, year) + } result <- query %>% collect()