[GIS] Find overlapping lines in same layer

lineoverlapping-featuresqgistopology

I try to identify overlapping lines in the same layer with QGIS.

My problem is that the lines are the same shape but not the same length. Maybe you can understand it with the picture below.

These are two lines and I want something that shows me that the blue line overlaps the orange one. I have a huge network of lines and a visible solution doesn't work.

I tried topology rules with QGIS but there is no suitable rule for them. The blue line is not a duplicate of the orange one. Then I tried the Check Geometries Plugin but it doesn't work either, because the lines don't intersect.

Unfortunately, I couldn't find a similar question. So, does anyone have an idea? Maybe with a python script? I'm not good enough to write a script by myself.

enter image description here

Best Answer

(update)

For finding overlap (and not report lines that cross or touch) within a layer, it is good advise to use the QGIS DB Manager and SQL. For instance:

select l1.id as id1, l2.id as id2,
  st_intersection(l1.geometry, l2.geometry) as geometry
  from lines l1, lines l2
  where
    (st_overlaps(l1.geometry, l2.geometry) or
     st_contains(l1.geometry, l2.geometry) or
     st_within(l1.geometry, l2.geometry)
    )
     and l1.id < l2.id;

enter image description here

A more elegant solution is to use ST_Relate with the DE9IM matrix '1********' (the overlap between the interiors of the lines is 1D - a line):

select l1.id as id1, l2.id as id2,
       st_intersection(l1.geometry, l2.geometry) as pure_overlaps
from lines l1, lines l2
where l1.id < l2.id and
      st_relate(l1.geometry, l2.geometry, '1********');

enter image description here

For finding overlapping lines between two layers QGIS has more to offer. You can identify the lines that overlap by using the QGIS Intersection (overlay) algorithm.

  1. Make sure that both layers have identifying attributes (e.g. one attribute with the name id).
  2. Select the first line layer as Input layer
  3. Select the second line layer as Overlay layer
  4. Specify other_ in the Overlay fields prefix to easily differentiate the attributes/fields from the two layers in the result.

The result will only contain the overlapping segments, and if you open the attribute table of the result layer, you will be able to see which lines from the second layer overlap the first one by looking at the field other_id from the second layer.

Lines that only touch will not "match", so only truly overlapping lines are found.

An even more flexible alternative is the QGIS Join attributes by location algorithm. Here you can specify the type of relationship, and overlaps is one of them.

The resulting layer contains joined line geometries, with attributes from the two overlapping lines. If the attributes of the second line layer are NULL in the result, there are no lines in the second layer that overlap this line from the first layer.

Related Question