3

I have the result of a network analysis (using the QNEAT3 plugin tool 'OD Matrix from Layers to Lines (m:n)') showing the shortest paths from set points, via the network, to multiple end points. This has created several duplicate "origin_id"'s showing the cost to each end point

I am looking to dissolve the result based on the "origin_id" but keeping the value with the lowest "total_cost". It feels as though there should be a simple tool for this, but I cannot find it.

I also thought I'd add an example of what it looks like for you, but since images do not seem to work properly here, please see the crude one below. It consists of two endpoints with two different origin points, and I wish to keep the lowest value in the "total_cost".

origin_id | destination_id | total_cost
24505  | 441       | 283,69
24505  | 442       | 320,74
24623  | 441       | 335,08
24623  | 442       | 331,4

In this example, I would like to keep 24505/283,69 and 24623/331,4 as they are the lowest values for each "group" of origin_id.

1
  • 1
    Do you want to Dissolve or just extract the least cost paths? Commented Mar 31 at 13:57

3 Answers 3

3

Apart from the solution proposed, I would suggest not to use OD matrix at all for your task. You can use QGIS native Shortest path (point to layer) algorithm and run it in batch mode, once for each start, point, and set the layer with destinations as Vector layer with end points.

To run the algorithm in batch mode for each point of the layer start, in batch mode dialog window for Start point click Autofill... > Add values by expression... and insert this expression (replace start with your layer's name):

 aggregate( 'start', 'array_agg', @geometry) 

For all other inputs, fill in the first row, then click Autofill... > Fill Down to copy the values to the following lines.

enter image description here

2

Use select by expression with this expression:

total_cost=array_sort (array_agg (total_cost,group_by:=origin_id))[1] 

Then invert the selection and delete the selected features.

Explanation: The expression creates an array (list) of all total_cost values, grouped by origin_id with array_agg() and sorts it in ascending order with array_sort(). It gets the 2nd element with index operator \[1\] (the first one is alway 0) to get the smallest value. Selected is the feature which corresponds to this value.

enter image description here

1

You can select or extract them with the expression:

"total_cost"= minimum( expression:="total_cost", group_by:="origin_id") 

enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.