0

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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Foaly
  • 323
  • 3
  • 12
  • 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 Sep 12 '19 at 11:13
  • Please provide more detail about the data structure/attribute – J.R Sep 12 '19 at 11:46
  • https://gis.stackexchange.com/questions/198317/join-merge-lines-that-touch-and-intersect-in-qgis

    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 Sep 12 '19 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 Sep 12 '19 at 13:38
  • try dissolve followed by multipart to singleparts – csk Sep 12 '19 at 19:18
  • Thank you for your help guys :-) – Foaly Sep 17 '19 at 17:06

1 Answers1

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")
ImanolUr
  • 1,102
  • 1
  • 10
  • 21