@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.
'Layer_2', as well as
'Layer_1', includes two fields, namely
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_2' accordingly, see the graphics below.
- 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
- In Expression dialogue execute the following expression
WHEN sum(num_geometries($geometry), group_by:= geom_to_wkt($geometry)) > 1 THEN 'Y'
'Y' will give you all overlapping lines, and
'N' lines that do not overlap.
P.S. If there is a 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.
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.
It's just a matter of running a few more tools.
- 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.
- Second, merge the layers. (Check possibilities above)
- 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).
- Finally, create the overlapping field and populate it using the above
P.S. Answer was completed with the help of @Gabriel C..