def hillslopes(shed_shp, streams, out_dir): wbt=WhiteboxTools() ensure_dir() dem=os.path.join(out_dir, 'H2O_shed_DEM.tif') pit_filled=os.path.join(out_dir, 'pit_filled_dem.tif') pointer=os.path.join(out_dir, 'pointer.tif') streams_path=os.path.join(scratch_dir, 'streams_partial.tif') hillslope_path=os.path.join(out_dir, 'hillslopes.tif') ensure_dir() wbt.breach_depressions(dem, pit_filled) ensure_dir() wbt.d8_pointer(pit_filled, pointer) watershed_streams(shed_shp, streams, out_dir) ensure_dir() hiilslope_shp=os.path.join(out_dir, 'hillslopes.shp') assert same_raster_extents(pointer, streams_path) wbt.hillslopes(pointer, streams_path, hillslope_path) hillslope_gdf=polygonize(hillslope_path, mask=None) hillslope_gdf=hillslope[hillslope_gdf.geometry.area>20] hillslope_gdf.to_file(hillslope_path) d_inf_pointer=os.path.join(out_dir, 'd_inf.tif') slope_path=os.path.join(out_dir, 'slope.tif') wbt.d_inf_pointer(pit_filled, d_inf_pointer) wbt.slope(pit_filled, output) wbt.clip(hillslope_shp, os.path.join(out_dir, 'buffers.shp',), hillslope_shp ) hillslope_gdf=gpd.read_file(hillslope_shp) for raster in [pit_filled, slope_path, d_inf_pointer]: wbt.clip_raster_to_polygon(i, polygons, output)
def geoprocess(fps, HUC12_code): '''Run all the wbt processes for the watershed. Used for running processes in multiple threads. ''' start = time.perf_counter() loc_fps = set_local_fps(fps, HUC12_code) if os.path.exists(loc_fps['RKLS']): return wbt = WhiteboxTools() #make shapefile of local lakes wbt.clip(loc_fps['waterbodies'], loc_fps['wshed_bounds'], loc_fps['wshed_lakes']) if os.path.exists(loc_fps['wshed_lakes']): lakes = gpd.read_file(loc_fps['wshed_lakes']) lakes['geometry'] = lakes.geometry.buffer(-1) lakes.to_file(loc_fps['wshed_lakes']) #erase lakes from the dem, for processing. wbt.erase_polygon_from_raster(loc_fps['dem'], loc_fps['wshed_lakes'], loc_fps['dem']) #prep_rasters_for_ls(loc_fps, wbt) #hydro-enforce the dem in two steps wbt.breach_single_cell_pits(loc_fps['dem'], loc_fps['pit_filled']) wbt.breach_depressions_least_cost(loc_fps['pit_filled'], loc_fps['pit_filled'], 1000, fill=True, max_cost=100) wbt.high_pass_filter(loc_fps['pit_filled'], loc_fps['high_pass'], 50, 50) wbt.d_inf_pointer(loc_fps['pit_filled'], loc_fps['pointer']) #make pointer wbt.d_inf_flow_accumulation(loc_fps['pointer'], loc_fps['sca_full'], pntr=True) #calc sca wbt.slope(loc_fps['pit_filled'], loc_fps['slope']) #calculate slope raster wbt.clip_raster_to_polygon(loc_fps['sca_full'], loc_fps['buffers'], loc_fps['sca'], maintain_dimensions=True) #make k-factor raster Feature_to_Raster(loc_fps['soils'], loc_fps['K_factors'], snap_raster=loc_fps['slope'], field_name='K_factor', NoData_value=-9999, data_type=gdal.GDT_Float32) #make subset of the R factor raster wbt.clip_raster_to_polygon( os.path.join(main_dir, 'intermediate_data', 'r_factor.tif'), loc_fps['wshed_bounds'], loc_fps['R']) ditch_detection(loc_fps, -.25, 5000) stop = time.perf_counter() minutes = (stop - start) / 60 with open(os.path.join(main_dir, 'intermediate_data', 'raster_log.txt'), 'a+') as f: f.write(f'watershed {HUC12_code} time to perform: {stop-start/60}') f.write('\n')