open shape files and merge into one data frame

## Reading layer `wwa_202206010000_202209300000' from data source 
##   `C:\Users\sherl\OneDrive\Documents\GRAD SCHOOL II\Sherlyn_PMST_CDS_Program\PE Final Project\WeatherDataCollection\MonsoonData_fromJohn\wwa_202206010000_202209300000\wwa_202206010000_202209300000.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 317 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -115.12 ymin: 36.03 xmax: -108.92 ymax: 41.08
## Geodetic CRS:  WGS 84
## Reading layer `wwa_202306010000_202309300000' from data source 
##   `C:\Users\sherl\OneDrive\Documents\GRAD SCHOOL II\Sherlyn_PMST_CDS_Program\PE Final Project\WeatherDataCollection\MonsoonData_fromJohn\wwa_202306010000_202309300000\wwa_202306010000_202309300000.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 179 features and 25 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: -114.05 ymin: 36.37 xmax: -108.83 ymax: 41.08
## Geodetic CRS:  WGS 84

Add a date and year column

sort into County warnings and polygon warnings

Find county names from county shapefile

Create year data with counties and polygons. Count number of events for each county

## `summarise()` has grouped output by 'Year'. You can override using the
## `.groups` argument.

Write total number of warnings per county to file

## Deleting layer `FloodWarnings21_23_County' using driver `ESRI Shapefile'
## Writing layer `FloodWarnings21_23_County' to data source 
##   `MonsoonData_fromJohn/FloodWarnings21_23_County.shp' using driver `ESRI Shapefile'
## Writing 20 features with 4 fields and geometry type Unknown (any).

Write separate file for polygon warnings

## Deleting layer `FloodWarnings21_23_Polygons' using driver `ESRI Shapefile'
## Writing layer `FloodWarnings21_23_Polygons' to data source 
##   `MonsoonData_fromJohn/FloodWarnings21_23_Polygons.shp' using driver `ESRI Shapefile'
## Writing 386 features with 27 fields and geometry type Multi Polygon.

Plot maps

Write Leaflet map

saveWidget(map,
           file = "MonsoonData_fromJohn/FlashFloodWarnings.html",
           selfcontained = F)

Zion NP Warnings

make data frame for warnings that happened in Zion (or intersect with Zion boundaries)

# read in National Park shapefile
national_parks_sf <- read_sf("Shapefiles/NationalParksBoundaries/NPS_Boundaries.shp")

# get zion geometry
zion_sf <- subset(national_parks_sf, PARKNAME == "Zion")
zion_sf <- st_transform(zion_sf, crs = st_crs(ffw21to23))

# drop unneeded columns from both data sets
ffw21to23_short <- subset(ffw21to23, select = -c(WFO, PHENOM, SIG, 
                                                 STATUS, HV_NWSLI,
                                                 HV_SEV, HV_CAUSE, HV_REC,
                                                 EMERGENC, WINDTAG, 
                                                 HAILTAG, TORNTAG))
zion_short <- subset(zion_sf, select = c(PARKNAME, Shape__Are, 
                                         Shape__Len, geometry))

# get flood warnings that intersect with Zion boundaries
ffwarn_zion <- st_intersection(ffw21to23_short, zion_short)
#write_sf(ffwarn_zion, "Shapefiles/ZionNP/ffwarnings_in_zion.shp",
#         append = F)
#write_sf(zion_sf, "Shapefiles/ZionNP/zion_nps_boundaries.shp",
#         append = F)
# map polygons only
ffwarn_zion_poly <- subset(ffwarn_zion, GTYPE == "P")

pal <- colorFactor(palette = "YlOrRd", levels = ffwarn_zion_poly$Year)

leaflet(ffwarn_zion_poly) %>%
  addTiles() %>%
  setView(lng = -112.943987, lat = 37.270708, zoom = 10) %>%
  addPolylines(weight = 1,
               opacity = 0.8,
               color = ~pal(Year),
               group = ffwarn_zion_poly$Year) %>%
  addLayersControl(overlayGroups = ffwarn_zion_poly$Year, 
                   options = layersControlOptions(collapsed = F)) %>%
  addPolylines(data = zion_sf,
               color = "black",
               weight = 3, 
               opacity = 0.6) %>%
  addLegend(position = "topright",
           pal = pal, values = ~Year, opacity = 1) 

Calculate the number of county wide flash flood warnings each year

# get county warnings
ffwarn_zion_county <- subset(ffwarn_zion, GTYPE == "C")

ffwarn_zion_county$NWS_UGC <- factor(ffwarn_zion_county$NWS_UGC,
                                     labels = c("IRON", "KANE", "WASHINGTON"))
county_totals <- ffwarn_zion_county %>% 
                    count(Year, NWS_UGC, name = "YearTotal")

ggplot(ffwarn_zion_county) + 
  geom_bar(aes(x = Year, fill = NWS_UGC),
           stat = "count",
           position = "dodge") +
  labs(title = "ZION National Park",
       x = "Number of Flash Flood Warning at the County Level",
       y = "Year",
       fill = "County") +
  geom_text(data = county_totals,
            aes(x = Year,
                y = NWS_UGC,
                label=YearTotal),
            position = position_dodge(width = 0.8, preserve = "total"),
            size = 3,
#            vjust=-0.8, hjust=-0.5, 
            colour = "gray25",
            show.legend = F) +
  theme_bw()