3

I have two layers ("Layer_1" and "Layer_2") of lines where they sometimes overlap and follow each other for a bit and then takes another route (see the image below).

  • "Layer_1": Red line
  • "Layer_2": Orange line
  • An overlap between Layer 1 and Layer 2: Green line

Example_of_layers

What I'd like to do is to merge these two layers so that there will only be

  1. one line where there is lines overlap including some attribute information saying this is a line which used to be an overlapping line.
  2. other lines having other attribute information, so that I can distinguish these three lines from each other.

It's OK if the output will be three layers, as long as I can distinguish these three categories of lines.

  • Do you have Layers consisting of line segments or they are monolines? – Taras Jan 8 at 13:35
  • Not sure what you mean by monolines? The lines are long and consists of many vertices. – oskarlin Jan 8 at 14:57
3

@oskarlin, I tried to recreate a similar example of your layers. However, I do not know whether your Layers consist of line segments (polylines) or continuous lines (monolines), therefore there are two cases that define the workflow.


CASE 1. When Layers (polylines) consist of segments, i.e. the polyline is split into sections at each vertex or intersection node, see the image below.

Case1_Example

'Layer_2', as well as 'Layer_1', includes two fields, namely "id" and "Value".

  1. For merging two shapefiles, you need to proceed with Vector -> Data Management Tools -> Merge vector layers. Moreover, there are other solutions available, e.g. the MMQGIS plugin does the job (MMQGIS -> Combine -> Merge layers) or SAGA GIS Merge Layers Module from the Processing Toolbox.

    It will provide you with a new polyline layer that includes geometries and attributes of polylines, 'Layer_1' and 'Layer_2' accordingly, see the graphics below. Case1_Output

  2. Then in a new shapefile create a column that defines the overlap value of your layers, e.g. string data type with a length of 1.
  3. In Expression dialogue execute the following expression

    CASE
    WHEN sum(num_geometries($geometry), group_by:= geom_to_wkt($geometry)) > 1 THEN 'Y'
    ELSE 'N'
    END
    

    'Y' will give you all overlapping lines, and 'N' lines that do not overlap.

    P.S. If there is difference in any part of the geometry, this expression fails to match them. @Kazuhito comment

    As you can notice from the image below, each Layer has only two line segments where exists an overlap. Moreover, you can easily distinguish between line segments based on values from the Attribute Table. Case1_Result

  4. At this last step, you will need to delete the duplicates based on geometry, however, I am not sure whether you have to remain the "Value" field in case it plays a primary role in your project.


CASE 2. When your layers contain continuous lines, see the image below.

Case2_Example

It's just a matter of running a few more tools.

  1. First, add a field to your original layers with an identifier that will mark features as being from that original layer when they are all merged.
  2. Second, merge the layers. (Check possibilities above)
  3. Then use the Union geoprocessing tool (Vector -> Geoprocessing Tools -> Union) to split features into overlapping and non-overlapping parts. The non-overlapping parts of lines will still be one multipart feature so if you want each segment to be separated, you'll need to run the Multipart to Singleparts... tool (Vector -> Geometry Tools -> Multipart to singleparts).
  4. Finally, create the overlap field and populate it using the above instructions.

P.S. Answer was completed with the help of @Gabriel C..

References:

  • Not sure if I understand you completely, but I can't see how this will work since the lines needs to be divided into parts everywhere there is a overlap. Not sure this solution does this? In your solution it only works if the whole line object overlap with another line right? My lines are just partially overlapping. – oskarlin Jan 8 at 14:56
  • This is so strange. Yesterday when I tried the Union geoprocessing tool this didn't work. The output was just an empty layer. When I try today, it worked. Thank you for making me do this again! – oskarlin Jan 8 at 15:26
  • @GabrielC. could you perhaps just add that as a new answer and I'll accept that one as a valid answer (just so that people in the future easier can see what the solution is)? – oskarlin Jan 8 at 15:28
  • @Gabriel C., it is not that clear to me, which layer the merged layer (the result of step 1) should be unioned with? – Taras Jan 10 at 8:52
  • Perfect! This answer is very good. – oskarlin Jan 10 at 9:50

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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