I'm looking for a script or plugin to merge lines that are touching in a dataset. There are thousands of features so it would be time and labor intensive to try to give each connected set of lines a common ID or to manually merge each "set" of lines.

I tried the following script:

    lines as a
left join
    lines as b
    st_touches(a.geom, b.geom)
group by
    st_touches(a.geom, b.geom)

Which definitely consolidated some of the segments (6k segments down to ~2k), but it didn't entirely merge all touching segments. Any suggestions for how to address this? I saw there used to be a QGIS plugin for merging multiple lines like this, but it seems like it's for an older version (I'm on 3.16)

I found a similar question (here) that says try st_snaptogrid, which i added as an "update" statement at the beginning of my query. This further reduced the features, but only by 100 or so, and when I tried to do the query in QGIS to test if the touching features were actually joined, I got an error. I know that there should be 200-300 features, so I have a feeeling that st_snaptogrid only fixed a handful of segments.



Your Answer

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

Browse other questions tagged or ask your own question.