mirror of
https://github.com/dylanbenzi/hurricane_normalization_app.git
synced 2026-07-30 05:08:57 +00:00
update db queries
This commit is contained in:
+71
-87
@@ -41,18 +41,6 @@ baseDir <- linuxdir
|
|||||||
|
|
||||||
config <- config::get(file = paste0(baseDir, "R/dataScripts/restructured/app/config.yml"))
|
config <- config::get(file = paste0(baseDir, "R/dataScripts/restructured/app/config.yml"))
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# LOCAL CON
|
|
||||||
#con <- dbConnect(
|
|
||||||
# RPostgres::Postgres(),
|
|
||||||
# user = "postgres",
|
|
||||||
# password = "oiuqBub8s9n65sgan09",
|
|
||||||
# host = "192.168.0.6",
|
|
||||||
# port = 5432,
|
|
||||||
# dbname = "hurricanedb"
|
|
||||||
#)
|
|
||||||
|
|
||||||
# SUPABASE CON
|
# SUPABASE CON
|
||||||
con <- dbConnect(
|
con <- dbConnect(
|
||||||
RPostgres::Postgres(),
|
RPostgres::Postgres(),
|
||||||
@@ -69,36 +57,9 @@ selected_storm_basin <- "AL"
|
|||||||
selected_lf_type = "LF"
|
selected_lf_type = "LF"
|
||||||
selected_lf_id = "2"
|
selected_lf_id = "2"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
bestTrackQry <- paste0("SELECT * FROM hurdat.best_track WHERE storm_basin = '", selected_storm_basin,
|
|
||||||
"' AND storm_name = '", selected_storm_name,
|
|
||||||
"' AND storm_year = ", selected_storm_year
|
|
||||||
)
|
|
||||||
|
|
||||||
qry <- "SELECT *, ST_X(ST_Transform(location::geometry, 4326)) as lon, ST_Y(ST_Transform(location::geometry, 4326)) as lat FROM hurdat.best_track WHERE storm_basin = 'AL' AND storm_name = 'KATRINA' AND storm_year = 2005"
|
|
||||||
|
|
||||||
xlallLandfallsNormalized <- read.csv(paste0(baseDir, "R/dataScripts/restructured/normalization/all-landfalls-normalized.csv"), header = T) %>%
|
xlallLandfallsNormalized <- read.csv(paste0(baseDir, "R/dataScripts/restructured/normalization/all-landfalls-normalized.csv"), header = T) %>%
|
||||||
select(-X)
|
select(-X)
|
||||||
|
|
||||||
katrinaTrack <- dbGetQuery(con, qry)
|
|
||||||
katrinaTrack <- katrinaTrack %>%
|
|
||||||
mutate(
|
|
||||||
date = make_datetime(datetime)
|
|
||||||
) %>%
|
|
||||||
select(
|
|
||||||
Date = date,
|
|
||||||
Latitude = lat,
|
|
||||||
Longitude = lon,
|
|
||||||
Pressure = pressure,
|
|
||||||
Windspeed = windspeed,
|
|
||||||
RMW = rmw,
|
|
||||||
record_identifier
|
|
||||||
)
|
|
||||||
|
|
||||||
katrinaLandfalls <- katrinaTrack %>%
|
|
||||||
filter(record_identifier == 'L') %>%
|
|
||||||
select(-record_identifier)
|
|
||||||
|
|
||||||
|
|
||||||
normalized2024 <- read.csv(paste0(baseDir, "R/dataScripts/restructured/normalization/2024-normalized.csv"), header = T) %>%
|
normalized2024 <- read.csv(paste0(baseDir, "R/dataScripts/restructured/normalization/2024-normalized.csv"), header = T) %>%
|
||||||
@@ -150,8 +111,6 @@ housing <- read.csv(paste0(baseDir, "Data/housing_units.csv"), stringsAsFactors
|
|||||||
metrics.pop_and_housing <- pop %>%
|
metrics.pop_and_housing <- pop %>%
|
||||||
left_join(housing, by = c("state_fips", "county_fips", "year"))
|
left_join(housing, by = c("state_fips", "county_fips", "year"))
|
||||||
|
|
||||||
#counties <- counties(state = "LA", cb = T)
|
|
||||||
|
|
||||||
weightedCounties <- read.csv(paste0(baseDir, "R/dataScripts/weighted-counties.csv"), header = T) %>%
|
weightedCounties <- read.csv(paste0(baseDir, "R/dataScripts/weighted-counties.csv"), header = T) %>%
|
||||||
mutate(
|
mutate(
|
||||||
weight = (PERCENTAGE/100),
|
weight = (PERCENTAGE/100),
|
||||||
@@ -208,18 +167,9 @@ storm_selection <- reactiveValues(
|
|||||||
is_selected = FALSE
|
is_selected = FALSE
|
||||||
)
|
)
|
||||||
|
|
||||||
growth_trends <- reactiveValues(
|
|
||||||
lf_id = NULL
|
|
||||||
)
|
|
||||||
|
|
||||||
######
|
######
|
||||||
|
|
||||||
###### SUPABASE PORT
|
###### SUPABASE PORT
|
||||||
|
|
||||||
hurdat.hurdat_storms <- dbGetQuery(con, "SELECT *, CONCAT(storm_basin, storm_number, storm_year) AS hurdatId FROM hurdat.hurdat_storms")
|
|
||||||
|
|
||||||
#hurdat.best_track <- dbGetQuery(con, "SELECT *, ST_X(ST_Transform(location::geometry, 4326)) as lon, ST_Y(ST_Transform(location::geometry, 4326)) as lat FROM hurdat.best_track")
|
|
||||||
|
|
||||||
econ.normalized_landfalls <- dbGetQuery(con, "SELECT * FROM econ.normalized_landfalls")
|
econ.normalized_landfalls <- dbGetQuery(con, "SELECT * FROM econ.normalized_landfalls")
|
||||||
|
|
||||||
econ.storm_base_loss <- reactive({
|
econ.storm_base_loss <- reactive({
|
||||||
@@ -248,16 +198,27 @@ econ.storm_base_loss <- reactive({
|
|||||||
dbGetQuery(con, query)
|
dbGetQuery(con, query)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
# LAZY LOAD DB TABLES
|
||||||
|
|
||||||
|
econ.normalized_landfalls <- tbl(con, I("econ.normalized_landfalls"))
|
||||||
|
econ.storm_base_loss <- tbl(con, I("econ.storm_base_loss"))
|
||||||
|
econ.usa_yearly <- tbl(con, I("econ.usa_yearly"))
|
||||||
|
|
||||||
|
fatal.storm_fatalities_type <- tbl(con, I("fatal.storm_fatalities_type"))
|
||||||
|
fatal.storm_total_fatalities <- tbl(con, I("fatal.storm_total_fatalities"))
|
||||||
|
|
||||||
|
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.affected_area_landfalls <- tbl(con, I("gis.affected_area_landfalls"))
|
||||||
|
|
||||||
|
hurdat.best_track <- tbl(con, I("hurdat.best_track"))
|
||||||
|
hurdat.hurdat_storms <- tbl(con, I("hurdat.hurdat_storms"))
|
||||||
|
|
||||||
|
metrics.geo_attributes <- tbl(con, I("metrics.geo_attributes"))
|
||||||
|
#metrics.pop_and_housing <- tbl(con, I("metrics.pop_and_housing"))
|
||||||
|
|
||||||
public.counties <- tbl(con, I("public.counties"))
|
public.counties <- tbl(con, I("public.counties"))
|
||||||
|
|
||||||
|
|
||||||
#allLandfallsNormalized <- econ.normalized_landfalls %>%
|
|
||||||
# left_join(econ.storm_base_loss, by = c("storm_basin", "storm_year", "storm_name", "lf_type", "lf_id")) %>%
|
|
||||||
# mutate(
|
|
||||||
# mmh = gdp_deflator * rwhu * affected_housing,
|
|
||||||
# mmp = gdp_deflator * rwpc * affected_population
|
|
||||||
# )
|
|
||||||
```
|
```
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
@@ -391,27 +352,54 @@ Col {data-width=500}
|
|||||||
|
|
||||||
### Storm Selector {data-height=500}
|
### Storm Selector {data-height=500}
|
||||||
```{r}
|
```{r}
|
||||||
h5("Select Storm Name and Year")
|
|
||||||
|
loss_storms <- econ.storm_base_loss %>%
|
||||||
|
select(
|
||||||
|
storm_basin, storm_year, storm_name
|
||||||
|
) %>%
|
||||||
|
distinct(
|
||||||
|
storm_basin, storm_year, storm_name
|
||||||
|
) %>%
|
||||||
|
left_join(
|
||||||
|
hurdat.hurdat_storms %>%
|
||||||
|
mutate(hurdatId = paste0(storm_basin, storm_number, storm_year)),
|
||||||
|
by = c("storm_basin", "storm_year", "storm_name")) %>%
|
||||||
|
select(
|
||||||
|
hurdatId, storm_name, storm_year
|
||||||
|
) %>%
|
||||||
|
collect()
|
||||||
|
|
||||||
dbStormYears <- dbGetQuery(con, "SELECT DISTINCT storm_year, storm_name FROM econ.storm_base_loss ORDER BY storm_year")
|
dbStormYears <- dbGetQuery(con, "SELECT DISTINCT storm_year, storm_name FROM econ.storm_base_loss ORDER BY storm_year")
|
||||||
|
|
||||||
fluidRow(
|
fluidRow(
|
||||||
column(6,
|
column(6,
|
||||||
selectInput("stormYear", "Select Year", choices = dbStormYears$storm_year)
|
h5("Select Storm Name and Year"),
|
||||||
|
selectInput("stormYear", "Select Year", choices = dbStormYears$storm_year),
|
||||||
|
selectInput("stormName", "Select Storm", choices = NULL),
|
||||||
|
actionButton("selectStorm", "Submit", class = "btn-primary")
|
||||||
),
|
),
|
||||||
column(6,
|
column(6,
|
||||||
selectInput("stormName", "Select Storm", choices = NULL)
|
#TODO: ADD TABLE
|
||||||
|
|
||||||
|
#DTOutput("storm_selector_table")
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
fluidRow(
|
output$storm_selector_table <- renderDT({
|
||||||
column(12,
|
datatable(
|
||||||
actionButton("selectStorm", "Submit", class = "btn-primary"))
|
loss_storms,
|
||||||
)
|
rownames = F,
|
||||||
|
options = list(
|
||||||
#h5("Search")
|
pageLength = 1000,
|
||||||
|
order = list(2, 'desc'),
|
||||||
#textInput("stormSearch", "Search by hurricane name, year")
|
searching = F,
|
||||||
|
paging = F,
|
||||||
|
info = F,
|
||||||
|
lengthChange = F,
|
||||||
|
server = T
|
||||||
|
)
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
observeEvent(input$stormYear, {
|
observeEvent(input$stormYear, {
|
||||||
stormsByChosenYear <- dbStormYears[dbStormYears$storm_year == input$stormYear, ]
|
stormsByChosenYear <- dbStormYears[dbStormYears$storm_year == input$stormYear, ]
|
||||||
@@ -500,17 +488,6 @@ dbUniq <- reactive({
|
|||||||
|
|
||||||
dbNormalizedLandfalls() %>%
|
dbNormalizedLandfalls() %>%
|
||||||
distinct(lfid)
|
distinct(lfid)
|
||||||
|
|
||||||
#re <- unique(res$lfid)
|
|
||||||
|
|
||||||
#cat(str(res))
|
|
||||||
#cat(str(re))
|
|
||||||
|
|
||||||
#cat("hi2")
|
|
||||||
|
|
||||||
#cat(str(res))
|
|
||||||
|
|
||||||
#return(res)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
lfNormalized <- reactive({
|
lfNormalized <- reactive({
|
||||||
@@ -550,7 +527,6 @@ fillCol(
|
|||||||
#checkboxInput("mmpSelect", "Display MMP", value = T)
|
#checkboxInput("mmpSelect", "Display MMP", value = T)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
#dygraphOutput("katrinaCostIndex")
|
|
||||||
dygraphOutput("costIndex")
|
dygraphOutput("costIndex")
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -558,10 +534,6 @@ fillCol(
|
|||||||
observe({
|
observe({
|
||||||
req(dbUniq(), nrow(dbUniq()) > 0)
|
req(dbUniq(), nrow(dbUniq()) > 0)
|
||||||
|
|
||||||
#cat("hi")
|
|
||||||
#cat(nrow(dbUniq()))
|
|
||||||
#cat(str(dbUniq()))
|
|
||||||
|
|
||||||
updateSelectInput(
|
updateSelectInput(
|
||||||
session,
|
session,
|
||||||
"lfSelect",
|
"lfSelect",
|
||||||
@@ -689,6 +661,12 @@ Column {data-width=550}
|
|||||||
### {data-height=1000 .no-padding}
|
### {data-height=1000 .no-padding}
|
||||||
|
|
||||||
```{r}
|
```{r}
|
||||||
|
growth_trends <- reactiveValues(
|
||||||
|
lf_id = NULL
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
dbStormCounties <- reactive({
|
dbStormCounties <- reactive({
|
||||||
req(storm_selection$is_selected)
|
req(storm_selection$is_selected)
|
||||||
|
|
||||||
@@ -828,7 +806,7 @@ output$popHu <- renderDygraph({
|
|||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
dygraphOutput("popHu")
|
#dygraphOutput("popHu")
|
||||||
```
|
```
|
||||||
|
|
||||||
### County Data {data-height=450 .no-padding}
|
### County Data {data-height=450 .no-padding}
|
||||||
@@ -852,7 +830,7 @@ output$katrinaCounties <- renderDT({
|
|||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
DTOutput("katrinaCounties")
|
#DTOutput("katrinaCounties")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -890,6 +868,12 @@ Data {data-navmenu="Compute"}
|
|||||||
|
|
||||||
Top 50 Storms
|
Top 50 Storms
|
||||||
===
|
===
|
||||||
|
```{r}
|
||||||
|
|
||||||
|
#DT with storm, hurdatid, base damage, mmh, mmp, maybe multipliers?, sparkline?
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
About
|
About
|
||||||
===
|
===
|
||||||
|
|||||||
Reference in New Issue
Block a user