From fd4d906eb4a974edf0df12cb3f4c1b2b8cc5998a Mon Sep 17 00:00:00 2001 From: Matthew Hollingworth Date: Sat, 13 Feb 2021 20:30:57 +1100 Subject: [PATCH] add --weights option to D8FlowAccumulation tool --- .../src/tools/hydro_analysis/d8_flow_accum.rs | 30 ++++++++++++++++++- whitebox_tools.py | 4 ++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/whitebox-tools-app/src/tools/hydro_analysis/d8_flow_accum.rs b/whitebox-tools-app/src/tools/hydro_analysis/d8_flow_accum.rs index d622ef52e..8fc6d0173 100755 --- a/whitebox-tools-app/src/tools/hydro_analysis/d8_flow_accum.rs +++ b/whitebox-tools-app/src/tools/hydro_analysis/d8_flow_accum.rs @@ -79,6 +79,15 @@ impl D8FlowAccumulation { optional: false, }); + parameters.push(ToolParameter { + name: "Input Weight File".to_owned(), + flags: vec!["-w".to_owned(), "--weights".to_owned()], + description: "Optional input weights raster file.".to_owned(), + parameter_type: ParameterType::ExistingFile(ParameterFileType::Raster), + default_value: None, + optional: false, + }); + parameters.push(ToolParameter { name: "Output File".to_owned(), flags: vec!["-o".to_owned(), "--output".to_owned()], @@ -194,6 +203,7 @@ impl WhiteboxTool for D8FlowAccumulation { verbose: bool, ) -> Result<(), Error> { let mut input_file = String::new(); + let mut weights_file = String::new(); let mut output_file = String::new(); let mut out_type = String::from("sca"); let mut log_transform = false; @@ -223,6 +233,12 @@ impl WhiteboxTool for D8FlowAccumulation { } else { input_file = args[i + 1].to_string(); } + } else if flag_val == "-w" || flag_val == "-weights" { + if keyval { + weights_file = vec[1].to_string(); + } else { + weights_file = args[i + 1].to_string(); + } } else if flag_val == "-o" || flag_val == "-output" { if keyval { output_file = vec[1].to_string(); @@ -284,6 +300,10 @@ impl WhiteboxTool for D8FlowAccumulation { if !output_file.contains(&sep) && !output_file.contains("/") { output_file = format!("{}{}", working_directory, output_file); } + let use_weights = !weights_file.is_empty(); + if use_weights && !weights_file.contains(&sep) && !weights_file.contains("/") { + weights_file = format!("{}{}", working_directory, weights_file); + } if verbose { println!("Reading data...") @@ -474,7 +494,12 @@ impl WhiteboxTool for D8FlowAccumulation { output.configs.nodata = out_nodata; output.configs.photometric_interp = PhotometricInterpretation::Continuous; // if the input is a pointer, this may not be the case by default. output.configs.data_type = DataType::F32; - output.reinitialize_values(1.0); + if use_weights { + let weights = Raster::new(&weights_file, "r")?; + output.set_data_from_raster(&weights)?; + } else { + output.reinitialize_values(1.0); + } drop(input); // calculate the number of inflowing cells @@ -674,6 +699,9 @@ impl WhiteboxTool for D8FlowAccumulation { self.get_tool_name() )); output.add_metadata_entry(format!("Input file: {}", input_file)); + if use_weights { + output.add_metadata_entry(format!("Input weights file: {}", weights_file)); + } output.add_metadata_entry(format!("Elapsed Time (excluding I/O): {}", elapsed_time)); if verbose { diff --git a/whitebox_tools.py b/whitebox_tools.py index 4ef75f84d..f94962d98 100755 --- a/whitebox_tools.py +++ b/whitebox_tools.py @@ -4579,13 +4579,14 @@ def burn_streams_at_roads(self, dem, streams, roads, output, width=None, callbac if width is not None: args.append("--width='{}'".format(width)) return self.run_tool('burn_streams_at_roads', args, callback) # returns 1 if error - def d8_flow_accumulation(self, i, output, out_type="cells", log=False, clip=False, pntr=False, esri_pntr=False, callback=None): + def d8_flow_accumulation(self, i, output, weights=None, out_type="cells", log=False, clip=False, pntr=False, esri_pntr=False, callback=None): """Calculates a D8 flow accumulation raster from an input DEM or flow pointer. Keyword arguments: i -- Input raster DEM or D8 pointer file. output -- Output raster file. + weights -- Optional input weights raster file. out_type -- Output type; one of 'cells' (default), 'catchment area', and 'specific contributing area'. log -- Optional flag to request the output be log-transformed. clip -- Optional flag to request clipping the display max by 1%. @@ -4596,6 +4597,7 @@ def d8_flow_accumulation(self, i, output, out_type="cells", log=False, clip=Fals args = [] args.append("--input='{}'".format(i)) args.append("--output='{}'".format(output)) + if weights is not None: args.append("--weights='{}'".format(weights)) args.append("--out_type={}".format(out_type)) if log: args.append("--log") if clip: args.append("--clip")