# Cumulative cost upstream a raster stream network with GRASS

I have a river network in raster and locations of many dams on the river. In the image above, the BLUE point represent one random cell. Dam 1 is downstream to the cell.

I am looking to calculate the cumulative installed capacity of all dam upstream each cell. Of course, the installed capacity data are collected.

In the example, the sum of Dam2 and Dam3 capacity should be assigned to the blue cell. And I want the result in raster.

Does anyone know how to solve the problem using GRASS?

Another solution, in addtionto Micha's approach, is to use the flow parameter in the tool `r.watershed`. For example, you could transform all your dams into a single raster map (0=no dam, 1=dam) and then use that map the following

``````r.watershed elevation=dem flow=dam_raster_map accumulation=cumulative_dams
``````

This approach accumulates all the dams in downstream direction (along the flow path). Assuming all your points of interest (blue points) are on the stream raster (i.e. same caveat as in Micha's approach: the target "random cell" must lie exactly on the stream network) then you could just use an `r.mapcalc` approach to only get the info from your new cumulative_dams map at your blue points or you could use `r.to.vect` to sample the raster at specific vector points.

• Thanks @Johannes for this solution, it works well! – BBR Nov 12 '18 at 9:08

This three step procedure might work for you. (One caveat: the target "random cell" must lie exactly on the stream network) Supposing you have a DEM of your area called "dem", and a vector points map "dams" with an attribute "capacity". And assuming you know the x,y coordinates of the random cell as random_x and random_y:

``````# Requisite region settings
g.region rast=dem
# Create a flow direction grid from the dem (do this only once)
r.watershed elevation=dem drainage=flowdir thresh=<suitable threshold for basins>
# See r.watershed man page for details about threshold setting

# delineate the upstream basin above your random cell
r.water.outlet input=flowdir output=basin coord=random_x,random_y --overwrite

# Select upstream dams that are within this basin
v.select ain=dams bin=basin output=upstream_dams operator=within --overwrite

# Now summarize the capacity of upstream dams
SQL="SELECT SUM(capacity) FROM upstream_dams;"
db.select sql=\$SQL
``````
• Thanks @Micha for this solution. It would definitely work by cell! But I need to have a raster result and the raster is very large. The solution by Johaness just suits my need. Many thanks! – BBR Nov 12 '18 at 9:06