I have a network project in QGIS:
Where arcs is a LineString layer, but all the arcs must be connected. I was able to detect subnetworks using the Disconnected Islands plugin which populates a NetworkGrp field.
The nodes layer is just the output of Extract Vertices algorithm, and wanted layer is what i need.
The conditions are:
- If there are 3 subnetworks, i need 2 lines connecting them. In general, one line less than the number of groups is needed.
- The lines can go from nearest points of each subnetwork group, without the need of a node there (preferred way, but they can go from one node to another one). Next to find the lines, i can clean the topology to add the required nodes and break the arcs accordingly.
- I need to solve this for a lot of unconnected networks, so i can't do it manually.
- I want to solve it in QGIS (using 3.22 right now), but can use GRASS GIS if needed. Python solutions are also welcome.
- There isn't a distance threshold, i just need to connect all groups in a layer. But i don't want to connect arcs or nodes of the same network subgroup.
- I need new arcs, but if a snap method is the only solution i can accept it.
The arcs layer to test can be downloaded from this link.


"group"with"NetworkGrp", replaced@layerwith''arcs'', replacedfilter:=group<>'withfilter:=NetworkGrp<>'and replaced all thecase ... endsentence with@lineto return all the lines, but it returns features without geometry. Also, I don't understand something likefilter:=NetworkGrp <> "NetworkGrp"can return something different to False. – Gabriel De Luca Dec 21 '22 at 00:17filter:=NetworkGrp <> 0if my current feature is in that group. But I think overlay_nearest function can't be used against the same layer (https://github.com/qgis/QGIS/issues/47201), because it does not evaluate a feature after being the current one? I will try against a copy. – Gabriel De Luca Dec 21 '22 at 00:41overlay_nearestworks on the same layer, for this I added thelimit:=10part. Depending on how many features you have, you might want to change that. As you see in the screenshot, in my case it works - also when using Geometry by expression. Can't test right now with your data, will come back later. – Babel Dec 21 '22 at 06:06filter:=group<>'withfilter:=NetworkGrp<>'. When you then use a value apt for your data in the case condition of 13000 (when length(@line) < 13000), then you get exactly two connecting line - see updated expression to match exactly your use case. It works for me in Geometry generator as well as Geometry by expression, see updated screenshot in the answer. – Babel Dec 21 '22 at 07:33limitin theoverlay_nearestfunction. It will automatically create the correct lines. Tested with your dataset in Geoemtry Generator (screenshot) as well as Geometry by Expression: both work. – Babel Dec 21 '22 at 07:56