4

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

3

5 Answers 5

5

(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.

5
  • 1
    He is only working with one layer
    – Jakob
    Mar 16, 2020 at 10:35
  • @Jakob What if he's a she? Or within that range? Or outside?
    – geozelot
    Mar 16, 2020 at 11:15
  • Then I might be wrong, not the first or last time ...
    – Jakob
    Mar 16, 2020 at 11:20
  • @Jakob fair enough ,) "OP is working with one layer" may be a good way to be on the safe side?
    – geozelot
    Mar 16, 2020 at 11:23
  • Safe side for me is not to jaywalk when a truck is parsing.
    – Jakob
    Mar 16, 2020 at 11:28
2

If you want to test the overlap on the same layer, you can use SQL to find which parts of the lines do overlap.

Open Databasemanager and go to Virtual Layers.

Open an SQL-window and paste following code:

select st_intersection(l1.geometry, l2.geometry) as geometry, l1.fid as l1_id, l2.fid as l2_id 
from line_layer as l1, line_layer as l2
where l1.fid <> l2.fid and covers(l1.geometry, l2.geometry)

Change twice line_layer to your layername.
And change four times fid to the ID of your layer

Check 'load as new layer'

The result gives you the shared geometry and the id of the two overlapping lines.

1

You can use a QGIS Virtual Layer to both get an attribute table with id of the geometries covering each other and maps showing where the geometry cover is.

You can make a new Virtual Layer from the Data Source Manager. Use this SQL code:

select m1.fid id1, m2.fid id2, st_length(m1.geometry) length1, st_length(m2.geometry) length2, m1.geometry 
from mylines m1
inner join mylines m2 on st_covers(m1.geometry, m2.geometry)
where m1.fid <> m2.fid

where
mylines is you layername,
fid is a unique id in your layer,
id1 and id2 are just alias for fid.

Length will tell you the which one of the geometries are the smallest. Pictured in the map is m1.geometry, you make another map showing m2.geometry:

select m1.fid id1, m2.fid id2, st_length(m2.geometry) length1, st_length(m2.geometry) length2, m1.geometry 
from mylines m1
inner join mylines m2 on st_covers(m1.geometry, m2.geometry)
where m1.fid <> m2.fid 

example1

Above are four lines. Line 2 is covering the part of line 0 (the bottom vertical segment)

example2

Above first SQL showing m1

example3

Second SQL showing m2

example4

Above attribute table showing cover list

1

I faced the same problem and according to me, the best way to solve it is to use v.clean GRASS tool with the following parameters.

Input feature type: line

Cleaning tool: break

The line will be divided into segments at the overlapping points. Then you can use "Delete duplicate geometries"

0

I faced the same problem, I used this code but it isn't work. My code is:

select l1.id as id1, l2.id as id2, st_intersection(l1.geometry, l2.geometry) as geometry from line l1, line 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

Are there any problems with my code or is this code affected by Qgis 3.6?

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.