Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add --weights option to D8FlowAccumulation tool #271

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 29 additions & 1 deletion whitebox-tools-app/src/tools/hydro_analysis/d8_flow_accum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()],
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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...")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 3 additions & 1 deletion whitebox_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -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%.
Expand All @@ -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")
Expand Down