From ecba12698320ecbbd7bdca47af5252f11f15d992 Mon Sep 17 00:00:00 2001 From: Gael-de-Sailly Date: Sat, 11 Apr 2020 14:27:56 +0200 Subject: [PATCH] Add a small random number to DEM when calculating flow direction, to smooth flat areas --- rivermapper.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/rivermapper.py b/rivermapper.py index 52b3aa4..ab35d55 100644 --- a/rivermapper.py +++ b/rivermapper.py @@ -18,11 +18,13 @@ neighbours_dirs = np.array([ neighbours_pattern = neighbours_dirs > 0 -def flow_dirs_lakes(dem): +def flow_dirs_lakes(dem, random=0.0625): (Y, X) = dem.shape dem_margin = np.zeros((Y+2, X+2)) dem_margin[1:-1,1:-1] = dem + if random > 0: + dem_margin += np.random.random(dem_margin.shape) * random # Initialize: list map borders borders = [] @@ -72,7 +74,7 @@ def accumulate(dirs, dem=None): (Y, X) = dirs.shape dirs_margin = np.zeros((Y+2,X+2))-1 dirs_margin[1:-1,1:-1] = dirs - quantity = np.zeros((Y, X), dtype='u4') + quantity = np.zeros((Y, X), dtype='i4') def calculate_quantity(y, x): if quantity[y,x] > 0: