3

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:

select
   (st_dump(st_linemerge(st_union(a.geom)))).geom
from
    lines as a
left join
    lines as b
on
    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.

Mark
  • 103
  • 5
  • 1
    You should look at st_clusterdbscan (more complicated) or st_clusterwithin. https://postgis.net/docs/ST_ClusterWithin.html – jbalk Oct 01 '21 at 23:11
  • 1
    also see this https://gis.stackexchange.com/questions/408695/compound-contiguous-links-to-lines-in-a-network-and-find-isolated-lines/408716#408716 – jbalk Oct 01 '21 at 23:16
  • 1
    ST_TOUCHES will return false if if the touch will not be on a vertex. So you have to split_with_lines before to get a 100% – eurojam Oct 02 '21 at 05:33
  • 1
    Can you add a screenshot showing your data? – BERA Oct 02 '21 at 07:55

0 Answers0