2021-06-01 19:07:09 +02:00
-- rivermapper.lua
2021-07-28 19:31:16 +02:00
-- This file provide functions to construct the river tree from an elevation model.
-- Based on a research paper:
--
-- Cordonnier, G., Bovy, B., and Braun, J.:
-- A versatile, linear complexity algorithm for flow routing in topographies with depressions,
-- Earth Surf. Dynam., 7, 549– 562, https://doi.org/10.5194/esurf-7-549-2019, 2019.
--
-- Big thanks to them for releasing this paper under a free license ! :)
2022-01-07 14:48:36 +01:00
-- The algorithm here makes use of most of the paper's concepts, including the Planar Borůvka algorithm.
2021-07-28 19:31:16 +02:00
-- Only flow_local and accumulate_flow are custom algorithms.
2022-01-07 14:48:36 +01:00
2021-06-01 19:07:09 +02:00
local function flow_local_semirandom ( plist )
2022-01-07 14:48:36 +01:00
-- Determines how water should flow at 1 node scale.
-- The straightforward approach would be "Water will flow to the lowest of the 4 neighbours", but here water flows to one of the lower neighbours, chosen randomly, but probability depends on height difference.
-- This makes rivers better follow the curvature of the topography at large scale, and be less biased by pure N/E/S/W directions.
-- 'plist': array of downward height differences (0 if upward)
2021-06-01 19:07:09 +02:00
local sum = 0
for i = 1 , # plist do
2022-01-07 14:48:36 +01:00
sum = sum + plist [ i ] -- Sum of probabilities
2021-06-01 19:07:09 +02:00
end
2022-01-07 14:48:36 +01:00
2021-06-01 19:07:09 +02:00
if sum == 0 then
return 0
end
local r = math.random ( ) * sum
for i = 1 , # plist do
local p = plist [ i ]
if r < p then
return i
end
r = r - p
end
return 0
end
2022-01-07 14:48:36 +01:00
-- Maybe implement more flow methods in the future?
2021-06-01 19:07:09 +02:00
local flow_methods = {
semirandom = flow_local_semirandom ,
}
2022-01-07 14:48:36 +01:00
-- Applies all steps of the flow routing, to calculate flow direction for every node, and lake surface elevation.
-- It's quite a hard piece of code, but we will go step by step and explain what's going on, so stay with me and... let's goooooooo!
local function flow_routing ( dem , dirs , lakes , method ) -- 'dirs' and 'lakes' are optional tables to reuse for memory optimization, they may contain any data.
2021-06-01 19:07:09 +02:00
method = method or ' semirandom '
2021-06-03 23:30:04 +02:00
local flow_local = flow_methods [ method ] or flow_local_semirandom
2021-06-01 19:07:09 +02:00
dirs = dirs or { }
lakes = lakes or { }
-- Localize for performance
local tremove = table.remove
local mmax = math.max
local X , Y = dem.X , dem.Y
dirs.X = X
dirs.Y = Y
lakes.X = X
lakes.Y = Y
local i = 1
local dirs2 = { }
for i = 1 , X * Y do
dirs2 [ i ] = 0
end
2022-01-07 14:48:36 +01:00
----------------------------------------
-- STEP 1: Find local flow directions --
----------------------------------------
-- Use the local flow function and fill the flow direction tables
2021-06-01 19:07:09 +02:00
local singular = { }
for y = 1 , Y do
for x = 1 , X do
local zi = dem [ i ]
2022-01-07 14:48:36 +01:00
local plist = { -- Get the height difference of the 4 neighbours (and 0 if uphill)
2021-06-01 19:07:09 +02:00
y < Y and mmax ( zi - dem [ i + X ] , 0 ) or 0 , -- Southward
2021-06-04 18:24:06 +02:00
x < X and mmax ( zi - dem [ i + 1 ] , 0 ) or 0 , -- Eastward
2021-06-01 19:07:09 +02:00
y > 1 and mmax ( zi - dem [ i - X ] , 0 ) or 0 , -- Northward
2021-06-04 18:24:06 +02:00
x > 1 and mmax ( zi - dem [ i - 1 ] , 0 ) or 0 , -- Westward
2021-06-01 19:07:09 +02:00
}
local d = flow_local ( plist )
2022-01-07 14:48:36 +01:00
-- 'dirs': Direction toward which water flow
-- 'dirs2': Directions from which water comes
2021-06-01 19:07:09 +02:00
dirs [ i ] = d
2022-01-07 14:48:36 +01:00
if d == 0 then -- If water can't flow from this node, add it to the list of singular nodes that will be resolved later
2021-06-01 19:07:09 +02:00
singular [ # singular + 1 ] = i
elseif d == 1 then
2021-06-04 18:24:06 +02:00
dirs2 [ i + X ] = dirs2 [ i + X ] + 1
2021-06-01 19:07:09 +02:00
elseif d == 2 then
2021-06-04 18:24:06 +02:00
dirs2 [ i + 1 ] = dirs2 [ i + 1 ] + 2
2021-06-01 19:07:09 +02:00
elseif d == 3 then
2021-06-04 18:24:06 +02:00
dirs2 [ i - X ] = dirs2 [ i - X ] + 4
2021-06-01 19:07:09 +02:00
elseif d == 4 then
2021-06-04 18:24:06 +02:00
dirs2 [ i - 1 ] = dirs2 [ i - 1 ] + 8
2021-06-01 19:07:09 +02:00
end
i = i + 1
end
end
2022-01-07 14:48:36 +01:00
--------------------------------------
-- STEP 2: Compute basins and links --
--------------------------------------
-- Now water can flow until it reaches a singular node (which is in most cases the bottom of a depression)
-- We will calculate the drainage basin of every singular node (all the nodes from which the water will flow in this singular node, directly or indirectly), make an adjacency list of basins, and find the lowest pass between each pair of adjacent basins (they are potential lake outlets)
2021-06-01 19:07:09 +02:00
local nbasins = # singular
local basin_id = { }
local links = { }
local basin_links
2022-01-07 14:48:36 +01:00
-- Function to analyse a link between two nodes
2021-06-01 19:07:09 +02:00
local function add_link ( i1 , i2 , b1 , isY )
2022-01-07 14:48:36 +01:00
-- i1, i2: coordinates of two nodes
-- b1: basin that contains i1
-- isY: whether the link is in Y direction
2021-06-01 19:07:09 +02:00
local b2
2022-01-07 14:48:36 +01:00
-- Note that basin number #0 represents the outside of the map; or if the coordinate is inside the map, means that the basin number is uninitialized.
if i2 == 0 then -- If outside the map
2021-06-01 19:07:09 +02:00
b2 = 0
else
b2 = basin_id [ i2 ]
2022-01-07 14:48:36 +01:00
if b2 == 0 then -- If basin of i2 is not already computed, skip
2021-06-01 19:07:09 +02:00
return
end
end
2022-01-07 14:48:36 +01:00
if b2 ~= b1 then -- If these two nodes don't belong to the same basin, we have found a link between two adjacent basins
local elev = i2 == 0 and dem [ i1 ] or mmax ( dem [ i1 ] , dem [ i2 ] ) -- Elevation of the highest of the two sides of the link (or only i1 if b2 is map outside)
2021-06-01 19:07:09 +02:00
local l2 = basin_links [ b2 ]
if not l2 then
l2 = { }
basin_links [ b2 ] = l2
end
2022-01-07 14:48:36 +01:00
if not l2.elev or l2.elev > elev then -- If this link is lower than the lowest registered link between these two basins, register it as the new lowest pass
2021-06-01 19:07:09 +02:00
l2.elev = elev
l2.i = mmax ( i1 , i2 )
l2.is_y = isY
l2 [ 1 ] = b2
l2 [ 2 ] = b1
end
end
end
for i = 1 , X * Y do
basin_id [ i ] = 0
end
2022-01-07 14:48:36 +01:00
2021-06-01 19:07:09 +02:00
for ib = 1 , nbasins do
2022-01-07 14:48:36 +01:00
-- Here we will recursively search upstream from the singular node to determine its drainage basin
local queue = { singular [ ib ] } -- Start with the singular node, then this queue will be filled with water donors neighbours
2021-06-01 19:07:09 +02:00
basin_links = { }
links [ # links + 1 ] = basin_links
while # queue > 0 do
local i = tremove ( queue )
basin_id [ i ] = ib
2022-01-07 14:48:36 +01:00
local d = dirs2 [ i ] -- Get the directions water is coming from
2021-06-01 19:07:09 +02:00
2022-01-07 14:48:36 +01:00
-- Iterate through the 4 directions
if d >= 8 then -- River coming from the East
2021-06-01 19:07:09 +02:00
d = d - 8
queue [ # queue + 1 ] = i + 1
2022-01-07 14:48:36 +01:00
-- If no river is coming from the East, we might be at the limit of two basins, thus we need to test adjacency.
2021-06-01 19:07:09 +02:00
elseif i % X > 0 then
add_link ( i , i + 1 , ib , false )
2022-01-07 14:48:36 +01:00
else -- If the eastern neighbour is outside the map
2021-06-01 19:07:09 +02:00
add_link ( i , 0 , ib , false )
end
2022-01-07 14:48:36 +01:00
if d >= 4 then -- River coming from the South
2021-06-04 18:24:06 +02:00
d = d - 4
queue [ # queue + 1 ] = i + X
elseif i <= X * ( Y - 1 ) then
add_link ( i , i + X , ib , true )
2021-06-01 19:07:09 +02:00
else
add_link ( i , 0 , ib , true )
end
2022-01-07 14:48:36 +01:00
if d >= 2 then -- River coming from the West
2021-06-04 18:24:06 +02:00
d = d - 2
2021-06-01 19:07:09 +02:00
queue [ # queue + 1 ] = i - 1
elseif i % X ~= 1 then
add_link ( i , i - 1 , ib , false )
else
add_link ( i , 0 , ib , false )
end
2021-06-04 18:24:06 +02:00
2022-01-07 14:48:36 +01:00
if d >= 1 then -- River coming from the North
2021-06-04 18:24:06 +02:00
queue [ # queue + 1 ] = i - X
elseif i > X then
add_link ( i , i - X , ib , true )
else
add_link ( i , 0 , ib , true )
end
2021-06-01 19:07:09 +02:00
end
end
dirs2 = nil
links [ 0 ] = { }
local nlinks = { }
for i = 0 , nbasins do
nlinks [ i ] = 0
end
2022-01-07 14:48:36 +01:00
-- Iterate through pairs of adjacent basins, and make the links reciprocal
2021-06-01 19:07:09 +02:00
for ib1 = 1 , # links do
for ib2 , link in pairs ( links [ ib1 ] ) do
if ib2 < ib1 then
links [ ib2 ] [ ib1 ] = link
nlinks [ ib1 ] = nlinks [ ib1 ] + 1
nlinks [ ib2 ] = nlinks [ ib2 ] + 1
end
end
end
2022-01-07 14:48:36 +01:00
-----------------------------------------------------
-- STEP 3: Compute minimal spanning tree of basins --
-----------------------------------------------------
-- We've got an adjacency list of basins with the elevation of their links.
-- We will build a minimal spanning tree of the basins (where costs are the elevation of the links). As demonstrated by Cordonnier et al., this finds the outlets of the basins, where water would naturally flow. This does not tell in which direction water is flowing, however.
-- We will use a version of Borůvka's algorithm, with Mareš' optimizations to approach linear complexity (see paper).
-- The concept of Borůvka's algorithm is to take elements and merge them with their lowest neighbour, until all elements are merged.
-- Mareš' optimizations mainly consist in skipping elements that have over 8 links, until extra links are removed when other elements are merged.
-- Note that for this step we are only working on basins, not grid nodes.
2021-06-01 19:07:09 +02:00
local lowlevel = { }
for i , n in pairs ( nlinks ) do
if n <= 8 then
lowlevel [ i ] = links [ i ]
end
end
2021-06-03 23:30:04 +02:00
local basin_graph = { }
2021-06-01 19:07:09 +02:00
for n = 1 , nbasins do
2022-01-07 14:48:36 +01:00
-- Iterate in lowlevel but its contents may change during the loop
-- 'next' called with only one argument always returns an element if table is not empty
2021-06-01 19:07:09 +02:00
local b1 , lnk1 = next ( lowlevel )
lowlevel [ b1 ] = nil
local b2
local lowest = math.huge
local lnk1 = links [ b1 ]
local i = 0
2022-01-07 14:48:36 +01:00
-- Look for lowest link
2021-06-01 19:07:09 +02:00
for bn , bdata in pairs ( lnk1 ) do
i = i + 1
if bdata.elev < lowest then
lowest = bdata.elev
b2 = bn
end
end
2022-01-07 14:48:36 +01:00
-- Add link to the graph, in both directions
2021-06-01 19:07:09 +02:00
local bound = lnk1 [ b2 ]
local bb1 , bb2 = bound [ 1 ] , bound [ 2 ]
if not basin_graph [ bb1 ] then
basin_graph [ bb1 ] = { }
end
if not basin_graph [ bb2 ] then
basin_graph [ bb2 ] = { }
end
basin_graph [ bb1 ] [ bb2 ] = bound
basin_graph [ bb2 ] [ bb1 ] = bound
-- Merge basin b1 into b2
local lnk2 = links [ b2 ]
-- First, remove the link between b1 and b2
lnk1 [ b2 ] = nil
lnk2 [ b1 ] = nil
nlinks [ b2 ] = nlinks [ b2 ] - 1
2022-01-07 14:48:36 +01:00
-- When the number of links is changing, we need to check whether the basin can be added to / removed from 'lowlevel'
2021-06-01 19:07:09 +02:00
if nlinks [ b2 ] == 8 then
lowlevel [ b2 ] = lnk2
end
-- Look for basin 1's neighbours, and add them to basin 2 if they have a lower pass
for bn , bdata in pairs ( lnk1 ) do
local lnkn = links [ bn ]
lnkn [ b1 ] = nil
2022-01-07 14:48:36 +01:00
if lnkn [ b2 ] then -- If bassin bn is also linked to b2
nlinks [ bn ] = nlinks [ bn ] - 1 -- Then bassin bn is losing a link because it keeps only one link toward b1/b2 after the merge
2021-06-01 19:07:09 +02:00
if nlinks [ bn ] == 8 then
lowlevel [ bn ] = lnkn
end
2022-01-07 14:48:36 +01:00
else -- If bn was linked to b1 but not to b2
nlinks [ b2 ] = nlinks [ b2 ] + 1 -- Then b2 is gaining a link to bn because of the merge
2021-06-01 19:07:09 +02:00
if nlinks [ b2 ] == 9 then
lowlevel [ b2 ] = nil
end
end
2022-01-07 14:48:36 +01:00
if not lnkn [ b2 ] or lnkn [ b2 ] . elev > bdata.elev then -- If the link b1-bn will become the new lowest link between b2 and bn, redirect the link to b2
2021-06-01 19:07:09 +02:00
lnkn [ b2 ] = bdata
lnk2 [ bn ] = bdata
end
end
end
2022-01-07 14:48:36 +01:00
--------------------------------------------------------------
-- STEP 4: Orient basin graph, and grid nodes inside basins --
--------------------------------------------------------------
-- We will finally solve those freaking singular nodes.
-- To orient the basin graph, we will consider that the ultimate basin water should flow into is the map outside (basin #0). We will start from it and recursively walk upstream to the neighbouring basins, using only links that are in the minimal spanning tree. This gives the flow direction of the links, and thus, the outlet of every basin.
-- This will also give lake elevation, which is the highest link encountered between map outside and the given basin on the spanning tree.
-- And within each basin, we need to modify flow directions to connect the singular node to the outlet.
2021-06-01 19:07:09 +02:00
local queue = { [ 0 ] = - math.huge }
local basin_lake = { }
for n = 1 , nbasins do
basin_lake [ n ] = 0
end
local reverse = { 3 , 4 , 1 , 2 , [ 0 ] = 0 }
for n = 1 , nbasins do
2022-01-07 14:48:36 +01:00
local b1 , elev1 = next ( queue ) -- Pop from queue
2021-06-01 19:07:09 +02:00
queue [ b1 ] = nil
basin_lake [ b1 ] = elev1
2022-01-07 14:48:36 +01:00
-- Iterate through b1's neighbours (according to the spanning tree)
2021-06-01 19:07:09 +02:00
for b2 , bound in pairs ( basin_graph [ b1 ] ) do
-- Make b2 flow into b1
2022-01-07 14:48:36 +01:00
local i = bound.i -- Get the coordinate of the link (which is the basin's outlet)
local dir = bound.is_y and 3 or 4 -- And get the direction (S/E/N/W)
2021-06-01 19:07:09 +02:00
if basin_id [ i ] ~= b2 then
dir = dir - 2
2022-01-07 14:48:36 +01:00
-- Coordinate 'i' refers to the side of the link with the highest X/Y position. In case it is in the wrong basin, take the other side by decrementing by one row/column.
2021-06-01 19:07:09 +02:00
if bound.is_y then
i = i - X
else
i = i - 1
end
elseif b1 == 0 then
dir = 0
end
2021-06-25 21:05:14 +02:00
2022-01-07 14:48:36 +01:00
-- Use the flow directions computed in STEP 2 to find the route from the outlet position to the singular node, and reverse this route to make the singular node flow into the outlet
-- This can make the river flow uphill, which may seem unnatural, but it can only happen below a lake (because outlet elevation defines lake surface elevation)
2021-06-01 19:07:09 +02:00
repeat
2022-01-07 14:48:36 +01:00
-- Assign i's direction to 'dir', and get i's former direction
2021-06-01 19:07:09 +02:00
dir , dirs [ i ] = dirs [ i ] , dir
2022-01-07 14:48:36 +01:00
-- Move i by following its former flow direction (downstream)
2021-06-01 19:07:09 +02:00
if dir == 1 then
i = i + X
2021-06-04 18:24:06 +02:00
elseif dir == 2 then
i = i + 1
2021-06-01 19:07:09 +02:00
elseif dir == 3 then
i = i - X
2021-06-04 18:24:06 +02:00
elseif dir == 4 then
i = i - 1
2021-06-01 19:07:09 +02:00
end
2022-01-07 14:48:36 +01:00
-- Reverse the flow direction for the next node, which will flow into i
2021-06-01 19:07:09 +02:00
dir = reverse [ dir ]
2022-01-07 14:48:36 +01:00
until dir == 0 -- Stop when reaching the singular node
-- Add basin b2 into the queue, and keep the highest link elevation, that will define the elevation of the lake in b2
2021-06-01 19:07:09 +02:00
queue [ b2 ] = mmax ( elev1 , bound.elev )
2022-01-07 14:48:36 +01:00
-- Remove b1 from b2's neighbours to avoid coming back to b1
2021-06-01 19:07:09 +02:00
basin_graph [ b2 ] [ b1 ] = nil
end
basin_graph [ b1 ] = nil
end
2022-01-07 14:48:36 +01:00
-- Every node will be assigned the lake elevation of the basin it belongs to.
-- If lake elevation is lower than ground elevation, it simply means that there is no lake here.
2021-06-01 19:07:09 +02:00
for i = 1 , X * Y do
lakes [ i ] = basin_lake [ basin_id [ i ] ]
end
2022-01-07 14:48:36 +01:00
-- That's it!
2021-06-01 19:07:09 +02:00
return dirs , lakes
end
2022-01-07 14:48:36 +01:00
2021-06-01 19:07:09 +02:00
local function accumulate ( dirs , waterq )
2022-01-07 14:48:36 +01:00
-- Calculates the river flow by determining the surface of the catchment area for every node
-- This means: how many nodes will give their water to that given node, directly or indirectly?
-- This is obtained by following rivers downstream and summing up the flow of every tributary, starting with a value of 1 at the sources.
-- This will give non-zero values for every node but only large values will be considered to be rivers.
2021-06-01 19:07:09 +02:00
waterq = waterq or { }
local X , Y = dirs.X , dirs.Y
local ndonors = { }
local waterq = { X = X , Y = Y }
for i = 1 , X * Y do
ndonors [ i ] = 0
waterq [ i ] = 1
end
2022-01-07 14:48:36 +01:00
-- Calculate the number of direct donors
2021-06-01 19:07:09 +02:00
for i1 = 1 , X * Y do
local i2
local dir = dirs [ i1 ]
if dir == 1 then
i2 = i1 + X
2021-06-04 18:24:06 +02:00
elseif dir == 2 then
i2 = i1 + 1
2021-06-01 19:07:09 +02:00
elseif dir == 3 then
i2 = i1 - X
2021-06-04 18:24:06 +02:00
elseif dir == 4 then
i2 = i1 - 1
2021-06-01 19:07:09 +02:00
end
if i2 then
ndonors [ i2 ] = ndonors [ i2 ] + 1
end
end
for i1 = 1 , X * Y do
2022-01-07 14:48:36 +01:00
-- Find sources (nodes that have no donor)
2021-06-01 19:07:09 +02:00
if ndonors [ i1 ] == 0 then
local i2 = i1
local dir = dirs [ i2 ]
local w = waterq [ i2 ]
2022-01-07 14:48:36 +01:00
-- Follow the water flow downstream: move 'i2' to the next node according to its flow direction
2021-06-01 19:07:09 +02:00
while dir > 0 do
if dir == 1 then
i2 = i2 + X
2021-06-04 18:24:06 +02:00
elseif dir == 2 then
i2 = i2 + 1
2021-06-01 19:07:09 +02:00
elseif dir == 3 then
i2 = i2 - X
2021-06-04 18:24:06 +02:00
elseif dir == 4 then
i2 = i2 - 1
2021-06-01 19:07:09 +02:00
end
2022-01-07 14:48:36 +01:00
-- Increment the water quantity of i2
2021-06-01 19:07:09 +02:00
w = w + waterq [ i2 ]
waterq [ i2 ] = w
2022-01-07 14:48:36 +01:00
-- Stop on an unresolved confluence (node with >1 donors) and decrease the number of remaining donors
-- When the ndonors of a confluence has decreased to 1, it means that its water quantity has already been incremented by its tributaries, so it can be resolved like a standard river section. However, do not decrease ndonors to zero to avoid considering it as a source.
2021-06-01 19:07:09 +02:00
if ndonors [ i2 ] > 1 then
ndonors [ i2 ] = ndonors [ i2 ] - 1
break
end
dir = dirs [ i2 ]
end
end
end
return waterq
end
return {
flow_routing = flow_routing ,
accumulate = accumulate ,
flow_methods = flow_methods ,
}