# Cumulative value/cost along shortest distance roads in QGIS

I have a road network linking multiple (ca 1830) agricultural production regions to several ports (ca 12). (lines in the pic below are roads, small black dots centroids for producing regions and larger dots ports) I would like to calculate the road path of shortest distance from each production region to each port. For each road connection I would then like to calculate a cumulative "cost" (really risk in this case) for travelling that road based on spatial distribution of flood risk (I have the risk data in both raster and vector format), values ranging from 0 to 1 (low to high risk: white to dark blue in the image below). I have tried calculating nearest way to port using QNEAT3, but it only provides me with distance, and not the pathway with the physical road travelled (and so I cant use it for calculating the cumulative risk value). and even if it did provide me with the actual physical shortest roads, I don't seem to find a great way to calculate cumulative values along multiple lines.

Does anyone know how to solve this?

• If you're cool with using the python editor, you should look into the networkX package. It will return a list of nodes comprising each shortest-distance path (and other cool stuff). You could also weight each link by the risk and compute shortest paths that minimize risk. I would be surprised if QNEAT3 doesn't somehow return the pathways, but I know nothing about it. – Jon Feb 27 '19 at 17:17
• Hi Jon! Thanks for the responce. Unfortunately i'm not python litterate (yet). QNEAT calculate shortest routes with pathways only betwwen two single points, which in this case would leave me doing 1830 separate analysis unfortunalely... – Frida_L Feb 27 '19 at 17:23
• I assume you have a network of roads, and each port is a node in the network and each production region is also a node in the network. You will have to compute the shortest path from each production node to the nearest port, if I understand correctly. networkX has tools that are great for this: networkx.github.io/documentation/stable/reference/algorithms/… That's really all I can offer since I don't know QNEAT, but if I were you I'd scour the QNEAT documentation. – Jon Feb 27 '19 at 17:28
• Have you tried the `shortest path` algorithm as explained in the Network Analysis Lesson of the QGIS Training Manual? It has batch processing. – csk Feb 27 '19 at 17:29
• Could incorporating the flooding-risk values in the cost attribute of the street network be a viable solution? By mixing distance cost with flood-risk cost you would be able to retrieve all cumulative costs for least-risky/shortest paths using the m:n OD-Matrix tool provided by QNEAT3. But this would also take into account the flood risk values at the routing level (eg. it would be a mix of least flood risk and shortest path). I did not include an m:n shortest path algorithm in QNEAT3, and QGIS does not support that kind of routing out of the box (only 1:n relations are possible). – root676 Feb 28 '19 at 7:42

There is a grass tool to calculate the whole distance matrix at once: v.net.allpairs. This tool asks for a layer of "roads" and a point layer an it calculates all the matrix between points. After ir you just need to remove the lines that go to starting points al leave just the lines that go to ports.

The problem with this tool is how to identify the points as this doens't ask for your id within the layer. But there is a way to solve it as it calculates sequentially by rownumber, so, you can first, use this as you known id to assign correctly those lines to each pair of points.

Here you have your layers: points, ports and roads: Now you have to merge your layers in an unique one that will work as index for you. Don't trust the fid, use your own attributes. After setting those, merge and add a new column using calculator. The result must be row_number (@row_number). Execute v.net.allpairs over this layer and set the threshold to connect points to the network: The result is the whole distance matrix with all points serving as "from" and "to" and the cost (measured in meters). Now you can several ways to link those "from" and "to" numbers to the indexer you created (you can join fields, filter & remove, etc.). The thing that you have to keep in mind is that "from_cat" must be a point and the "to_cat" must be a port. If you finally dissolve by from/to/cost you will get all the paths, in this case, 10 points for 5 ports should result in 50 paths. Once you have all this paths you can do several things but I think is prefearable to merge with the polygons with your costs. This would split each line in portions defined by the polygons (and their cost values). Then, for each path you can calculate matematically the cost, for example, multiplying the length of the segment by the risk per meter.

May be this is overexplained, but you can build a model with the indexer and the grass tool and all will be easier.