I have a complex hydrographic layer where all the hydrographic lines are separate even if it's from the same river.

How do I select all the vectors who are touching themselves without select vector from another river using automation ?

I'm on Qgis.

  • 1
    How does your attribute table look like? Are all features, which belong to the same river, marked accordingly, e.g. by an ID or the rivers name?
    – Erik
    Commented Sep 12, 2019 at 11:13
  • Please provide more detail about the data structure/attribute
    – J.R
    Commented Sep 12, 2019 at 11:46
  • gis.stackexchange.com/questions/198317/… Please take a look at this. If your vectors are lines, this might help you in merging and then grouping by the "river" attribute.
    – raaj
    Commented Sep 12, 2019 at 12:56
  • All the ID are differents. Actually, my problem is exactly the same as the raaj's link. Problem is, the script from your link is not working on QGIS3 ...
    – Foaly
    Commented Sep 12, 2019 at 13:38
  • try dissolve followed by multipart to singleparts
    – csk
    Commented Sep 12, 2019 at 19:18

1 Answer 1


If I understand correctly, there is not in attribute table anything that identifies the rivers by name or something. So keeping information from the table does not seem an issue either. Otherwise, maybe a spatial join could be tried to join that information afterwards.

This code with python, geopandas and shapely's ops.linemerge should do the trick.

import geopandas as gpd
from shapely.ops import linemerge

def merge_lines():
    lines_gdf = gpd.read_file("you_file.geojson") # or .shp or wathever
    geometries = list(lines_gdf.geometry)
    merged_lines = linemerge(geometries)
    lines_dict = {}
    for i, line in enumerate(merged_lines):
        lines_dict[i] = gpd.GeoSeries({
            'id': i,
            'geometry': line

    new_lines_gdf = gpd.GeoDataFrame(lines_dict)
    new_lines_gdf.to_file("your_new_file.geojson", driver="GeoJSON")
  • Okay, you have to tell me where to write this code, because I'm a total newbie in QGIS :D
    – Foaly
    Commented Sep 12, 2019 at 14:34
  • 1
    That's with python as stand alone, outside of QGIS. For automation tends to be faster and easier.
    – ImanolUr
    Commented Sep 12, 2019 at 14:58
  • Ok, I'm gonna try this soon ;) Thank you !
    – Foaly
    Commented Sep 17, 2019 at 17:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.