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.
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.
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")
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:56dissolvefollowed bymultipart to singleparts– csk Sep 12 '19 at 19:18