The goal of this analysis is to delineate watersheds for points associated with the CBW-LULC project. This script relies on both the spatial capabilities of R (Raster, sp, and RGDAL packages) in addition to the hydrologic analysis tools in WhiteBox Tools scripting software. Required datasets inlcude a NHDplus 30m DEM, the gagesII database, and finally, NLCD-LULC data from the USGS. All code is house at GitHub

For more on wateshed delineation in WhiteBox GAT and WhiteBoxTools, see this block post from Jon Lindsay.

Step 1: Data Organization

Note, data is currently housed in Nate's SESYNC workspace. He will eventually migrate this to the Palmer Lab folder.

#Clear Memory
rm(list=ls(all=TRUE))

#Define relevant working directories
data_dir<-"//storage.research.sesync.org/njones-data/Research Projects/LULC_CBW/Spatial_Data/"
scratch_dir<-"C:\\ScratchWorkspace\\"
wbt_dir<-"C:/WBT/whitebox_tools"

#Add appropriate libararies
library(sf)
library(raster)
library(rgdal)
library(rgeos)
library(dplyr)
library(devtools)

#Download package from GIT
install_github("FloodHydrology/InundationHydRology")
library(InundationHydRology)

#Download required data
dem<-raster(paste0(data_dir,"NHDPlus02/Elev_Unit_a/elev_cm"))
  p<-dem@crs
HUC08<-st_read(paste0(data_dir,"NHDPlus02/Subbasin.shp"))
  HUC08<-st_transform(HUC08, crs=paste(p))
LULC2001<-raster(paste0(data_dir,"nlcd_2001_landcover_2011_edition_2014_10_10/nlcd_2001_landcover_2011_edition_2014_10_10/nlcd_2001_landcover_2011_edition_2014_10_10.img"))
LULC2006<-raster(paste0(data_dir,"nlcd_2006_landcover_2011_edition_2014_10_10/nlcd_2006_landcover_2011_edition_2014_10_10.img"))
LULC2011<-raster(paste0(data_dir,"nlcd_2011_landcover_2011_edition_2014_10_10/nlcd_2011_landcover_2011_edition_2014_10_10.img"))

Step 2: Delineate watershed using the LULC_WatershedAnalysis r_script in the InundationHydRology package.

#Define pnts
pnts<-read.csv(paste0(data_dir,"input.csv"))
pnts<-st_as_sf(pnts, 
               coords=c("Longitude","Latitude"), 
               crs="+proj=longlat +ellps=GRS80 +datum=NAD83 +no_defs")
pnts<-st_transform(pnts, paste(dem@crs))

#Select Subbasin
mask<-HUC08
mask<-mask[pnts,]

#For testing
mask<-mask[1,]
pnts<-st_intersection(pnts, mask)

fun<-function(n){
  tryCatch(LULC_WatershedAnalysis(  
                dem=dem,  
                pnts=pnts,
                unique_id = "pnts.FieldActivityId",
                mask=mask[n,], 
                threshold=1000,
                LULC2001=LULC2001,
                LULC2006=LULC2006,
                LULC2011=LULC2011, 
                data_dir = data_dir, 
                output_dir = paste0(data_dir,"watershed/")), 
           error=function(e) c(n, rep(0,24)))}

#Run function
x<-lapply(seq(1,length(mask)), fun)

#pull together data
output<-bind_rows(x)
save.image("initial_output.R")

So, I've encourntered problems at i=23. I'm not sure why. Next step will be to investigate this.



FloodHydrology/InundationHydRology documentation built on June 15, 2019, 5:14 a.m.